diff --git a/main.go b/cmd/go-migrate/main.go similarity index 87% rename from main.go rename to cmd/go-migrate/main.go index 274c0c3..1ea03dc 100644 --- a/main.go +++ b/cmd/go-migrate/main.go @@ -7,7 +7,7 @@ import ( "log" "os" - "git.petrovv.com/go-migrate/lib" + "git.petrovv.com/go-migrate" "github.com/jackc/pgx/v5/pgxpool" "github.com/joho/godotenv" ) @@ -44,13 +44,13 @@ func generateMigrations(schemaDir string) { defer db.Close() ctx := context.Background() - migrations, err := lib.GetMigrations(ctx, db, schemaDir) + migrations, err := migrate.GetMigrations(ctx, db, schemaDir) if err != nil { log.Fatal(err) } if len(migrations) == 0 { - fmt.Println("✓ Database schema is up to date") + fmt.Println("Database schema is up to date") return } @@ -66,13 +66,13 @@ func applyMigrations(schemaDir string) { defer db.Close() ctx := context.Background() - migrations, err := lib.GetMigrations(ctx, db, schemaDir) + migrations, err := migrate.GetMigrations(ctx, db, schemaDir) if err != nil { log.Fatal(err) } if len(migrations) == 0 { - fmt.Println("✓ Database schema is up to date") + fmt.Println("Database schema is up to date") 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 { diff --git a/lib/lib.go b/migrate.go similarity index 99% rename from lib/lib.go rename to migrate.go index 3b61d23..7b6493e 100644 --- a/lib/lib.go +++ b/migrate.go @@ -1,4 +1,4 @@ -package lib +package migrate import ( "context" @@ -981,10 +981,13 @@ func parseForeignKey(def string) *ForeignKey { return fk } +// LoadDesiredSchema loads schema definitions from SQL files in the given directory func LoadDesiredSchema(dir string) (*Schema, error) { 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) { // Load current database schema currentSchema, err := GetCurrentSchema(ctx, db)