mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update database migrator
This commit is contained in:
@ -181,7 +181,6 @@ func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
||||
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
where, args := []string{"id = " + placeholder(1)}, []any{delete.ID}
|
||||
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
||||
println("stmt", stmt, delete.ID)
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to delete memo")
|
||||
|
@ -1,19 +1,3 @@
|
||||
-- drop all tables first (PostgreSQL style)
|
||||
DROP TABLE IF EXISTS migration_history CASCADE;
|
||||
DROP TABLE IF EXISTS system_setting CASCADE;
|
||||
DROP TABLE IF EXISTS "user" CASCADE;
|
||||
DROP TABLE IF EXISTS user_setting CASCADE;
|
||||
DROP TABLE IF EXISTS memo CASCADE;
|
||||
DROP TABLE IF EXISTS memo_organizer CASCADE;
|
||||
DROP TABLE IF EXISTS memo_relation CASCADE;
|
||||
DROP TABLE IF EXISTS resource CASCADE;
|
||||
DROP TABLE IF EXISTS tag CASCADE;
|
||||
DROP TABLE IF EXISTS activity CASCADE;
|
||||
DROP TABLE IF EXISTS storage CASCADE;
|
||||
DROP TABLE IF EXISTS idp CASCADE;
|
||||
DROP TABLE IF EXISTS inbox CASCADE;
|
||||
DROP TABLE IF EXISTS webhook CASCADE;
|
||||
|
||||
-- migration_history
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
|
@ -1,19 +1,3 @@
|
||||
-- drop all tables first (PostgreSQL style)
|
||||
DROP TABLE IF EXISTS migration_history CASCADE;
|
||||
DROP TABLE IF EXISTS system_setting CASCADE;
|
||||
DROP TABLE IF EXISTS "user" CASCADE;
|
||||
DROP TABLE IF EXISTS user_setting CASCADE;
|
||||
DROP TABLE IF EXISTS memo CASCADE;
|
||||
DROP TABLE IF EXISTS memo_organizer CASCADE;
|
||||
DROP TABLE IF EXISTS memo_relation CASCADE;
|
||||
DROP TABLE IF EXISTS resource CASCADE;
|
||||
DROP TABLE IF EXISTS tag CASCADE;
|
||||
DROP TABLE IF EXISTS activity CASCADE;
|
||||
DROP TABLE IF EXISTS storage CASCADE;
|
||||
DROP TABLE IF EXISTS idp CASCADE;
|
||||
DROP TABLE IF EXISTS inbox CASCADE;
|
||||
DROP TABLE IF EXISTS webhook CASCADE;
|
||||
|
||||
-- migration_history
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
|
@ -15,13 +15,13 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
//go:embed migration
|
||||
var migrationFS embed.FS
|
||||
|
||||
const (
|
||||
latestSchemaFileName = "LATEST__SCHEMA.sql"
|
||||
)
|
||||
|
||||
//go:embed migration
|
||||
var migrationFS embed.FS
|
||||
|
||||
func (d *DB) Migrate(ctx context.Context) error {
|
||||
if d.profile.IsDev() {
|
||||
return d.nonProdMigrate(ctx)
|
||||
@ -54,8 +54,6 @@ func (d *DB) nonProdMigrate(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
println("no tables in the database. start migration")
|
||||
|
||||
buf, err := migrationFS.ReadFile("migration/dev/" + latestSchemaFileName)
|
||||
if err != nil {
|
||||
return errors.Errorf("failed to read latest schema file: %s", err)
|
||||
@ -66,12 +64,6 @@ func (d *DB) nonProdMigrate(ctx context.Context) error {
|
||||
return errors.Errorf("failed to exec SQL %s: %s", stmt, err)
|
||||
}
|
||||
|
||||
// In demo mode, we should seed the database.
|
||||
if d.profile.Mode == "demo" {
|
||||
if err := d.seed(ctx); err != nil {
|
||||
return errors.Wrap(err, "failed to seed")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -153,35 +145,6 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:embed seed
|
||||
var seedFS embed.FS
|
||||
|
||||
func (d *DB) seed(ctx context.Context) error {
|
||||
filenames, err := fs.Glob(seedFS, "seed/*.sql")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read seed files")
|
||||
}
|
||||
|
||||
sort.Strings(filenames)
|
||||
// Loop over all seed files and execute them in order.
|
||||
for _, filename := range filenames {
|
||||
buf, err := seedFS.ReadFile(filename)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to read seed file, filename=%s", filename)
|
||||
}
|
||||
|
||||
for _, stmt := range strings.Split(string(buf), ";") {
|
||||
if strings.TrimSpace(stmt) == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := d.db.ExecContext(ctx, stmt); err != nil {
|
||||
return errors.Wrapf(err, "seed error: %s", stmt)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// minorDirRegexp is a regular expression for minor version directory.
|
||||
var minorDirRegexp = regexp.MustCompile(`^migration/prod/[0-9]+\.[0-9]+$`)
|
||||
|
||||
|
@ -1,4 +0,0 @@
|
||||
TRUNCATE TABLE memo_organizer;
|
||||
TRUNCATE TABLE resource;
|
||||
TRUNCATE TABLE memo;
|
||||
TRUNCATE TABLE "user";
|
Reference in New Issue
Block a user