diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-07-13 01:32:23 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-07-13 01:32:23 +0900 |
| commit | 3754f11c33a6b4ac756a0d89426764fd9032cf53 (patch) | |
| tree | 47bda36abe8340eb6b0eea988c55a90202436409 /backend/main.go | |
| parent | 732c71f7c2e5f0a37c03c2efedcfa8e5360e3fda (diff) | |
| download | feedaka-3754f11c33a6b4ac756a0d89426764fd9032cf53.tar.gz feedaka-3754f11c33a6b4ac756a0d89426764fd9032cf53.tar.zst feedaka-3754f11c33a6b4ac756a0d89426764fd9032cf53.zip | |
feat(backend): implement db schema migrationv0.2.0
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/backend/main.go b/backend/main.go index 0efe66a..ecfcbae 100644 --- a/backend/main.go +++ b/backend/main.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "embed" + "flag" "fmt" "log" "net/http" @@ -36,15 +37,8 @@ var ( queries *db.Queries //go:embed public/* publicFS embed.FS - //go:embed db/schema.sql - dbSchema string ) -func initDB(db *sql.DB) error { - _, err := db.Exec(dbSchema) - return err -} - func fetchOneFeed(feedID int64, url string, ctx context.Context) error { log.Printf("Fetching %s...\n", url) fp := gofeed.NewParser() @@ -150,7 +144,14 @@ func scheduled(ctx context.Context, d time.Duration, fn func()) { } func main() { + // Parse command line flags + var migrate = flag.Bool("migrate", false, "Run database migrations") + flag.Parse() + port := os.Getenv("FEEDAKA_PORT") + if port == "" { + port = "8080" + } var err error database, err = sql.Open("sqlite3", "feedaka.db") @@ -159,7 +160,18 @@ func main() { } defer database.Close() - err = initDB(database) + // Migration mode + if *migrate { + log.Println("Running database migrations...") + err = db.RunMigrations(database) + if err != nil { + log.Fatalf("Migration failed: %v", err) + } + log.Println("Migrations completed successfully") + return + } + + err = db.ValidateSchemaVersion(database) if err != nil { log.Fatal(err) } |
