From 6a3b052fa2f9306a32485381ce6abb6d7ed4fbbe Mon Sep 17 00:00:00 2001 From: steven Date: Mon, 3 Oct 2022 08:49:20 +0800 Subject: [PATCH] chore: don't clean data in dev mode --- store/db/db.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/store/db/db.go b/store/db/db.go index ebf7a941..52db6007 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -51,22 +51,22 @@ func (db *DB) Open(ctx context.Context) (err error) { db.Db = sqlDB // If mode is dev, we should migrate and seed the database. if db.profile.Mode == "dev" { - if err := db.applyLatestSchema(ctx); err != nil { - return fmt.Errorf("failed to apply latest schema: %w", err) - } - if err := db.seed(ctx); err != nil { - return fmt.Errorf("failed to seed: %w", err) + if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) { + if err := db.applyLatestSchema(ctx); err != nil { + return fmt.Errorf("failed to apply latest schema: %w", err) + } + if err := db.seed(ctx); err != nil { + return fmt.Errorf("failed to seed: %w", err) + } } } else { // If db file not exists, we should migrate the database. if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) { - err := db.applyLatestSchema(ctx) - if err != nil { + if err := db.applyLatestSchema(ctx); err != nil { return fmt.Errorf("failed to apply latest schema: %w", err) } } else { - err := db.createMigrationHistoryTable(ctx) - if err != nil { + if err := db.createMigrationHistoryTable(ctx); err != nil { return fmt.Errorf("failed to create migration_history table: %w", err) }