mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: add version checker
This commit is contained in:
@@ -153,12 +153,20 @@ func (db *DB) compareMigrationHistory() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentVersion := common.Version
|
currentVersion := common.Version
|
||||||
migrationHistory, err := upsertMigrationHistory(db.Db, currentVersion)
|
migrationHistoryFind := MigrationHistoryFind{
|
||||||
|
Version: currentVersion,
|
||||||
|
}
|
||||||
|
migrationHistory, err := findMigrationHistory(db.Db, &migrationHistoryFind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if migrationHistory == nil {
|
if migrationHistory == nil {
|
||||||
return fmt.Errorf("failed to upsert migration history")
|
// ...do schema migration,
|
||||||
|
// then upsert the newest version to migration_history
|
||||||
|
_, err := upsertMigrationHistory(db.Db, currentVersion)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@@ -2,6 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MigrationHistory struct {
|
type MigrationHistory struct {
|
||||||
@@ -9,6 +10,59 @@ type MigrationHistory struct {
|
|||||||
Version string
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MigrationHistoryFind struct {
|
||||||
|
Version string
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMigrationHistoryList(db *sql.DB, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||||
|
where, args := []string{"1 = 1"}, []interface{}{}
|
||||||
|
|
||||||
|
where, args = append(where, "version = ?"), append(args, find.Version)
|
||||||
|
|
||||||
|
rows, err := db.Query(`
|
||||||
|
SELECT
|
||||||
|
version,
|
||||||
|
created_ts
|
||||||
|
FROM
|
||||||
|
migration_history
|
||||||
|
WHERE `+strings.Join(where, " AND ")+`
|
||||||
|
ORDER BY created_ts DESC`,
|
||||||
|
args...,
|
||||||
|
)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMigrationHistory(db *sql.DB, find *MigrationHistoryFind) (*MigrationHistory, error) {
|
||||||
|
list, err := findMigrationHistoryList(db, find)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(list) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return list[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, error) {
|
func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, error) {
|
||||||
row, err := db.Query(`
|
row, err := db.Query(`
|
||||||
INSERT INTO migration_history (
|
INSERT INTO migration_history (
|
||||||
|
Reference in New Issue
Block a user