Skip to content

Commit

Permalink
Removed outdated migration config string type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Sep 9, 2024
1 parent 86140ad commit c6528ed
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 78 deletions.
2 changes: 1 addition & 1 deletion drizzle-orm/src/aws-data-api/pg/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AwsDataApiPgDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: AwsDataApiPgDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/better-sqlite3/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { BetterSQLite3Database } from './driver.ts';

export function migrate<TSchema extends Record<string, unknown>>(
db: BetterSQLite3Database<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/bun-sqlite/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { BunSQLiteDatabase } from './driver.ts';

export function migrate<TSchema extends Record<string, unknown>>(
db: BunSQLiteDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
db.dialect.migrate(migrations, db.session, config);
Expand Down
8 changes: 2 additions & 6 deletions drizzle-orm/src/d1/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import type { DrizzleD1Database } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: DrizzleD1Database<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
const migrationsTable = config === undefined
? '__drizzle_migrations'
: typeof config === 'string'
? '__drizzle_migrations'
: config.migrationsTable ?? '__drizzle_migrations';
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
Expand Down
8 changes: 2 additions & 6 deletions drizzle-orm/src/libsql/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import type { LibSQLDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: LibSQLDatabase<TSchema>,
config: MigrationConfig | string,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
const migrationsTable = config === undefined
? '__drizzle_migrations'
: typeof config === 'string'
? '__drizzle_migrations'
: config.migrationsTable ?? '__drizzle_migrations';
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';

const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
Expand Down
16 changes: 2 additions & 14 deletions drizzle-orm/src/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';

export interface KitConfig {
out: string;
Expand All @@ -20,19 +19,8 @@ export interface MigrationMeta {
bps: boolean;
}

export function readMigrationFiles(config: string | MigrationConfig): MigrationMeta[] {
let migrationFolderTo: string | undefined;
if (typeof config === 'string') {
const configAsString = fs.readFileSync(path.resolve('.', config), 'utf8');
const jsonConfig = JSON.parse(configAsString) as KitConfig;
migrationFolderTo = jsonConfig.out;
} else {
migrationFolderTo = config.migrationsFolder;
}

if (!migrationFolderTo) {
throw new Error('no migration folder defined');
}
export function readMigrationFiles(config: MigrationConfig): MigrationMeta[] {
const migrationFolderTo = config.migrationsFolder;

const migrationQueries: MigrationMeta[] = [];

Expand Down
28 changes: 13 additions & 15 deletions drizzle-orm/src/monomigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,63 @@ export async function migrate(
| PostgresJsDatabase<any>
| VercelPgDatabase<any>
| TiDBServerlessDatabase<any>,
config:
| string
| MigrationConfig,
config: MigrationConfig,
) {
switch ((<any> db).constructor[entityKind]) {
case 'AwsDataApiPgDatabase': {
const { migrate } = await import('./aws-data-api/pg/migrator');

return migrate(db as AwsDataApiPgDatabase, config as string | MigrationConfig);
return migrate(db as AwsDataApiPgDatabase, config as MigrationConfig);
}
case 'BetterSQLite3Database': {
const { migrate } = await import('./better-sqlite3/migrator');

return migrate(db as BetterSQLite3Database, config as string | MigrationConfig);
return migrate(db as BetterSQLite3Database, config as MigrationConfig);
}
case 'BunSQLiteDatabase': {
const { migrate } = await import('./bun-sqlite/migrator');

return migrate(db as BunSQLiteDatabase, config as string | MigrationConfig);
return migrate(db as BunSQLiteDatabase, config as MigrationConfig);
}
case 'D1Database': {
const { migrate } = await import('./d1/migrator');

return migrate(db as DrizzleD1Database, config as string | MigrationConfig);
return migrate(db as DrizzleD1Database, config as MigrationConfig);
}
case 'LibSQLDatabase': {
const { migrate } = await import('./libsql/migrator');

return migrate(db as LibSQLDatabase, config as string | MigrationConfig);
return migrate(db as LibSQLDatabase, config as MigrationConfig);
}
case 'MySql2Database': {
const { migrate } = await import('./mysql2/migrator');

return migrate(db as MySql2Database, config as string | MigrationConfig);
return migrate(db as MySql2Database, config as MigrationConfig);
}
case 'NeonHttpDatabase': {
const { migrate } = await import('./neon-http/migrator');

return migrate(db as NeonHttpDatabase, config as string | MigrationConfig);
return migrate(db as NeonHttpDatabase, config as MigrationConfig);
}
case 'NeonServerlessDatabase': {
const { migrate } = await import('./neon-serverless/migrator');

return migrate(db as NeonDatabase, config as string | MigrationConfig);
return migrate(db as NeonDatabase, config as MigrationConfig);
}
case 'NodePgDatabase': {
const { migrate } = await import('./node-postgres/migrator');

return migrate(db as NodePgDatabase, config as string | MigrationConfig);
return migrate(db as NodePgDatabase, config as MigrationConfig);
}
case 'PlanetScaleDatabase': {
const { migrate } = await import('./planetscale-serverless/migrator');

return migrate(db as PlanetScaleDatabase, config as string | MigrationConfig);
return migrate(db as PlanetScaleDatabase, config as MigrationConfig);
}
case 'PostgresJsDatabase': {
const { migrate } = await import('./postgres-js/migrator');

return migrate(db as PostgresJsDatabase, config as string | MigrationConfig);
return migrate(db as PostgresJsDatabase, config as MigrationConfig);
}
case 'TiDBServerlessDatabase': {
const { migrate } = await import('./tidb-serverless/migrator');
Expand All @@ -98,7 +96,7 @@ export async function migrate(
case 'VercelPgDatabase': {
const { migrate } = await import('./vercel-postgres/migrator');

return migrate(db as VercelPgDatabase, config as string | MigrationConfig);
return migrate(db as VercelPgDatabase, config as MigrationConfig);
}
}
}
11 changes: 2 additions & 9 deletions drizzle-orm/src/mysql2/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import type { MySql2Database } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: MySql2Database<TSchema>,
config: MigrationConfig | string,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);

const preparedConfig = typeof config === 'string'
? {
migrationsFolder: config,
}
: config;

await db.dialect.migrate(migrations, db.session, preparedConfig);
await db.dialect.migrate(migrations, db.session, config);
}
8 changes: 3 additions & 5 deletions drizzle-orm/src/neon-http/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import type { NeonHttpDatabase } from './driver.ts';
*/
export async function migrate<TSchema extends Record<string, unknown>>(
db: NeonHttpDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
const migrationsTable = typeof config === 'string'
? '__drizzle_migrations'
: config.migrationsTable ?? '__drizzle_migrations';
const migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';
const migrationsSchema = config.migrationsSchema ?? 'drizzle';
const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/neon-serverless/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { NeonDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: NeonDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/node-postgres/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { NodePgDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: NodePgDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-proxy/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ProxyMigrator = (migrationQueries: string[]) => Promise<void>;
export async function migrate<TSchema extends Record<string, unknown>>(
db: PgRemoteDatabase<TSchema>,
callback: ProxyMigrator,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);

Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pglite/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PgliteDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: PgliteDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
10 changes: 2 additions & 8 deletions drizzle-orm/src/planetscale-serverless/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import type { PlanetScaleDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: PlanetScaleDatabase<TSchema>,
config: MigrationConfig | string,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);

const preparedConfig = typeof config === 'string'
? {
migrationsFolder: config,
}
: config;

await db.dialect.migrate(migrations, db.session, preparedConfig);
await db.dialect.migrate(migrations, db.session, config);
}
2 changes: 1 addition & 1 deletion drizzle-orm/src/postgres-js/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PostgresJsDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: PostgresJsDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/sql-js/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SQLJsDatabase } from './driver.ts';

export function migrate<TSchema extends Record<string, unknown>>(
db: SQLJsDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
db.dialect.migrate(migrations, db.session, config);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/sqlite-proxy/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ProxyMigrator = (migrationQueries: string[]) => Promise<void>;
export async function migrate<TSchema extends Record<string, unknown>>(
db: SqliteRemoteDatabase<TSchema>,
callback: ProxyMigrator,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);

Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/vercel-postgres/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { VercelPgDatabase } from './driver.ts';

export async function migrate<TSchema extends Record<string, unknown>>(
db: VercelPgDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
await db.dialect.migrate(migrations, db.session, config);
Expand Down
6 changes: 2 additions & 4 deletions drizzle-orm/src/xata-http/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ export interface MigrationConfig {
* @param config - path to migration folder generated by drizzle-kit
*/ export async function migrate<TSchema extends Record<string, unknown>>(
db: XataHttpDatabase<TSchema>,
config: string | MigrationConfig,
config: MigrationConfig,
) {
const migrations = readMigrationFiles(config);
const migrationsTable = typeof config === 'string'
? '__drizzle_migrations'
: config.migrationsTable ?? '__drizzle_migrations';
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';
const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
id SERIAL PRIMARY KEY,
Expand Down

0 comments on commit c6528ed

Please sign in to comment.