Skip to content
Merged
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
2 changes: 1 addition & 1 deletion migrations/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func dropSQLiteSchema(db *database.DB) error {
if err := db.Raw(`
SELECT type, name FROM sqlite_master
WHERE type IN ('table', 'view')
AND name NOT LIKE 'sqlite_%'
AND name NOT LIKE 'sqlite\_%' ESCAPE '\'
ORDER BY type DESC, name`).Scan(&objects).Error; err != nil {
return fmt.Errorf("migrations: list sqlite objects: %w", err)
}
Expand Down
46 changes: 46 additions & 0 deletions migrations/reset_sqlite_prefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package migrations

import (
"path/filepath"
"testing"

"github.com/gombit-dev/gombit/config"
"github.com/gombit-dev/gombit/database"
)

func TestDropSQLiteSchemaOnlyPreservesReservedPrefix(t *testing.T) {
cfg := config.DatabaseConfig{
Driver: config.DatabaseDriverSQLite,
DSN: "file:" + filepath.Join(t.TempDir(), "reset.db") + "?cache=shared&_fk=1",
}
db, err := database.Open(cfg)
if err != nil {
t.Fatal(err)
}
defer func() { _ = db.Close() }()

for _, ddl := range []string{
"CREATE TABLE normal_table (id INTEGER PRIMARY KEY)",
"CREATE TABLE sqlitex_cache (id INTEGER PRIMARY KEY)",
"CREATE TABLE sqlite3_import (id INTEGER PRIMARY KEY)",
"CREATE TABLE SqlitEy_cache (id INTEGER PRIMARY KEY)",
"CREATE TABLE reserved_anchor (id INTEGER PRIMARY KEY AUTOINCREMENT)",
"CREATE VIEW sqlitex_view AS SELECT id FROM reserved_anchor",
} {
if err := db.Exec(ddl).Error; err != nil {
t.Fatalf("fixture %q: %v", ddl, err)
}
}
if err := dropSQLiteSchema(db); err != nil {
t.Fatalf("dropSQLiteSchema: %v", err)
}

var remaining []struct{ Name string }
if err := db.Raw("SELECT name FROM sqlite_master WHERE type IN ('table','view') ORDER BY name").
Scan(&remaining).Error; err != nil {
t.Fatal(err)
}
if len(remaining) != 1 || remaining[0].Name != "sqlite_sequence" {
t.Fatalf("remaining SQLite objects = %+v, want only sqlite_sequence", remaining)
}
}
Loading