Skip to content

Commit

Permalink
run migrations from script
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Dec 22, 2023
1 parent 084806c commit 323e931
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 6 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Install golang-migrate
run: |
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
which migrate
- name: Run migrations
run: make migrateup
run: go run db/scripts/migrate.go

- name: Run Unit tests
run: make testci
Expand Down
107 changes: 107 additions & 0 deletions db/scripts/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"database/sql"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/aradwann/eenergy/util"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
config, err := util.LoadConfig(".", "app")
if err != nil {
fmt.Fprintf(os.Stderr, "error loading config: %v\n", err)
os.Exit(1)
}
db, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer db.Close()

runDBMigrations(db, config.MigrationsURL)

}

func runDBMigrations(db *sql.DB, migrationsURL string) {

driver, err := pgx.WithInstance(db, &pgx.Config{})
if err != nil {
// log.Fatal().Msg("cannot create postgres driver")
fmt.Printf("cannot create postgres driver %s", err)
}
migration, err := migrate.NewWithDatabaseInstance(
migrationsURL,
"eenergy", driver)
if err != nil {
// log.Fatal().Msg("cannot create new migrate instance")
fmt.Printf("cannot create new migrate instance %s", err)
}
migration.Up()
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
// log.Fatal().Msg("failed to run migrate up")
fmt.Printf("failed to run migrate up %s", err)

}

// log.Info().Msg("DB migrated successfully")
fmt.Println("DB migrated successfully")

// Run unversioned migrations
err = runUnversionedMigrations(db, "./db/migrations/procs")
if err != nil {
fmt.Println("Error applying unversioned migrations:", err)
os.Exit(1)
}

fmt.Println("Unversioned migrations applied successfully")

}

func runUnversionedMigrations(db *sql.DB, migrationDir string) error {
// Get a list of SQL files in the migration directory
files, err := filepath.Glob(filepath.Join(migrationDir, "*.sql"))
if err != nil {
return err
}

// Sort files to ensure execution order
// Note: You may need a custom sorting logic if file names include version numbers
// For simplicity, we assume alphabetical order here.
// Sorting ensures that the files are executed in the correct order.
sortFiles(files)

// Execute each SQL file
for _, file := range files {
contents, err := os.ReadFile(file)
if err != nil {
return err
}

// Execute the SQL content
_, err = db.Exec(string(contents))
if err != nil {
return fmt.Errorf("error executing SQL file %s: %w", file, err)
}

fmt.Printf("Executed migration: %s\n", file)
}

return nil
}

// Simple alphabetical sorting function
func sortFiles(files []string) {
sort.Slice(files, func(i, j int) bool {
return strings.Compare(files[i], files[j]) < 0
})
}

0 comments on commit 323e931

Please sign in to comment.