From c0b6ecbf1c7118b2d23a7b333d55b591cd12b37d Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 18:21:18 +0200 Subject: [PATCH 1/4] Moves database initialization to OnInitialize --- cmd/root.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ab8236d..d31aa54 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,32 +54,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. `, - 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() { err := rootCmd.Execute() + if err != nil { os.Exit(1) } } func init() { - cobra.OnInitialize(initConfig) + cobra.OnInitialize(initConfig, initDB) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") } @@ -115,3 +102,13 @@ func initConfig() { os.Exit(1) } } + +func initDB() { + db, err := database.Connect(appContext.Config.DB.Path) + + if err != nil { + fmt.Printf("Could not connect to the database: %s", err) + } + + appContext.DB = db +} From ccc85259cb0044d34fe7b919a367cbe271cbee40 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 19:49:03 +0200 Subject: [PATCH 2/4] Changes database initialization --- internal/database/database.go | 19 +++++++++++++------ internal/database/feed.go | 29 +++++++++++++++++++++++++++++ main.go | 4 +++- 3 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 internal/database/feed.go diff --git a/internal/database/database.go b/internal/database/database.go index 25f2740..1b8a1aa 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -8,15 +8,18 @@ import ( migrate "github.com/rubenv/sql-migrate" ) +var db *sql.DB + //go:embed migrations/* 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" - db, err := sql.Open("sqlite3", path+dbOptions) + db, err = sql.Open("sqlite3", path+dbOptions) if err != nil { - return nil, err + return err } migrations := migrate.EmbedFileSystemMigrationSource{ @@ -24,9 +27,13 @@ func Connect(path string) (*sql.DB, error) { Root: "migrations", } - if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil { - return nil, err + if _, migrateErr := migrate.Exec(db, "sqlite3", migrations, migrate.Up); migrateErr != nil { + return migrateErr } - return db, nil + if pingErr := db.Ping(); pingErr != nil { + return pingErr + } + + return nil } diff --git a/internal/database/feed.go b/internal/database/feed.go new file mode 100644 index 0000000..48c74a2 --- /dev/null +++ b/internal/database/feed.go @@ -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 +} diff --git a/main.go b/main.go index 01c938c..9396a14 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,9 @@ along with this program. If not, see . */ package main -import "freed/cmd" +import ( + "freed/cmd" +) func main() { cmd.Execute() From 4436b83a157e4c4ac27f96504244bef71f904a40 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 19:49:38 +0200 Subject: [PATCH 3/4] Adapts database initialization --- cmd/root.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d31aa54..05d1d33 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,6 @@ along with this program. If not, see . package cmd import ( - "database/sql" "fmt" "freed/internal/database" "os" @@ -37,7 +36,6 @@ type Config struct { type AppContext struct { Config Config - DB *sql.DB } var ( @@ -67,7 +65,6 @@ func Execute() { func init() { cobra.OnInitialize(initConfig, initDB) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") } @@ -104,11 +101,8 @@ func initConfig() { } func initDB() { - db, err := database.Connect(appContext.Config.DB.Path) - - if err != nil { - fmt.Printf("Could not connect to the database: %s", err) + if err := database.Open(appContext.Config.DB.Path); err != nil { + fmt.Printf("Could not connect to database: %s", err) + os.Exit(1) } - - appContext.DB = db } From 9fdc8cad49fabcfb7504661ed39dc85aca2a9be2 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 19:52:58 +0200 Subject: [PATCH 4/4] Fixes db init call in root cmd --- cmd/root.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 05d1d33..5397fa5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,6 +65,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig, initDB) + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") } @@ -101,8 +102,9 @@ func initConfig() { } func initDB() { - if err := database.Open(appContext.Config.DB.Path); err != nil { - fmt.Printf("Could not connect to database: %s", err) - os.Exit(1) + err := database.Open(appContext.Config.DB.Path) + + if err != nil { + fmt.Printf("Could not connect to the database: %s", err) } }