|
| 1 | +--- |
| 2 | +title: Baseline |
| 3 | +description: Create a baseline migration for an existing database schema |
| 4 | +--- |
| 5 | + |
| 6 | +## Command |
| 7 | + |
| 8 | +``` |
| 9 | +$ pgroll baseline <version> <target directory> |
| 10 | +``` |
| 11 | + |
| 12 | +This command creates a baseline migration for an existing database schema. It captures the current schema state without applying any changes, providing a starting point for future migrations. |
| 13 | + |
| 14 | +Use `pgroll baseline` when: |
| 15 | +- Starting to use pgroll with an existing database that already has a schema |
| 16 | +- You want to consolidate a long migration history into a clean starting point |
| 17 | + |
| 18 | +The command requires two arguments: |
| 19 | +1. `version` - The version name for the baseline (e.g., "01_initial_schema") |
| 20 | +2. `target directory` - The directory where the placeholder migration file will be written |
| 21 | + |
| 22 | +The optional `--json` flag can be used to write the placeholder migration file in JSON format instead of YAML. |
| 23 | + |
| 24 | +### How it works |
| 25 | + |
| 26 | +When the `pgroll baseline` command is run, it: |
| 27 | +1. Captures the current database schema state in pgroll's internal tracking |
| 28 | +2. Creates an empty placeholder migration file in the target directory |
| 29 | +3. Records the baseline in `pgroll`'s internal state |
| 30 | + |
| 31 | +**Important**: After running the command, you should manually complete the placeholder migration file: |
| 32 | +1. Use a tool like `pg_dump` to extract the DDL statements for your schema |
| 33 | +2. Copy those statements (CREATE TABLE, CREATE INDEX, etc.) into the placeholder migration file's raw SQL section |
| 34 | +3. This completed migration file can then be used to reconstruct the schema in other environments |
| 35 | + |
| 36 | +Future migrations will build upon this baseline. |
| 37 | + |
| 38 | +<Warning> |
| 39 | +Creating a baseline will restart your migration history. The command will prompt for confirmation before proceeding. |
| 40 | +</Warning> |
| 41 | + |
| 42 | +### Effects on migration history |
| 43 | + |
| 44 | +Creating a baseline: |
| 45 | +- Creates a "reset point" in your migration history |
| 46 | +- Previous migrations become part of the baseline and are no longer individually visible |
| 47 | +- When using commands like `pull` and `migrate`, only migrations after the most recent baseline are considered |
| 48 | + |
| 49 | +### Examples |
| 50 | + |
| 51 | +#### Create a baseline with default YAML format |
| 52 | + |
| 53 | +``` |
| 54 | +pgroll baseline 01_initial_schema ./migrations |
| 55 | +``` |
| 56 | + |
| 57 | +#### Create a baseline with JSON format |
| 58 | + |
| 59 | +``` |
| 60 | +pgroll baseline 01_initial_schema ./migrations --json |
| 61 | +``` |
0 commit comments