Restructures database access

Makes it simpler as long as the application is just the CLI
This commit is contained in:
Dennis Schoepf 2024-08-30 17:07:14 +02:00
parent db7ed2fa3b
commit b70a232343
2 changed files with 15 additions and 10 deletions

View file

@ -3,21 +3,22 @@ package database
import (
"database/sql"
"embed"
"fmt"
_ "github.com/mattn/go-sqlite3"
migrate "github.com/rubenv/sql-migrate"
)
var db *sql.DB
//go:embed migrations/*
var dbMigrations embed.FS
func Connect(filename string) (*sql.DB, error) {
func Init(filename string) error {
dbOptions := "?_fk=on&_journal=WAL&sync=normal"
db, err := sql.Open("sqlite3", filename+dbOptions)
if err != nil {
return nil, err
return err
}
migrations := migrate.EmbedFileSystemMigrationSource{
@ -25,13 +26,9 @@ func Connect(filename string) (*sql.DB, error) {
Root: "migrations",
}
_, migrateErr := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
if migrateErr != nil {
return nil, err
if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil {
return err
}
fmt.Println("Applied migrations - Database is ready!")
return db, nil
return db.Ping()
}

View file

@ -0,0 +1,8 @@
package database
import "fmt"
func AddFeed(url string) error {
fmt.Printf("Trying to add new feed by URL: %s", url)
return nil
}