chore: update server profile

pull/65/head
boojack 3 years ago
parent 0b3c77c79c
commit c3c2882dc5

@ -1,8 +1,8 @@
package api package api
import "memos/common" import "memos/server/profile"
type SystemStatus struct { type SystemStatus struct {
Owner *User `json:"owner"` Owner *User `json:"owner"`
Profile *common.Profile `json:"profile"` Profile *profile.Profile `json:"profile"`
} }

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
"memos/common"
"memos/server" "memos/server"
"memos/server/profile"
"memos/store" "memos/store"
DB "memos/store/db" DB "memos/store/db"
) )
@ -22,7 +22,7 @@ const (
) )
type Main struct { type Main struct {
profile *common.Profile profile *profile.Profile
} }
func (m *Main) Run() error { func (m *Main) Run() error {
@ -44,7 +44,7 @@ func (m *Main) Run() error {
} }
func Execute() { func Execute() {
profile := common.GetProfile() profile := profile.GetProfile()
m := Main{ m := Main{
profile: profile, profile: profile,
} }

@ -1,13 +1,15 @@
package common package profile
import ( import (
"fmt" "fmt"
"memos/common"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
) )
// Profile is the configuration to start main server.
type Profile struct { type Profile struct {
// Mode can be "prod" or "dev" // Mode can be "prod" or "dev"
Mode string `json:"mode"` Mode string `json:"mode"`
@ -69,6 +71,6 @@ func GetProfile() *Profile {
Mode: mode, Mode: mode,
Port: port, Port: port,
DSN: dsn, DSN: dsn,
Version: Version, Version: common.Version,
} }
} }

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"memos/common" "memos/server/profile"
"memos/store" "memos/store"
"time" "time"
@ -16,12 +16,12 @@ import (
type Server struct { type Server struct {
e *echo.Echo e *echo.Echo
Profile *common.Profile Profile *profile.Profile
Store *store.Store Store *store.Store
} }
func NewServer(profile *common.Profile) *Server { func NewServer(profile *profile.Profile) *Server {
e := echo.New() e := echo.New()
e.Debug = true e.Debug = true
e.HideBanner = true e.HideBanner = true

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"memos/common" "memos/common"
"memos/server/profile"
"os" "os"
"sort" "sort"
@ -20,6 +21,7 @@ var migrationFS embed.FS
var seedFS embed.FS var seedFS embed.FS
type DB struct { type DB struct {
// sqlite db connection instance
Db *sql.DB Db *sql.DB
// datasource name // datasource name
DSN string DSN string
@ -28,7 +30,7 @@ type DB struct {
} }
// NewDB returns a new instance of DB associated with the given datasource name. // 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{ db := &DB{
DSN: profile.DSN, DSN: profile.DSN,
mode: profile.Mode, mode: profile.Mode,
@ -76,31 +78,9 @@ func (db *DB) Open() (err error) {
} }
func (db *DB) migrate() error { func (db *DB) migrate() error {
table, err := findTable(db, "migration_history") err := db.compareMigrationHistory()
if err != nil { if err != nil {
return err return fmt.Errorf("failed to compare migration history, err=%w", 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)
}
} }
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/*.sql", "migration")) 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() return tx.Commit()
} }
func FormatError(err error) error { // compareMigrationHistory compares migration history data
if err == nil { func (db *DB) compareMigrationHistory() error {
return nil 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'))
);
`)
} }
switch err { migrationHistoryList, err := findMigrationHistoryList(db)
case sql.ErrNoRows: if err != nil {
return errors.New("data not found")
default:
return err 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
} }

@ -2,7 +2,6 @@ package db
import ( import (
"fmt" "fmt"
"time"
) )
type MigrationHistory struct { type MigrationHistory struct {
@ -10,7 +9,7 @@ type MigrationHistory struct {
Version string Version string
} }
func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) { func findMigrationHistoryList(db *DB) ([]*MigrationHistory, error) {
rows, err := db.Db.Query(` rows, err := db.Db.Query(`
SELECT SELECT
version, version,
@ -40,16 +39,14 @@ func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) {
return migrationHistoryList, nil return migrationHistoryList, nil
} }
func createMigrationHistoy(db *DB, version string) error { func createMigrationHistory(db *DB, version string) error {
result, err := db.Db.Exec(` result, err := db.Db.Exec(`
INSERT INTO migration_history ( INSERT INTO migration_history (
version, version
created_ts
) )
VALUES (?, ?) VALUES (?)
`, `,
version, version,
time.Now().Unix(),
) )
if err != nil { if err != nil {
return err return err

@ -25,7 +25,7 @@ func findTable(db *DB, tableName string) (*Table, error) {
args..., args...,
) )
if err != nil { if err != nil {
return nil, FormatError(err) return nil, err
} }
defer rows.Close() defer rows.Close()
@ -36,14 +36,14 @@ func findTable(db *DB, tableName string) (*Table, error) {
&table.Name, &table.Name,
&table.SQL, &table.SQL,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, err
} }
tableList = append(tableList, &table) tableList = append(tableList, &table)
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
return nil, FormatError(err) return nil, err
} }
if len(tableList) == 0 { if len(tableList) == 0 {

@ -2,17 +2,17 @@ package store
import ( import (
"database/sql" "database/sql"
"memos/common" "memos/server/profile"
) )
// Store provides database access to all raw objects // Store provides database access to all raw objects
type Store struct { type Store struct {
db *sql.DB db *sql.DB
profile *common.Profile profile *profile.Profile
} }
// New creates a new instance of Store // 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{ return &Store{
db: db, db: db,
profile: profile, profile: profile,

Loading…
Cancel
Save