Skip to content

Commit

Permalink
11
Browse files Browse the repository at this point in the history
  • Loading branch information
dengfuping committed Dec 30, 2024
1 parent e30ffa7 commit 90f433f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 89 deletions.
5 changes: 3 additions & 2 deletions drizzle-kit/src/serializer/mysqlSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ export const MySqlSquasher = {
return { name, columns: columns.split(',') };
},
squashFK: (fk: ForeignKey) => {
return `${fk.name};${fk.tableFrom};${fk.columnsFrom.join(',')};${fk.tableTo};${fk.columnsTo.join(',')};${fk.onUpdate ?? ''
};${fk.onDelete ?? ''}`;
return `${fk.name};${fk.tableFrom};${fk.columnsFrom.join(',')};${fk.tableTo};${fk.columnsTo.join(',')};${
fk.onUpdate ?? ''
};${fk.onDelete ?? ''}`;
},
unsquashFK: (input: string): ForeignKey => {
const [
Expand Down
209 changes: 123 additions & 86 deletions drizzle-kit/src/sqlgenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ class PgCreateRoleConvertor extends Convertor {
return statement.type === 'create_role' && dialect === 'postgresql';
}
override convert(statement: JsonCreateRoleStatement): string | string[] {
return `CREATE ROLE "${statement.name}"${statement.values.createDb || statement.values.createRole || !statement.values.inherit
? ` WITH${statement.values.createDb ? ' CREATEDB' : ''}${statement.values.createRole ? ' CREATEROLE' : ''}${statement.values.inherit ? '' : ' NOINHERIT'
}`
: ''
};`;
return `CREATE ROLE "${statement.name}"${
statement.values.createDb || statement.values.createRole || !statement.values.inherit
? ` WITH${statement.values.createDb ? ' CREATEDB' : ''}${statement.values.createRole ? ' CREATEROLE' : ''}${
statement.values.inherit ? '' : ' NOINHERIT'
}`
: ''
};`;
}
}

Expand All @@ -191,8 +193,9 @@ class PgAlterRoleConvertor extends Convertor {
return statement.type === 'alter_role' && dialect === 'postgresql';
}
override convert(statement: JsonAlterRoleStatement): string | string[] {
return `ALTER ROLE "${statement.name}"${` WITH${statement.values.createDb ? ' CREATEDB' : ' NOCREATEDB'}${statement.values.createRole ? ' CREATEROLE' : ' NOCREATEROLE'
}${statement.values.inherit ? ' INHERIT' : ' NOINHERIT'}`};`;
return `ALTER ROLE "${statement.name}"${` WITH${statement.values.createDb ? ' CREATEDB' : ' NOCREATEDB'}${
statement.values.createRole ? ' CREATEROLE' : ' NOCREATEROLE'
}${statement.values.inherit ? ' INHERIT' : ' NOINHERIT'}`};`;
}
}

Expand Down Expand Up @@ -268,14 +271,14 @@ class PgAlterPolicyConvertor extends Convertor {
const usingPart = newPolicy.using
? ` USING (${newPolicy.using})`
: oldPolicy.using
? ` USING (${oldPolicy.using})`
: '';
? ` USING (${oldPolicy.using})`
: '';

const withCheckPart = newPolicy.withCheck
? ` WITH CHECK (${newPolicy.withCheck})`
: oldPolicy.withCheck
? ` WITH CHECK (${oldPolicy.withCheck})`
: '';
? ` WITH CHECK (${oldPolicy.withCheck})`
: '';

return `ALTER POLICY "${oldPolicy.name}" ON ${tableNameWithSchema} TO ${newPolicy.to}${usingPart}${withCheckPart};`;
}
Expand Down Expand Up @@ -333,14 +336,14 @@ class PgAlterIndPolicyConvertor extends Convertor {
const usingPart = newPolicy.using
? ` USING (${newPolicy.using})`
: oldPolicy.using
? ` USING (${oldPolicy.using})`
: '';
? ` USING (${oldPolicy.using})`
: '';

const withCheckPart = newPolicy.withCheck
? ` WITH CHECK (${newPolicy.withCheck})`
: oldPolicy.withCheck
? ` WITH CHECK (${oldPolicy.withCheck})`
: '';
? ` WITH CHECK (${oldPolicy.withCheck})`
: '';

return `ALTER POLICY "${oldPolicy.name}" ON ${oldPolicy.on} TO ${newPolicy.to}${usingPart}${withCheckPart};`;
}
Expand Down Expand Up @@ -416,20 +419,26 @@ class PgCreateTableConvertor extends Convertor {
: `"${unsquashedIdentity?.name}"`;

const identity = unsquashedIdentity
? ` GENERATED ${unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${unsquashedIdentity.increment
? ` INCREMENT BY ${unsquashedIdentity.increment}`
: ''
}${unsquashedIdentity.minValue
? ` MINVALUE ${unsquashedIdentity.minValue}`
: ''
}${unsquashedIdentity.maxValue
? ` MAXVALUE ${unsquashedIdentity.maxValue}`
: ''
}${unsquashedIdentity.startWith
? ` START WITH ${unsquashedIdentity.startWith}`
: ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${unsquashedIdentity.cycle ? ` CYCLE` : ''
? ` GENERATED ${
unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${
unsquashedIdentity.increment
? ` INCREMENT BY ${unsquashedIdentity.increment}`
: ''
}${
unsquashedIdentity.minValue
? ` MINVALUE ${unsquashedIdentity.minValue}`
: ''
}${
unsquashedIdentity.maxValue
? ` MAXVALUE ${unsquashedIdentity.maxValue}`
: ''
}${
unsquashedIdentity.startWith
? ` START WITH ${unsquashedIdentity.startWith}`
: ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${
unsquashedIdentity.cycle ? ` CYCLE` : ''
})`
: '';

Expand All @@ -452,8 +461,9 @@ class PgCreateTableConvertor extends Convertor {
for (const uniqueConstraint of uniqueConstraints) {
statement += ',\n';
const unsquashedUnique = PgSquasher.unsquashUnique(uniqueConstraint);
statement += `\tCONSTRAINT "${unsquashedUnique.name}" UNIQUE${unsquashedUnique.nullsNotDistinct ? ' NULLS NOT DISTINCT' : ''
}(\"${unsquashedUnique.columns.join(`","`)}\")`;
statement += `\tCONSTRAINT "${unsquashedUnique.name}" UNIQUE${
unsquashedUnique.nullsNotDistinct ? ' NULLS NOT DISTINCT' : ''
}(\"${unsquashedUnique.columns.join(`","`)}\")`;
// statement += `\n`;
}
}
Expand Down Expand Up @@ -539,7 +549,7 @@ class MySqlCreateTableConvertor extends Convertor {
.map((it) => {
return internals?.indexes
? internals?.indexes[unsquashedUnique.name]?.columns[it]
?.isExpression
?.isExpression
? it
: `\`${it}\``
: `\`${it}\``;
Expand Down Expand Up @@ -623,7 +633,7 @@ class SingleStoreCreateTableConvertor extends Convertor {
.map((it) => {
return internals?.indexes
? internals?.indexes[unsquashedUnique.name]?.columns[it]
?.isExpression
?.isExpression
? it
: `\`${it}\``
: `\`${it}\``;
Expand Down Expand Up @@ -901,8 +911,9 @@ class PgAlterViewSchemaConvertor extends Convertor {
convert(st: JsonAlterViewAlterSchemaStatement) {
const { fromSchema, toSchema, name, materialized } = st;

const statement = `ALTER${materialized ? ' MATERIALIZED' : ''
} VIEW "${fromSchema}"."${name}" SET SCHEMA "${toSchema}";`;
const statement = `ALTER${
materialized ? ' MATERIALIZED' : ''
} VIEW "${fromSchema}"."${name}" SET SCHEMA "${toSchema}";`;

return statement;
}
Expand Down Expand Up @@ -1007,20 +1018,26 @@ class PgAlterTableAlterColumnSetGenerated extends Convertor {
: `"${unsquashedIdentity?.name}"`;

const identityStatement = unsquashedIdentity
? ` GENERATED ${unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${unsquashedIdentity.increment
? ` INCREMENT BY ${unsquashedIdentity.increment}`
: ''
}${unsquashedIdentity.minValue
? ` MINVALUE ${unsquashedIdentity.minValue}`
: ''
}${unsquashedIdentity.maxValue
? ` MAXVALUE ${unsquashedIdentity.maxValue}`
: ''
}${unsquashedIdentity.startWith
? ` START WITH ${unsquashedIdentity.startWith}`
: ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${unsquashedIdentity.cycle ? ` CYCLE` : ''
? ` GENERATED ${
unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${
unsquashedIdentity.increment
? ` INCREMENT BY ${unsquashedIdentity.increment}`
: ''
}${
unsquashedIdentity.minValue
? ` MINVALUE ${unsquashedIdentity.minValue}`
: ''
}${
unsquashedIdentity.maxValue
? ` MAXVALUE ${unsquashedIdentity.maxValue}`
: ''
}${
unsquashedIdentity.startWith
? ` START WITH ${unsquashedIdentity.startWith}`
: ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${
unsquashedIdentity.cycle ? ` CYCLE` : ''
})`
: '';

Expand Down Expand Up @@ -1071,7 +1088,8 @@ class PgAlterTableAlterColumnAlterGenerated extends Convertor {

if (unsquashedOldIdentity.type !== unsquashedIdentity.type) {
statementsToReturn.push(
`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET GENERATED ${unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET GENERATED ${
unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
};`,
);
}
Expand Down Expand Up @@ -1108,7 +1126,8 @@ class PgAlterTableAlterColumnAlterGenerated extends Convertor {

if (unsquashedOldIdentity.cycle !== unsquashedIdentity.cycle) {
statementsToReturn.push(
`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET ${unsquashedIdentity.cycle ? `CYCLE` : 'NO CYCLE'
`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET ${
unsquashedIdentity.cycle ? `CYCLE` : 'NO CYCLE'
};`,
);
}
Expand All @@ -1130,8 +1149,9 @@ class PgAlterTableAddUniqueConstraintConvertor extends Convertor {
? `"${statement.schema}"."${statement.tableName}"`
: `"${statement.tableName}"`;

return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${unsquashed.name}" UNIQUE${unsquashed.nullsNotDistinct ? ' NULLS NOT DISTINCT' : ''
}("${unsquashed.columns.join('","')}");`;
return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${unsquashed.name}" UNIQUE${
unsquashed.nullsNotDistinct ? ' NULLS NOT DISTINCT' : ''
}("${unsquashed.columns.join('","')}");`;
}
}

Expand Down Expand Up @@ -1191,8 +1211,9 @@ class MySQLAlterTableAddUniqueConstraintConvertor extends Convertor {
convert(statement: JsonCreateUniqueConstraint): string {
const unsquashed = MySqlSquasher.unsquashUnique(statement.data);

return `ALTER TABLE \`${statement.tableName}\` ADD CONSTRAINT \`${unsquashed.name}\` UNIQUE(\`${unsquashed.columns.join('`,`')
}\`);`;
return `ALTER TABLE \`${statement.tableName}\` ADD CONSTRAINT \`${unsquashed.name}\` UNIQUE(\`${
unsquashed.columns.join('`,`')
}\`);`;
}
}

Expand Down Expand Up @@ -1228,8 +1249,9 @@ class SingleStoreAlterTableAddUniqueConstraintConvertor extends Convertor {
convert(statement: JsonCreateUniqueConstraint): string {
const unsquashed = SingleStoreSquasher.unsquashUnique(statement.data);

return `ALTER TABLE \`${statement.tableName}\` ADD CONSTRAINT \`${unsquashed.name}\` UNIQUE(\`${unsquashed.columns.join('`,`')
}\`);`;
return `ALTER TABLE \`${statement.tableName}\` ADD CONSTRAINT \`${unsquashed.name}\` UNIQUE(\`${
unsquashed.columns.join('`,`')
}\`);`;
}
}
class SingleStoreAlterTableDropUniqueConstraintConvertor extends Convertor {
Expand Down Expand Up @@ -1266,9 +1288,11 @@ class CreatePgSequenceConvertor extends Convertor {

const sequenceWithSchema = schema ? `"${schema}"."${name}"` : `"${name}"`;

return `CREATE SEQUENCE ${sequenceWithSchema}${values.increment ? ` INCREMENT BY ${values.increment}` : ''}${values.minValue ? ` MINVALUE ${values.minValue}` : ''
}${values.maxValue ? ` MAXVALUE ${values.maxValue}` : ''}${values.startWith ? ` START WITH ${values.startWith}` : ''
}${values.cache ? ` CACHE ${values.cache}` : ''}${values.cycle ? ` CYCLE` : ''};`;
return `CREATE SEQUENCE ${sequenceWithSchema}${values.increment ? ` INCREMENT BY ${values.increment}` : ''}${
values.minValue ? ` MINVALUE ${values.minValue}` : ''
}${values.maxValue ? ` MAXVALUE ${values.maxValue}` : ''}${
values.startWith ? ` START WITH ${values.startWith}` : ''
}${values.cache ? ` CACHE ${values.cache}` : ''}${values.cycle ? ` CYCLE` : ''};`;
}
}

Expand Down Expand Up @@ -1335,9 +1359,11 @@ class AlterPgSequenceConvertor extends Convertor {

const sequenceWithSchema = schema ? `"${schema}"."${name}"` : `"${name}"`;

return `ALTER SEQUENCE ${sequenceWithSchema}${increment ? ` INCREMENT BY ${increment}` : ''}${minValue ? ` MINVALUE ${minValue}` : ''
}${maxValue ? ` MAXVALUE ${maxValue}` : ''}${startWith ? ` START WITH ${startWith}` : ''}${cache ? ` CACHE ${cache}` : ''
}${cycle ? ` CYCLE` : ''};`;
return `ALTER SEQUENCE ${sequenceWithSchema}${increment ? ` INCREMENT BY ${increment}` : ''}${
minValue ? ` MINVALUE ${minValue}` : ''
}${maxValue ? ` MAXVALUE ${maxValue}` : ''}${startWith ? ` START WITH ${startWith}` : ''}${
cache ? ` CACHE ${cache}` : ''
}${cycle ? ` CYCLE` : ''};`;
}
}

Expand Down Expand Up @@ -1719,20 +1745,26 @@ class PgAlterTableAddColumnConvertor extends Convertor {
: `"${unsquashedIdentity?.name}"`;

const identityStatement = unsquashedIdentity
? ` GENERATED ${unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${unsquashedIdentity.increment
? ` GENERATED ${
unsquashedIdentity.type === 'always' ? 'ALWAYS' : 'BY DEFAULT'
} AS IDENTITY (sequence name ${identityWithSchema}${
unsquashedIdentity.increment
? ` INCREMENT BY ${unsquashedIdentity.increment}`
: ''
}${unsquashedIdentity.minValue
}${
unsquashedIdentity.minValue
? ` MINVALUE ${unsquashedIdentity.minValue}`
: ''
}${unsquashedIdentity.maxValue
}${
unsquashedIdentity.maxValue
? ` MAXVALUE ${unsquashedIdentity.maxValue}`
: ''
}${unsquashedIdentity.startWith
}${
unsquashedIdentity.startWith
? ` START WITH ${unsquashedIdentity.startWith}`
: ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${unsquashedIdentity.cycle ? ` CYCLE` : ''
}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ''}${
unsquashedIdentity.cycle ? ` CYCLE` : ''
})`
: '';

Expand Down Expand Up @@ -1821,10 +1853,11 @@ export class SQLiteAlterTableAddColumnConvertor extends Convertor {
const referenceAsObject = referenceData
? SQLiteSquasher.unsquashFK(referenceData)
: undefined;
const referenceStatement = `${referenceAsObject
? ` REFERENCES ${referenceAsObject.tableTo}(${referenceAsObject.columnsTo})`
: ''
}`;
const referenceStatement = `${
referenceAsObject
? ` REFERENCES ${referenceAsObject.tableTo}(${referenceAsObject.columnsTo})`
: ''
}`;
// const autoincrementStatement = `${autoincrement ? 'AUTO_INCREMENT' : ''}`
const generatedStatement = generated
? ` GENERATED ALWAYS AS ${generated.as} ${generated.type.toUpperCase()}`
Expand Down Expand Up @@ -2972,8 +3005,9 @@ class PgAlterTableCreateCompositePrimaryKeyConvertor extends Convertor {
? `"${statement.schema}"."${statement.tableName}"`
: `"${statement.tableName}"`;

return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.constraintName}" PRIMARY KEY("${columns.join('","')
}");`;
return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.constraintName}" PRIMARY KEY("${
columns.join('","')
}");`;
}
}
class PgAlterTableDeleteCompositePrimaryKeyConvertor extends Convertor {
Expand Down Expand Up @@ -3007,8 +3041,9 @@ class PgAlterTableAlterCompositePrimaryKeyConvertor extends Convertor {
? `"${statement.schema}"."${statement.tableName}"`
: `"${statement.tableName}"`;

return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${statement.oldConstraintName}";\n${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newConstraintName}" PRIMARY KEY("${newColumns.join('","')
}");`;
return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${statement.oldConstraintName}";\n${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newConstraintName}" PRIMARY KEY("${
newColumns.join('","')
}");`;
}
}

Expand Down Expand Up @@ -3403,10 +3438,12 @@ class CreatePgIndexConvertor extends Convertor {
const value = columns
.map(
(it) =>
`${it.isExpression ? it.expression : `"${it.expression}"`}${it.opclass ? ` ${it.opclass}` : it.asc ? '' : ' DESC'
}${(it.asc && it.nulls && it.nulls === 'last') || it.opclass
? ''
: ` NULLS ${it.nulls!.toUpperCase()}`
`${it.isExpression ? it.expression : `"${it.expression}"`}${
it.opclass ? ` ${it.opclass}` : it.asc ? '' : ' DESC'
}${
(it.asc && it.nulls && it.nulls === 'last') || it.opclass
? ''
: ` NULLS ${it.nulls!.toUpperCase()}`
}`,
)
.join(',');
Expand All @@ -3426,11 +3463,11 @@ class CreatePgIndexConvertor extends Convertor {
return reversedString;
}

return `CREATE ${indexPart}${concurrently ? ' CONCURRENTLY' : ''
} "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${Object.keys(withMap!).length !== 0
? ` WITH (${reverseLogic(withMap!)})`
: ''
}${where ? ` WHERE ${where}` : ''};`;
return `CREATE ${indexPart}${
concurrently ? ' CONCURRENTLY' : ''
} "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${
Object.keys(withMap!).length !== 0
}${where ? ` WHERE ${where}` : ''};`;
}
}

Expand Down
Loading

0 comments on commit 90f433f

Please sign in to comment.