diff options
author | uvok cheetah | 2023-12-09 17:50:39 +0000 |
---|---|---|
committer | uvok cheetah | 2023-12-09 17:55:24 +0000 |
commit | 21c1bf9b63cd0ff01db000b13b46edd40ba6fec9 (patch) | |
tree | 58138445540c8c06bef01af9bc26e5e8844a79eb /persistence | |
parent | 5d1dcea0f50ebc9dbb4a5af787d12654abb6377b (diff) |
Add Migrate function for sqlite module
needed for tests
Diffstat (limited to 'persistence')
-rw-r--r-- | persistence/sqlite/sqliteRepository.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/persistence/sqlite/sqliteRepository.go b/persistence/sqlite/sqliteRepository.go index dfeb23a..9e0e3f5 100644 --- a/persistence/sqlite/sqliteRepository.go +++ b/persistence/sqlite/sqliteRepository.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "log" + "os" _ "modernc.org/sqlite" @@ -92,3 +93,28 @@ func (repo *sqliteRepository) checkInit() (*sql.DB, error) { } return db, nil } + +func (repo *sqliteRepository) Migrate() error { + db, err := repo.checkInit() + if err != nil { + return err + } + var schema_version int64 + err = db.QueryRow("PRAGMA user_version").Scan(&schema_version) + + if err != nil { + return err + } + // Initial / fresh table + if schema_version == 0 { + var sql []byte + sql, err = os.ReadFile("./0001_Create_Initial_Tables.sql") + if err != nil { + return err + } + sqlString := string(sql) + _, err = db.Exec(sqlString) + } + + return err +} |