mirror of
https://github.com/usememos/memos.git
synced 2025-02-19 12:50:41 +01:00
chore: fix sqlite migrator
This commit is contained in:
parent
7791fb10d8
commit
c0422dea5b
@ -15,9 +15,10 @@ func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
// As mysql driver is not fully implemented, we use sqlite for now in prod mode.
|
// As mysql driver is not fully implemented, we use sqlite for now in prod mode.
|
||||||
if profile.Mode == "prod" {
|
if profile.Mode == "prod" && profile.Driver != "sqlite" {
|
||||||
driver, err = sqlite.NewDB(profile)
|
return nil, errors.New("Only SQLite is supported in prod mode")
|
||||||
} else {
|
}
|
||||||
|
|
||||||
switch profile.Driver {
|
switch profile.Driver {
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
driver, err = sqlite.NewDB(profile)
|
driver, err = sqlite.NewDB(profile)
|
||||||
@ -26,7 +27,6 @@ func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
|
|||||||
default:
|
default:
|
||||||
return nil, errors.New("unknown db driver")
|
return nil, errors.New("unknown db driver")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to create db driver")
|
return nil, errors.Wrap(err, "failed to create db driver")
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ var seedFS embed.FS
|
|||||||
|
|
||||||
// Migrate applies the latest schema to the database.
|
// Migrate applies the latest schema to the database.
|
||||||
func (d *DB) Migrate(ctx context.Context) error {
|
func (d *DB) Migrate(ctx context.Context) error {
|
||||||
|
currentVersion := version.GetCurrentVersion(d.profile.Mode)
|
||||||
if d.profile.Mode == "prod" {
|
if d.profile.Mode == "prod" {
|
||||||
_, err := os.Stat(d.profile.DSN)
|
_, err := os.Stat(d.profile.DSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -31,17 +32,27 @@ func (d *DB) Migrate(ctx context.Context) error {
|
|||||||
if err := d.applyLatestSchema(ctx); err != nil {
|
if err := d.applyLatestSchema(ctx); err != nil {
|
||||||
return errors.Wrap(err, "failed to apply latest schema")
|
return errors.Wrap(err, "failed to apply latest schema")
|
||||||
}
|
}
|
||||||
|
// 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")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return errors.Wrap(err, "failed to get db file stat")
|
return errors.Wrap(err, "failed to get db file stat")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If db file exists, we should check if we need to migrate the database.
|
// If db file exists, we should check if we need to migrate the database.
|
||||||
currentVersion := version.GetCurrentVersion(d.profile.Mode)
|
|
||||||
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to find migration history")
|
return errors.Wrap(err, "failed to find migration history")
|
||||||
}
|
}
|
||||||
|
// If no migration history, we should apply the latest version migration and upsert the migration history.
|
||||||
if len(migrationHistoryList) == 0 {
|
if len(migrationHistoryList) == 0 {
|
||||||
|
minorVersion := version.GetMinorVersion(currentVersion)
|
||||||
|
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
|
||||||
|
}
|
||||||
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
|
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
|
||||||
Version: currentVersion,
|
Version: currentVersion,
|
||||||
})
|
})
|
||||||
@ -60,7 +71,6 @@ func (d *DB) Migrate(ctx context.Context) error {
|
|||||||
|
|
||||||
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
|
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
|
||||||
minorVersionList := getMinorVersionList()
|
minorVersionList := getMinorVersionList()
|
||||||
|
|
||||||
// backup the raw database file before migration
|
// backup the raw database file before migration
|
||||||
rawBytes, err := os.ReadFile(d.profile.DSN)
|
rawBytes, err := os.ReadFile(d.profile.DSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,7 +81,6 @@ func (d *DB) Migrate(ctx context.Context) error {
|
|||||||
return errors.Wrap(err, "failed to write raw database file")
|
return errors.Wrap(err, "failed to write raw database file")
|
||||||
}
|
}
|
||||||
println("succeed to copy a backup database file")
|
println("succeed to copy a backup database file")
|
||||||
|
|
||||||
println("start migrate")
|
println("start migrate")
|
||||||
for _, minorVersion := range minorVersionList {
|
for _, minorVersion := range minorVersionList {
|
||||||
normalizedVersion := minorVersion + ".0"
|
normalizedVersion := minorVersion + ".0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user