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
64 changes: 64 additions & 0 deletions drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import {
prepareGoogleSqlMigrationSnapshot,
prepareMySqlDbPushSnapshot,
prepareMySqlMigrationSnapshot,
preparePgDbPushSnapshot,
Expand All @@ -20,6 +21,7 @@ import { MySqlSchema, mysqlSchema, squashMysqlScheme, ViewSquashed } from '../..
import { PgSchema, pgSchema, Policy, Role, squashPgScheme, View } from '../../serializer/pgSchema';
import { SQLiteSchema, sqliteSchema, squashSqliteScheme, View as SQLiteView } from '../../serializer/sqliteSchema';
import {
applyGooglesqlSnapshotsDiff,
applyLibSQLSnapshotsDiff,
applyMysqlSnapshotsDiff,
applyPgSnapshotsDiff,
Expand Down Expand Up @@ -55,6 +57,7 @@ import {
schema,
} from '../views';
import { ExportConfig, GenerateConfig } from './utils';
import { googlesqlSchema, squashGooglesqlScheme } from 'src/serializer/googlesqlSchema';

export type Named = {
name: string;
Expand Down Expand Up @@ -608,6 +611,67 @@ export const prepareAndMigrateMysql = async (config: GenerateConfig) => {
}
};

export const prepareAndMigrateGooglesql = async (config: GenerateConfig) => {
const outFolder = config.out;
const schemaPath = config.schema;
const casing = config.casing;

try {
// TODO: remove
assertV1OutFolder(outFolder); // TODO: SPANNER - what to do with this?

const { snapshots, journal } = prepareMigrationFolder(outFolder, 'googlesql');
const { prev, cur, custom } = await prepareGoogleSqlMigrationSnapshot(
snapshots,
schemaPath,
casing,
);

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

if (config.custom) {
writeResult({
cur: custom,
sqlStatements: [],
journal,
outFolder,
name: config.name,
breakpoints: config.breakpoints,
type: 'custom',
prefixMode: config.prefix,
});
return;
}

const squashedPrev = squashGooglesqlScheme(validatedPrev);
const squashedCur = squashGooglesqlScheme(validatedCur);

const { sqlStatements, statements, _meta } = await applyGooglesqlSnapshotsDiff(
squashedPrev,
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
);

writeResult({
cur,
sqlStatements,
journal,
_meta,
outFolder,
name: config.name,
breakpoints: config.breakpoints,
prefixMode: config.prefix,
});
} catch (e) {
console.error(e);
}
};

// Not needed for now
function singleStoreSchemaSuggestions(
curSchema: TypeOf<typeof singlestoreSchema>,
Expand Down
58 changes: 44 additions & 14 deletions drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ import {
SqliteCredentials,
sqliteCredentials,
} from '../validations/sqlite';
import {
printConfigConnectionIssues as printIssuesSpanner,
// SpannerCredentials,
spannerCredentials,
} from '../validations/spanner';

import { studioCliParams, studioConfig } from '../validations/studio';
import { error, grey } from '../views';

Expand Down Expand Up @@ -444,8 +450,15 @@ export const preparePushConfig = async (
),
);
process.exit(1);
} else if (config.dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER
}

if (config.dialect === 'googlesql') {
console.log(
error(
`You can't use 'push' command with googlesql dialect`, // TODO: SPANNER - not a priority
),
);
process.exit(1);
}

assertUnreachable(config.dialect);
Expand Down Expand Up @@ -645,8 +658,15 @@ export const preparePullConfig = async (
prefix: config.migrations?.prefix || 'index',
entities: config.entities,
};
} else if (dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER
}

if (dialect === 'googlesql') {
console.log(
error(
`You can't use 'pull' command with googlesql dialect`, // TODO: SPANNER - not a priority
),
);
process.exit(1);
}

assertUnreachable(dialect);
Expand Down Expand Up @@ -758,15 +778,15 @@ export const prepareStudioConfig = async (options: Record<string, unknown>) => {
),
);
process.exit(1);
} else if (dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER - not a priority
return {
dialect,
schema,
host,
port,
credentials: null as any,
};
}

if (dialect === 'googlesql') {
console.log(
error(
`You can't use 'studio' command with googlesql dialect`, // TODO: SPANNER - not a priority
),
);
process.exit(1);
}

assertUnreachable(dialect);
Expand Down Expand Up @@ -880,7 +900,17 @@ export const prepareMigrateConfig = async (configPath: string | undefined) => {
}

if (dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER
console.log(
error(
`You can't use 'migrate' command with googlesql dialect (YET)`,
),
);
process.exit(1);
const parsed = spannerCredentials.safeParse(config);
if (!parsed.success) {
printIssuesSpanner(config);
process.exit(1);
}
return {
dialect,
out,
Expand Down
10 changes: 8 additions & 2 deletions drizzle-kit/src/cli/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { assertOrmCoreVersion, assertPackages, assertStudioNodeVersion, ormVersi
import { assertCollisions, drivers, prefixes } from './validations/common';
import { withStyle } from './validations/outputs';
import { error, grey, MigrateProgress } from './views';
import { prepareAndMigrateGooglesql } from './commands/migrate';

const optionDialect = string('dialect')
.enum(...dialects)
Expand Down Expand Up @@ -107,7 +108,7 @@ export const generate = command({
);
process.exit(1);
} else if (dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER
await prepareAndMigrateGooglesql(opts);
} else {
assertUnreachable(dialect);
}
Expand Down Expand Up @@ -211,7 +212,12 @@ export const migrate = command({
);
process.exit(1);
} else if (dialect === 'googlesql') {
throw new Error('Not implemented'); // TODO: SPANNER
console.log(
error(
`You can't use 'migrate' command with googlesql dialect (yet)`, // TODO: SPANNER - high priority
),
);
process.exit(1);
} else {
assertUnreachable(dialect);
}
Expand Down
65 changes: 0 additions & 65 deletions drizzle-kit/src/cli/validations/googlesql.ts

This file was deleted.

10 changes: 5 additions & 5 deletions drizzle-kit/src/cli/validations/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const outputs = {
studio: {
drivers: (param: string) =>
withStyle.error(
`"${param}" is not a valid driver. Available drivers: "pg", "mysql2", "better-sqlite", "libsql", "turso". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference`,
`"${param}" is not a valid driver. Available drivers: "pg", "mysql2", "better-sqlite", "libsql", "turso", "spanner". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference`,
),
noCredentials: () =>
withStyle.error(
Expand Down Expand Up @@ -91,10 +91,10 @@ export const outputs = {
googlesql: {
connection: {
driver: () => withStyle.error(`Only "spanner" is available options for "--driver"`),
required: () =>
withStyle.error(
`Either "url" or "host", "database" are required for database connection`, // TODO: SPANNER - write proper error message
),
// required: () =>
// withStyle.error(
// `Either "url" or "host", "database" are required for database connection`, // TODO: SPANNER - write proper error message
// ),
},
},
};
35 changes: 35 additions & 0 deletions drizzle-kit/src/cli/validations/spanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { boolean, coerce, object, string, TypeOf, union } from 'zod';
import { error } from '../views';
import { wrapParam } from './common';
import { outputs } from './outputs';

// TODO: SPANNER - add proper credentials config

export const spannerCredentials = object({
projectId: string().min(1),
instanceId: string().min(1),
databaseId: string().min(1),
});

export type SpannerCredentials = TypeOf<typeof spannerCredentials>;

// TODO: SPANNER - add proper connection issues
// export const printCliConnectionIssues = (options: any) => {
// const { uri, host, database } = options || {};

// if (!uri && (!host || !database)) {
// console.log(outputs.googlesql.connection.required());
// }
// };

// TODO: SPANNER - add proper connection issues
export const printConfigConnectionIssues = (
options: Record<string, unknown>,
) => {
let text = `Please provide required params for Spanner driver:\n`;
console.log(error(text));
console.log(wrapParam('projectId', options.projectId));
console.log(wrapParam('instanceId', options.instanceId));
console.log(wrapParam('databaseId', options.databaseId));
process.exit(1);
};
14 changes: 7 additions & 7 deletions drizzle-kit/src/jsonStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,18 +1755,18 @@ export const prepareAlterColumnsGooglesql = (

// const table = json2.tables[tableName];
// const snapshotColumn = table.columns[columnName];

// const columnType = snapshotColumn.type;
// const columnDefault = snapshotColumn.default;
// const columnOnUpdate = 'onUpdate' in snapshotColumn ? snapshotColumn.onUpdate : undefined;
// const columnNotNull = table.columns[columnName].notNull;

// const columnAutoIncrement = 'autoincrement' in snapshotColumn
// ? snapshotColumn.autoincrement ?? false
// : false;

// const columnPk = table.columns[columnName].primaryKey;

// if (column.autoincrement?.type === 'added') {
// statements.push({
// type: 'alter_table_alter_column_set_autoincrement',
Expand All @@ -1781,12 +1781,12 @@ export const prepareAlterColumnsGooglesql = (
// columnPk,
// });
// }

// if (column.autoincrement?.type === 'changed') {
// const type = column.autoincrement.new
// ? 'alter_table_alter_column_set_autoincrement'
// : 'alter_table_alter_column_drop_autoincrement';

// statements.push({
// type,
// tableName,
Expand All @@ -1800,7 +1800,7 @@ export const prepareAlterColumnsGooglesql = (
// columnPk,
// });
// }

// if (column.autoincrement?.type === 'deleted') {
// statements.push({
// type: 'alter_table_alter_column_drop_autoincrement',
Expand Down
Loading