Skip to content

Commit

Permalink
fix: problems
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Nov 8, 2024
1 parent 037e60e commit 5fca0d0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/docs/03-cli/04-migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions docs/docs/03-cli/05-rollback.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions docs/docs/03-cli/07-schema.md
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 23 additions & 7 deletions docs/docs/04-api/100-migrating-in-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
```

Expand Down
1 change: 0 additions & 1 deletion pkg/schema/detect_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fca0d0

Please sign in to comment.