Skip to content

Commit

Permalink
remain fix of non singlestore files and common files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Nov 13, 2024
1 parent 9a68340 commit dc3d78c
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 166 deletions.
1 change: 1 addition & 0 deletions drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export const introspectMysql = async (
const schema = { id: originUUID, prevId: '', ...res } as MySqlSchema;
const ts = mysqlSchemaToTypeScript(schema, casing);
const relationsTs = relationsToTypeScript(schema, casing);
const { internal, ...schemaWithoutInternals } = schema;

const schemaFile = join(out, 'schema.ts');
writeFileSync(schemaFile, ts.file);
Expand Down
13 changes: 2 additions & 11 deletions drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,9 @@ export const prepareAndMigratePg = async (config: GenerateConfig) => {
};

export const preparePgPush = async (
schemaPath: string | string[],
snapshot: PgSchema,
schemaFilter: string[],
casing: CasingType | undefined,
cur: PgSchema,
prev: PgSchema,
) => {
const { prev, cur } = await preparePgDbPushSnapshot(
snapshot,
schemaPath,
casing,
schemaFilter,
);

const validatedPrev = pgSchema.parse(prev);
const validatedCur = pgSchema.parse(cur);

Expand Down
28 changes: 12 additions & 16 deletions drizzle-kit/src/cli/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import chalk from 'chalk';
import { randomUUID } from 'crypto';
import { render } from 'hanji';
import { serializePg } from 'src/serializer';
import { fromJson } from '../../sqlgenerator';
import { Select } from '../selector-ui';
import { Entities } from '../validations/cli';
Expand Down Expand Up @@ -320,20 +322,15 @@ export const pgPush = async (
const { pgPushIntrospect } = await import('./pgIntrospect');

const db = await preparePostgresDB(credentials);
const { schema } = await pgPushIntrospect(
db,
tablesFilter,
schemasFilter,
entities,
);
const serialized = await serializePg(schemaPath, casing, schemasFilter);

const { schema } = await pgPushIntrospect(db, tablesFilter, schemasFilter, entities, serialized);

const { preparePgPush } = await import('./migrate');

const statements = await preparePgPush(
schemaPath,
{ id: randomUUID(), prevId: schema.id, ...serialized },
schema,
schemasFilter,
casing,
);

try {
Expand Down Expand Up @@ -405,9 +402,7 @@ export const pgPush = async (
}${
matViewsToRemove.length > 0
? ` remove ${matViewsToRemove.length} ${
matViewsToRemove.length > 1
? 'materialized views'
: 'materialize view'
matViewsToRemove.length > 1 ? 'materialized views' : 'materialize view'
},`
: ' '
}`
Expand Down Expand Up @@ -464,6 +459,7 @@ export const sqlitePush = async (
tablesToRemove,
tablesToTruncate,
infoToPrint,
schemasToRemove,
} = await sqliteSuggestions(
db,
statements.statements,
Expand Down Expand Up @@ -537,15 +533,15 @@ export const sqlitePush = async (
render(`\n[${chalk.blue('i')}] No changes detected`);
} else {
if (!('driver' in credentials)) {
await db.query('begin');
await db.run('begin');
try {
for (const dStmnt of statementsToExecute) {
await db.query(dStmnt);
await db.run(dStmnt);
}
await db.query('commit');
await db.run('commit');
} catch (e) {
console.error(e);
await db.query('rollback');
await db.run('rollback');
process.exit(1);
}
}
Expand Down
10 changes: 3 additions & 7 deletions drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
sqliteCredentials,
} from '../validations/sqlite';
import { studioCliParams, studioConfig } from '../validations/studio';
import { error } from '../views';
import { error, grey } from '../views';

// NextJs default config is target: es5, which esbuild-register can't consume
const assertES5 = async (unregister: () => void) => {
Expand Down Expand Up @@ -769,12 +769,8 @@ export const drizzleConfigFromFile = async (
): Promise<CliConfig> => {
const prefix = process.env.TEST_CONFIG_PATH_PREFIX || '';

const defaultTsConfigExists = existsSync(
resolve(join(prefix, 'drizzle.config.ts')),
);
const defaultJsConfigExists = existsSync(
resolve(join(prefix, 'drizzle.config.js')),
);
const defaultTsConfigExists = existsSync(resolve(join(prefix, 'drizzle.config.ts')));
const defaultJsConfigExists = existsSync(resolve(join(prefix, 'drizzle.config.js')));
const defaultJsonConfigExists = existsSync(
join(resolve('drizzle.config.json')),
);
Expand Down
4 changes: 1 addition & 3 deletions drizzle-kit/src/cli/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ const optionDriver = string()
.enum(...drivers)
.desc('Database driver');

const optionCasing = string()
.enum('camelCase', 'snake_case')
.desc('Casing for serialization');
const optionCasing = string().enum('camelCase', 'snake_case').desc('Casing for serialization');

export const generate = command({
name: 'generate',
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/cli/validations/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const outputs = {
),
noDialect: () =>
withStyle.error(
`Please specify 'dialect' param in config, either of 'pg', 'mysql', 'sqlite' or singlestore`,
`Please specify 'dialect' param in config, either of 'postgresql', 'mysql', 'sqlite', turso or singlestore`,
),
},
common: {
Expand Down
11 changes: 8 additions & 3 deletions drizzle-kit/src/cli/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ export const schema = (schema: CommonSchema): string => {
.map((t) => {
const columnsCount = Object.values(t.columns).length;
const indexesCount = Object.values(t.indexes).length;
// should we have fks?
// const foreignKeys = Object.values(t.foreignKeys).length;
let foreignKeys: number = 0;
// Singlestore doesn't have foreign keys
if (schema.dialect !== 'singlestore') {
// @ts-expect-error
foreignKeys = Object.values(t.foreignKeys).length;
}

return `${chalk.bold.blue(t.name)} ${
chalk.gray(
`${columnsCount} columns ${indexesCount} indexes`,
`${columnsCount} columns ${indexesCount} indexes ${foreignKeys} fks`,
)
}`;
})
Expand Down
22 changes: 6 additions & 16 deletions drizzle-kit/src/jsonStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3116,9 +3116,7 @@ export const prepareAddCompositePrimaryKeyPg = (
tableName,
data: it,
schema,
constraintName: json2.tables[`${schema || 'public'}.${tableName}`].compositePrimaryKeys[
unsquashed.name
].name,
constraintName: PgSquasher.unsquashPK(it).name,
} as JsonCreateCompositePK;
});
};
Expand All @@ -3136,9 +3134,7 @@ export const prepareDeleteCompositePrimaryKeyPg = (
tableName,
data: it,
schema,
constraintName: json1.tables[`${schema || 'public'}.${tableName}`].compositePrimaryKeys[
PgSquasher.unsquashPK(it).name
].name,
constraintName: PgSquasher.unsquashPK(it).name,
} as JsonDeleteCompositePK;
});
};
Expand All @@ -3158,12 +3154,8 @@ export const prepareAlterCompositePrimaryKeyPg = (
old: it.__old,
new: it.__new,
schema,
oldConstraintName: json1.tables[`${schema || 'public'}.${tableName}`].compositePrimaryKeys[
PgSquasher.unsquashPK(it.__old).name
].name,
newConstraintName: json2.tables[`${schema || 'public'}.${tableName}`].compositePrimaryKeys[
PgSquasher.unsquashPK(it.__new).name
].name,
oldConstraintName: PgSquasher.unsquashPK(it.__old).name,
newConstraintName: PgSquasher.unsquashPK(it.__new).name,
} as JsonAlterCompositePK;
});
};
Expand Down Expand Up @@ -3276,7 +3268,7 @@ export const prepareAddCompositePrimaryKeyMySql = (
type: 'create_composite_pk',
tableName,
data: it,
constraintName: json2.tables[tableName].compositePrimaryKeys[unsquashed.name].name,
constraintName: unsquashed.name,
} as JsonCreateCompositePK);
}
return res;
Expand All @@ -3289,13 +3281,11 @@ export const prepareDeleteCompositePrimaryKeyMySql = (
json1: MySqlSchema,
): JsonDeleteCompositePK[] => {
return Object.values(pks).map((it) => {
const unsquashed = MySqlSquasher.unsquashPK(it);
return {
type: 'delete_composite_pk',
tableName,
data: it,
constraintName: json1.tables[tableName].compositePrimaryKeys[
MySqlSquasher.unsquashPK(it).name
].name,
} as JsonDeleteCompositePK;
});
};
Expand Down
60 changes: 40 additions & 20 deletions drizzle-kit/src/snapshotsDiffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,10 @@ export const applyPgSnapshotsDiff = async (
const { renamed, created, deleted } = await policyResolver({
tableName: entry.name,
schema: entry.schema,
deleted: entry.policies.deleted.map(PgSquasher.unsquashPolicy),
created: entry.policies.added.map(PgSquasher.unsquashPolicy),
deleted: entry.policies.deleted.map(
action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy,
),
created: entry.policies.added.map(action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy),
});

if (created.length > 0) {
Expand Down Expand Up @@ -1058,7 +1060,9 @@ export const applyPgSnapshotsDiff = async (
] || [];

const newName = columnChangeFor(policyKey, rens);
const unsquashedPolicy = PgSquasher.unsquashPolicy(policy);
const unsquashedPolicy = action === 'push'
? PgSquasher.unsquashPolicyPush(policy)
: PgSquasher.unsquashPolicy(policy);
unsquashedPolicy.name = newName;
policy = PgSquasher.squashPolicy(unsquashedPolicy);
return newName;
Expand All @@ -1083,8 +1087,12 @@ export const applyPgSnapshotsDiff = async (
}[];

const { renamed: indPolicyRenames, created, deleted } = await indPolicyResolver({
deleted: indPolicyRes.deleted.map((t) => PgSquasher.unsquashPolicy(t.values)),
created: indPolicyRes.added.map((t) => PgSquasher.unsquashPolicy(t.values)),
deleted: indPolicyRes.deleted.map((t) =>
action === 'push' ? PgSquasher.unsquashPolicyPush(t.values) : PgSquasher.unsquashPolicy(t.values)
),
created: indPolicyRes.added.map((t) =>
action === 'push' ? PgSquasher.unsquashPolicyPush(t.values) : PgSquasher.unsquashPolicy(t.values)
),
});

if (created.length > 0) {
Expand Down Expand Up @@ -1250,22 +1258,22 @@ export const applyPgSnapshotsDiff = async (
// This part is needed to make sure that same columns in a table are not triggered for change
// there is a case where orm and kit are responsible for pk name generation and one of them is not sorting name
// We double-check that pk with same set of columns are both in added and deleted diffs
let addedColumns: string[] = [];
let addedColumns: { name: string; columns: string[] } | undefined;
for (const addedPkName of Object.keys(it.addedCompositePKs)) {
const addedPkColumns = it.addedCompositePKs[addedPkName];
addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
addedColumns = PgSquasher.unsquashPK(addedPkColumns);
}

let deletedColumns: string[] = [];
let deletedColumns: { name: string; columns: string[] } | undefined;
for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
deletedColumns = PgSquasher.unsquashPK(deletedPkColumns);
}

// Don't need to sort, but need to add tests for it
// addedColumns.sort();
// deletedColumns.sort();
const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
const doPerformDeleteAndCreate = JSON.stringify(addedColumns ?? {}) !== JSON.stringify(deletedColumns ?? {});

let addedCompositePKs: JsonCreateCompositePK[] = [];
let deletedCompositePKs: JsonDeleteCompositePK[] = [];
Expand Down Expand Up @@ -1436,10 +1444,14 @@ export const applyPgSnapshotsDiff = async (
typedResult.alteredPolicies.forEach(({ values }) => {
// return prepareAlterIndPolicyJson(json1.policies[it.name], json2.policies[it.name]);

const policy = PgSquasher.unsquashPolicy(values);
const policy = action === 'push' ? PgSquasher.unsquashPolicyPush(values) : PgSquasher.unsquashPolicy(values);

const newPolicy = PgSquasher.unsquashPolicy(json2.policies[policy.name].values);
const oldPolicy = PgSquasher.unsquashPolicy(json1.policies[policy.name].values);
const newPolicy = action === 'push'
? PgSquasher.unsquashPolicyPush(json2.policies[policy.name].values)
: PgSquasher.unsquashPolicy(json2.policies[policy.name].values);
const oldPolicy = action === 'push'
? PgSquasher.unsquashPolicyPush(json2.policies[policy.name].values)
: PgSquasher.unsquashPolicy(json1.policies[policy.name].values);

if (newPolicy.as !== oldPolicy.as) {
jsonDropIndPoliciesStatements.push(
Expand Down Expand Up @@ -1509,8 +1521,12 @@ export const applyPgSnapshotsDiff = async (
alteredTables.forEach((it) => {
// handle policies
Object.keys(it.alteredPolicies).forEach((policyName: string) => {
const newPolicy = PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__new);
const oldPolicy = PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__old);
const newPolicy = action === 'push'
? PgSquasher.unsquashPolicyPush(it.alteredPolicies[policyName].__new)
: PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__new);
const oldPolicy = action === 'push'
? PgSquasher.unsquashPolicyPush(it.alteredPolicies[policyName].__old)
: PgSquasher.unsquashPolicy(it.alteredPolicies[policyName].__old);

if (newPolicy.as !== oldPolicy.as) {
jsonDropPoliciesStatements.push(
Expand Down Expand Up @@ -1584,7 +1600,8 @@ export const applyPgSnapshotsDiff = async (
}

// handle table.isRLSEnabled
if (table.isRLSEnabled !== tableInPreviousState.isRLSEnabled) {
const wasRlsEnabled = tableInPreviousState ? tableInPreviousState.isRLSEnabled : false;
if (table.isRLSEnabled !== wasRlsEnabled) {
if (table.isRLSEnabled) {
// was force enabled
jsonEnableRLSStatements.push({ type: 'enable_rls', tableName: table.name, schema: table.schema });
Expand Down Expand Up @@ -1777,7 +1794,11 @@ export const applyPgSnapshotsDiff = async (

jsonCreatePoliciesStatements.push(...([] as JsonCreatePolicyStatement[]).concat(
...(createdTables.map((it) =>
prepareCreatePolicyJsons(it.name, it.schema, Object.values(it.policies).map(PgSquasher.unsquashPolicy))
prepareCreatePolicyJsons(
it.name,
it.schema,
Object.values(it.policies).map(action === 'push' ? PgSquasher.unsquashPolicyPush : PgSquasher.unsquashPolicy),
)
)),
));
const createViews: JsonCreatePgViewStatement[] = [];
Expand Down Expand Up @@ -2052,7 +2073,7 @@ export const applyPgSnapshotsDiff = async (
return true;
});

const sqlStatements = fromJson(filteredEnumsJsonStatements, 'postgresql');
const sqlStatements = fromJson(filteredEnumsJsonStatements, 'postgresql', action);

const uniqueSqlStatements: string[] = [];
sqlStatements.forEach((ss) => {
Expand Down Expand Up @@ -2628,12 +2649,11 @@ export const applyMysqlSnapshotsDiff = async (
jsonStatements.push(...jsonDeletedCompositePKs);
jsonStatements.push(...jsonTableAlternations);
jsonStatements.push(...jsonAddedCompositePKs);
jsonStatements.push(...jsonAddColumnsStatemets);

jsonStatements.push(...jsonAddedUniqueConstraints);
jsonStatements.push(...jsonDeletedUniqueConstraints);

jsonStatements.push(...jsonAddColumnsStatemets);

jsonStatements.push(...jsonCreateReferencesForCreatedTables);
jsonStatements.push(...jsonCreateIndexesForCreatedTables);
jsonStatements.push(...jsonCreatedCheckConstraints);
Expand Down
Loading

0 comments on commit dc3d78c

Please sign in to comment.