← 返回列表
全部 Skill›数据库 Databases›n8n-db-migrations

n8n-db-migrations

作者 @n8n-io · 语言 en
Authors n8n database migrations. Use when creating or modifying files under packages/@n8n/db/src/migrations/, when the user asks to add a column, table, index, foreign key, or backfill, or when the user mentions DB migrations or TypeORM migrations.
数据库 DatabasesSQL 数据库通用执行命令访问网络敏感路径待翻译
存储库
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
210,427
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

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.ts

Migration 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; the migration-timestamp lint rule catches drift). — Creating Migrations
  • [ ] Identifiers go through escape.tableName(...) / escape.columnName(...). Never hand-write n8n_table prefixes. (CI) checks table names in raw SQL. — Always escape identifiers
  • [ ] Match column type to value semantics. Native uuid for UUIDs, timestampTimezone() for timestamps, a numeric type for numbers, bool for booleans, json for structured data. Never varchar as a catch-all. — Column types
  • [ ] Pick the narrowest sane type within that category: int/smallint not bigint when range allows; text not varchar(255) for unbounded strings; never double for version numbers. — Column types
  • [ ] A generated id key 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 an id column. — Primary Keys
  • [ ] Default notNull, relax only when justified. PK is implicitly NOT NULL. Migration's notNull matches the entity's nullability. (CI) reports .notNull chained 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 .withEnumCheck column has a .comment. — [Constrain enum-lik
评论
Nn8n-io
n8n-io
n8n-io/n8n
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@n8n-io
所属大类数据库 Databases / SQL 数据库
国内可用性通用
最近源活动—
风险提示执行命令、访问网络、敏感路径
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。