Restructures database access
Makes it simpler as long as the application is just the CLI
This commit is contained in:
parent
db7ed2fa3b
commit
b70a232343
2 changed files with 15 additions and 10 deletions
|
|
@ -3,21 +3,22 @@ package database
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
//go:embed migrations/*
|
//go:embed migrations/*
|
||||||
var dbMigrations embed.FS
|
var dbMigrations embed.FS
|
||||||
|
|
||||||
func Connect(filename string) (*sql.DB, error) {
|
func Init(filename string) error {
|
||||||
dbOptions := "?_fk=on&_journal=WAL&sync=normal"
|
dbOptions := "?_fk=on&_journal=WAL&sync=normal"
|
||||||
db, err := sql.Open("sqlite3", filename+dbOptions)
|
db, err := sql.Open("sqlite3", filename+dbOptions)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
migrations := migrate.EmbedFileSystemMigrationSource{
|
migrations := migrate.EmbedFileSystemMigrationSource{
|
||||||
|
|
@ -25,13 +26,9 @@ func Connect(filename string) (*sql.DB, error) {
|
||||||
Root: "migrations",
|
Root: "migrations",
|
||||||
}
|
}
|
||||||
|
|
||||||
_, migrateErr := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
|
if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil {
|
||||||
|
return err
|
||||||
if migrateErr != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Applied migrations - Database is ready!")
|
return db.Ping()
|
||||||
|
|
||||||
return db, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
internal/database/feed.go
Normal file
8
internal/database/feed.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue