mirror of
https://github.com/R0m1k3/FlowReader.git
synced 2026-10-11 17:28:05 +02:00
fix(backend): solve 500 errors with auto-migrations and standardized filenames
This commit is contained in:
1 parent
8ed1782de6
commit
502b76c5d6
6 files changed
+83
-36
No files matched your search
@@ -6,6 +6,11 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -36,26 +41,53 @@ func Connect(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
||||
}
|
||||
|
||||
// RunMigrations executes pending database migrations.
|
||||
// Note: For production, use golang-migrate CLI. This is for development convenience.
|
||||
func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
// Check if schema_migrations table exists
|
||||
// Check if users table exists
|
||||
var exists bool
|
||||
err := pool.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'schema_migrations'
|
||||
AND table_name = 'users'
|
||||
)
|
||||
`).Scan(&exists)
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking schema_migrations: %w", err)
|
||||
return fmt.Errorf("checking users table: %w", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
log.Println("Migrations table not found. Run 'make migrate-up' to apply migrations.")
|
||||
if exists {
|
||||
log.Println("Database already initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Println("Database migrations verified")
|
||||
log.Println("Initializing database and applying migrations...")
|
||||
|
||||
// Read migration files
|
||||
entries, err := os.ReadDir("./migrations")
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading migrations dir: %w", err)
|
||||
}
|
||||
|
||||
var upFiles []string
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".up.sql") {
|
||||
upFiles = append(upFiles, entry.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(upFiles)
|
||||
|
||||
for _, file := range upFiles {
|
||||
log.Printf("Applying migration: %s", file)
|
||||
content, err := os.ReadFile(filepath.Join("./migrations", file))
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading migration %s: %w", file, err)
|
||||
}
|
||||
|
||||
if _, err := pool.Exec(ctx, string(content)); err != nil {
|
||||
return fmt.Errorf("executing migration %s: %w", file, err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("All migrations applied successfully")
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user