Merge pull request #6 from dennisschoepf/refactor/database

Moves database initialization to OnInitialize
This commit is contained in:
Dennis Schoepf 2024-09-03 19:56:25 +02:00 committed by GitHub
commit 9bc607472f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 55 additions and 24 deletions

View file

@ -17,7 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd package cmd
import ( import (
"database/sql"
"fmt" "fmt"
"freed/internal/database" "freed/internal/database"
"os" "os"
@ -37,7 +36,6 @@ type Config struct {
type AppContext struct { type AppContext struct {
Config Config Config Config
DB *sql.DB
} }
var ( var (
@ -54,32 +52,19 @@ Where f[r]eed is different to other feed aggregators or bookmarking services is
The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration. The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration.
`, `,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(appContext.Config.DB.Path)
if err != nil {
fmt.Printf("Could not connect to the database: %s", err)
}
appContext.DB = db
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if appContext.DB != nil {
appContext.DB.Close()
}
},
} }
) )
func Execute() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig, initDB)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
} }
@ -115,3 +100,11 @@ func initConfig() {
os.Exit(1) os.Exit(1)
} }
} }
func initDB() {
err := database.Open(appContext.Config.DB.Path)
if err != nil {
fmt.Printf("Could not connect to the database: %s", err)
}
}

View file

@ -8,15 +8,18 @@ import (
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(path string) (*sql.DB, error) { func Open(path string) error {
var err error
dbOptions := "?_fk=on&_journal=WAL&sync=normal" dbOptions := "?_fk=on&_journal=WAL&sync=normal"
db, err := sql.Open("sqlite3", path+dbOptions) db, err = sql.Open("sqlite3", path+dbOptions)
if err != nil { if err != nil {
return nil, err return err
} }
migrations := migrate.EmbedFileSystemMigrationSource{ migrations := migrate.EmbedFileSystemMigrationSource{
@ -24,9 +27,13 @@ func Connect(path string) (*sql.DB, error) {
Root: "migrations", Root: "migrations",
} }
if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil { if _, migrateErr := migrate.Exec(db, "sqlite3", migrations, migrate.Up); migrateErr != nil {
return nil, err return migrateErr
} }
return db, nil if pingErr := db.Ping(); pingErr != nil {
return pingErr
}
return nil
} }

29
internal/database/feed.go Normal file
View file

@ -0,0 +1,29 @@
package database
type FeedType string
const (
RSS FeedType = "RSS"
)
type Feed struct {
Name string
Url string
FeedType FeedType
}
func (f Feed) Insert() (int64, error) {
result, err := db.Exec("INSERT INTO feed (name, url, type) VALUES (?,?,?)", f.Name, f.Url, f.FeedType)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}

View file

@ -16,7 +16,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package main package main
import "freed/cmd" import (
"freed/cmd"
)
func main() { func main() {
cmd.Execute() cmd.Execute()