diff --git a/pkg/database/migrate/sqlite.go b/pkg/database/migrate/sqlite.go index 99264b92..84936080 100644 --- a/pkg/database/migrate/sqlite.go +++ b/pkg/database/migrate/sqlite.go @@ -19,7 +19,7 @@ func SQLite(tx *sql.Tx) *MigrationDriver { } func (s *sqliteDriver) CreateTable(tableName string, args []string) (sql.Result, error) { - return s.Tx.Exec(fmt.Sprintf("CREATE TABLE %s (%s)", tableName, strings.Join(args, ", "))) + return s.Tx.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s)", tableName, strings.Join(args, ", "))) } func (s *sqliteDriver) RenameTable(tableName, newName string) (sql.Result, error) { diff --git a/pkg/database/migrate/sqlite_test.go b/pkg/database/migrate/sqlite_test.go index 7e4987f7..37bc15b4 100644 --- a/pkg/database/migrate/sqlite_test.go +++ b/pkg/database/migrate/sqlite_test.go @@ -290,6 +290,29 @@ func TestMigrateCreateTable(t *testing.T) { } } +func TestMigrateExistingCreateTable(t *testing.T) { + defer tearDown() + if err := setUp(); err != nil { + t.Fatalf("Error preparing database: %q", err) + } + + if _, err := db.Exec(testSchema); err != nil { + t.Fatalf("Can not create database: %q", err) + } + + mgr := New(db) + rev := &revision1{} + if err := mgr.Add(rev).Migrate(); err != nil { + t.Fatalf("Can not migrate: %q", err) + } + + var current int64 + db.QueryRow("SELECT max(revision) FROM migration").Scan(¤t) + if current != rev.Revision() { + t.Fatalf("Did not successfully migrate") + } +} + func TestMigrateRenameTable(t *testing.T) { defer tearDown() if err := setUp(); err != nil {