Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix: schema migration for minor version
boojackde7058532aac
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.goindex 7a3e0bb33..f86306b4c 100644--- a/bin/server/cmd/root.go+++ b/bin/server/cmd/root.go@@ -36,11 +36,10 @@ func (m *Main) Run() error {storeInstance := store.New(db.Db, m.profile)s.Store = storeInstance- if err := s.Run(); err != nil {- return err- }+ println(greetingBanner)+ fmt.Printf("Version %s has started at :%d\n", m.profile.Version, m.profile.Port)- return nil+ return s.Run()}func Execute() {@@ -56,8 +55,6 @@ func Execute() {println("dsn:", profile.DSN)println("version:", profile.Version)println("---")- println(greetingBanner)- fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)if err := m.Run(); err != nil {fmt.Printf("error: %+v\n", err)diff --git a/store/db/db.go b/store/db/db.goindex b2ca66e4e..adae5d0c9 100644--- a/store/db/db.go+++ b/store/db/db.go@@ -80,9 +80,8 @@ func (db *DB) Open() (err error) {return err}if migrationHistory == nil {- migrationHistory, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{- Version: currentVersion,- Statement: "",+ migrationHistory, err = upsertMigrationHistory(db.Db, &MigrationHistoryUpsert{+ Version: currentVersion,})if err != nil {return err@@ -91,16 +90,18 @@ func (db *DB) Open() (err error) {if common.IsVersionGreaterThan(currentVersion, migrationHistory.Version) {minorVersionList := getMinorVersionList()-+ println("start migrate")for _, minorVersion := range minorVersionList {normalizedVersion := minorVersion + ".0"if common.IsVersionGreaterThan(normalizedVersion, migrationHistory.Version) && common.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {+ println("applying migration for", normalizedVersion)err := db.applyMigrationForMinorVersion(minorVersion)if err != nil {return fmt.Errorf("failed to apply minor version migration: %w", err)}}}+ println("end migrate")}}}@@ -148,9 +149,8 @@ func (db *DB) applyMigrationForMinorVersion(minorVersion string) error {}// upsert the newest version to migration_history- if _, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{- Version: minorVersion + ".0",- Statement: migrationStmt,+ if _, err = upsertMigrationHistory(db.Db, &MigrationHistoryUpsert{+ Version: minorVersion + ".0",}); err != nil {return err}@@ -221,29 +221,12 @@ func getMinorVersionList() []string {// createMigrationHistoryTable creates the migration_history table if it doesn't exist.func (db *DB) createMigrationHistoryTable() error {- table, err := findTable(db.Db, "migration_history")- if err != nil {- return err- }-- // TODO(steven): Drop the migration_history table if it exists temporarily.- if table != nil {- err = db.execute(`- DROP TABLE IF EXISTS migration_history;- `)- if err != nil {- return err- }- }-- err = createTable(db.Db, `- CREATE TABLE migration_history (+ if err := createTable(db.Db, `+ CREATE TABLE IF NOT EXISTS migration_history (version TEXT NOT NULL PRIMARY KEY,- statement TEXT NOT NULL DEFAULT '',created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')));- `)- if err != nil {+ `); err != nil {return err}diff --git a/store/db/migration/0.2/00__user_role.sql b/store/db/migration/0.2/00__user_role.sqlindex c883c1995..942dc6c02 100644--- a/store/db/migration/0.2/00__user_role.sql+++ b/store/db/migration/0.2/00__user_role.sql@@ -1,7 +1,6 @@-- change user role field from "OWNER"/"USER" to "HOST"/"USER".PRAGMA foreign_keys = off;-BEGIN TRANSACTION;DROP TABLE IF EXISTS _user_old;@@ -52,5 +51,4 @@ WHEREDROP TABLE IF EXISTS _user_old;-COMMIT;PRAGMA foreign_keys = on;diff --git a/store/db/migration_history.go b/store/db/migration_history.goindex 6144a989a..8e74c0c50 100644--- a/store/db/migration_history.go+++ b/store/db/migration_history.go@@ -7,13 +7,11 @@ import (type MigrationHistory struct {Version string- Statement stringCreatedTs int64}-type MigrationHistoryCreate struct {- Version string- Statement string+type MigrationHistoryUpsert struct {+ Version string}type MigrationHistoryFind struct {@@ -71,21 +69,18 @@ func findMigrationHistory(db *sql.DB, find *MigrationHistoryFind) (*MigrationHis}}-func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*MigrationHistory, error) {+func upsertMigrationHistory(db *sql.DB, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {row, err := db.Query(`INSERT INTO migration_history (- version,- statement+ version)- VALUES (?, ?)+ VALUES (?)ON CONFLICT(version) DO UPDATESET- version=EXCLUDED.version,- statement=EXCLUDED.statement- RETURNING version, statement, created_ts+ version=EXCLUDED.version+ RETURNING version, created_ts`,- create.Version,- create.Statement,+ upsert.Version,)if err != nil {return nil, err@@ -96,7 +91,6 @@ func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*Migratvar migrationHistory MigrationHistoryif err := row.Scan(&migrationHistory.Version,- &migrationHistory.Statement,&migrationHistory.CreatedTs,); err != nil {return nil, errdiff --git a/store/db/table.go b/store/db/table.goindex a32549dc3..bd069ea20 100644--- a/store/db/table.go+++ b/store/db/table.go@@ -10,6 +10,7 @@ type Table struct {SQL string}+//lint:ignore U1000 Ignore unused function temporarily for debuggingfunc findTable(db *sql.DB, tableName string) (*Table, error) {where, args := []string{"1 = 1"}, []interface{}{}