Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate cassandra column type #243

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions connectors/cassandra/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ func checkClusteringKeys(ed *dosa.EntityDefinition, md *gocql.TableMetadata) err
}
return nil
}

func checkColumns(ed *dosa.EntityDefinition, md *gocql.TableMetadata, schemaErrors *RepairableSchemaMismatchError) {
// Check each column
for _, col := range ed.Columns {
_, ok := md.Columns[col.Name]
if !ok {
schemaErrors.MissingColumns = append(schemaErrors.MissingColumns, MissingColumn{Column: *col, Tablename: ed.Name})
cassandraCol, ok := md.Columns[col.Name]
if ok && cassandraCol.Type.Type().String() == cassandraType(col.Type) {
continue
}
// TODO: check column type

schemaErrors.MissingColumns = append(schemaErrors.MissingColumns, MissingColumn{Column: *col, Tablename: ed.Name})
}
}

Expand Down
84 changes: 81 additions & 3 deletions connectors/cassandra/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ func TestCompareStructToSchemaWrongPk(t *testing.T) {
assert.Contains(t, err.Error(), `"test"`)
}

// test that the cluster key in gocql doesn't match cluster key in dosa definition. c1 != c2
func TestCompareStructToSchemaWrongCk(t *testing.T) {
ed := dosa.EntityDefinition{
Key: &dosa.PrimaryKey{
PartitionKeys: []string{"p1"},
ClusteringKeys: []*dosa.ClusteringKey{{Name: "c1", Descending: true}},
},
Name: "test",
Columns: []*dosa.ColumnDefinition{
{Name: "p1", Type: dosa.String},
{Name: "c1", Type: dosa.String},
},
}
md := gocql.TableMetadata{
PartitionKey: []*gocql.ColumnMetadata{
{Name: "p1", Type: TestType{typ: gocql.TypeText}},
},
ClusteringColumns: []*gocql.ColumnMetadata{
{Name: "c2", Type: TestType{typ: gocql.TypeText}},
},
Columns: map[string]*gocql.ColumnMetadata{
"p1": {Name: "p1", Type: TestType{typ: gocql.TypeText}},
}}
missing := RepairableSchemaMismatchError{}
err := compareStructToSchema(&ed, &md, &missing)
assert.Error(t, err)
assert.Contains(t, err.Error(), `"test"`)
}

func TestCompareStructToSchemaMissingColumn(t *testing.T) {
ed := dosa.EntityDefinition{Key: &dosa.PrimaryKey{
PartitionKeys: []string{"p1"}},
Expand All @@ -60,21 +89,70 @@ func TestCompareStructToSchemaMissingColumn(t *testing.T) {
},
}
md := gocql.TableMetadata{PartitionKey: []*gocql.ColumnMetadata{
{Name: "p1", Type: TestType{typ: gocql.TypeVarchar}},
{Name: "p1", Type: TestType{typ: gocql.TypeText}},
},
Columns: map[string]*gocql.ColumnMetadata{
"p1": {Name: "p1", Type: TestType{typ: gocql.TypeVarchar}},
"p1": {Name: "p1", Type: TestType{typ: gocql.TypeText}},
}}
missing := RepairableSchemaMismatchError{}
err := compareStructToSchema(&ed, &md, &missing)
assert.Nil(t, err)
assert.NoError(t, err)
assert.True(t, missing.HasMissing())
assert.Equal(t, 1, len(missing.MissingColumns))
assert.Equal(t, "test", missing.MissingColumns[0].Tablename)
assert.Equal(t, "c1", missing.MissingColumns[0].Column.Name)
assert.Contains(t, missing.Error(), "Missing 1 column")
}

// dosa string type is a gocql text type, not varchar
func TestCompareStructToSchemaMissingColumnType(t *testing.T) {
ed := dosa.EntityDefinition{Key: &dosa.PrimaryKey{
PartitionKeys: []string{"pk"}},
Name: "test",
Columns: []*dosa.ColumnDefinition{
{Name: "pk", Type: dosa.String},
},
}
md := gocql.TableMetadata{
PartitionKey: []*gocql.ColumnMetadata{
{Name: "pk", Type: TestType{typ: gocql.TypeVarchar}},
},
Columns: map[string]*gocql.ColumnMetadata{
"pk": {Name: "pk", Type: TestType{typ: gocql.TypeVarchar}},
},
}
missing := RepairableSchemaMismatchError{}
err := compareStructToSchema(&ed, &md, &missing)
assert.NoError(t, err)
assert.True(t, missing.HasMissing())
assert.Equal(t, 1, len(missing.MissingColumns))
assert.Equal(t, "test", missing.MissingColumns[0].Tablename)
assert.Equal(t, "pk", missing.MissingColumns[0].Column.Name)
assert.Contains(t, missing.Error(), "Missing 1 column")
}

func TestCompareStructToSchema(t *testing.T) {
ed := dosa.EntityDefinition{Key: &dosa.PrimaryKey{
PartitionKeys: []string{"p1"}},
Name: "test",
Columns: []*dosa.ColumnDefinition{
{Name: "p1", Type: dosa.Int32},
},
}
md := gocql.TableMetadata{
PartitionKey: []*gocql.ColumnMetadata{
{Name: "p1", Type: TestType{typ: gocql.TypeInt}},
},
Columns: map[string]*gocql.ColumnMetadata{
"p1": {Name: "p1", Type: TestType{typ: gocql.TypeInt}},
},
}
missing := RepairableSchemaMismatchError{}
err := compareStructToSchema(&ed, &md, &missing)
assert.NoError(t, err)
assert.False(t, missing.HasMissing())
}

type TestType struct {
typ gocql.Type
}
Expand Down