Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 105 additions & 216 deletions drizzle-kit/src/jsonStatements.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion drizzle-kit/src/schemaValidator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { enum as enumType, TypeOf, union } from 'zod';
import { googlesqlSchemaSquashed } from './serializer/googlesqlSchema';
import { mysqlSchema, mysqlSchemaSquashed } from './serializer/mysqlSchema';
import { pgSchema, pgSchemaSquashed } from './serializer/pgSchema';
import { singlestoreSchema, singlestoreSchemaSquashed } from './serializer/singlestoreSchema';
import { sqliteSchema, SQLiteSchemaSquashed } from './serializer/sqliteSchema';
import { googlesqlSchemaSquashed } from './serializer/googlesqlSchema';

export const dialects = ['postgresql', 'mysql', 'sqlite', 'turso', 'singlestore', 'gel', 'googlesql'] as const;
export const dialect = enumType(dialects);
Expand Down
166 changes: 14 additions & 152 deletions drizzle-kit/src/serializer/googlesqlSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const index = object({
name: string(),
columns: string().array(),
isUnique: boolean(),
using: enumType(['btree', 'hash']).optional(),
algorithm: enumType(['default', 'inplace', 'copy']).optional(),
lock: enumType(['default', 'none', 'shared', 'exclusive']).optional(),
}).strict();

const fk = object({
Expand All @@ -19,7 +16,6 @@ const fk = object({
columnsFrom: string().array(),
tableTo: string(),
columnsTo: string().array(),
onUpdate: string().optional(),
onDelete: string().optional(),
}).strict();

Expand All @@ -28,7 +24,6 @@ const column = object({
type: string(),
primaryKey: boolean(),
notNull: boolean(),
autoincrement: boolean().optional(),
default: any().optional(),
onUpdate: any().optional(),
generated: object({
Expand All @@ -37,50 +32,27 @@ const column = object({
}).optional(),
}).strict();

const tableV3 = object({
name: string(),
columns: record(string(), column),
indexes: record(string(), index),
foreignKeys: record(string(), fk),
}).strict();

const compositePK = object({
name: string(),
columns: string().array(),
}).strict();

const uniqueConstraint = object({
name: string(),
columns: string().array(),
}).strict();

const checkConstraint = object({
name: string(),
value: string(),
}).strict();

const tableV4 = object({
name: string(),
schema: string().optional(),
columns: record(string(), column),
indexes: record(string(), index),
foreignKeys: record(string(), fk),
}).strict();

const table = object({
name: string(),
columns: record(string(), column),
indexes: record(string(), index),
foreignKeys: record(string(), fk),
compositePrimaryKeys: record(string(), compositePK),
uniqueConstraints: record(string(), uniqueConstraint).default({}),
checkConstraint: record(string(), checkConstraint).default({}),
}).strict();

const viewMeta = object({
algorithm: enumType(['undefined', 'merge', 'temptable']),
sqlSecurity: enumType(['definer', 'invoker']),
withCheckOption: enumType(['local', 'cascaded']).optional(),
}).strict();

export const view = object({
Expand Down Expand Up @@ -120,123 +92,68 @@ const schemaHash = object({
prevId: string(),
});

export const schemaInternalV3 = object({
version: literal('3'),
dialect: dialect,
tables: record(string(), tableV3),
}).strict();

export const schemaInternalV4 = object({
version: literal('4'),
dialect: dialect,
tables: record(string(), tableV4),
schemas: record(string(), string()),
}).strict();

export const schemaInternalV5 = object({
version: literal('5'),
dialect: dialect,
tables: record(string(), table),
schemas: record(string(), string()),
_meta: object({
schemas: record(string(), string()),
tables: record(string(), string()),
columns: record(string(), string()),
}),
internal: kitInternals,
}).strict();

export const schemaInternal = object({
version: literal('5'),
version: literal('0'),
dialect: dialect,
tables: record(string(), table),
schemas: record(string(), string()), // TODO: SPANNER - verify
views: record(string(), view).default({}),
_meta: object({
tables: record(string(), string()),
columns: record(string(), string()),
schemas: record(string(), string()), // TODO: SPANNER - verify
}),
internal: kitInternals,
}).strict();

export const schemaV3 = schemaInternalV3.merge(schemaHash);
export const schemaV4 = schemaInternalV4.merge(schemaHash);
export const schemaV5 = schemaInternalV5.merge(schemaHash);
export const schema = schemaInternal.merge(schemaHash);

const tableSquashedV4 = object({
name: string(),
schema: string().optional(),
columns: record(string(), column),
indexes: record(string(), string()),
foreignKeys: record(string(), string()),
}).strict();

const tableSquashed = object({
name: string(),
schema: string().optional(),
columns: record(string(), column),
indexes: record(string(), string()),
foreignKeys: record(string(), string()),
compositePrimaryKeys: record(string(), string()),
uniqueConstraints: record(string(), string()).default({}),
checkConstraints: record(string(), string()).default({}),
}).strict();

const viewSquashed = view.omit({
algorithm: true,
sqlSecurity: true,
withCheckOption: true,
}).extend({ meta: string() });

export const schemaSquashed = object({
version: literal('5'),
version: literal('0'),
dialect: dialect,
tables: record(string(), tableSquashed),
views: record(string(), viewSquashed),
}).strict();

export const schemaSquashedV4 = object({
version: literal('4'),
dialect: dialect,
tables: record(string(), tableSquashedV4),
schemas: record(string(), string()),
}).strict();

export type Dialect = TypeOf<typeof dialect>;
export type Column = TypeOf<typeof column>;
export type Table = TypeOf<typeof table>;
export type TableV4 = TypeOf<typeof tableV4>;
export type GoogleSqlSchema = TypeOf<typeof schema>;
export type GoogleSqlSchemaV3 = TypeOf<typeof schemaV3>;
export type GoogleSqlSchemaV4 = TypeOf<typeof schemaV4>;
export type GoogleSqlSchemaV5 = TypeOf<typeof schemaV5>;
export type GoogleSqlSchemaInternal = TypeOf<typeof schemaInternal>;
export type GoogleSqlKitInternals = TypeOf<typeof kitInternals>;
export type GoogleSqlSchemaSquashed = TypeOf<typeof schemaSquashed>;
export type GoogleSqlSchemaSquashedV4 = TypeOf<typeof schemaSquashedV4>;
export type Index = TypeOf<typeof index>;
export type ForeignKey = TypeOf<typeof fk>;
export type PrimaryKey = TypeOf<typeof compositePK>;
export type UniqueConstraint = TypeOf<typeof uniqueConstraint>;
export type CheckConstraint = TypeOf<typeof checkConstraint>;
export type View = TypeOf<typeof view>;
export type ViewSquashed = TypeOf<typeof viewSquashed>;

export const GoogleSqlSquasher = {
squashIdx: (idx: Index) => {
index.parse(idx);
return `${idx.name};${idx.columns.join(',')};${idx.isUnique};${idx.using ?? ''};${idx.algorithm ?? ''};${
idx.lock ?? ''
}`;
return `${idx.name};${idx.columns.join(',')};${idx.isUnique}`;
},
unsquashIdx: (input: string): Index => {
const [name, columnsString, isUnique, using, algorithm, lock] = input.split(';');
const [name, columnsString, isUnique] = input.split(';');
const destructed = {
name,
columns: columnsString.split(','),
isUnique: isUnique === 'true',
using: using ? using : undefined,
algorithm: algorithm ? algorithm : undefined,
lock: lock ? lock : undefined,
};
return index.parse(destructed);
},
Expand All @@ -247,17 +164,10 @@ export const GoogleSqlSquasher = {
const splitted = pk.split(';');
return { name: splitted[0], columns: splitted[1].split(',') };
},
squashUnique: (unq: UniqueConstraint) => {
return `${unq.name};${unq.columns.join(',')}`;
},
unsquashUnique: (unq: string): UniqueConstraint => {
const [name, columns] = unq.split(';');
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 ?? ''}`;
fk.onDelete ?? ''
}`;
},
unsquashFK: (input: string): ForeignKey => {
const [
Expand All @@ -266,7 +176,6 @@ export const GoogleSqlSquasher = {
columnsFromStr,
tableTo,
columnsToStr,
onUpdate,
onDelete,
] = input.split(';');

Expand All @@ -276,7 +185,6 @@ export const GoogleSqlSquasher = {
columnsFrom: columnsFromStr.split(','),
tableTo,
columnsTo: columnsToStr.split(','),
onUpdate,
onDelete,
});
return result;
Expand All @@ -290,53 +198,18 @@ export const GoogleSqlSquasher = {
return { name, value };
},
squashView: (view: View): string => {
return `${view.algorithm};${view.sqlSecurity};${view.withCheckOption}`;
return `${view.sqlSecurity}`;
},
unsquashView: (meta: string): SquasherViewMeta => {
const [algorithm, sqlSecurity, withCheckOption] = meta.split(';');
const [sqlSecurity] = meta.split(';');
const toReturn = {
algorithm: algorithm,
sqlSecurity: sqlSecurity,
withCheckOption: withCheckOption !== 'undefined' ? withCheckOption : undefined,
};

return viewMeta.parse(toReturn);
},
};

export const squashGooglesqlSchemeV4 = (
json: GoogleSqlSchemaV4,
): GoogleSqlSchemaSquashedV4 => {
const mappedTables = Object.fromEntries(
Object.entries(json.tables).map((it) => {
const squashedIndexes = mapValues(it[1].indexes, (index) => {
return GoogleSqlSquasher.squashIdx(index);
});

const squashedFKs = mapValues(it[1].foreignKeys, (fk) => {
return GoogleSqlSquasher.squashFK(fk);
});

return [
it[0],
{
name: it[1].name,
schema: it[1].schema,
columns: it[1].columns,
indexes: squashedIndexes,
foreignKeys: squashedFKs,
},
];
}),
);
return {
version: '4',
dialect: json.dialect,
tables: mappedTables,
schemas: json.schemas,
};
};

export const squashGooglesqlScheme = (json: GoogleSqlSchema): GoogleSqlSchemaSquashed => {
const mappedTables = Object.fromEntries(
Object.entries(json.tables).map((it) => {
Expand All @@ -352,13 +225,6 @@ export const squashGooglesqlScheme = (json: GoogleSqlSchema): GoogleSqlSchemaSqu
return GoogleSqlSquasher.squashPK(pk);
});

const squashedUniqueConstraints = mapValues(
it[1].uniqueConstraints,
(unq) => {
return GoogleSqlSquasher.squashUnique(unq);
},
);

const squashedCheckConstraints = mapValues(it[1].checkConstraint, (check) => {
return GoogleSqlSquasher.squashCheck(check);
});
Expand All @@ -371,7 +237,6 @@ export const squashGooglesqlScheme = (json: GoogleSqlSchema): GoogleSqlSchemaSqu
indexes: squashedIndexes,
foreignKeys: squashedFKs,
compositePrimaryKeys: squashedPKs,
uniqueConstraints: squashedUniqueConstraints,
checkConstraints: squashedCheckConstraints,
},
];
Expand All @@ -393,24 +258,21 @@ export const squashGooglesqlScheme = (json: GoogleSqlSchema): GoogleSqlSchemaSqu
);

return {
version: '5',
version: '0',
dialect: json.dialect,
tables: mappedTables,
views: mappedViews,
};
};

export const googlesqlSchema = schema;
export const googlesqlSchemaV3 = schemaV3;
export const googlesqlSchemaV4 = schemaV4;
export const googlesqlSchemaV5 = schemaV5;
export const googlesqlSchemaSquashed = schemaSquashed;

// no prev version
export const backwardCompatibleGooglesqlSchema = union([googlesqlSchemaV5, schema]);
export const backwardCompatibleGooglesqlSchema = union([googlesqlSchema, schema]);

export const dryGoogleSql = googlesqlSchema.parse({
version: '5',
version: '0',
dialect: 'googlesql',
id: originUUID,
prevId: '',
Expand Down
Loading