Existing databases
Adopting a database that is already in production.
When a database already has its schema, you do not want to run your initial changes against it. Baseline records them as applied without executing them.
- Script the current schema as your first change(s), e.g. with SSMS, SqlPackage or Badger SQL. Keep them as the real history for new environments.
- Tag that point:
trek tag baseline. - For each existing database:
trek baseline --to @baseline -e prod. - From here on, add changes normally. New, empty databases run the full plan, including the initial changes.
trek add initial-schema --irreversible # paste the scripted schema into deploy.sql
trek tag baseline
trek baseline --to @baseline -e prod
trek status -e prod # initial-schema shown as (baseline)
Baseline only works on a database with no deployed changes, takes the deploy lock, records the environment, and writes a schema fingerprint so later drift is detected.
Coming from Flyway, DbUp, EF Core or grate
For Flyway, DbUp, Entity Framework Core, grate, RoundhousE, Sqitch and SQL projects (SSDT), trek import does the conversion and trek baseline --from reads each database's history for you; see Moving from other tools. For anything else:
- Convert your versioned scripts into changes (one folder each) in their existing order, or collapse history into one
initial-schemachange. - Write revert scripts for the changes you might want to undo; mark the rest
!irreversible. - Move repeatable/"anytime" scripts into
repeatable/. - Replace placeholders such as
${name}(Flyway) with{name}or keep$(name). - Baseline each existing database. The old tool's history table can stay; trek uses its own
trekschema.