From a284332a9135a28eb18c7150160e676338aa2ce2 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Wed, 26 Mar 2014 14:26:51 -0700 Subject: [PATCH] migrate/sqlite: create table only if it doesn't exist fixes #224 --- pkg/database/migrate/sqlite.go | 2 +- pkg/database/migrate/sqlite_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/database/migrate/sqlite.go b/pkg/database/migrate/sqlite.go index 99264b92a..84936080e 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 7e4987f76..37bc15b45 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 {