feat: schema migration handler (#100)

* chore: update about site dialog

* feat: schema migration

* chore: lint with golangci
This commit is contained in:
boojack 2022-07-02 10:47:16 +08:00 committed by GitHub
parent 3c58953e56
commit 536627007d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 245 additions and 91 deletions

View file

@ -1,4 +1,61 @@
package common package common
import (
"strconv"
"strings"
)
// Version is the service current released version. // Version is the service current released version.
// Semantic versioning: https://semver.org/
var Version = "0.1.3" var Version = "0.1.3"
// DevVersion is the service current development version.
var DevVersion = "0.2.0"
func GetCurrentVersion(mode string) string {
if mode == "dev" {
return DevVersion
}
return Version
}
func GetMinorVersion(version string) string {
versionList := strings.Split(version, ".")
if len(versionList) < 3 {
return ""
}
return versionList[0] + "." + versionList[1]
}
// convSemanticVersionToInt converts version string to int.
func convSemanticVersionToInt(version string) int {
versionList := strings.Split(version, ".")
if len(versionList) < 3 {
return 0
}
major, err := strconv.Atoi(versionList[0])
if err != nil {
return 0
}
minor, err := strconv.Atoi(versionList[1])
if err != nil {
return 0
}
patch, err := strconv.Atoi(versionList[2])
if err != nil {
return 0
}
return major*10000 + minor*100 + patch
}
// IsVersionGreaterThanOrEqualTo returns true if version is greater than or equal to target.
func IsVersionGreaterOrEqualThan(version, target string) bool {
return convSemanticVersionToInt(version) >= convSemanticVersionToInt(target)
}
// IsVersionGreaterThan returns true if version is greater than target.
func IsVersionGreaterThan(version, target string) bool {
return convSemanticVersionToInt(version) > convSemanticVersionToInt(target)
}

View file

@ -176,8 +176,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
} }
c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
return nil
}) })
g.GET("/memo/amount", func(c echo.Context) error { g.GET("/memo/amount", func(c echo.Context) error {

View file

@ -72,6 +72,6 @@ func GetProfile() *Profile {
Mode: mode, Mode: mode,
Port: port, Port: port,
DSN: dsn, DSN: dsn,
Version: common.Version, Version: common.GetCurrentVersion(mode),
} }
} }

View file

@ -118,7 +118,10 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
c.Response().Writer.WriteHeader(http.StatusOK) c.Response().Writer.WriteHeader(http.StatusOK)
c.Response().Writer.Header().Set("Content-Type", resource.Type) c.Response().Writer.Header().Set("Content-Type", resource.Type)
c.Response().Writer.Write(resource.Blob) if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write resource blob").SetInternal(err)
}
return nil return nil
}) })
@ -135,7 +138,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
} }
c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
return nil
}) })
} }

View file

@ -109,7 +109,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
} }
c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
return nil
}) })
} }

View file

@ -261,7 +261,10 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
c.Response().Writer.WriteHeader(http.StatusOK) c.Response().Writer.WriteHeader(http.StatusOK)
c.Response().Writer.Header().Set("Content-Type", resource.Type) c.Response().Writer.Header().Set("Content-Type", resource.Type)
c.Response().Writer.Write(resource.Blob) if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write response").SetInternal(err)
}
return nil return nil
}) })
} }

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"regexp"
"sort" "sort"
"github.com/usememos/memos/common" "github.com/usememos/memos/common"
@ -52,51 +53,108 @@ func (db *DB) Open() (err error) {
} }
db.Db = sqlDB db.Db = sqlDB
// If db file not exists, we should migrate and seed the database. // If mode is dev, we should migrate and seed the database.
if _, err := os.Stat(db.DSN); errors.Is(err, os.ErrNotExist) { if db.mode == "dev" {
if err := db.migrate(); err != nil { if err := db.applyLatestSchema(); err != nil {
return fmt.Errorf("failed to migrate: %w", err) return fmt.Errorf("failed to apply latest schema: %w", err)
} }
// If mode is dev, then seed the database. if err := db.seed(); err != nil {
if db.mode == "dev" { return fmt.Errorf("failed to seed: %w", err)
if err := db.seed(); err != nil {
return fmt.Errorf("failed to seed: %w", err)
}
} }
} else { } else {
// If db file exists and mode is dev, we should migrate and seed the database. // If db file not exists, we should migrate the database.
if db.mode == "dev" { if _, err := os.Stat(db.DSN); errors.Is(err, os.ErrNotExist) {
if err := db.migrate(); err != nil { err := db.applyLatestSchema()
return fmt.Errorf("failed to migrate: %w", err) if err != nil {
return fmt.Errorf("failed to apply latest schema: %w", err)
} }
if err := db.seed(); err != nil { } else {
return fmt.Errorf("failed to seed: %w", err) err := db.createMigrationHistoryTable()
if err != nil {
return fmt.Errorf("failed to create migration_history table: %w", err)
}
currentVersion := common.GetCurrentVersion(db.mode)
migrationHistory, err := findMigrationHistory(db.Db, &MigrationHistoryFind{})
if err != nil {
return err
}
if migrationHistory == nil {
migrationHistory, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{
Version: currentVersion,
Statement: "",
})
if err != nil {
return err
}
}
if common.IsVersionGreaterThan(currentVersion, migrationHistory.Version) {
minorVersionList := getMinorVersionList()
for _, minorVersion := range minorVersionList {
normalizedVersion := minorVersion + ".0"
if common.IsVersionGreaterThan(normalizedVersion, migrationHistory.Version) && common.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {
err := db.applyMigrationForMinorVersion(minorVersion)
if err != nil {
return fmt.Errorf("failed to apply minor version migration: %w", err)
}
}
}
} }
} }
} }
err = db.compareMigrationHistory()
if err != nil {
return fmt.Errorf("failed to compare migration history, err=%w", err)
}
return err return err
} }
func (db *DB) migrate() error { const (
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/*.sql", "migration")) latestSchemaFileName = "LATEST__SCHEMA.sql"
)
func (db *DB) applyLatestSchema() error {
latestSchemaPath := fmt.Sprintf("%s/%s", "migration", latestSchemaFileName)
buf, err := migrationFS.ReadFile(latestSchemaPath)
if err != nil {
return fmt.Errorf("failed to read latest schema %q, error %w", latestSchemaPath, err)
}
stmt := string(buf)
if err := db.execute(stmt); err != nil {
return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err)
}
return nil
}
func (db *DB) applyMigrationForMinorVersion(minorVersion string) error {
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/%s/*.sql", "migration", minorVersion))
if err != nil { if err != nil {
return err return err
} }
sort.Strings(filenames) sort.Strings(filenames)
migrationStmt := ""
// Loop over all migration files and execute them in order. // Loop over all migration files and execute them in order.
for _, filename := range filenames { for _, filename := range filenames {
if err := db.executeFile(migrationFS, filename); err != nil { buf, err := migrationFS.ReadFile(filename)
return fmt.Errorf("migrate error: name=%q err=%w", filename, err) if err != nil {
return fmt.Errorf("failed to read minor version migration file, filename=%s err=%w", filename, err)
}
stmt := string(buf)
migrationStmt += stmt
if err := db.execute(stmt); err != nil {
return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err)
} }
} }
// upsert the newest version to migration_history
if _, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{
Version: minorVersion + ".0",
Statement: migrationStmt,
}); err != nil {
return err
}
return nil return nil
} }
@ -110,64 +168,84 @@ func (db *DB) seed() error {
// Loop over all seed files and execute them in order. // Loop over all seed files and execute them in order.
for _, filename := range filenames { for _, filename := range filenames {
if err := db.executeFile(seedFS, filename); err != nil { buf, err := seedFS.ReadFile(filename)
return fmt.Errorf("seed error: name=%q err=%w", filename, err) if err != nil {
return fmt.Errorf("failed to read seed file, filename=%s err=%w", filename, err)
}
stmt := string(buf)
if err := db.execute(stmt); err != nil {
return fmt.Errorf("seed error: statement:%s err=%w", stmt, err)
} }
} }
return nil return nil
} }
// executeFile runs a single seed file within a transaction. // excecute runs a single SQL statement within a transaction.
func (db *DB) executeFile(FS embed.FS, name string) error { func (db *DB) execute(stmt string) error {
tx, err := db.Db.Begin() tx, err := db.Db.Begin()
if err != nil { if err != nil {
return err return err
} }
defer tx.Rollback() defer tx.Rollback()
// Read and execute SQL file. if _, err := tx.Exec(stmt); err != nil {
if buf, err := fs.ReadFile(FS, name); err != nil {
return err
} else if _, err := tx.Exec(string(buf)); err != nil {
return err return err
} }
return tx.Commit() return tx.Commit()
} }
// compareMigrationHistory compares migration history data // minorDirRegexp is a regular expression for minor version directory.
func (db *DB) compareMigrationHistory() error { var minorDirRegexp = regexp.MustCompile(`^migration/[0-9]+\.[0-9]+$`)
func getMinorVersionList() []string {
minorVersionList := []string{}
if err := fs.WalkDir(migrationFS, "migration", func(path string, file fs.DirEntry, err error) error {
if err != nil {
return err
}
if file.IsDir() && minorDirRegexp.MatchString(path) {
minorVersionList = append(minorVersionList, file.Name())
}
return nil
}); err != nil {
panic(err)
}
sort.Strings(minorVersionList)
return minorVersionList
}
// createMigrationHistoryTable creates the migration_history table if it doesn't exist.
func (db *DB) createMigrationHistoryTable() error {
table, err := findTable(db.Db, "migration_history") table, err := findTable(db.Db, "migration_history")
if err != nil { if err != nil {
return err return err
} }
if table == nil {
if err := createTable(db.Db, `
CREATE TABLE migration_history (
version TEXT NOT NULL PRIMARY KEY,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
);
`); err != nil {
return err
}
}
currentVersion := common.Version // TODO(steven): Drop the migration_history table if it exists temporarily.
migrationHistoryFind := MigrationHistoryFind{ if table != nil {
Version: currentVersion, err = db.execute(`
} DROP TABLE IF EXISTS migration_history;
migrationHistory, err := findMigrationHistory(db.Db, &migrationHistoryFind) `)
if err != nil {
return err
}
if migrationHistory == nil {
// ...do schema migration,
// then upsert the newest version to migration_history
_, err := upsertMigrationHistory(db.Db, currentVersion)
if err != nil { if err != nil {
return err return err
} }
} }
err = createTable(db.Db, `
CREATE TABLE 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 {
return err
}
return nil return nil
} }

View file

@ -1,5 +0,0 @@
DROP TABLE IF EXISTS `memo_organizer`;
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `shortcut`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `user`;

View file

@ -1,3 +1,10 @@
-- drop all tables
DROP TABLE IF EXISTS `memo_organizer`;
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `shortcut`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `user`;
-- user -- user
CREATE TABLE user ( CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,

View file

@ -6,18 +6,26 @@ import (
) )
type MigrationHistory struct { type MigrationHistory struct {
CreatedTs int64
Version string Version string
Statement string
CreatedTs int64
}
type MigrationHistoryCreate struct {
Version string
Statement string
} }
type MigrationHistoryFind struct { type MigrationHistoryFind struct {
Version string Version *string
} }
func findMigrationHistoryList(db *sql.DB, find *MigrationHistoryFind) ([]*MigrationHistory, error) { func findMigrationHistoryList(db *sql.DB, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
where, args = append(where, "version = ?"), append(args, find.Version) if v := find.Version; v != nil {
where, args = append(where, "version = ?"), append(args, *v)
}
rows, err := db.Query(` rows, err := db.Query(`
SELECT SELECT
@ -63,18 +71,21 @@ func findMigrationHistory(db *sql.DB, find *MigrationHistoryFind) (*MigrationHis
} }
} }
func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, error) { func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*MigrationHistory, error) {
row, err := db.Query(` row, err := db.Query(`
INSERT INTO migration_history ( INSERT INTO migration_history (
version version,
statement
) )
VALUES (?) VALUES (?, ?)
ON CONFLICT(version) DO UPDATE ON CONFLICT(version) DO UPDATE
SET SET
version=EXCLUDED.version version=EXCLUDED.version,
RETURNING version, created_ts statement=EXCLUDED.statement
RETURNING version, statement, created_ts
`, `,
version, create.Version,
create.Statement,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -82,9 +93,10 @@ func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, erro
defer row.Close() defer row.Close()
row.Next() row.Next()
migrationHistory := MigrationHistory{} var migrationHistory MigrationHistory
if err := row.Scan( if err := row.Scan(
&migrationHistory.Version, &migrationHistory.Version,
&migrationHistory.Statement,
&migrationHistory.CreatedTs, &migrationHistory.CreatedTs,
); err != nil { ); err != nil {
return nil, err return nil, err

View file

@ -45,13 +45,19 @@ const AboutSiteDialog: React.FC<Props> = ({ destroy }: Props) => {
</p> </p>
<br /> <br />
<p> <p>
<a href="https://github.com/usememos/memos">🏗 Source code</a>, and built by <a href="https://github.com/boojack">Steven 🐯</a>. <a className="mr-2" href="https://github.com/usememos/memos">
🏗 Source code
</a>
<Only when={profile !== undefined}>
<>
version:
<span className="pre-text">
{profile?.version}-{profile?.mode}
</span>
🎉
</>
</Only>
</p> </p>
<Only when={profile !== undefined}>
<p className="updated-time-text">
version: <span className="pre-text">{profile?.version}</span> 🎉
</p>
</Only>
</div> </div>
</> </>
); );

View file

@ -11,14 +11,10 @@
> p { > p {
@apply my-1; @apply my-1;
&.updated-time-text {
@apply flex flex-row justify-start items-center w-full text-sm mt-3 pt-2 border-t-2 border-t-gray-200 text-gray-600 whitespace-pre-wrap font-mono;
}
} }
.pre-text { .pre-text {
@apply font-mono; @apply font-mono mx-1;
} }
a { a {