Skip to content

Commit b5832da

Browse files
author
Henrik Johansson
authored
Merge pull request #238 from scylladb/issue_232
Namespacing index names with table names
2 parents 4e6a2fa + d2d2c92 commit b5832da

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (t *Table) GetCreateTable(ks Keyspace) string {
234234
stmt = stmt + " WITH compaction = " + t.CompactionStrategy.ToCQL() + ";"
235235
}*/
236236
if len(t.TableOptions) > 0 {
237-
stmt = stmt + "WITH " + strings.Join(t.TableOptions, " AND ") + ";"
237+
stmt = stmt + " WITH " + strings.Join(t.TableOptions, " AND ") + ";"
238238
}
239239
return stmt
240240
}
@@ -469,7 +469,7 @@ func createTable(sc SchemaConfig, tableName string) Table {
469469
}
470470
var indexes []IndexDef
471471
if sc.CQLFeature > CQL_FEATURE_BASIC {
472-
indexes = createIndexes(numColumns, columns)
472+
indexes = createIndexes(tableName, numColumns, columns)
473473
}
474474

475475
var mvs []MaterializedView
@@ -484,7 +484,7 @@ func createTable(sc SchemaConfig, tableName string) Table {
484484
return table
485485
}
486486

487-
func createIndexes(numColumns int, columns []ColumnDef) []IndexDef {
487+
func createIndexes(tableName string, numColumns int, columns []ColumnDef) []IndexDef {
488488
if numColumns <= 0 {
489489
return nil
490490
}
@@ -497,7 +497,7 @@ func createIndexes(numColumns int, columns []ColumnDef) []IndexDef {
497497
indexes := make([]IndexDef, 0, numIndexes)
498498
for i, col := range columns {
499499
if col.Type.Indexable() && typeIn(col, typesForIndex) {
500-
indexes = append(indexes, IndexDef{Name: genIndexName("col", i), Column: col.Name, ColumnIdx: i})
500+
indexes = append(indexes, IndexDef{Name: genIndexName(tableName+"_col", i), Column: col.Name, ColumnIdx: i})
501501
createdCount++
502502
}
503503
if createdCount == numIndexes {

schema_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/google/go-cmp/cmp"
21+
"github.com/scylladb/gemini/tableopts"
2122
)
2223

2324
func TestSchemaConfigValidate(t *testing.T) {
@@ -96,6 +97,11 @@ func TestSchemaConfigValidate(t *testing.T) {
9697
}
9798
}
9899

100+
func options(cql string) []string {
101+
opt, _ := tableopts.FromCQL(cql)
102+
return []string{opt.ToCQL()}
103+
}
104+
99105
func TestGetCreateSchema(t *testing.T) {
100106
ks := Keyspace{Name: "ks1"}
101107
tests := map[string]struct {
@@ -111,11 +117,11 @@ func TestGetCreateSchema(t *testing.T) {
111117
},
112118
"single_partition_key_compact": {
113119
table: &Table{
114-
Name: "tbl0",
115-
PartitionKeys: createColumns(1, "pk"),
116-
CompactionStrategy: NewLeveledCompactionStrategy(),
120+
Name: "tbl0",
121+
PartitionKeys: createColumns(1, "pk"),
122+
TableOptions: options("compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160}"),
117123
},
118-
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text, PRIMARY KEY ((pk0))) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160};",
124+
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text, PRIMARY KEY ((pk0))) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'sstable_size_in_mb':160,'tombstone_compaction_interval':86400,'tombstone_threshold':0.2};",
119125
},
120126
"single_partition_key_single_column": {
121127
table: &Table{
@@ -151,12 +157,12 @@ func TestGetCreateSchema(t *testing.T) {
151157
},
152158
"single_partition_key_single_clustering_key_compact": {
153159
table: &Table{
154-
Name: "tbl0",
155-
PartitionKeys: createColumns(1, "pk"),
156-
ClusteringKeys: createColumns(1, "ck"),
157-
CompactionStrategy: NewLeveledCompactionStrategy(),
160+
Name: "tbl0",
161+
PartitionKeys: createColumns(1, "pk"),
162+
ClusteringKeys: createColumns(1, "ck"),
163+
TableOptions: options("compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160}"),
158164
},
159-
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text,ck0 text, PRIMARY KEY ((pk0), ck0)) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160};",
165+
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text,ck0 text, PRIMARY KEY ((pk0), ck0)) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'sstable_size_in_mb':160,'tombstone_compaction_interval':86400,'tombstone_threshold':0.2};",
160166
},
161167
"single_partition_key_single_clustering_key_single_column": {
162168
table: &Table{

0 commit comments

Comments
 (0)