From e55ea4df7b2bb1e8486b00202502929e8e387f9d Mon Sep 17 00:00:00 2001 From: Nicholas Keller Date: Fri, 24 Jul 2026 00:40:16 -0400 Subject: [PATCH] Add intial database migrations --- .gitignore | 1 + README.md | 1 + db/migrations/1784776258.sql | 19 +++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ src/db/update.go | 24 ++++++++++++++++++++++++ src/main.go | 24 ++++++++++++++++++++++++ 7 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 db/migrations/1784776258.sql create mode 100644 go.mod create mode 100644 go.sum create mode 100644 src/db/update.go create mode 100644 src/main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f86a49a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +db/blogging.db \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e768b5 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/db/migrations/1784776258.sql b/db/migrations/1784776258.sql new file mode 100644 index 0000000..4e26d4a --- /dev/null +++ b/db/migrations/1784776258.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS posts ( + id integer primary key autoincrement not null, + created_at datetime default CURRENT_TIMESTAMP, + creator_id integer not null references users, + + title text not null, + description text, + content text, + tags text + -- public boolean +); + +CREATE TABLE IF NOT EXISTS users ( + id integer primary key autoincrement not null, + created_at datetime default CURRENT_TIMESTAMP, + name text, + username text not null unique, + password text +); \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..626b0ac --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module nkeller.dev/blog_software + +go 1.26.5 + +require github.com/mattn/go-sqlite3 v1.14.48 // direct diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cdf11e6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.48 h1:7XHIgl0a8HwOaiK4E47ozLkST78rR9+OtNGx27D/TFs= +github.com/mattn/go-sqlite3 v1.14.48/go.mod h1:6JTjA44L93a0QCyJef5YvlPoKXntQPjzWv5gtm9sB6w= diff --git a/src/db/update.go b/src/db/update.go new file mode 100644 index 0000000..fffa359 --- /dev/null +++ b/src/db/update.go @@ -0,0 +1,24 @@ +package db + +import ( + "database/sql" + "os" + "log" +) + +func CheckError(e error) { + if e != nil { + panic(e) + } +} + +func Update(db *sql.DB, migrations []string) { + for _, x := range migrations { + data, err := os.ReadFile("./db/migrations/" + x + ".sql") + CheckError(err) + res, err := db.Exec(string(data)) + _ = res + CheckError(err) + log.Print("Applied migration " + x) + } +} \ No newline at end of file diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..6b7d2ff --- /dev/null +++ b/src/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "database/sql" + dbpkg "nkeller.dev/blog_software/src/db" + _ "github.com/mattn/go-sqlite3" +) + +type User struct { + ID int + Name string +} + +func main() { + // DB and Migrations + db, err := sql.Open("sqlite3", "./db/blogging.db") + dbpkg.CheckError(err) + defer db.Close() + migrations := [...]string{ + "1784776258", + } + dbpkg.Update(db, migrations[:]) + +}