From 5fca0d0e6fe87313a8270e061138b1ed35629b4a Mon Sep 17 00:00:00 2001 From: Alexis Viscogliosi Date: Fri, 8 Nov 2024 11:11:39 +0100 Subject: [PATCH] fix: problems --- docs/docs/03-cli/04-migrate.md | 1 + docs/docs/03-cli/05-rollback.md | 1 + docs/docs/03-cli/07-schema.md | 11 +++++++++ docs/docs/04-api/100-migrating-in-go.md | 30 +++++++++++++++++++------ pkg/schema/detect_migrations.go | 1 - 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 docs/docs/03-cli/07-schema.md diff --git a/docs/docs/03-cli/04-migrate.md b/docs/docs/03-cli/04-migrate.md index f8fefc4..6ea849e 100644 --- a/docs/docs/03-cli/04-migrate.md +++ b/docs/docs/03-cli/04-migrate.md @@ -18,4 +18,5 @@ amigo migrate - `--timeout` is the timeout for the migration (default is 2m0s). - `--version` will apply a specific version. The format is `20240502083700` or `20240502083700_name.go`. - `--continue-on-error` will not rollback the migration if an error occurs. +- `-d` dump the schema after migrating diff --git a/docs/docs/03-cli/05-rollback.md b/docs/docs/03-cli/05-rollback.md index 6691db5..6f31454 100644 --- a/docs/docs/03-cli/05-rollback.md +++ b/docs/docs/03-cli/05-rollback.md @@ -18,3 +18,4 @@ amigo rollback - `--version` will rollback a specific version. The format is `20240502083700` or `20240502083700_name.go`. - `--steps` will rollback the last `n` migrations. (default is 1) - `--continue-on-error` will not rollback the migration if an error occurs. +- `-d` dump the schema after migrating \ No newline at end of file diff --git a/docs/docs/03-cli/07-schema.md b/docs/docs/03-cli/07-schema.md new file mode 100644 index 0000000..2c26385 --- /dev/null +++ b/docs/docs/03-cli/07-schema.md @@ -0,0 +1,11 @@ +# Schema + +The `schema` command allow to dump the schema to `db/schema.sql` (default path) + +Database supported: +- postgres (via pg_dump) + +## Flags + +- `--schema-db-dump-schema` will change the schema (default public) to dump +- `--schema-out-path` will change the path to output the file (db/schema.sql) \ No newline at end of file diff --git a/docs/docs/04-api/100-migrating-in-go.md b/docs/docs/04-api/100-migrating-in-go.md index 1f99be3..7c168e1 100644 --- a/docs/docs/04-api/100-migrating-in-go.md +++ b/docs/docs/04-api/100-migrating-in-go.md @@ -7,7 +7,7 @@ package main import ( "database/sql" - "example/pg/migrations" + "example/pg/db/migrations" "github.com/alexisvisco/amigo/pkg/amigo" "github.com/alexisvisco/amigo/pkg/amigoctx" "github.com/alexisvisco/amigo/pkg/types" @@ -23,15 +23,31 @@ func main() { panic(err) } - err = amigo.NewAmigo(amigoctx.NewContext().WithDSN(dsn)).RunMigrations(amigo.RunMigrationParams{ - DB: db, - Direction: types.MigrationDirectionUp, - Migrations: migrations.Migrations, - LogOutput: os.Stdout, - }) + err = migrateDatabase(dsn, db) if err != nil { panic(err) + } +} + +func migrateDatabase(databaseURL string, rawDB *sql.DB) error { + dumpSchemaAfterMigrating := os.Getenv("DUMP_SCHEMA_AFTER_MIGRATING") == "true" + + a := amigoctx.NewContext(). + WithDSN(databaseURL). // you need to provide the dsn too, in order for amigo to detect the driver + WithShowSQL(true). // will show you sql queries + WithDumpSchemaAfterMigrating(dumpSchemaAfterMigrating) // will create/modify the schema.sql in db folder (only in local is suitable) + + err := amigo.NewAmigo(a).RunMigrations(amigo.RunMigrationParams{ + DB: rawDB, + Direction: amigotypes.MigrationDirectionUp, // will migrate the database up + Migrations: migrations.Migrations, // where migrations are located (db/migrations) + Logger: slog.Default(), // you can also only specify the LogOutput and it will use the default amigo logger at your desired output (io.Writer) + }) + if err != nil { + return fmt.Errorf("failed to migrate database: %w", err) } + + return nil } ``` diff --git a/pkg/schema/detect_migrations.go b/pkg/schema/detect_migrations.go index dc9ddd7..8f87725 100644 --- a/pkg/schema/detect_migrations.go +++ b/pkg/schema/detect_migrations.go @@ -15,7 +15,6 @@ func (m *Migrator[T]) detectMigrationsToExec( version *string, steps *int, // only used for rollback ) (migrationsToApply []Migration, firstRun bool) { - s.FindAppliedVersions() appliedVersions, err := utils.PanicToError1(s.FindAppliedVersions) if isTableDoesNotExists(err) { firstRun = true