feat: add migrate sql data
This commit is contained in:
parent
8540470c62
commit
49a21a0449
|
@ -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
|
||||
}
|
||||
|
|
8
scripts/sqliteMigrations.sql
Normal file
8
scripts/sqliteMigrations.sql
Normal file
|
@ -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);
|
Loading…
Reference in New Issue
Block a user