make package
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.petrovv.com/go-migrate/lib"
|
"git.petrovv.com/go-migrate"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
@@ -44,13 +44,13 @@ func generateMigrations(schemaDir string) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
migrations, err := lib.GetMigrations(ctx, db, schemaDir)
|
migrations, err := migrate.GetMigrations(ctx, db, schemaDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(migrations) == 0 {
|
if len(migrations) == 0 {
|
||||||
fmt.Println("✓ Database schema is up to date")
|
fmt.Println("Database schema is up to date")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,13 +66,13 @@ func applyMigrations(schemaDir string) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
migrations, err := lib.GetMigrations(ctx, db, schemaDir)
|
migrations, err := migrate.GetMigrations(ctx, db, schemaDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(migrations) == 0 {
|
if len(migrations) == 0 {
|
||||||
fmt.Println("✓ Database schema is up to date")
|
fmt.Println("Database schema is up to date")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ func applyMigrations(schemaDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("✓ All migrations applied successfully")
|
fmt.Println("All migrations applied successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDBConnection() *pgxpool.Pool {
|
func getDBConnection() *pgxpool.Pool {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package lib
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -981,10 +981,13 @@ func parseForeignKey(def string) *ForeignKey {
|
|||||||
return fk
|
return fk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadDesiredSchema loads schema definitions from SQL files in the given directory
|
||||||
func LoadDesiredSchema(dir string) (*Schema, error) {
|
func LoadDesiredSchema(dir string) (*Schema, error) {
|
||||||
return loadDesiredSchema(dir)
|
return loadDesiredSchema(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMigrations compares the current database schema with the desired schema
|
||||||
|
// and returns a list of SQL migration statements
|
||||||
func GetMigrations(ctx context.Context, db *pgxpool.Pool, schemaDir string) ([]string, error) {
|
func GetMigrations(ctx context.Context, db *pgxpool.Pool, schemaDir string) ([]string, error) {
|
||||||
// Load current database schema
|
// Load current database schema
|
||||||
currentSchema, err := GetCurrentSchema(ctx, db)
|
currentSchema, err := GetCurrentSchema(ctx, db)
|
||||||
Reference in New Issue
Block a user