memos/store/db/migration_history.go

62 lines
1 KiB
Go
Raw Normal View History

2022-05-22 00:59:22 +08:00
package db
import (
"fmt"
)
type MigrationHistory struct {
CreatedTs int64
Version string
}
2022-05-22 09:29:34 +08:00
func findMigrationHistoryList(db *DB) ([]*MigrationHistory, error) {
2022-05-22 00:59:22 +08:00
rows, err := db.Db.Query(`
SELECT
version,
created_ts
FROM
migration_history
ORDER BY created_ts DESC
`)
if err != nil {
return nil, err
}
defer rows.Close()
migrationHistoryList := make([]*MigrationHistory, 0)
for rows.Next() {
var migrationHistory MigrationHistory
if err := rows.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
); err != nil {
return nil, err
}
migrationHistoryList = append(migrationHistoryList, &migrationHistory)
}
return migrationHistoryList, nil
}
2022-05-22 09:29:34 +08:00
func createMigrationHistory(db *DB, version string) error {
2022-05-22 00:59:22 +08:00
result, err := db.Db.Exec(`
INSERT INTO migration_history (
2022-05-22 09:29:34 +08:00
version
2022-05-22 00:59:22 +08:00
)
2022-05-22 09:29:34 +08:00
VALUES (?)
2022-05-22 00:59:22 +08:00
`,
version,
)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("failed to create migration history with %s", version)
}
return nil
}