From 21c1bf9b63cd0ff01db000b13b46edd40ba6fec9 Mon Sep 17 00:00:00 2001 From: uvok cheetah Date: Sat, 9 Dec 2023 17:50:39 +0000 Subject: Add Migrate function for sqlite module needed for tests --- persistence/sqlite/sqliteRepository.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'persistence/sqlite') 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 +} -- cgit v1.2.3