Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"symfony/finder": "^6.4 || ^7.4 || ^8.0",
"symfony/framework-bundle": "^6.4 || ^7.4 || ^8.0",
"symfony/http-client": "^6.4 || ^7.4 || ^8.0",
"symfony/lock": "^6.4 || ^7.4 || ^8.0",
"symfony/messenger": "^6.4 || ^7.4 || ^8.0",
"symfony/options-resolver": "^6.4 || ^7.4 || ^8.0",
"symfony/password-hasher": "^6.4 || ^7.4 || ^8.0",
Expand Down
85 changes: 84 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions documentation/components/bridges/symfony-postgresql-bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ flow_postgresql:
generate_rollback: true # Generate rollback files automatically
drop_if_exists: false # Emit IF EXISTS on diff-generated DROP statements
mode: all_pending # Which pending migrations migrate runs: all_pending or strict_order
lock:
type: advisory # advisory, symfony, service or none
timeout: 300 # Seconds flow:migrations:migrate waits for a busy lock
ttl: 3600 # type: symfony - seconds the lock lives
factory_service_id: lock.factory # type: symfony - the LockFactory service
service_id: ~ # type: service - your MigrationLock service
```

#### `mode`
Expand All @@ -243,6 +249,95 @@ the latest executed migration.
- `strict_order`: `flow:migrations:migrate` stops with an error and exit code 1. It runs no migration. Run the older
version with `flow:migrations:execute`, or set the mode to `all_pending`.

#### `lock`

A lock stops two processes from running the same migrations at the same time, for example two application instances
that deploy at once. `flow:migrations:migrate` and `flow:migrations:execute` take the lock. A `--dry-run` takes no
lock.

```yaml
flow_postgresql:
migrations:
lock:
type: advisory # default, a session advisory lock in PostgreSQL
timeout: 300
```

```yaml
framework:
lock: '%env(LOCK_DSN)%' # registers the lock.factory service

flow_postgresql:
migrations:
lock:
type: symfony # requires symfony/lock
factory_service_id: lock.factory
ttl: 3600
```

```yaml
flow_postgresql:
migrations:
lock:
type: service
service_id: App\Migrations\MyMigrationLock # implements Flow\PostgreSql\Migrations\Lock\MigrationLock
```

```yaml
flow_postgresql:
migrations:
lock:
type: none
```

Each type accepts only its own keys. Any other key fails at configuration time.

| type | keys |
|------------|----------------------------------------|
| `advisory` | `timeout` |
| `symfony` | `timeout`, `ttl`, `factory_service_id` |
| `service` | `timeout`, `service_id` |
| `none` | - |

If the lock is busy, `flow:migrations:migrate` waits up to `timeout` seconds:

```
$ bin/console flow:migrations:migrate
! [NOTE] Another process is running migrations. Waiting up to 300 seconds for it to finish...
[OK] Already up to date.
```

`flow:migrations:execute` does not wait. It fails at once.

The advisory lock needs a direct session with PostgreSQL. If migrations connect through a pooler in transaction mode
(for example PgBouncer), use `type: symfony` with a store outside the database. A lock outside the database must
outlive the longest migration run, so set `ttl` above its duration.

Without the bundle, pass the lock to `Configuration`. The default is `AdvisoryMigrationLock`. `Migrator` never waits.
A busy lock throws `MigrationLockedException`:

```php
<?php

use Flow\PostgreSql\Migrations\Configuration;
use Flow\PostgreSql\Migrations\Exception\MigrationLockedException;
use Flow\PostgreSql\Migrations\Lock\NoMigrationLock;

$configuration = new Configuration(
client: $client,
targetCatalogProvider: $catalogProvider,
migrationsDirectory: __DIR__ . '/migrations',
migrationsNamespace: 'App\\Migrations',
lock: new NoMigrationLock(),
);

try {
$migrator->migrate();
} catch (MigrationLockedException $exception) {
// another process runs the migrations: wait and retry, skip, or fail
}
```

#### `drop_if_exists`

To add `IF EXISTS` to every `DROP` that the diff generates, set `drop_if_exists: true`. Use it for convergence
Expand Down
Loading