mirror of
https://github.com/usememos/memos.git
synced 2024-12-24 11:44:44 +03:00
fix: upsert migration history
This commit is contained in:
parent
0945b14deb
commit
87e6277977
@ -142,26 +142,23 @@ func (db *DB) compareMigrationHistory() error {
|
||||
return err
|
||||
}
|
||||
if table == nil {
|
||||
createTable(db, `
|
||||
if err := createTable(db, `
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
|
||||
);
|
||||
`)
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
migrationHistoryList, err := findMigrationHistoryList(db)
|
||||
currentVersion := common.Version
|
||||
migrationHistory, err := upsertMigrationHistory(db.Db, currentVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(migrationHistoryList) == 0 {
|
||||
createMigrationHistory(db, common.Version)
|
||||
} else {
|
||||
migrationHistory := migrationHistoryList[0]
|
||||
if migrationHistory.Version != common.Version {
|
||||
createMigrationHistory(db, common.Version)
|
||||
}
|
||||
if migrationHistory == nil {
|
||||
return fmt.Errorf("failed to upsert migration history")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -1,7 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
@ -9,53 +9,32 @@ type MigrationHistory struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
func findMigrationHistoryList(db *DB) ([]*MigrationHistory, error) {
|
||||
rows, err := db.Db.Query(`
|
||||
SELECT
|
||||
func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, error) {
|
||||
row, err := db.Query(`
|
||||
INSERT INTO migration_history (
|
||||
version
|
||||
)
|
||||
VALUES (?)
|
||||
ON CONFLICT(version) DO UPDATE
|
||||
SET
|
||||
version=EXCLUDED.version
|
||||
RETURNING version, created_ts
|
||||
`,
|
||||
version,
|
||||
created_ts
|
||||
FROM
|
||||
migration_history
|
||||
ORDER BY created_ts DESC
|
||||
`)
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
defer row.Close()
|
||||
|
||||
migrationHistoryList := make([]*MigrationHistory, 0)
|
||||
for rows.Next() {
|
||||
var migrationHistory MigrationHistory
|
||||
if err := rows.Scan(
|
||||
row.Next()
|
||||
migrationHistory := MigrationHistory{}
|
||||
if err := row.Scan(
|
||||
&migrationHistory.Version,
|
||||
&migrationHistory.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migrationHistoryList = append(migrationHistoryList, &migrationHistory)
|
||||
}
|
||||
|
||||
return migrationHistoryList, nil
|
||||
}
|
||||
|
||||
func createMigrationHistory(db *DB, version string) error {
|
||||
result, err := db.Db.Exec(`
|
||||
INSERT INTO migration_history (
|
||||
version
|
||||
)
|
||||
VALUES (?)
|
||||
`,
|
||||
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
|
||||
return &migrationHistory, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user