Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto migration #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func main() {
os.Exit(1)
}

migrationsDir := "./migrations"
Copy link
Contributor

@prakashchoudhary07 prakashchoudhary07 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were going to remove the ./migrations and use cmd/migrations, as we were going to let bun handle it?
For versioning and other feature?

if err := utils.ApplyMigrations(db, migrationsDir); err != nil {
log.Fatalf("failed to apply migrations: %v", err)
os.Exit(1)
}


port := flag.String("port", os.Getenv("PORT"), "server address to listen on")
flag.Parse()

Expand Down
8 changes: 8 additions & 0 deletions migrations/20230912194711_init.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
BEGIN;

-- Check and drop the tiny_url table first if it exists.
DROP TABLE IF EXISTS tiny_url;

-- Now it is safe to drop the users table.
DROP TABLE IF EXISTS users;

COMMIT;
52 changes: 52 additions & 0 deletions utils/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package utils

import (
"log"
"os"
"path/filepath"
"strings"

"github.com/uptrace/bun"
)

func ApplyMigrations(db *bun.DB, migrationsDir string) error {
log.Println("Starting migration process...")
files, err := os.ReadDir(migrationsDir)
if err != nil {
log.Printf("Error reading migration directory: %v", err)
return err
}

for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".sql") {
log.Printf("Applying migration from file: %s", file.Name())
content, err := os.ReadFile(filepath.Join(migrationsDir, file.Name()))
if err != nil {
log.Printf("Error reading file %s: %v", file.Name(), err)
return err
}

tx, err := db.Begin()
if err != nil {
log.Printf("Error starting transaction: %v", err)
return err
}

_, err = tx.Exec(string(content))
if err != nil {
tx.Rollback()
log.Printf("Error executing migration %s: %v", file.Name(), err)
return err
}

err = tx.Commit()
if err != nil {
log.Printf("Error committing transaction for %s: %v", file.Name(), err)
return err
}
log.Printf("Successfully applied migration: %s", file.Name())
}
}
log.Println("Migration process completed successfully.")
return nil
}
Loading