From c3c2882dc588320a2a846352f2beb58732544dc4 Mon Sep 17 00:00:00 2001 From: boojack Date: Sun, 22 May 2022 09:29:34 +0800 Subject: [PATCH] chore: update server profile --- api/system.go | 6 +-- bin/server/cmd/root.go | 6 +-- {common => server/profile}/profile.go | 6 ++- server/server.go | 6 +-- store/db/db.go | 67 +++++++++++++-------------- store/db/migration_history.go | 11 ++--- store/db/table.go | 6 +-- store/store.go | 6 +-- 8 files changed, 56 insertions(+), 58 deletions(-) rename {common => server/profile}/profile.go (92%) diff --git a/api/system.go b/api/system.go index 71aa7806..66756a4d 100644 --- a/api/system.go +++ b/api/system.go @@ -1,8 +1,8 @@ package api -import "memos/common" +import "memos/server/profile" type SystemStatus struct { - Owner *User `json:"owner"` - Profile *common.Profile `json:"profile"` + Owner *User `json:"owner"` + Profile *profile.Profile `json:"profile"` } diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go index 8573e22d..9ff059de 100644 --- a/bin/server/cmd/root.go +++ b/bin/server/cmd/root.go @@ -4,8 +4,8 @@ import ( "fmt" "os" - "memos/common" "memos/server" + "memos/server/profile" "memos/store" DB "memos/store/db" ) @@ -22,7 +22,7 @@ const ( ) type Main struct { - profile *common.Profile + profile *profile.Profile } func (m *Main) Run() error { @@ -44,7 +44,7 @@ func (m *Main) Run() error { } func Execute() { - profile := common.GetProfile() + profile := profile.GetProfile() m := Main{ profile: profile, } diff --git a/common/profile.go b/server/profile/profile.go similarity index 92% rename from common/profile.go rename to server/profile/profile.go index 845bc1f2..8c4f6f57 100644 --- a/common/profile.go +++ b/server/profile/profile.go @@ -1,13 +1,15 @@ -package common +package profile import ( "fmt" + "memos/common" "os" "path/filepath" "strconv" "strings" ) +// Profile is the configuration to start main server. type Profile struct { // Mode can be "prod" or "dev" Mode string `json:"mode"` @@ -69,6 +71,6 @@ func GetProfile() *Profile { Mode: mode, Port: port, DSN: dsn, - Version: Version, + Version: common.Version, } } diff --git a/server/server.go b/server/server.go index 81357d04..c5c3c676 100644 --- a/server/server.go +++ b/server/server.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "memos/common" + "memos/server/profile" "memos/store" "time" @@ -16,12 +16,12 @@ import ( type Server struct { e *echo.Echo - Profile *common.Profile + Profile *profile.Profile Store *store.Store } -func NewServer(profile *common.Profile) *Server { +func NewServer(profile *profile.Profile) *Server { e := echo.New() e.Debug = true e.HideBanner = true diff --git a/store/db/db.go b/store/db/db.go index 155e0f6d..5eb432cc 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -7,6 +7,7 @@ import ( "fmt" "io/fs" "memos/common" + "memos/server/profile" "os" "sort" @@ -20,6 +21,7 @@ var migrationFS embed.FS var seedFS embed.FS type DB struct { + // sqlite db connection instance Db *sql.DB // datasource name DSN string @@ -28,7 +30,7 @@ type DB struct { } // NewDB returns a new instance of DB associated with the given datasource name. -func NewDB(profile *common.Profile) *DB { +func NewDB(profile *profile.Profile) *DB { db := &DB{ DSN: profile.DSN, mode: profile.Mode, @@ -76,31 +78,9 @@ func (db *DB) Open() (err error) { } func (db *DB) migrate() error { - table, err := findTable(db, "migration_history") + err := db.compareMigrationHistory() if err != nil { - return err - } - if table == nil { - createTable(db, ` - CREATE TABLE migration_history ( - version TEXT NOT NULL PRIMARY KEY, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) - ); - `) - } - - migrationHistoryList, err := findMigrationHistoyList(db) - if err != nil { - return err - } - - if len(migrationHistoryList) == 0 { - createMigrationHistoy(db, common.Version) - } else { - migrationHistory := migrationHistoryList[0] - if migrationHistory.Version != common.Version { - createMigrationHistoy(db, common.Version) - } + return fmt.Errorf("failed to compare migration history, err=%w", err) } filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/*.sql", "migration")) @@ -154,15 +134,34 @@ func (db *DB) executeFile(FS embed.FS, name string) error { return tx.Commit() } -func FormatError(err error) error { - if err == nil { - return nil - } - - switch err { - case sql.ErrNoRows: - return errors.New("data not found") - default: +// compareMigrationHistory compares migration history data +func (db *DB) compareMigrationHistory() error { + table, err := findTable(db, "migration_history") + if err != nil { return err } + if table == nil { + createTable(db, ` + CREATE TABLE migration_history ( + version TEXT NOT NULL PRIMARY KEY, + created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) + ); + `) + } + + migrationHistoryList, err := findMigrationHistoryList(db) + 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) + } + } + + return nil } diff --git a/store/db/migration_history.go b/store/db/migration_history.go index 382f1ee9..b2c875fe 100644 --- a/store/db/migration_history.go +++ b/store/db/migration_history.go @@ -2,7 +2,6 @@ package db import ( "fmt" - "time" ) type MigrationHistory struct { @@ -10,7 +9,7 @@ type MigrationHistory struct { Version string } -func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) { +func findMigrationHistoryList(db *DB) ([]*MigrationHistory, error) { rows, err := db.Db.Query(` SELECT version, @@ -40,16 +39,14 @@ func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) { return migrationHistoryList, nil } -func createMigrationHistoy(db *DB, version string) error { +func createMigrationHistory(db *DB, version string) error { result, err := db.Db.Exec(` INSERT INTO migration_history ( - version, - created_ts + version ) - VALUES (?, ?) + VALUES (?) `, version, - time.Now().Unix(), ) if err != nil { return err diff --git a/store/db/table.go b/store/db/table.go index fe36f4a6..ba70eb6c 100644 --- a/store/db/table.go +++ b/store/db/table.go @@ -25,7 +25,7 @@ func findTable(db *DB, tableName string) (*Table, error) { args..., ) if err != nil { - return nil, FormatError(err) + return nil, err } defer rows.Close() @@ -36,14 +36,14 @@ func findTable(db *DB, tableName string) (*Table, error) { &table.Name, &table.SQL, ); err != nil { - return nil, FormatError(err) + return nil, err } tableList = append(tableList, &table) } if err := rows.Err(); err != nil { - return nil, FormatError(err) + return nil, err } if len(tableList) == 0 { diff --git a/store/store.go b/store/store.go index 34528b82..442eadef 100644 --- a/store/store.go +++ b/store/store.go @@ -2,17 +2,17 @@ package store import ( "database/sql" - "memos/common" + "memos/server/profile" ) // Store provides database access to all raw objects type Store struct { db *sql.DB - profile *common.Profile + profile *profile.Profile } // New creates a new instance of Store -func New(db *sql.DB, profile *common.Profile) *Store { +func New(db *sql.DB, profile *profile.Profile) *Store { return &Store{ db: db, profile: profile,