summaryrefslogtreecommitdiff
path: root/template/database
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2025-01-05 16:39:13 -0800
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2025-01-05 16:39:13 -0800
commitd25ec27fb1c3df175e1b98af1fdc26881d68a1ab (patch)
treea79c729b92c0d0a52b19b37c3a506b988f1e0b80 /template/database
downloadwhois-d25ec27fb1c3df175e1b98af1fdc26881d68a1ab.tar.gz
whois-d25ec27fb1c3df175e1b98af1fdc26881d68a1ab.zip
initial commit by simponic-infra
Diffstat (limited to 'template/database')
-rw-r--r--template/database/conn.go17
-rw-r--r--template/database/migrate.go39
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
+}