diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-05 16:39:13 -0800 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-05 16:39:13 -0800 |
commit | d25ec27fb1c3df175e1b98af1fdc26881d68a1ab (patch) | |
tree | a79c729b92c0d0a52b19b37c3a506b988f1e0b80 /template/database | |
download | whois-d25ec27fb1c3df175e1b98af1fdc26881d68a1ab.tar.gz whois-d25ec27fb1c3df175e1b98af1fdc26881d68a1ab.zip |
initial commit by simponic-infra
Diffstat (limited to 'template/database')
-rw-r--r-- | template/database/conn.go | 17 | ||||
-rw-r--r-- | template/database/migrate.go | 39 |
2 files changed, 56 insertions, 0 deletions
diff --git a/template/database/conn.go b/template/database/conn.go new file mode 100644 index 0000000..be27586 --- /dev/null +++ b/template/database/conn.go @@ -0,0 +1,17 @@ +package database + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "log" +) + +func MakeConn(databasePath *string) *sql.DB { + log.Println("opening database at", *databasePath, "with foreign keys enabled") + dbConn, err := sql.Open("sqlite3", *databasePath+"?_foreign_keys=on") + if err != nil { + panic(err) + } + + return dbConn +} diff --git a/template/database/migrate.go b/template/database/migrate.go new file mode 100644 index 0000000..64457bc --- /dev/null +++ b/template/database/migrate.go @@ -0,0 +1,39 @@ +package database + +import ( + "log" + + "database/sql" + + _ "github.com/mattn/go-sqlite3" +) + +type Migrator func(*sql.DB) (*sql.DB, error) + +func DoNothing(dbConn *sql.DB) (*sql.DB, error) { + log.Println("doing nothing") + + _, err := dbConn.Exec(`SELECT 0;`) + if err != nil { + return dbConn, err + } + + return dbConn, nil +} + +func Migrate(dbConn *sql.DB) (*sql.DB, error) { + log.Println("migrating database") + + migrations := []Migrator{ + DoNothing, + } + + for _, migration := range migrations { + dbConn, err := migration(dbConn) + if err != nil { + return dbConn, err + } + } + + return dbConn, nil +} |