Merge pull request #226 from Clever/sqlite-table-exists-error

migrate/sqlite: create table only if it doesn't exist
This commit is contained in:
Brad Rydzewski 2014-03-26 20:37:44 -07:00
commit 6e2b74eac7
2 changed files with 24 additions and 1 deletions

View file

@ -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) {

View file

@ -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(&current)
if current != rev.Revision() {
t.Fatalf("Did not successfully migrate")
}
}
func TestMigrateRenameTable(t *testing.T) {
defer tearDown()
if err := setUp(); err != nil {