Skip to content

Commit

Permalink
fix: prevent Maximum call stack size exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 20, 2024
1 parent 10d2230 commit 6705fbd
Show file tree
Hide file tree
Showing 32 changed files with 342 additions and 286 deletions.
5 changes: 3 additions & 2 deletions drizzle-kit/src/cli/commands/libSqlPushUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SQLiteDropTableConvertor,
SqliteRenameTableConvertor,
} from '../../sqlgenerator';
import { push_array } from '../../utils';

export const getOldTableName = (
tableName: string,
Expand Down Expand Up @@ -299,11 +300,11 @@ export const libSqlLogSuggestionsAndReturn = async (
.filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName)
.map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);

tablesReferencingCurrent.push(...tablesRefs);
push_array(tablesReferencingCurrent, tablesRefs);
}

if (!tablesReferencingCurrent.length) {
statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
push_array(statementsToExecute, _moveDataStatements(tableName, json2, dataLoss));
continue;
}

Expand Down
9 changes: 5 additions & 4 deletions drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
schema,
} from '../views';
import { ExportConfig, GenerateConfig } from './utils';
import { push_array } from '../../utils';

export type Named = {
name: string;
Expand Down Expand Up @@ -1126,7 +1127,7 @@ export const promptColumnsConflicts = async <T extends Named>(
chalk.gray(`--- all columns conflicts in ${tableName} table resolved ---\n`),
);

result.deleted.push(...leftMissing);
push_array(result.deleted, leftMissing);
return result;
};

Expand Down Expand Up @@ -1198,7 +1199,7 @@ export const promptNamedConflict = async <T extends Named>(
index += 1;
} while (index < newItems.length);
console.log(chalk.gray(`--- all ${entity} conflicts resolved ---\n`));
result.deleted.push(...leftMissing);
push_array(result.deleted, leftMissing);
return result;
};

Expand Down Expand Up @@ -1288,7 +1289,7 @@ export const promptNamedWithSchemasConflict = async <T extends NamedWithSchema>(
index += 1;
} while (index < newItems.length);
console.log(chalk.gray(`--- all ${entity} conflicts resolved ---\n`));
result.deleted.push(...leftMissing);
push_array(result.deleted, leftMissing);
return result;
};

Expand Down Expand Up @@ -1347,7 +1348,7 @@ export const promptSchemasConflict = async <T extends Named>(
index += 1;
} while (index < newSchemas.length);
console.log(chalk.gray('--- all schemas conflicts resolved ---\n'));
result.deleted.push(...leftMissing);
push_array(result.deleted, leftMissing);
return result;
};

Expand Down
3 changes: 2 additions & 1 deletion drizzle-kit/src/cli/commands/pgPushUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PgSquasher } from '../../serializer/pgSchema';
import { fromJson } from '../../sqlgenerator';
import type { DB } from '../../utils';
import { Select } from '../selector-ui';
import { push_array } from '../../utils';

// export const filterStatements = (statements: JsonStatement[]) => {
// return statements.filter((statement) => {
Expand Down Expand Up @@ -252,7 +253,7 @@ export const pgSuggestions = async (db: DB, statements: JsonStatement[]) => {
}
const stmnt = fromJson([statement], 'postgresql', 'push');
if (typeof stmnt !== 'undefined') {
statementsToExecute.push(...stmnt);
push_array(statementsToExecute, stmnt);
}
}

Expand Down
8 changes: 4 additions & 4 deletions drizzle-kit/src/cli/commands/sqlitePushUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../sqlgenerator';

import type { JsonStatement } from '../../jsonStatements';
import { findAddedAndRemoved, type SQLiteDB } from '../../utils';
import { findAddedAndRemoved, push_array, type SQLiteDB } from '../../utils';

export const _moveDataStatements = (
tableName: string,
Expand Down Expand Up @@ -283,11 +283,11 @@ export const logSuggestionsAndReturn = async (
.filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName)
.map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);

tablesReferencingCurrent.push(...tablesRefs);
push_array(tablesReferencingCurrent, tablesRefs);
}

if (!tablesReferencingCurrent.length) {
statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
push_array(statementsToExecute, _moveDataStatements(tableName, json2, dataLoss));
continue;
}

Expand All @@ -298,7 +298,7 @@ export const logSuggestionsAndReturn = async (
if (pragmaState) {
statementsToExecute.push(`PRAGMA foreign_keys=OFF;`);
}
statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
push_array(statementsToExecute, _moveDataStatements(tableName, json2, dataLoss));
if (pragmaState) {
statementsToExecute.push(`PRAGMA foreign_keys=ON;`);
}
Expand Down
3 changes: 2 additions & 1 deletion drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from '../validations/sqlite';
import { studioCliParams, studioConfig } from '../validations/studio';
import { error, grey } from '../views';
import { push_array } from '../../utils';

// NextJs default config is target: es5, which esbuild-register can't consume
const assertES5 = async (unregister: () => void) => {
Expand Down Expand Up @@ -323,7 +324,7 @@ export const preparePushConfig = async (
: schemasFilterConfig
: [];

tablesFilter.push(...getTablesFilterByExtensions(config));
push_array(tablesFilter, getTablesFilterByExtensions(config));

if (config.dialect === 'postgresql') {
const parsed = postgresCredentials.safeParse(config);
Expand Down
15 changes: 8 additions & 7 deletions drizzle-kit/src/introspect-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './serializer/mysqlSchema';
import { indexName } from './serializer/mysqlSerializer';
import { unescapeSingleQuotes } from './utils';
import { push_array } from './utils';

const mysqlImportsList = new Set([
'mysqlTable',
Expand Down Expand Up @@ -158,11 +159,11 @@ export const schemaToTypeScript = (
(it) => 'check',
);

res.mysql.push(...idxImports);
res.mysql.push(...fkImpots);
res.mysql.push(...pkImports);
res.mysql.push(...uniqueImports);
res.mysql.push(...checkImports);
push_array(res.mysql, idxImports);
push_array(res.mysql, fkImpots);
push_array(res.mysql, pkImports);
push_array(res.mysql, uniqueImports);
push_array(res.mysql, checkImports);

const columnImports = Object.values(it.columns)
.map((col) => {
Expand All @@ -189,7 +190,7 @@ export const schemaToTypeScript = (
return mysqlImportsList.has(type);
});

res.mysql.push(...columnImports);
push_array(res.mysql, columnImports);
return res;
},
{ mysql: [] as string[] },
Expand Down Expand Up @@ -223,7 +224,7 @@ export const schemaToTypeScript = (
return mysqlImportsList.has(type);
});

imports.mysql.push(...columnImports);
push_array(imports.mysql, columnImports);
});

const tableStatements = Object.values(schema.tables).map((table) => {
Expand Down
18 changes: 9 additions & 9 deletions drizzle-kit/src/introspect-pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
UniqueConstraint,
} from './serializer/pgSchema';
import { indexName } from './serializer/pgSerializer';
import { unescapeSingleQuotes } from './utils';
import { unescapeSingleQuotes, push_array } from './utils';

const pgImportsList = new Set([
'pgTable',
Expand Down Expand Up @@ -348,12 +348,12 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
res.pg.push('pgSchema');
}

res.pg.push(...idxImports);
res.pg.push(...fkImpots);
res.pg.push(...pkImports);
res.pg.push(...uniqueImports);
res.pg.push(...policiesImports);
res.pg.push(...checkImports);
push_array(res.pg, idxImports);
push_array(res.pg, fkImpots);
push_array(res.pg, pkImports);
push_array(res.pg, uniqueImports);
push_array(res.pg, policiesImports);
push_array(res.pg, checkImports);

const columnImports = Object.values(it.columns)
.map((col) => {
Expand All @@ -372,7 +372,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
return pgImportsList.has(type);
});

res.pg.push(...columnImports);
push_array(res.pg, columnImports);
return res;
},
{ pg: [] as string[] },
Expand Down Expand Up @@ -403,7 +403,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
return pgImportsList.has(type);
});

imports.pg.push(...columnImports);
push_array(imports.pg, columnImports);
});
});

Expand Down
11 changes: 6 additions & 5 deletions drizzle-kit/src/introspect-singlestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
UniqueConstraint,
} from './serializer/singlestoreSchema';
import { indexName } from './serializer/singlestoreSerializer';
import { push_array } from './utils';

// time precision to fsp
// {mode: "string"} for timestamp by default
Expand Down Expand Up @@ -151,9 +152,9 @@ export const schemaToTypeScript = (
(it) => 'unique',
);

res.singlestore.push(...idxImports);
res.singlestore.push(...pkImports);
res.singlestore.push(...uniqueImports);
push_array(res.singlestore, idxImports);
push_array(res.singlestore, pkImports);
push_array(res.singlestore, uniqueImports);

const columnImports = Object.values(it.columns)
.map((col) => {
Expand Down Expand Up @@ -183,7 +184,7 @@ export const schemaToTypeScript = (
return singlestoreImportsList.has(type);
});

res.singlestore.push(...columnImports);
push_array(res.singlestore, columnImports);
return res;
},
{ singlestore: [] as string[] },
Expand Down Expand Up @@ -220,7 +221,7 @@ export const schemaToTypeScript = (
return singlestoreImportsList.has(type);
});
imports.singlestore.push(...columnImports);
push_array(imports.singlestore, columnImports);
}); */

const tableStatements = Object.values(schema.tables).map((table) => {
Expand Down
15 changes: 8 additions & 7 deletions drizzle-kit/src/introspect-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
SQLiteSchemaInternal,
UniqueConstraint,
} from './serializer/sqliteSchema';
import { push_array } from './utils';

const sqliteImportsList = new Set([
'sqliteTable',
Expand Down Expand Up @@ -96,11 +97,11 @@ export const schemaToTypeScript = (
(it) => 'check',
);

res.sqlite.push(...idxImports);
res.sqlite.push(...fkImpots);
res.sqlite.push(...pkImports);
res.sqlite.push(...uniqueImports);
res.sqlite.push(...checkImports);
push_array(res.sqlite, idxImports);
push_array(res.sqlite, fkImpots);
push_array(res.sqlite, pkImports);
push_array(res.sqlite, uniqueImports);
push_array(res.sqlite, checkImports);

const columnImports = Object.values(it.columns)
.map((col) => {
Expand All @@ -110,7 +111,7 @@ export const schemaToTypeScript = (
return sqliteImportsList.has(type);
});

res.sqlite.push(...columnImports);
push_array(res.sqlite, columnImports);
return res;
},
{ sqlite: [] as string[] },
Expand All @@ -127,7 +128,7 @@ export const schemaToTypeScript = (
return sqliteImportsList.has(type);
});

imports.sqlite.push(...columnImports);
push_array(imports.sqlite, columnImports);
});

const tableStatements = Object.values(schema.tables).map((table) => {
Expand Down
5 changes: 3 additions & 2 deletions drizzle-kit/src/serializer/mysqlImports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { is } from 'drizzle-orm';
import { AnyMySqlTable, MySqlTable, MySqlView } from 'drizzle-orm/mysql-core';
import { safeRegister } from '../cli/commands/utils';
import { push_array } from '../utils';

export const prepareFromExports = (exports: Record<string, unknown>) => {
const tables: AnyMySqlTable[] = [];
Expand Down Expand Up @@ -30,8 +31,8 @@ export const prepareFromMySqlImports = async (imports: string[]) => {
const i0: Record<string, unknown> = require(`${it}`);
const prepared = prepareFromExports(i0);

tables.push(...prepared.tables);
views.push(...prepared.views);
push_array(tables, prepared.tables);
push_array(views, prepared.views);
}
unregister();
return { tables: Array.from(new Set(tables)), views };
Expand Down
17 changes: 9 additions & 8 deletions drizzle-kit/src/serializer/pgImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PgView,
} from 'drizzle-orm/pg-core';
import { safeRegister } from '../cli/commands/utils';
import { push_array } from '../utils';

export const prepareFromExports = (exports: Record<string, unknown>) => {
const tables: AnyPgTable[] = [];
Expand Down Expand Up @@ -81,14 +82,14 @@ export const prepareFromPgImports = async (imports: string[]) => {
const i0: Record<string, unknown> = require(`${it}`);
const prepared = prepareFromExports(i0);

tables.push(...prepared.tables);
enums.push(...prepared.enums);
schemas.push(...prepared.schemas);
sequences.push(...prepared.sequences);
views.push(...prepared.views);
matViews.push(...prepared.matViews);
roles.push(...prepared.roles);
policies.push(...prepared.policies);
push_array(tables, prepared.tables);
push_array(enums, prepared.enums);
push_array(schemas, prepared.schemas);
push_array(sequences, prepared.sequences);
push_array(views, prepared.views);
push_array(matViews, prepared.matViews);
push_array(roles, prepared.roles);
push_array(policies, prepared.policies);
}
unregister();

Expand Down
9 changes: 5 additions & 4 deletions drizzle-kit/src/serializer/pgSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
} from '../serializer/pgSchema';
import { type DB, escapeSingleQuotes, isPgArrayType } from '../utils';
import { getColumnCasing, sqlToStr } from './utils';
import { push_array } from '../utils';

export const indexName = (tableName: string, columns: string[]) => {
return `${tableName}_${columns.join('_')}_index`;
Expand Down Expand Up @@ -930,7 +931,7 @@ function prepareRoles(entities?: {
if (typeof entities.roles === 'object') {
if (entities.roles.provider) {
if (entities.roles.provider === 'supabase') {
excludeRoles.push(...[
push_array(excludeRoles, [
'anon',
'authenticator',
'authenticated',
Expand All @@ -941,14 +942,14 @@ function prepareRoles(entities?: {
'supabase_admin',
]);
} else if (entities.roles.provider === 'neon') {
excludeRoles.push(...['authenticated', 'anonymous']);
push_array(excludeRoles, ['authenticated', 'anonymous']);
}
}
if (entities.roles.include) {
includeRoles.push(...entities.roles.include);
push_array(includeRoles, entities.roles.include);
}
if (entities.roles.exclude) {
excludeRoles.push(...entities.roles.exclude);
push_array(excludeRoles, entities.roles.exclude);
}
} else {
useRoles = entities.roles;
Expand Down
Loading

0 comments on commit 6705fbd

Please sign in to comment.