mirror of
https://github.com/usememos/memos.git
synced 2025-02-19 12:50:41 +01:00
parent
b34aded376
commit
07e82c3f4a
@ -50,13 +50,18 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
|||||||
db.DBInstance = sqliteDB
|
db.DBInstance = sqliteDB
|
||||||
|
|
||||||
if db.profile.Mode == "prod" {
|
if db.profile.Mode == "prod" {
|
||||||
// If db file not exists, we should migrate the database.
|
_, err := os.Stat(db.profile.DSN)
|
||||||
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
if err != nil {
|
||||||
|
// If db file not exists, we should create a new one with latest schema.
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
if err := db.applyLatestSchema(ctx); err != nil {
|
if err := db.applyLatestSchema(ctx); err != nil {
|
||||||
return fmt.Errorf("failed to apply latest schema: %w", err)
|
return fmt.Errorf("failed to apply latest schema, err: %w", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to get db file stat, err: %w", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// If db file exists, we should check if we need to migrate the database.
|
||||||
currentVersion := version.GetCurrentVersion(db.profile.Mode)
|
currentVersion := version.GetCurrentVersion(db.profile.Mode)
|
||||||
migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,6 +115,7 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
|||||||
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
|
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// In non-prod mode, we should always migrate the database.
|
// In non-prod mode, we should always migrate the database.
|
||||||
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
||||||
|
@ -67,7 +67,7 @@ func findMigrationHistoryList(ctx context.Context, tx *sql.Tx, find *MigrationHi
|
|||||||
FROM
|
FROM
|
||||||
migration_history
|
migration_history
|
||||||
WHERE ` + strings.Join(where, " AND ") + `
|
WHERE ` + strings.Join(where, " AND ") + `
|
||||||
ORDER BY version DESC
|
ORDER BY created_ts DESC
|
||||||
`
|
`
|
||||||
rows, err := tx.QueryContext(ctx, query, args...)
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user