migrate/sqlite: create table only if it doesn't exist

fixes #224
This commit is contained in:
Rafael Garcia 2014-03-26 14:26:51 -07:00
parent 9f43d5c647
commit a284332a91
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 {