2023-09-27 06:56:20 +03:00
|
|
|
package sqlite
|
2022-02-03 10:32:03 +03:00
|
|
|
|
|
|
|
import (
|
2022-08-07 05:17:56 +03:00
|
|
|
"context"
|
2022-02-03 10:32:03 +03:00
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2022-05-01 06:06:29 +03:00
|
|
|
"os"
|
2022-07-02 05:47:16 +03:00
|
|
|
"regexp"
|
2022-02-03 10:32:03 +03:00
|
|
|
"sort"
|
2022-07-22 18:21:12 +03:00
|
|
|
"time"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-09-17 17:55:13 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-09-12 12:25:34 +03:00
|
|
|
"github.com/usememos/memos/server/version"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
|
|
|
|
2022-03-28 18:38:12 +03:00
|
|
|
//go:embed migration
|
|
|
|
var migrationFS embed.FS
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
//go:embed seed
|
|
|
|
var seedFS embed.FS
|
|
|
|
|
2023-08-26 02:33:45 +03:00
|
|
|
// Migrate applies the latest schema to the database.
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) Migrate(ctx context.Context) error {
|
2023-10-05 19:34:06 +03:00
|
|
|
currentVersion := version.GetCurrentVersion(d.profile.Mode)
|
2023-09-27 06:56:20 +03:00
|
|
|
if d.profile.Mode == "prod" {
|
|
|
|
_, err := os.Stat(d.profile.DSN)
|
2022-11-27 03:52:43 +03:00
|
|
|
if err != nil {
|
2023-06-20 07:18:04 +03:00
|
|
|
// If db file not exists, we should create a new one with latest schema.
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := d.applyLatestSchema(ctx); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to apply latest schema")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
2023-10-05 19:34:06 +03:00
|
|
|
// Upsert the newest version to migration_history.
|
|
|
|
if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
|
|
|
|
Version: currentVersion,
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to upsert migration history")
|
|
|
|
}
|
2023-06-20 07:18:04 +03:00
|
|
|
} else {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to get db file stat")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If db file exists, we should check if we need to migrate the database.
|
2023-09-27 06:56:20 +03:00
|
|
|
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
2023-01-14 03:00:07 +03:00
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to find migration history")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
2023-10-05 19:34:06 +03:00
|
|
|
// If no migration history, we should apply the latest version migration and upsert the migration history.
|
2023-06-20 07:18:04 +03:00
|
|
|
if len(migrationHistoryList) == 0 {
|
2023-10-05 19:34:06 +03:00
|
|
|
minorVersion := version.GetMinorVersion(currentVersion)
|
|
|
|
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
|
|
|
|
}
|
2023-09-27 06:56:20 +03:00
|
|
|
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
|
2023-06-20 07:18:04 +03:00
|
|
|
Version: currentVersion,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to upsert migration history")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
|
|
|
return nil
|
2022-11-27 03:52:43 +03:00
|
|
|
}
|
2023-01-14 03:00:07 +03:00
|
|
|
|
2023-06-20 07:18:04 +03:00
|
|
|
migrationHistoryVersionList := []string{}
|
|
|
|
for _, migrationHistory := range migrationHistoryList {
|
|
|
|
migrationHistoryVersionList = append(migrationHistoryVersionList, migrationHistory.Version)
|
|
|
|
}
|
|
|
|
sort.Sort(version.SortVersion(migrationHistoryVersionList))
|
|
|
|
latestMigrationHistoryVersion := migrationHistoryVersionList[len(migrationHistoryVersionList)-1]
|
2022-07-22 18:21:12 +03:00
|
|
|
|
2023-06-20 07:18:04 +03:00
|
|
|
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
|
|
|
|
minorVersionList := getMinorVersionList()
|
|
|
|
// backup the raw database file before migration
|
2023-09-27 06:56:20 +03:00
|
|
|
rawBytes, err := os.ReadFile(d.profile.DSN)
|
2023-06-20 07:18:04 +03:00
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to read raw database file")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
2023-09-27 06:56:20 +03:00
|
|
|
backupDBFilePath := fmt.Sprintf("%s/memos_%s_%d_backup.db", d.profile.Data, d.profile.Version, time.Now().Unix())
|
2023-06-20 07:18:04 +03:00
|
|
|
if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to write raw database file")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
|
|
|
println("succeed to copy a backup database file")
|
|
|
|
println("start migrate")
|
|
|
|
for _, minorVersion := range minorVersionList {
|
|
|
|
normalizedVersion := minorVersion + ".0"
|
|
|
|
if version.IsVersionGreaterThan(normalizedVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {
|
|
|
|
println("applying migration for", normalizedVersion)
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to apply minor version migration")
|
2023-06-20 07:18:04 +03:00
|
|
|
}
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-20 07:18:04 +03:00
|
|
|
println("end migrate")
|
2022-07-22 18:21:12 +03:00
|
|
|
|
2023-06-20 07:18:04 +03:00
|
|
|
// remove the created backup db file after migrate succeed
|
|
|
|
if err := os.Remove(backupDBFilePath); err != nil {
|
|
|
|
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
|
|
|
|
}
|
2022-05-01 06:06:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-19 08:36:45 +03:00
|
|
|
} else {
|
2023-02-19 11:34:15 +03:00
|
|
|
// In non-prod mode, we should always migrate the database.
|
2023-09-27 06:56:20 +03:00
|
|
|
if _, err := os.Stat(d.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
|
|
|
if err := d.applyLatestSchema(ctx); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to apply latest schema")
|
2023-02-19 08:36:45 +03:00
|
|
|
}
|
|
|
|
// In demo mode, we should seed the database.
|
2023-09-27 06:56:20 +03:00
|
|
|
if d.profile.Mode == "demo" {
|
|
|
|
if err := d.seed(ctx); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to seed")
|
2023-02-19 08:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-10-26 18:00:09 +03:00
|
|
|
return nil
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
latestSchemaFileName = "LATEST__SCHEMA.sql"
|
|
|
|
)
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) applyLatestSchema(ctx context.Context) error {
|
2023-02-19 11:34:15 +03:00
|
|
|
schemaMode := "dev"
|
2023-09-27 06:56:20 +03:00
|
|
|
if d.profile.Mode == "prod" {
|
2023-02-19 11:34:15 +03:00
|
|
|
schemaMode = "prod"
|
|
|
|
}
|
2023-09-29 07:47:49 +03:00
|
|
|
latestSchemaPath := fmt.Sprintf("migration/%s/%s", schemaMode, latestSchemaFileName)
|
2022-07-02 05:47:16 +03:00
|
|
|
buf, err := migrationFS.ReadFile(latestSchemaPath)
|
2022-05-21 19:59:22 +03:00
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "failed to read latest schema %q", latestSchemaPath)
|
2022-05-21 19:59:22 +03:00
|
|
|
}
|
2022-07-02 05:47:16 +03:00
|
|
|
stmt := string(buf)
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := d.execute(ctx, stmt); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "migrate error: %s", stmt)
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
2022-07-01 15:39:48 +03:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error {
|
2022-07-25 16:17:46 +03:00
|
|
|
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/%s/*.sql", "migration/prod", minorVersion))
|
2022-03-28 18:38:12 +03:00
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to read ddl files")
|
2022-03-28 18:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(filenames)
|
2022-07-02 05:47:16 +03:00
|
|
|
migrationStmt := ""
|
2022-03-28 18:38:12 +03:00
|
|
|
|
|
|
|
// Loop over all migration files and execute them in order.
|
|
|
|
for _, filename := range filenames {
|
2022-07-02 05:47:16 +03:00
|
|
|
buf, err := migrationFS.ReadFile(filename)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "failed to read minor version migration file, filename=%s", filename)
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
stmt := string(buf)
|
|
|
|
migrationStmt += stmt
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := d.execute(ctx, stmt); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "migrate error: %s", stmt)
|
2022-03-28 18:38:12 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-02 05:47:16 +03:00
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
// Upsert the newest version to migration_history.
|
2022-11-27 03:52:43 +03:00
|
|
|
version := minorVersion + ".0"
|
2023-09-27 06:56:20 +03:00
|
|
|
if _, err = d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
|
2022-11-27 03:52:43 +03:00
|
|
|
Version: version,
|
2022-07-02 05:47:16 +03:00
|
|
|
}); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "failed to upsert migration history with version: %s", version)
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
return nil
|
2022-03-28 18:38:12 +03:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) seed(ctx context.Context) error {
|
2022-02-03 10:32:03 +03:00
|
|
|
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to read seed files")
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(filenames)
|
|
|
|
|
|
|
|
// Loop over all seed files and execute them in order.
|
|
|
|
for _, filename := range filenames {
|
2022-07-02 05:47:16 +03:00
|
|
|
buf, err := seedFS.ReadFile(filename)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "failed to read seed file, filename=%s", filename)
|
2022-07-02 05:47:16 +03:00
|
|
|
}
|
|
|
|
stmt := string(buf)
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := d.execute(ctx, stmt); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrapf(err, "seed error: %s", stmt)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
// execute runs a single SQL statement within a transaction.
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) execute(ctx context.Context, stmt string) error {
|
2023-09-27 06:56:20 +03:00
|
|
|
tx, err := d.db.Begin()
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2022-08-07 05:17:56 +03:00
|
|
|
if _, err := tx.ExecContext(ctx, stmt); err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Wrap(err, "failed to execute statement")
|
2022-08-07 05:17:56 +03:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
return tx.Commit()
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-07-02 05:47:16 +03:00
|
|
|
// minorDirRegexp is a regular expression for minor version directory.
|
2022-07-25 16:17:46 +03:00
|
|
|
var minorDirRegexp = regexp.MustCompile(`^migration/prod/[0-9]+\.[0-9]+$`)
|
2022-07-02 05:47:16 +03:00
|
|
|
|
|
|
|
func getMinorVersionList() []string {
|
|
|
|
minorVersionList := []string{}
|
|
|
|
|
|
|
|
if err := fs.WalkDir(migrationFS, "migration", func(path string, file fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
2022-07-01 15:08:25 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-07-02 05:47:16 +03:00
|
|
|
if file.IsDir() && minorDirRegexp.MatchString(path) {
|
|
|
|
minorVersionList = append(minorVersionList, file.Name())
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-07-02 05:47:16 +03:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
2022-07-01 20:06:28 +03:00
|
|
|
}
|
2022-07-02 05:47:16 +03:00
|
|
|
|
2023-01-07 08:58:42 +03:00
|
|
|
sort.Sort(version.SortVersion(minorVersionList))
|
2022-07-02 05:47:16 +03:00
|
|
|
|
|
|
|
return minorVersionList
|
|
|
|
}
|