working on sqlite migrations
This commit is contained in:
parent
921d9caebc
commit
f58d75ebe8
6 changed files with 107 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -19,4 +19,4 @@ go.work.sum
|
||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
wargh.db
|
||||||
|
|
|
||||||
64
db/db.go
Normal file
64
db/db.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
|
|
@ -1,3 +1,7 @@
|
||||||
module wargh
|
module wargh
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
||||||
|
)
|
||||||
|
|
|
||||||
4
go.sum
Normal file
4
go.sum
Normal file
|
|
@ -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=
|
||||||
26
main.go
Normal file
26
main.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
8
notes.txt
Normal file
8
notes.txt
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Reference in a new issue