Skip to content

Commit

Permalink
fix drizzle-kit non singlestore files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Nov 12, 2024
1 parent 746e091 commit 9a68340
Show file tree
Hide file tree
Showing 24 changed files with 1,013 additions and 91 deletions.
5 changes: 3 additions & 2 deletions drizzle-kit/src/cli/commands/pgIntrospect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderWithTask } from 'hanji';
import { Minimatch } from 'minimatch';
import { originUUID } from '../../global';
import type { PgSchema } from '../../serializer/pgSchema';
import type { PgSchema, PgSchemaInternal } from '../../serializer/pgSchema';
import { fromDatabase } from '../../serializer/pgSerializer';
import type { DB } from '../../utils';
import { Entities } from '../validations/cli';
Expand All @@ -12,6 +12,7 @@ export const pgPushIntrospect = async (
filters: string[],
schemaFilters: string[],
entities: Entities,
tsSchema?: PgSchemaInternal,
) => {
const matchers = filters.map((it) => {
return new Minimatch(it);
Expand Down Expand Up @@ -45,7 +46,7 @@ export const pgPushIntrospect = async (
);
const res = await renderWithTask(
progress,
fromDatabase(db, filter, schemaFilters, entities),
fromDatabase(db, filter, schemaFilters, entities, undefined, tsSchema),
);

const schema = { id: originUUID, prevId: '', ...res } as PgSchema;
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/cli/commands/pgPushUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export const pgSuggestions = async (db: DB, statements: JsonStatement[]) => {
}
}
}
const stmnt = fromJson([statement], 'postgresql');
const stmnt = fromJson([statement], 'postgresql', 'push');
if (typeof stmnt !== 'undefined') {
statementsToExecute.push(...stmnt);
}
Expand Down
13 changes: 10 additions & 3 deletions drizzle-kit/src/introspect-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
UniqueConstraint,
} from './serializer/mysqlSchema';
import { indexName } from './serializer/mysqlSerializer';
import { unescapeSingleQuotes } from './utils';

// time precision to fsp
// {mode: "string"} for timestamp by default
Expand Down Expand Up @@ -679,8 +680,9 @@ const column = (
)
} })`;

const mappedDefaultValue = mapColumnDefault(defaultValue, isExpression);
out += defaultValue
? `.default(${mapColumnDefault(defaultValue, isExpression)})`
? `.default(${isExpression ? mappedDefaultValue : unescapeSingleQuotes(mappedDefaultValue, true)})`
: '';
return out;
}
Expand Down Expand Up @@ -787,10 +789,15 @@ const column = (
}

if (lowered.startsWith('enum')) {
const values = lowered.substring('enum'.length + 1, lowered.length - 1);
const values = lowered
.substring('enum'.length + 1, lowered.length - 1)
.split(',')
.map((v) => unescapeSingleQuotes(v, true))
.join(',');
let out = `${casing(name)}: mysqlEnum(${dbColumnName({ name, casing: rawCasing, withMode: true })}[${values}])`;
const mappedDefaultValue = mapColumnDefault(defaultValue, isExpression);
out += defaultValue
? `.default(${mapColumnDefault(defaultValue, isExpression)})`
? `.default(${isExpression ? mappedDefaultValue : unescapeSingleQuotes(mappedDefaultValue, true)})`
: '';
return out;
}
Expand Down
42 changes: 28 additions & 14 deletions drizzle-kit/src/introspect-pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import './@types/utils';
import { toCamelCase } from 'drizzle-orm/casing';
import { Casing } from './cli/validations/common';
import { vectorOps } from './extensions/vector';
import { assertUnreachable } from './global';
import {
CheckConstraint,
Expand All @@ -25,6 +24,7 @@ import {
UniqueConstraint,
} from './serializer/pgSchema';
import { indexName } from './serializer/pgSerializer';
import { unescapeSingleQuotes } from './utils';

const pgImportsList = new Set([
'pgTable',
Expand Down Expand Up @@ -436,7 +436,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
const func = enumSchema ? `${enumSchema}.enum` : 'pgEnum';

const values = Object.values(it.values)
.map((it) => `'${it}'`)
.map((it) => `'${unescapeSingleQuotes(it, false)}'`)
.join(', ');
return `export const ${withCasing(paramName, casing)} = ${func}("${it.name}", [${values}])\n`;
})
Expand Down Expand Up @@ -690,7 +690,9 @@ const mapDefault = (
}

if (enumTypes.has(`${typeSchema}.${type.replace('[]', '')}`)) {
return typeof defaultValue !== 'undefined' ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return typeof defaultValue !== 'undefined'
? `.default(${mapColumnDefault(unescapeSingleQuotes(defaultValue, true), isExpression)})`
: '';
}

if (lowered.startsWith('integer')) {
Expand Down Expand Up @@ -737,18 +739,20 @@ const mapDefault = (
if (lowered.startsWith('timestamp')) {
return defaultValue === 'now()'
? '.defaultNow()'
: defaultValue === 'CURRENT_TIMESTAMP'
? '.default(sql`CURRENT_TIMESTAMP`)'
: defaultValue
: /^'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d{2}(:\d{2})?)?'$/.test(defaultValue) // Matches 'YYYY-MM-DD HH:MI:SS', 'YYYY-MM-DD HH:MI:SS.FFFFFF', 'YYYY-MM-DD HH:MI:SS+TZ', 'YYYY-MM-DD HH:MI:SS.FFFFFF+TZ' and 'YYYY-MM-DD HH:MI:SS+HH:MI'
? `.default(${mapColumnDefault(defaultValue, isExpression)})`
: defaultValue
? `.default(sql\`${defaultValue}\`)`
: '';
}

if (lowered.startsWith('time')) {
return defaultValue === 'now()'
? '.defaultNow()'
: defaultValue
: /^'\d{2}:\d{2}(:\d{2})?(\.\d+)?'$/.test(defaultValue) // Matches 'HH:MI', 'HH:MI:SS' and 'HH:MI:SS.FFFFFF'
? `.default(${mapColumnDefault(defaultValue, isExpression)})`
: defaultValue
? `.default(sql\`${defaultValue}\`)`
: '';
}

Expand All @@ -759,15 +763,17 @@ const mapDefault = (
if (lowered === 'date') {
return defaultValue === 'now()'
? '.defaultNow()'
: defaultValue === 'CURRENT_DATE'
? `.default(sql\`${defaultValue}\`)`
: defaultValue
: /^'\d{4}-\d{2}-\d{2}'$/.test(defaultValue) // Matches 'YYYY-MM-DD'
? `.default(${defaultValue})`
: defaultValue
? `.default(sql\`${defaultValue}\`)`
: '';
}

if (lowered.startsWith('text')) {
return typeof defaultValue !== 'undefined' ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return typeof defaultValue !== 'undefined'
? `.default(${mapColumnDefault(unescapeSingleQuotes(defaultValue, true), isExpression)})`
: '';
}

if (lowered.startsWith('jsonb')) {
Expand Down Expand Up @@ -801,7 +807,9 @@ const mapDefault = (
}

if (lowered.startsWith('varchar')) {
return typeof defaultValue !== 'undefined' ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return typeof defaultValue !== 'undefined'
? `.default(${mapColumnDefault(unescapeSingleQuotes(defaultValue, true), isExpression)})`
: '';
}

if (lowered.startsWith('point')) {
Expand All @@ -821,7 +829,9 @@ const mapDefault = (
}

if (lowered.startsWith('char')) {
return typeof defaultValue !== 'undefined' ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return typeof defaultValue !== 'undefined'
? `.default(${mapColumnDefault(unescapeSingleQuotes(defaultValue, true), isExpression)})`
: '';
}

return '';
Expand Down Expand Up @@ -1219,7 +1229,11 @@ const createTableIndexes = (tableName: string, idxs: Index[], casing: Casing): s
} else {
return `table.${withCasing(it.expression, casing)}${it.asc ? '.asc()' : '.desc()'}${
it.nulls === 'first' ? '.nullsFirst()' : '.nullsLast()'
}${it.opclass && vectorOps.includes(it.opclass) ? `.op("${it.opclass}")` : ''}`;
}${
it.opclass
? `.op("${it.opclass}")`
: ''
}`;
}
})
.join(', ')
Expand Down
4 changes: 1 addition & 3 deletions drizzle-kit/src/introspect-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,8 @@ const mapColumnDefault = (defaultValue: any) => {

if (
typeof defaultValue === 'string'
&& defaultValue.startsWith("'")
&& defaultValue.endsWith("'")
) {
return defaultValue.substring(1, defaultValue.length - 1);
return defaultValue.substring(1, defaultValue.length - 1).replaceAll('"', '\\"').replaceAll("''", "'");
}

return defaultValue;
Expand Down
24 changes: 16 additions & 8 deletions drizzle-kit/src/serializer/mysqlSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ import {
UniqueConstraint,
View,
} from '../serializer/mysqlSchema';
import type { DB } from '../utils';
import { type DB, escapeSingleQuotes } from '../utils';
import { getColumnCasing, sqlToStr } from './utils';

export const indexName = (tableName: string, columns: string[]) => {
return `${tableName}_${columns.join('_')}_index`;
};

const handleEnumType = (type: string) => {
let str = type.split('(')[1];
str = str.substring(0, str.length - 1);
const values = str.split(',').map((v) => `'${escapeSingleQuotes(v.substring(1, v.length - 1))}'`);
return `enum(${values.join(',')})`;
};

export const generateMySqlSnapshot = (
tables: AnyMySqlTable[],
views: MySqlView[],
Expand Down Expand Up @@ -68,7 +75,8 @@ export const generateMySqlSnapshot = (
columns.forEach((column) => {
const name = getColumnCasing(column, casing);
const notNull: boolean = column.notNull;
const sqlTypeLowered = column.getSQLType().toLowerCase();
const sqlType = column.getSQLType();
const sqlTypeLowered = sqlType.toLowerCase();
const autoIncrement = typeof (column as any).autoIncrement === 'undefined'
? false
: (column as any).autoIncrement;
Expand All @@ -77,7 +85,7 @@ export const generateMySqlSnapshot = (

const columnToSet: Column = {
name,
type: column.getSQLType(),
type: sqlType.startsWith('enum') ? handleEnumType(sqlType) : sqlType,
primaryKey: false,
// If field is autoincrement it's notNull by default
// notNull: autoIncrement ? true : notNull,
Expand Down Expand Up @@ -141,7 +149,7 @@ export const generateMySqlSnapshot = (
columnToSet.default = sqlToStr(column.default, casing);
} else {
if (typeof column.default === 'string') {
columnToSet.default = `'${column.default}'`;
columnToSet.default = `'${escapeSingleQuotes(column.default)}'`;
} else {
if (sqlTypeLowered === 'json') {
columnToSet.default = `'${JSON.stringify(column.default)}'`;
Expand Down Expand Up @@ -544,9 +552,9 @@ function clearDefaults(defaultValue: any, collate: string) {
.substring(collate.length, defaultValue.length)
.replace(/\\/g, '');
if (resultDefault.startsWith("'") && resultDefault.endsWith("'")) {
return `('${resultDefault.substring(1, resultDefault.length - 1)}')`;
return `('${escapeSingleQuotes(resultDefault.substring(1, resultDefault.length - 1))}')`;
} else {
return `'${resultDefault}'`;
return `'${escapeSingleQuotes(resultDefault.substring(1, resultDefault.length - 1))}'`;
}
} else {
return `(${resultDefault})`;
Expand Down Expand Up @@ -665,14 +673,14 @@ export const fromDatabase = async (
}

const newColumn: Column = {
default: columnDefault === null
default: columnDefault === null || columnDefault === undefined
? undefined
: /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault)
&& !['decimal', 'char', 'varchar'].some((type) => columnType.startsWith(type))
? Number(columnDefault)
: isDefaultAnExpression
? clearDefaults(columnDefault, collation)
: `'${columnDefault}'`,
: `'${escapeSingleQuotes(columnDefault)}'`,
autoincrement: isAutoincrement,
name: columnName,
type: changedType,
Expand Down
17 changes: 12 additions & 5 deletions drizzle-kit/src/serializer/pgSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { vectorOps } from 'src/extensions/vector';
import { mapValues, originUUID, snapshotVersion } from '../global';

import { any, array, boolean, enum as enumType, literal, number, object, record, string, TypeOf, union } from 'zod';
Expand Down Expand Up @@ -240,6 +239,7 @@ export const policy = object({
using: string().optional(),
withCheck: string().optional(),
on: string().optional(),
schema: string().optional(),
}).strict();

export const policySquashed = object({
Expand Down Expand Up @@ -554,10 +554,7 @@ export const PgSquasher = {
return `${idx.name};${
idx.columns
.map(
(c) =>
`${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${
c.opclass && vectorOps.includes(c.opclass) ? c.opclass : ''
}`,
(c) => `${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${c.opclass ? c.opclass : ''}`,
)
.join(',,')
};${idx.isUnique};${idx.concurrently};${idx.method};${idx.where};${JSON.stringify(idx.with)}`;
Expand Down Expand Up @@ -657,6 +654,16 @@ export const PgSquasher = {
squashPolicyPush: (policy: Policy) => {
return `${policy.name}--${policy.as}--${policy.for}--${policy.to?.join(',')}--${policy.on}`;
},
unsquashPolicyPush: (policy: string): Policy => {
const splitted = policy.split('--');
return {
name: splitted[0],
as: splitted[1] as Policy['as'],
for: splitted[2] as Policy['for'],
to: splitted[3].split(','),
on: splitted[4] !== 'undefined' ? splitted[4] : undefined,
};
},
squashPK: (pk: PrimaryKey) => {
return `${pk.columns.join(',')};${pk.name}`;
},
Expand Down
Loading

0 comments on commit 9a68340

Please sign in to comment.