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

Fix metadata updates for dropIndexes #3358

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Changes from 4 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
113 changes: 113 additions & 0 deletions internal/backends/sqlite/metadata/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,116 @@ func TestCreateDropSameStress(t *testing.T) {
})
}
}

func TestIndexesCreateDrop(t *testing.T) {
t.Parallel()
ctx := testutil.Ctx(t)

sp, err := state.NewProvider("")
require.NoError(t, err)

r, err := NewRegistry("file:./?mode=memory", testutil.Logger(t), sp)
require.NoError(t, err)
t.Cleanup(r.Close)

dbName := testutil.DatabaseName(t)

db, err := r.DatabaseGetOrCreate(ctx, dbName)
require.NoError(t, err)
require.NotNil(t, db)

t.Cleanup(func() {
r.DatabaseDrop(ctx, dbName)
})

collectionName := testutil.CollectionName(t)

toCreate := []IndexInfo{
{
Name: "index_non_unique",
Key: []IndexKeyPair{
{
Field: "f1",
Descending: false,
},
{
Field: "f2",
Descending: true,
},
},
},
{
Name: "index_unique",
Key: []IndexKeyPair{
{
Field: "foo",
Descending: false,
},
},
Unique: true,
},
}

err = r.IndexesCreate(ctx, dbName, collectionName, toCreate)
require.NoError(t, err)

collection := r.CollectionGet(ctx, dbName, collectionName)

t.Run("NonUniqueIndex", func(t *testing.T) {
indexName := collection.TableName + "_index_non_unique"
q := fmt.Sprintf("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = '%s'", indexName)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
row := db.QueryRowContext(ctx, q)

var sql string
require.NoError(t, row.Scan(&sql))

expected := fmt.Sprintf(
`CREATE INDEX "%s" ON "%s" (_ferretdb_sjson->'$.f1', _ferretdb_sjson->'$.f2' DESC)`,
indexName, collection.TableName,
)
require.Equal(t, expected, sql)
})

t.Run("UniqueIndex", func(t *testing.T) {
indexName := collection.TableName + "_index_unique"
q := fmt.Sprintf("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = '%s'", indexName)
row := db.QueryRowContext(ctx, q)

var sql string
require.NoError(t, row.Scan(&sql))

expected := fmt.Sprintf(
`CREATE UNIQUE INDEX "%s" ON "%s" (_ferretdb_sjson->'$.foo')`,
indexName, collection.TableName,
)
require.Equal(t, expected, sql)
})

t.Run("DefaultIndex", func(t *testing.T) {
indexName := collection.TableName + "__id_"
q := "SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?"
row := db.QueryRowContext(ctx, q, indexName)

var sql string
require.NoError(t, row.Scan(&sql))

expected := fmt.Sprintf(
`CREATE UNIQUE INDEX "%s" ON "%s" (_ferretdb_sjson->'$._id')`,
indexName, collection.TableName,
)
require.Equal(t, expected, sql)
})

t.Run("DropIndexes", func(t *testing.T) {
toDrop := []string{"index_non_unique", "index_unique"}
err = r.IndexesDrop(ctx, dbName, collectionName, toDrop)
require.NoError(t, err)

q := "SELECT count(*) FROM sqlite_master WHERE type = 'index' AND tbl_name = ?"
row := db.QueryRowContext(ctx, q, collection.TableName)

var count int
require.NoError(t, row.Scan(&count))
require.Equal(t, 1, count) // only default index
})
}