knex-schema-and-migrations
Use when defining database migrations with knex_dart's Migrator — code-first, SQL-directory, or schema-input styles.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Use when defining database migrations with knex_dart's Migrator — code-first, SQL-directory, or schema-input styles.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Use when configuring connection pools, executing raw SQL, or tapping observability streams on knex_dart driver clients.
Use when choosing between SQL-only knex_dart usage and a live driver package, or when writing the first connected query.
Use when writing INSERT, UPDATE, DELETE, or conflict-handling queries with knex_dart.
Use when instrumenting knex_dart live driver wrappers with OpenTelemetry spans, DB client duration metrics, hooks, transactions, or stream/query interceptor behavior.
Use when generating SQL with knex_dart query builders: filtering, joins, grouping, CTEs, unions, and SQL inspection.
Use when creating tables, altering schema, or running atomic write flows with knex_dart driver wrappers.
| name | knex-schema-and-migrations |
| description | Use when defining database migrations with knex_dart's Migrator — code-first, SQL-directory, or schema-input styles. |
| metadata | {"knex_dart_version":"1.2.1"} |
Migrations run through the Migrator class accessed via knex.migrate. The Knex facade (not the driver wrappers like KnexPostgres) is the entry point.
Knex accepts any Client subclass from package:knex_dart. Currently only SQLiteClient extends Client directly — Postgres and MySQL wrappers manage their own internal clients and are not passed to Knex directly.
import 'package:knex_dart/knex_dart.dart';
import 'package:knex_dart_sqlite/knex_dart_sqlite.dart';
final client = await SQLiteClient.connect(filename: 'app.db');
final db = Knex(client);
// Now db.migrate is available
For PostgreSQL or MySQL, execute schema changes via executeSchema() on the driver wrapper directly instead of using the Knex facade:
import 'package:knex_dart_postgres/knex_dart_postgres.dart';
final db = await KnexPostgres.connect(
host: 'localhost',
database: 'myapp',
username: 'user',
password: 'pass',
);
await db.executeSchema((schema) {
schema.createTable('users', (t) {
t.increments('id');
t.string('email').notNullable().unique();
});
});
All three source styles share the same three lifecycle methods:
await migrator.latest(); // run all pending migrations
await migrator.rollback(); // revert the latest batch
final status = await migrator.status(); // [{name, status: 'completed'|'pending'}]
Use SqlMigration for plain SQL up/down pairs. Name migrations with a sortable prefix (e.g. 001_, 002_).
final migrator = db.migrate.fromCode([
const SqlMigration(
name: '001_create_users',
upSql: [
'CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL)',
],
downSql: ['DROP TABLE users'],
),
const SqlMigration(
name: '002_add_active_column',
upSql: ['ALTER TABLE users ADD COLUMN active BOOLEAN NOT NULL DEFAULT true'],
downSql: ['ALTER TABLE users DROP COLUMN active'],
),
]);
await migrator.latest();
Files must follow the naming convention <name>.up.sql / <name>.down.sql. Units run in lexicographic order.
migrations/
001_create_users.up.sql
001_create_users.down.sql
002_add_index.up.sql
final migrator = db.migrate.fromSqlDir('./migrations');
await migrator.latest();
Use fromConfig() to read the directory from MigrationConfig.directory (default ./migrations).
await db.migrate.fromConfig().latest();
Converts a JSON Schema (or any registered adapter) into CREATE TABLE DDL and runs it as a migration.
final migrator = db.migrate.fromSchema(
name: '001_bootstrap',
input: {
'type': 'object',
'title': 'users',
'properties': {
'id': {'type': 'integer'},
'email': {'type': 'string'},
},
},
ifNotExists: true,
dropOnDown: true,
);
await migrator.latest();
JsonSchemaAdapter is auto-registered when no adapter or registry is passed.
The migrator creates a knex_migrations table in the target database to track applied migrations. Override via MigrationConfig:
final client = await SQLiteClient.connect(filename: 'app.db');
final db = Knex(
client,
// Knex accepts a KnexConfig — configure via client.config before wrapping
);
Default table name is knex_migrations. Default directory is ./migrations.
disableTransactions defaults to true. Set it to false only for single-connection drivers (like SQLite) where transactional correctness is guaranteed.
// SQLite: safe to enable transactions
final client = await SQLiteClient.connect(filename: 'app.db');
// Supply MigrationConfig via KnexConfig at client creation time
KnexMigrationException before any SQL runs..down.sql means rollback() will throw for that migration.SchemaAstMigration requires dropOnDown: true for automatic rollback, otherwise rollback throws.https://docs.knex.mahawarkartikey.in/raw/migration/migrations.mdhttps://docs.knex.mahawarkartikey.in/raw/query-building/schema-builder.mdhttps://docs.knex.mahawarkartikey.in/raw/migration/from-knex-js.md