From f58d75ebe806c56a1f5226168a22b1cb70a024fa Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:35:06 +0200 Subject: [PATCH] working on sqlite migrations --- .gitignore | 2 +- db/db.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++++ go.sum | 4 ++++ main.go | 26 ++++++++++++++++++++++ notes.txt | 8 +++++++ 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 db/db.go create mode 100644 go.sum create mode 100644 main.go create mode 100644 notes.txt diff --git a/.gitignore b/.gitignore index 7ee591f..4b619e8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,4 @@ go.work.sum # env file .env - +wargh.db diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..46afc2d --- /dev/null +++ b/db/db.go @@ -0,0 +1,64 @@ +package db + +import ( + "database/sql" + "log" + "os" + + _ "github.com/mattn/go-sqlite3" +) + +type DBConfig struct { + DBPath string + MigrationsPath string +} + +var config *DBConfig = nil + +func Init(_config *DBConfig) { + config = _config +} + +func checkConfig() { + if config == nil { + log.Fatal("DBConfig not initialized! Call db.Init!") + os.Exit(1) + } +} + +func open(dbPath string) (*sql.DB, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + /* if database is empty. we must create + '_migration` table and apply all releveant migrations + */ + _, err = db.Query("SELECT id FROM `_migration` LIMIT 1;") + if err != nil { + // create table + _, err = db.Exec(` + CREATE TABLE _migration ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + seq INTEGER NOT NULL UNIQUE, + name TEXT NOT NULL UNIQUE + );`) + // can't create migration table - fatal + if err != nil { + return nil, err + } + } + return db, nil +} + +func Open() (*sql.DB, error) { + checkConfig() + + db, err := open(config.DBPath) + if err != nil { + return nil, err + } + + return db, nil +} diff --git a/go.mod b/go.mod index 7f0944a..b64cfc1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module wargh go 1.23.2 + +require ( + github.com/mattn/go-sqlite3 v1.14.24 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0de2071 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +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.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c12892b --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + "os" + "wargh/db" +) + +const DB_PATH = "wargh.db" + +func main() { + db.Init(&db.DBConfig{ + DBPath: DB_PATH, + MigrationsPath: "", + }) + + DB, err := db.Open() + if err != nil { + log.Fatal(err) + os.Exit(1) + } + defer DB.Close() + + _, err = DB.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT NOT NULL);") + log.Fatal(err) +} diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..2297028 --- /dev/null +++ b/notes.txt @@ -0,0 +1,8 @@ +wargh is web-application to manage recursively wgetted websites. + +That implies a need for database to track record of archived sites, their names and files. +For such database shall be used sqlite. + +But most important is admin UI, that starts and manages ongoing wget jobs. +Output of wget process should be piped through websocket to frontend, where its rendered +with https://github.com/xtermjs/xterm.js \ No newline at end of file