Skip to content

Commit d964d85

Browse files
committed
+
1 parent 9f01dbf commit d964d85

File tree

8 files changed

+49
-741
lines changed

8 files changed

+49
-741
lines changed

drizzle-kit/src/cli/commands/pull-mysql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const handle = async (
4141
});
4242
const { ddl } = interimToDDL(schema);
4343

44-
const ts = ddlToTypeScript(ddl, schema.viewColumns, casing);
44+
const ts = ddlToTypeScript(ddl, schema.viewColumns, casing, "mysql");
4545
const relations = relationsToTypeScript(ddl.fks.list(), casing);
4646

4747
const schemaFile = join(out, 'schema.ts');

drizzle-kit/src/cli/commands/pull-singlestore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const handle = async (
3838

3939
const { ddl } = interimToDDL(res);
4040

41-
const ts = ddlToTypeScript(ddl, res.viewColumns, casing);
41+
const ts = ddlToTypeScript(ddl, res.viewColumns, casing, "singlestore");
4242
const relations = relationsToTypeScript(ddl.fks.list(), casing);
4343

4444
const schemaFile = join(out, 'schema.ts');

drizzle-kit/src/cli/commands/up-postgres.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ export const upToV8 = (it: Record<string, any>): { snapshot: PostgresSnapshot; h
229229
createRole: role.createRole,
230230
createDb: role.createDb,
231231
inherit: role.inherit,
232+
bypassRls: null,
233+
canLogin: null,
234+
connLimit: null,
235+
password: null,
236+
replication: null,
237+
superuser: null,
238+
validUntil: null,
232239
});
233240
}
234241

drizzle-kit/src/dialects/mysql/grammar.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const parseParams = (type: string) => {
3535

3636
export interface SqlType<MODE = unknown> {
3737
is(type: string): boolean;
38-
drizzleImport(): Import;
38+
drizzleImport(vendor?: 'singlestore' | 'mysql'): Import;
3939
defaultFromDrizzle(value: unknown, mode?: MODE): Column['default'];
4040
defaultFromIntrospect(value: string): Column['default'];
4141
defaultToSQL(value: Column['default']): string;
@@ -498,7 +498,7 @@ export const Year: SqlType = {
498498

499499
export const Enum: SqlType = {
500500
is: (type) => /^(?:enum)(?:[\s(].*)?$/i.test(type),
501-
drizzleImport: () => 'mysqlEnum',
501+
drizzleImport: (vendor) => vendor === 'mysql' ? 'mysqlEnum' : 'singlestoreEnum',
502502
defaultFromDrizzle: (value) => {
503503
return String(value);
504504
},
@@ -517,7 +517,7 @@ export const Enum: SqlType = {
517517
},
518518
};
519519

520-
export const typeFor = (sqlType: string): SqlType | null => {
520+
export const typeFor = (sqlType: string): SqlType => {
521521
if (Boolean.is(sqlType)) return Boolean;
522522
if (TinyInt.is(sqlType)) return TinyInt;
523523
if (SmallInt.is(sqlType)) return SmallInt;
@@ -543,7 +543,7 @@ export const typeFor = (sqlType: string): SqlType | null => {
543543
if (Time.is(sqlType)) return Time;
544544
if (Year.is(sqlType)) return Year;
545545
if (Enum.is(sqlType)) return Enum;
546-
return null;
546+
throw new Error(`unknown sql type: ${sqlType}`);
547547
};
548548

549549
type InvalidDefault = 'text_no_parentecies';

drizzle-kit/src/dialects/mysql/typescript.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ export const imports = [
3232
'varchar',
3333
'year',
3434
'mysqlEnum',
35+
'singlestoreEnum',
36+
// TODO: add new type BSON
37+
// TODO: add new type Blob
38+
// TODO: add new type UUID
39+
// TODO: add new type GUID
40+
// TODO: add new type Vector
41+
// TODO: add new type GeoPoint
3542
] as const;
3643
export type Import = typeof imports[number];
3744

3845
const mysqlImportsList = new Set([
3946
'mysqlTable',
47+
'singlestoreTable',
4048
...imports,
4149
]);
4250

@@ -106,6 +114,7 @@ export const ddlToTypeScript = (
106114
ddl: MysqlDDL,
107115
viewColumns: ViewColumn[],
108116
casing: Casing,
117+
vendor: 'mysql' | 'singlestore',
109118
) => {
110119
const withCasing = prepareCasing(casing);
111120

@@ -115,9 +124,9 @@ export const ddlToTypeScript = (
115124
}
116125

117126
const imports = new Set<string>([
118-
'mysqlTable',
119-
'mysqlSchema',
120-
'AnyMySqlColumn',
127+
vendor === 'mysql' ? 'mysqlTable' : 'signlestoreTable',
128+
vendor === 'mysql' ? 'mysqlSchema' : 'singlestoreSchema',
129+
vendor === 'mysql' ? 'AnyMySqlColumn' : 'AnySinsgleStoreColumn',
121130
]);
122131

123132
const viewEntities = viewColumns.map((it) => {
@@ -132,24 +141,25 @@ export const ddlToTypeScript = (
132141
if (it.entityType === 'fks') imports.add('foreignKey');
133142
if (it.entityType === 'pks' && (it.columns.length > 1 || it.nameExplicit)) imports.add('primaryKey');
134143
if (it.entityType === 'checks') imports.add('check');
135-
if (it.entityType === 'views') imports.add('mysqlView');
144+
if (it.entityType === 'views') imports.add(vendor === 'mysql' ? 'mysqlView' : 'singlestoreView');
136145

137146
if (it.entityType === 'columns' || it.entityType === 'viewColumn') {
138147
const grammarType = typeFor(it.type);
139-
if (grammarType) imports.add(grammarType.drizzleImport());
148+
if (grammarType) imports.add(grammarType.drizzleImport(vendor));
140149
if (mysqlImportsList.has(it.type)) imports.add(it.type);
141150
}
142151
}
143152

144153
const tableStatements = [] as string[];
145154
for (const table of ddl.tables.list()) {
146-
let statement = `export const ${withCasing(table.name)} = mysqlTable("${table.name}", {\n`;
155+
let statement = `export const ${withCasing(table.name)} = ${vendor}Table("${table.name}", {\n`;
147156
statement += createTableColumns(
148157
ddl.columns.list({ table: table.name }),
149158
ddl.pks.one({ table: table.name }),
150159
ddl.fks.list({ table: table.name }),
151160
withCasing,
152161
casing,
162+
vendor,
153163
);
154164
statement += '}';
155165

@@ -190,8 +200,8 @@ export const ddlToTypeScript = (
190200
const columns = viewColumns.filter((x) => x.view === view.name);
191201

192202
let statement = '';
193-
statement += `export const ${withCasing(name)} = mysqlView("${name}", {\n`;
194-
statement += createViewColumns(columns, withCasing, casing);
203+
statement += `export const ${withCasing(name)} = ${vendor}View("${name}", {\n`;
204+
statement += createViewColumns(columns, withCasing, casing, vendor);
195205
statement += '})';
196206

197207
statement += algorithm ? `.algorithm("${algorithm}")` : '';
@@ -206,7 +216,7 @@ export const ddlToTypeScript = (
206216
[...imports].join(
207217
', ',
208218
)
209-
} } from "drizzle-orm/mysql-core"\nimport { sql } from "drizzle-orm"\n\n`;
219+
} } from "drizzle-orm/${vendor}-core"\nimport { sql } from "drizzle-orm"\n\n`;
210220

211221
let decalrations = '';
212222
decalrations += tableStatements.join('\n\n');
@@ -250,12 +260,12 @@ const column = (
250260
rawCasing: Casing,
251261
defaultValue: Column['default'],
252262
autoincrement: boolean,
253-
onUpdate: boolean,
263+
vendor: 'mysql' | 'singlestore',
254264
) => {
255265
let lowered = type.startsWith('enum(') ? type : type.toLowerCase();
256266
if (lowered.startsWith('enum')) {
257267
const values = parseEnum(lowered).map((it) => `"${it.replaceAll("''", "'").replaceAll('"', '\\"')}"`).join(',');
258-
let out = `${casing(name)}: mysqlEnum(${dbColumnName({ name, casing: rawCasing, withMode: true })}[${values}])`;
268+
let out = `${casing(name)}: ${vendor}Enum(${dbColumnName({ name, casing: rawCasing, withMode: true })}[${values}])`;
259269

260270
const { default: def } = Enum.toTs('', defaultValue);
261271
out += def ? `.default(${def})` : '';
@@ -290,14 +300,15 @@ const createTableColumns = (
290300
fks: ForeignKey[],
291301
casing: (val: string) => string,
292302
rawCasing: Casing,
303+
vendor: 'mysql' | 'singlestore',
293304
): string => {
294305
let statement = '';
295306

296307
for (const it of columns) {
297308
const isPK = pk && pk.columns.length === 1 && pk.columns[0] === it.name;
298309

299310
statement += '\t';
300-
statement += column(it.type, it.name, casing, rawCasing, it.default, it.autoIncrement, it.onUpdateNow);
311+
statement += column(it.type, it.name, casing, rawCasing, it.default, it.autoIncrement, vendor);
301312

302313
statement += isPK ? '.primaryKey()' : '';
303314
statement += it.notNull && !isPK ? '.notNull()' : '';
@@ -317,7 +328,7 @@ const createTableColumns = (
317328
const onUpdate = fk.onUpdate !== 'NO ACTION' ? fk.onUpdate : null;
318329
const params = { onDelete, onUpdate };
319330

320-
const typeSuffix = isCyclic(fk) ? ': AnyMySqlColumn' : '';
331+
const typeSuffix = isCyclic(fk) ? vendor === 'mysql' ? ': AnyMySqlColumn' : ': AnySinsgleStoreColumn' : '';
321332

322333
const paramsStr = objToStatement2(params);
323334
if (paramsStr) {
@@ -340,12 +351,17 @@ const createTableColumns = (
340351
return statement;
341352
};
342353

343-
const createViewColumns = (columns: ViewColumn[], casing: (value: string) => string, rawCasing: Casing) => {
354+
const createViewColumns = (
355+
columns: ViewColumn[],
356+
casing: (value: string) => string,
357+
rawCasing: Casing,
358+
vendor: 'mysql' | 'singlestore',
359+
) => {
344360
let statement = '';
345361

346362
for (const it of columns) {
347363
statement += '\n';
348-
statement += column(it.type, it.name, casing, rawCasing, null, false, false);
364+
statement += column(it.type, it.name, casing, rawCasing, null, false, vendor);
349365
statement += it.notNull ? '.notNull()' : '';
350366
statement += ',\n';
351367
}

drizzle-kit/src/dialects/singlestore/convertor.ts

Whitespace-only changes.

drizzle-kit/src/dialects/singlestore/drizzle.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { escapeSingleQuotes } from 'src/utils';
1212
import { safeRegister } from '../../utils/utils-node';
1313
import { getColumnCasing, sqlToStr } from '../drizzle';
1414
import { Column, InterimSchema } from '../mysql/ddl';
15+
import { typeFor } from '../mysql/grammar';
1516

1617
const handleEnumType = (type: string) => {
1718
let str = type.split('(')[1];
@@ -23,37 +24,12 @@ const handleEnumType = (type: string) => {
2324
export const defaultFromColumn = (column: AnySingleStoreColumn, casing?: Casing): Column['default'] => {
2425
if (typeof column.default === 'undefined') return null;
2526

26-
const sqlTypeLowered = column.getSQLType().toLowerCase();
2727
if (is(column.default, SQL)) {
28-
return { value: sqlToStr(column.default, casing), type: 'unknown' };
28+
return sqlToStr(column.default, casing);
2929
}
30-
const sqlType = column.getSQLType();
31-
if (sqlType.startsWith('binary') || sqlType === 'text') {
32-
return { value: String(column.default), type: 'text' };
33-
}
34-
35-
if (sqlTypeLowered === 'json') {
36-
return { value: JSON.stringify(column.default), type: 'json' };
37-
}
38-
39-
if (column.default instanceof Date) {
40-
if (sqlTypeLowered === 'date') {
41-
return { value: column.default.toISOString().split('T')[0], type: 'string' };
42-
}
43-
44-
if (sqlTypeLowered.startsWith('datetime') || sqlTypeLowered.startsWith('timestamp')) {
45-
return { value: column.default.toISOString().replace('T', ' ').slice(0, 23), type: 'string' };
46-
}
47-
48-
throw new Error(`unexpected default: ${column.default}`);
49-
}
50-
51-
const type = typeof column.default;
52-
if (type === 'string' || type === 'number' || type === 'bigint' || type === 'boolean') {
53-
return { value: String(column.default), type: type };
54-
}
55-
56-
throw new Error(`unexpected default: ${column.default}`);
30+
31+
const grammarType = typeFor(column.getSQLType().toLocaleLowerCase());
32+
return grammarType.defaultFromDrizzle(column.default);
5733
};
5834

5935
export const upper = <T extends string>(value: T | undefined): Uppercase<T> | null => {

0 commit comments

Comments
 (0)