n8n-db-migrations
n8n Migration Guidelines
Rule of thumb: a migration PR needs an approval from the @n8n-io/migrations-review team, unless a member of that team wrote it. The fixes they ask for are predictable — work through the Pre-flight checklist before requesting review. The rest of this document explains the *why* for each item and covers deeper topics.
Table of Contents
- Overview
- Pre-flight checklist
- Common Guidance
- Schema Migrations
- Data Migrations
- Cross-database Compatibility
- Tests
- General Design Guidance
- Schema documentation
Overview
Directory Structure
packages/@n8n/db/src/migrations/
├── common/ # Default — DSL handles SQLite + Postgres
├── postgresdb/ # PostgreSQL-specific migrations
├── sqlite/ # SQLite-specific migrations
├── dsl/ # Schema builder DSL (table, column, indices)
├── __tests__/ # Migration tests
├── migration-types.ts
└── migration-helpers.tsMigration Types
| Interface | When to use |
|---|---|
| ReversibleMigration | Schema changes that can be cleanly undone (add/drop column, create/drop table). Requires a working down(). |
| IrreversibleMigration | Data transformations, destructive changes, or anything where down() would lose data. No down() allowed. |
MigrationContext API
Source of truth: packages/@n8n/db/src/migrations/migration-types.ts. Check the source for exact signatures when in doubt.
interface MigrationContext {
// Database info
dbType: 'postgresdb' | 'sqlite';
isSqlite: boolean;
isPostgres: boolean;
tablePrefix: string;
dbName: string;
// Schema DSL
schemaBuilder: { createTable, dropTable, addColumns, dropColumns, column,
createIndex, dropIndex, addForeignKey, dropForeignKey,
addNotNull, dropNotNull };
// Query execution
runQuery<T>(sql: string, namedParameters?: object): Promise<T>;
runInBatches<T>(query: string, operation: (rows: T[]) => Promise<void>, limit?: number): Promise<void>;
copyTable(from: string, to: string, fromFields?: string[], toFields?: string[], batchSize?: number): Promise<void>;
// Utilities
escape: { tableName(n: string): string; columnName(n: string): string; indexName(n: string): string };
parseJson<T>(data: string | T): T;
loadSurveyFromDisk(): string | null;
logger: Logger;
migrationName: string;
queryRunner: QueryRunner; // Avoid direct use — prefer runQuery()
}DSL Type Mapping Reference
Source of truth: packages/@n8n/db/src/migrations/dsl/column.ts.
| DSL type | PostgreSQL | SQLite |
|---|---|---|
| int | int | integer |
| bigint | bigint | integer |
| smallint | smallint | integer |
| varchar(N) | varchar(N) | varchar(N) *(length not enforced)* |
| text | text | text |
| json | json | text |
| uuid | uuid | varchar |
| bool | boolean | boolean |
| double | double precision | real |
| binary | bytea | blob |
| date | date | date |
| timestampTimezone | timestamptz | datetime |
| timestampNoTimezone | timestamp | datetime |
| timestamp *(deprecated)* | timestamp | datetime |
Default precision for the timestamp variants is 3 ms; override with .timestampTimezone(6).
Pre-flight checklist
Run through this before requesting review. Each item is a real, recurring reviewer flag; the link points to the section that explains the rule.
CI checks the items marked (CI) on each migration file that a PR adds. The migration-conventions rule in @n8n/code-health does these checks. To run them locally, set CODE_HEALTH_ADDED_FILES to your new migration paths and run pnpm --filter=@n8n/code-health check --rule=migration-conventions. For an approved exception, add the violation to .code-health-baseline.json.
- [ ] Migration was scaffolded with
pnpm --filter=@n8n/db migration:new(timestamp + registration are automatic; themigration-timestamplint rule catches drift). — Creating Migrations - [ ] Identifiers go through
escape.tableName(...)/escape.columnName(...). Never hand-writen8n_tableprefixes. (CI) checks table names in raw SQL. — Always escape identifiers - [ ] Match column type to value semantics. Native
uuidfor UUIDs,timestampTimezone()for timestamps, a numeric type for numbers,boolfor booleans,jsonfor structured data. Nevervarcharas a catch-all. — Column types - [ ] Pick the narrowest sane type within that category:
int/smallintnotbigintwhen range allows;textnotvarchar(255)for unbounded strings; neverdoublefor version numbers. — Column types - [ ] A generated
idkey is an integer (int.primary.autoGenerate2). A UUID key is a UUIDv7. For a UUID or string key, write the reason in the PR description. A join table uses its foreign-key columns as the key, not anidcolumn. — Primary Keys - [ ] Default
notNull, relax only when justified. PK is implicitly NOT NULL. Migration'snotNullmatches the entity's nullability. (CI) reports.notNullchained with.primary. — NOT NULL and entity parity - [ ] Enum-like columns carry
.withEnumCheck([...])AND.comment('explains values'). Opaque IDs / unix timestamps / JSON shapes also get.comment(). (CI) checks that each.withEnumCheckcolumn has a.comment. — [Constrain enum-lik
