From 024ddebc22784f03829c29312c0827af8058cc85 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Wed, 31 Dec 2025 11:36:11 -0500 Subject: [PATCH] run migrations on db init --- go.mod | 4 ++++ go.sum | 8 ++++++++ main.go | 30 +++++++++++------------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 1c4ab75..81f59fa 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,10 @@ require ( ) require ( + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/pressly/goose/v3 v3.26.0 // indirect + github.com/sethvargo/go-retry v0.3.0 // indirect + go.uber.org/multierr v1.11.0 // indirect golang.org/x/mod v0.30.0 // indirect golang.org/x/net v0.47.0 // indirect golang.org/x/sync v0.18.0 // indirect diff --git a/go.sum b/go.sum index dad74db..8225808 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,16 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs= github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= github.com/miekg/dns v1.1.69 h1:Kb7Y/1Jo+SG+a2GtfoFUfDkG//csdRPwRLkCsxDG9Sc= github.com/miekg/dns v1.1.69/go.mod h1:7OyjD9nEba5OkqQ/hB4fy3PIoxafSZJtducccIelz3g= +github.com/pressly/goose/v3 v3.26.0 h1:KJakav68jdH0WDvoAcj8+n61WqOIaPGgH0bJWS6jpmM= +github.com/pressly/goose/v3 v3.26.0/go.mod h1:4hC1KrritdCxtuFsqgs1R4AU5bWtTAf+cnWvfhf2DNY= +github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE= +github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk= golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc= golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY= diff --git a/main.go b/main.go index b52ba50..0beca5e 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "embed" "encoding/json" "fmt" "log" @@ -15,8 +16,12 @@ import ( "github.com/joho/godotenv" _ "github.com/mattn/go-sqlite3" "github.com/miekg/dns" + "github.com/pressly/goose/v3" ) +//go:embed migrations/*.sql +var embedMigrations embed.FS + type RecordRequest struct { Domain string `json:"domain"` IP string `json:"ip"` @@ -28,31 +33,18 @@ type UpstreamRequest struct { } func initDB(dbPath string) *sql.DB { - db, err := sql.Open("sqlite3", dbPath) if err != nil { log.Fatal(err) } - queryRecords := ` - CREATE TABLE IF NOT EXISTS records ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - domain TEXT NOT NULL UNIQUE, - ip TEXT NOT NULL, - record_type TEXT DEFAULT 'A' - );` - - queryUpstreams := ` - CREATE TABLE IF NOT EXISTS upstreams ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - address TEXT NOT NULL UNIQUE - );` - - if _, err := db.Exec(queryRecords); err != nil { - log.Fatalf("Error creating records table: %v", err) + if err := goose.SetDialect("sqlite3"); err != nil { + log.Fatal(err) } - if _, err := db.Exec(queryUpstreams); err != nil { - log.Fatalf("Error creating upstreams table: %v", err) + + goose.SetBaseFS(embedMigrations) + if err := goose.Up(db, "migrations"); err != nil { + log.Fatal(err) } return db