Add intial database migrations
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
db/blogging.db
|
||||||
@@ -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
|
||||||
|
);
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module nkeller.dev/blog_software
|
||||||
|
|
||||||
|
go 1.26.5
|
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.48 // direct
|
||||||
@@ -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=
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
@@ -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[:])
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user