Skip to content

Commit ff4b5b5

Browse files
committed
Merge branch 'migrator-init' of https://github.com/drizzle-team/drizzle-orm into beta
2 parents 2cd01ec + 65e6dd9 commit ff4b5b5

File tree

43 files changed

+990
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+990
-551
lines changed

drizzle-kit/src/cli/commands/introspect.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
applySqliteSnapshotsDiff,
2929
} from '../../snapshotsDiffer';
3030
import { prepareOutFolder } from '../../utils';
31+
import { connectToMySQL, connectToSQLite } from '../connections';
3132
import { Entities } from '../validations/cli';
3233
import type { Casing, Prefix } from '../validations/common';
3334
import { GelCredentials } from '../validations/gel';
@@ -80,9 +81,12 @@ export const introspectPostgres = async (
8081
schemasFilter: string[],
8182
prefix: Prefix,
8283
entities: Entities,
84+
db?: Awaited<ReturnType<typeof import('../connections')['preparePostgresDB']>>,
8385
) => {
84-
const { preparePostgresDB } = await import('../connections');
85-
const db = await preparePostgresDB(credentials);
86+
if (!db) {
87+
const { preparePostgresDB } = await import('../connections');
88+
db = await preparePostgresDB(credentials);
89+
}
8690

8791
const matchers = tablesFilter.map((it) => {
8892
return new Minimatch(it);
@@ -195,7 +199,6 @@ export const introspectPostgres = async (
195199
)
196200
} 🚀`,
197201
);
198-
process.exit(0);
199202
};
200203

201204
function gelToRelationsPull(schema: GelSchema): SchemaForPull {
@@ -226,9 +229,12 @@ export const introspectGel = async (
226229
schemasFilter: string[],
227230
prefix: Prefix,
228231
entities: Entities,
232+
db?: Awaited<ReturnType<typeof import('../connections')['prepareGelDB']>>,
229233
) => {
230-
const { prepareGelDB } = await import('../connections');
231-
const db = await prepareGelDB(credentials);
234+
if (!db) {
235+
const { prepareGelDB } = await import('../connections');
236+
db = await prepareGelDB(credentials);
237+
}
232238

233239
const matchers = tablesFilter.map((it) => {
234240
return new Minimatch(it);
@@ -341,7 +347,6 @@ export const introspectGel = async (
341347
)
342348
} 🚀`,
343349
);
344-
process.exit(0);
345350
};
346351

347352
function mysqlToRelationsPull(schema: MySqlSchema): SchemaForPull {
@@ -366,9 +371,12 @@ export const introspectMysql = async (
366371
credentials: MysqlCredentials,
367372
tablesFilter: string[],
368373
prefix: Prefix,
374+
connection?: Awaited<ReturnType<typeof import('../connections')['connectToMySQL']>>,
369375
) => {
370-
const { connectToMySQL } = await import('../connections');
371-
const { db, database } = await connectToMySQL(credentials);
376+
if (!connection) {
377+
const { connectToMySQL } = await import('../connections');
378+
connection = await connectToMySQL(credentials);
379+
}
372380

373381
const matchers = tablesFilter.map((it) => {
374382
return new Minimatch(it);
@@ -400,7 +408,7 @@ export const introspectMysql = async (
400408
const progress = new IntrospectProgress();
401409
const res = await renderWithTask(
402410
progress,
403-
fromMysqlDatabase(db, database, filter, (stage, count, status) => {
411+
fromMysqlDatabase(connection.db, connection.database, filter, (stage, count, status) => {
404412
progress.update(stage, count, status);
405413
}),
406414
);
@@ -468,7 +476,6 @@ export const introspectMysql = async (
468476
)
469477
} 🚀`,
470478
);
471-
process.exit(0);
472479
};
473480

474481
export const introspectSingleStore = async (
@@ -478,9 +485,12 @@ export const introspectSingleStore = async (
478485
credentials: SingleStoreCredentials,
479486
tablesFilter: string[],
480487
prefix: Prefix,
488+
connection?: Awaited<ReturnType<typeof import('../connections')['connectToSingleStore']>>,
481489
) => {
482-
const { connectToSingleStore } = await import('../connections');
483-
const { db, database } = await connectToSingleStore(credentials);
490+
if (!connection) {
491+
const { connectToSingleStore } = await import('../connections');
492+
connection = await connectToSingleStore(credentials);
493+
}
484494

485495
const matchers = tablesFilter.map((it) => {
486496
return new Minimatch(it);
@@ -512,7 +522,7 @@ export const introspectSingleStore = async (
512522
const progress = new IntrospectProgress();
513523
const res = await renderWithTask(
514524
progress,
515-
fromSingleStoreDatabase(db, database, filter, (stage, count, status) => {
525+
fromSingleStoreDatabase(connection.db, connection.database, filter, (stage, count, status) => {
516526
progress.update(stage, count, status);
517527
}),
518528
);
@@ -565,7 +575,6 @@ export const introspectSingleStore = async (
565575
)
566576
}] You schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
567577
);
568-
process.exit(0);
569578
};
570579

571580
function sqliteToRelationsPull(schema: SQLiteSchema): SchemaForPull {
@@ -590,10 +599,12 @@ export const introspectSqlite = async (
590599
credentials: SqliteCredentials,
591600
tablesFilter: string[],
592601
prefix: Prefix,
602+
db?: Awaited<ReturnType<typeof import('../connections')['connectToSQLite']>>,
593603
) => {
594-
const { connectToSQLite } = await import('../connections');
595-
const db = await connectToSQLite(credentials);
596-
604+
if (!db) {
605+
const { connectToSQLite } = await import('../connections');
606+
db = await connectToSQLite(credentials);
607+
}
597608
const matchers = tablesFilter.map((it) => {
598609
return new Minimatch(it);
599610
});
@@ -693,7 +704,6 @@ export const introspectSqlite = async (
693704
)
694705
} 🚀`,
695706
);
696-
process.exit(0);
697707
};
698708

699709
export const introspectLibSQL = async (
@@ -703,9 +713,12 @@ export const introspectLibSQL = async (
703713
credentials: LibSQLCredentials,
704714
tablesFilter: string[],
705715
prefix: Prefix,
716+
db?: Awaited<ReturnType<typeof import('../connections')['connectToLibSQL']>>,
706717
) => {
707-
const { connectToLibSQL } = await import('../connections');
708-
const db = await connectToLibSQL(credentials);
718+
if (!db) {
719+
const { connectToLibSQL } = await import('../connections');
720+
db = await connectToLibSQL(credentials);
721+
}
709722

710723
const matchers = tablesFilter.map((it) => {
711724
return new Minimatch(it);
@@ -806,7 +819,6 @@ export const introspectLibSQL = async (
806819
)
807820
} 🚀`,
808821
);
809-
process.exit(0);
810822
};
811823

812824
const withCasing = (value: string, casing: Casing) => {

drizzle-kit/src/cli/commands/migrate.ts

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -493,35 +493,30 @@ export const prepareMySQLPush = async (
493493
snapshot: MySqlSchema,
494494
casing: CasingType | undefined,
495495
) => {
496-
try {
497-
const { prev, cur } = await prepareMySqlDbPushSnapshot(
498-
snapshot,
499-
schemaPath,
500-
casing,
501-
);
496+
const { prev, cur } = await prepareMySqlDbPushSnapshot(
497+
snapshot,
498+
schemaPath,
499+
casing,
500+
);
502501

503-
const validatedPrev = mysqlSchema.parse(prev);
504-
const validatedCur = mysqlSchema.parse(cur);
502+
const validatedPrev = mysqlSchema.parse(prev);
503+
const validatedCur = mysqlSchema.parse(cur);
505504

506-
const squashedPrev = squashMysqlScheme(validatedPrev);
507-
const squashedCur = squashMysqlScheme(validatedCur);
505+
const squashedPrev = squashMysqlScheme(validatedPrev);
506+
const squashedCur = squashMysqlScheme(validatedCur);
508507

509-
const { sqlStatements, statements } = await applyMysqlSnapshotsDiff(
510-
squashedPrev,
511-
squashedCur,
512-
tablesResolver,
513-
columnsResolver,
514-
mySqlViewsResolver,
515-
validatedPrev,
516-
validatedCur,
517-
'push',
518-
);
508+
const { sqlStatements, statements } = await applyMysqlSnapshotsDiff(
509+
squashedPrev,
510+
squashedCur,
511+
tablesResolver,
512+
columnsResolver,
513+
mySqlViewsResolver,
514+
validatedPrev,
515+
validatedCur,
516+
'push',
517+
);
519518

520-
return { sqlStatements, statements, validatedCur, validatedPrev };
521-
} catch (e) {
522-
console.error(e);
523-
process.exit(1);
524-
}
519+
return { sqlStatements, statements, validatedCur, validatedPrev };
525520
};
526521

527522
export const prepareAndMigrateMysql = async (config: GenerateConfig) => {
@@ -643,35 +638,30 @@ export const prepareSingleStorePush = async (
643638
snapshot: SingleStoreSchema,
644639
casing: CasingType | undefined,
645640
) => {
646-
try {
647-
const { prev, cur } = await prepareSingleStoreDbPushSnapshot(
648-
snapshot,
649-
schemaPath,
650-
casing,
651-
);
641+
const { prev, cur } = await prepareSingleStoreDbPushSnapshot(
642+
snapshot,
643+
schemaPath,
644+
casing,
645+
);
652646

653-
const validatedPrev = singlestoreSchema.parse(prev);
654-
const validatedCur = singlestoreSchema.parse(cur);
647+
const validatedPrev = singlestoreSchema.parse(prev);
648+
const validatedCur = singlestoreSchema.parse(cur);
655649

656-
const squashedPrev = squashSingleStoreScheme(validatedPrev);
657-
const squashedCur = squashSingleStoreScheme(validatedCur);
650+
const squashedPrev = squashSingleStoreScheme(validatedPrev);
651+
const squashedCur = squashSingleStoreScheme(validatedCur);
658652

659-
const { sqlStatements, statements } = await applySingleStoreSnapshotsDiff(
660-
squashedPrev,
661-
squashedCur,
662-
tablesResolver,
663-
columnsResolver,
664-
/* singleStoreViewsResolver, */
665-
validatedPrev,
666-
validatedCur,
667-
'push',
668-
);
653+
const { sqlStatements, statements } = await applySingleStoreSnapshotsDiff(
654+
squashedPrev,
655+
squashedCur,
656+
tablesResolver,
657+
columnsResolver,
658+
/* singleStoreViewsResolver, */
659+
validatedPrev,
660+
validatedCur,
661+
'push',
662+
);
669663

670-
return { sqlStatements, statements, validatedCur, validatedPrev };
671-
} catch (e) {
672-
console.error(e);
673-
process.exit(1);
674-
}
664+
return { sqlStatements, statements, validatedCur, validatedPrev };
675665
};
676666

677667
export const prepareAndMigrateSingleStore = async (config: GenerateConfig) => {

drizzle-kit/src/cli/commands/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ export const preparePullConfig = async (
485485
schemasFilter: string[];
486486
prefix: Prefix;
487487
entities: Entities;
488+
init: boolean;
489+
migrationsSchema: string | undefined;
490+
migrationsTable: string | undefined;
488491
}
489492
> => {
490493
const raw = flattenPull(
@@ -502,6 +505,7 @@ export const preparePullConfig = async (
502505

503506
const config = parsed.data;
504507
const dialect = config.dialect;
508+
const { schema, table } = parsed.data.migrations || {};
505509

506510
const tablesFilterConfig = config.tablesFilter;
507511
const tablesFilter = tablesFilterConfig
@@ -545,6 +549,9 @@ export const preparePullConfig = async (
545549
schemasFilter,
546550
prefix: config.migrations?.prefix || 'index',
547551
entities: config.entities,
552+
init: !!options.init,
553+
migrationsSchema: schema,
554+
migrationsTable: table,
548555
};
549556
}
550557

@@ -564,6 +571,9 @@ export const preparePullConfig = async (
564571
schemasFilter,
565572
prefix: config.migrations?.prefix || 'index',
566573
entities: config.entities,
574+
init: !!options.init,
575+
migrationsSchema: schema,
576+
migrationsTable: table,
567577
};
568578
}
569579

@@ -584,6 +594,9 @@ export const preparePullConfig = async (
584594
schemasFilter,
585595
prefix: config.migrations?.prefix || 'index',
586596
entities: config.entities,
597+
init: !!options.init,
598+
migrationsSchema: schema,
599+
migrationsTable: table,
587600
};
588601
}
589602

@@ -603,6 +616,9 @@ export const preparePullConfig = async (
603616
schemasFilter,
604617
prefix: config.migrations?.prefix || 'index',
605618
entities: config.entities,
619+
init: !!options.init,
620+
migrationsSchema: schema,
621+
migrationsTable: table,
606622
};
607623
}
608624

@@ -622,6 +638,9 @@ export const preparePullConfig = async (
622638
schemasFilter,
623639
prefix: config.migrations?.prefix || 'index',
624640
entities: config.entities,
641+
init: !!options.init,
642+
migrationsSchema: schema,
643+
migrationsTable: table,
625644
};
626645
}
627646

@@ -641,6 +660,9 @@ export const preparePullConfig = async (
641660
schemasFilter,
642661
prefix: config.migrations?.prefix || 'index',
643662
entities: config.entities,
663+
init: !!options.init,
664+
migrationsSchema: schema,
665+
migrationsTable: table,
644666
};
645667
}
646668

0 commit comments

Comments
 (0)