From 49a21a04498b3c73a90118d2896ee51fb925eaf5 Mon Sep 17 00:00:00 2001 From: Liangzhao Che Date: Thu, 1 Jun 2023 20:07:02 +0800 Subject: [PATCH] feat: add migrate sql data --- internal/infrastructure/sqlite.go | 10 +++++++++- scripts/sqliteMigrations.sql | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 scripts/sqliteMigrations.sql diff --git a/internal/infrastructure/sqlite.go b/internal/infrastructure/sqlite.go index 841c8fb..191f97e 100644 --- a/internal/infrastructure/sqlite.go +++ b/internal/infrastructure/sqlite.go @@ -5,6 +5,7 @@ import ( "bill-go-fiber/internal/label" "gorm.io/driver/sqlite" "gorm.io/gorm" + "os" ) func ConnectToSqlite() (*gorm.DB, error) { @@ -24,7 +25,14 @@ func ConnectToSqlite() (*gorm.DB, error) { panic("failed to migrate database.\n") } - db.Create(&label.Label{ID: 1, Name: "bill"}) + // migrate data + go func() { + sql, err := os.ReadFile("scripts/sqliteMigrations.sql") + if err != nil { + panic("failed to read migration sql file: " + err.Error()) + } + db.Exec(string(sql)) + }() return db, nil } diff --git a/scripts/sqliteMigrations.sql b/scripts/sqliteMigrations.sql new file mode 100644 index 0000000..2b4fc93 --- /dev/null +++ b/scripts/sqliteMigrations.sql @@ -0,0 +1,8 @@ +INSERT INTO labels (id, type, name, count) +VALUES (1, '', 'bill', 0), + (2, '', 'bill', 0), + (3, '', 'bill', 0), + (4, '', 'bill', 0), + (5, '', 'bill', 0), + (6, '', 'bill', 0), + (7, '', 'bill', 0);