fix: create migration_history table

This commit is contained in:
boojack 2022-07-01 20:26:56 +08:00
parent 68aae72723
commit 5a1ea00943
2 changed files with 11 additions and 11 deletions

View File

@ -137,12 +137,12 @@ func (db *DB) executeFile(FS embed.FS, name string) error {
// compareMigrationHistory compares migration history data // compareMigrationHistory compares migration history data
func (db *DB) compareMigrationHistory() error { func (db *DB) compareMigrationHistory() error {
table, err := findTable(db, "migration_history") table, err := findTable(db.Db, "migration_history")
if err != nil { if err != nil {
return err return err
} }
if table == nil { if table == nil {
if err := createTable(db, ` if err := createTable(db.Db, `
CREATE TABLE migration_history ( CREATE TABLE migration_history (
version TEXT NOT NULL PRIMARY KEY, version TEXT NOT NULL PRIMARY KEY,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))

View File

@ -1,7 +1,7 @@
package db package db
import ( import (
"fmt" "database/sql"
"strings" "strings"
) )
@ -10,13 +10,13 @@ type Table struct {
SQL string SQL string
} }
func findTable(db *DB, tableName string) (*Table, error) { func findTable(db *sql.DB, tableName string) (*Table, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
where, args = append(where, "type = ?"), append(args, "table") where, args = append(where, "type = ?"), append(args, "table")
where, args = append(where, "name = ?"), append(args, tableName) where, args = append(where, "name = ?"), append(args, tableName)
rows, err := db.Db.Query(` rows, err := db.Query(`
SELECT SELECT
tbl_name, tbl_name,
sql sql
@ -53,13 +53,13 @@ func findTable(db *DB, tableName string) (*Table, error) {
} }
} }
func createTable(db *DB, sql string) error { func createTable(db *sql.DB, sql string) error {
result, err := db.Db.Exec(sql) result, err := db.Exec(sql)
if err != nil {
rows, _ := result.RowsAffected() return err
if rows == 0 {
return fmt.Errorf("failed to create table with %s", sql)
} }
_, err = result.RowsAffected()
return err return err
} }