Skip to content

Commit

Permalink
Merge pull request #3005 from drizzle-team/drizzle-kit/views
Browse files Browse the repository at this point in the history
Drizzle-kit/views
  • Loading branch information
AndriiSherman authored Oct 14, 2024
2 parents cb4af54 + f9ec555 commit e7d524d
Show file tree
Hide file tree
Showing 47 changed files with 7,762 additions and 1,638 deletions.
6 changes: 6 additions & 0 deletions drizzle-kit/src/@types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare global {
squashSpaces(): string;
capitalise(): string;
camelCase(): string;
snake_case(): string;

concatIf(it: string, condition: boolean): string;
}

Expand Down Expand Up @@ -44,6 +46,10 @@ String.prototype.concatIf = function(it: string, condition: boolean) {
return condition ? `${this}${it}` : String(this);
};

String.prototype.snake_case = function() {
return this && this.length > 0 ? `${this.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)}` : String(this);
};

Array.prototype.random = function() {
return this[~~(Math.random() * this.length)];
};
Expand Down
16 changes: 13 additions & 3 deletions drizzle-kit/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { randomUUID } from 'crypto';
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
import { LibSQLDatabase } from 'drizzle-orm/libsql';
import type { MySql2Database } from 'drizzle-orm/mysql2';
import { PgDatabase } from 'drizzle-orm/pg-core';
import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
tablesResolver,
viewsResolver,
} from './cli/commands/migrate';
import { pgPushIntrospect } from './cli/commands/pgIntrospect';
import { pgSuggestions } from './cli/commands/pgPushUtils';
Expand Down Expand Up @@ -45,6 +47,8 @@ export const generateDrizzleJson = (
prepared.enums,
prepared.schemas,
prepared.sequences,
prepared.views,
prepared.matViews,
casing,
schemaFilters,
);
Expand Down Expand Up @@ -76,6 +80,7 @@ export const generateMigration = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -119,6 +124,7 @@ export const pushSchema = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -151,7 +157,7 @@ export const generateSQLiteDrizzleJson = async (

const id = randomUUID();

const snapshot = generateSqliteSnapshot(prepared.tables, casing);
const snapshot = generateSqliteSnapshot(prepared.tables, prepared.views, casing);

return {
...snapshot,
Expand All @@ -177,6 +183,7 @@ export const generateSQLiteMigration = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -217,6 +224,7 @@ export const pushSQLiteSchema = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -255,7 +263,7 @@ export const generateMySQLDrizzleJson = async (

const id = randomUUID();

const snapshot = generateMySqlSnapshot(prepared.tables, casing);
const snapshot = generateMySqlSnapshot(prepared.tables, prepared.views, casing);

return {
...snapshot,
Expand All @@ -281,6 +289,7 @@ export const generateMySQLMigration = async (
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -322,6 +331,7 @@ export const pushMySQLSchema = async (
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down
16 changes: 14 additions & 2 deletions drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { dryPg, type PgSchema, squashPgScheme } from '../../serializer/pgSchema'
import { fromDatabase as fromPostgresDatabase } from '../../serializer/pgSerializer';
import { drySQLite, type SQLiteSchema, squashSqliteScheme } from '../../serializer/sqliteSchema';
import { fromDatabase as fromSqliteDatabase } from '../../serializer/sqliteSerializer';
import { applyMysqlSnapshotsDiff, applyPgSnapshotsDiff, applySqliteSnapshotsDiff } from '../../snapshotsDiffer';
import {
applyLibSQLSnapshotsDiff,
applyMysqlSnapshotsDiff,
applyPgSnapshotsDiff,
applySqliteSnapshotsDiff,
} from '../../snapshotsDiffer';
import { prepareOutFolder } from '../../utils';
import type { Casing, Prefix } from '../validations/common';
import { LibSQLCredentials } from '../validations/libsql';
Expand All @@ -25,9 +30,12 @@ import { IntrospectProgress } from '../views';
import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
tablesResolver,
viewsResolver,
writeResult,
} from './migrate';

Expand Down Expand Up @@ -100,6 +108,7 @@ export const introspectPostgres = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
dryPg,
schema,
);
Expand Down Expand Up @@ -210,6 +219,7 @@ export const introspectMysql = async (
squashMysqlScheme(schema),
tablesResolver,
columnsResolver,
mySqlViewsResolver,
dryMySql,
schema,
);
Expand Down Expand Up @@ -321,6 +331,7 @@ export const introspectSqlite = async (
squashSqliteScheme(schema),
tablesResolver,
columnsResolver,
sqliteViewsResolver,
drySQLite,
schema,
);
Expand Down Expand Up @@ -427,11 +438,12 @@ export const introspectLibSQL = async (
const { snapshots, journal } = prepareOutFolder(out, 'sqlite');

if (snapshots.length === 0) {
const { sqlStatements, _meta } = await applySqliteSnapshotsDiff(
const { sqlStatements, _meta } = await applyLibSQLSnapshotsDiff(
squashSqliteScheme(drySQLite),
squashSqliteScheme(schema),
tablesResolver,
columnsResolver,
sqliteViewsResolver,
drySQLite,
schema,
);
Expand Down
82 changes: 78 additions & 4 deletions drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { render } from 'hanji';
import path, { join } from 'path';
import { TypeOf } from 'zod';
import type { CommonSchema } from '../../schemaValidator';
import { MySqlSchema, mysqlSchema, squashMysqlScheme } from '../../serializer/mysqlSchema';
import { PgSchema, pgSchema, squashPgScheme } from '../../serializer/pgSchema';
import { SQLiteSchema, sqliteSchema, squashSqliteScheme } from '../../serializer/sqliteSchema';
import { MySqlSchema, mysqlSchema, squashMysqlScheme, ViewSquashed } from '../../serializer/mysqlSchema';
import { PgSchema, pgSchema, squashPgScheme, View } from '../../serializer/pgSchema';
import { SQLiteSchema, sqliteSchema, squashSqliteScheme, View as SQLiteView } from '../../serializer/sqliteSchema';
import {
applyLibSQLSnapshotsDiff,
applyMysqlSnapshotsDiff,
Expand Down Expand Up @@ -92,6 +92,72 @@ export const tablesResolver = async (
}
};

export const viewsResolver = async (
input: ResolverInput<View>,
): Promise<ResolverOutputWithMoved<View>> => {
try {
const { created, deleted, moved, renamed } = await promptNamedWithSchemasConflict(
input.created,
input.deleted,
'view',
);

return {
created: created,
deleted: deleted,
moved: moved,
renamed: renamed,
};
} catch (e) {
console.error(e);
throw e;
}
};

export const mySqlViewsResolver = async (
input: ResolverInput<ViewSquashed & { schema: '' }>,
): Promise<ResolverOutputWithMoved<ViewSquashed>> => {
try {
const { created, deleted, moved, renamed } = await promptNamedWithSchemasConflict(
input.created,
input.deleted,
'view',
);

return {
created: created,
deleted: deleted,
moved: moved,
renamed: renamed,
};
} catch (e) {
console.error(e);
throw e;
}
};

export const sqliteViewsResolver = async (
input: ResolverInput<SQLiteView & { schema: '' }>,
): Promise<ResolverOutputWithMoved<SQLiteView>> => {
try {
const { created, deleted, moved, renamed } = await promptNamedWithSchemasConflict(
input.created,
input.deleted,
'view',
);

return {
created: created,
deleted: deleted,
moved: moved,
renamed: renamed,
};
} catch (e) {
console.error(e);
throw e;
}
};

export const sequencesResolver = async (
input: ResolverInput<Sequence>,
): Promise<ResolverOutputWithMoved<Sequence>> => {
Expand Down Expand Up @@ -200,6 +266,7 @@ export const prepareAndMigratePg = async (config: GenerateConfig) => {
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -245,6 +312,7 @@ export const preparePgPush = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -328,6 +396,7 @@ export const prepareMySQLPush = async (
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -381,6 +450,7 @@ export const prepareAndMigrateMysql = async (config: GenerateConfig) => {
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -441,6 +511,7 @@ export const prepareAndMigrateSqlite = async (config: GenerateConfig) => {
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -502,6 +573,7 @@ export const prepareAndMigrateLibSQL = async (config: GenerateConfig) => {
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -540,6 +612,7 @@ export const prepareSQLitePush = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -572,6 +645,7 @@ export const prepareLibSQLPush = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -665,7 +739,7 @@ export const promptColumnsConflicts = async <T extends Named>(
export const promptNamedWithSchemasConflict = async <T extends NamedWithSchema>(
newItems: T[],
missingItems: T[],
entity: 'table' | 'enum' | 'sequence',
entity: 'table' | 'enum' | 'sequence' | 'view',
): Promise<{
created: T[];
renamed: { from: T; to: T }[];
Expand Down
Loading

0 comments on commit e7d524d

Please sign in to comment.