Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rls schema definition #3193

Merged
merged 35 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4045fbc
Add pgPolicy
dankochetov Aug 23, 2024
2758da9
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Aug 26, 2024
fb1e950
Add all policies for generate logic
AndriiSherman Aug 27, 2024
474710e
Add all policies api and pgRole into generate and pull
AndriiSherman Aug 30, 2024
5ec6b99
Add roles generate and introspect logic
AndriiSherman Sep 1, 2024
d934de8
Finish all logic for roles and policies in Postgres
AndriiSherman Sep 2, 2024
9f172d7
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Sep 5, 2024
e591c5d
Add neon import
AndriiSherman Sep 5, 2024
7960cfd
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Sep 5, 2024
dc783a2
Fix tests for kit and orm
AndriiSherman Sep 6, 2024
109837d
Update pglite tests
AndriiSherman Sep 6, 2024
7bea25c
Fix role import
AndriiSherman Sep 6, 2024
7469abe
Update naming for crudPolicy keys
AndriiSherman Sep 6, 2024
277e6de
Fix sql to query mapping in policy
AndriiSherman Sep 6, 2024
d496e6f
Create policies separately
AndriiSherman Sep 6, 2024
2475c4f
Add roles to policy name
AndriiSherman Sep 6, 2024
8944ef1
Fix tests after policy update
AndriiSherman Sep 6, 2024
b75016b
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 1, 2024
1c02f66
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 8, 2024
7ee4741
Merge branch 'main' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 8, 2024
b16ebac
Add rls fixes and 3 param as array
AndriiSherman Oct 9, 2024
0986503
Add policies to counter in drizzle pull
AndriiSherman Oct 9, 2024
225f91d
Fix tests
AndriiSherman Oct 10, 2024
c32d487
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 11, 2024
662aa35
Fix serializer and tests
AndriiSherman Oct 11, 2024
ddba14b
Make pglite 0.2.5 for now
AndriiSherman Oct 11, 2024
a5ec472
Update deps
AndriiSherman Oct 11, 2024
d6b5d7a
Merge branch 'main' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 18, 2024
79c1f66
Merge branch 'main' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 18, 2024
35471bd
Add enableRLS()
AndriiSherman Oct 21, 2024
211174c
Merge branch 'main' of github.com:drizzle-team/drizzle-orm into rls-s…
AndriiSherman Oct 22, 2024
31c8041
Add enableRLS function for pg tables
AndriiSherman Oct 23, 2024
4cb1bdb
Fix tests
AndriiSherman Oct 23, 2024
6a162f5
Add rls for supabase
AndriiSherman Oct 24, 2024
542bebe
Merge beta
AndriiSherman Oct 24, 2024
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
7,603 changes: 0 additions & 7,603 deletions drizzle-kit/pnpm-lock.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions drizzle-kit/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
policyResolver,
roleResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
Expand Down Expand Up @@ -47,6 +49,7 @@ export const generateDrizzleJson = (
prepared.enums,
prepared.schemas,
prepared.sequences,
prepared.roles,
prepared.views,
prepared.matViews,
casing,
Expand Down Expand Up @@ -78,6 +81,8 @@ export const generateMigration = async (
schemasResolver,
enumsResolver,
sequencesResolver,
policyResolver,
roleResolver,
tablesResolver,
columnsResolver,
viewsResolver,
Expand Down Expand Up @@ -122,6 +127,8 @@ export const pushSchema = async (
schemasResolver,
enumsResolver,
sequencesResolver,
policyResolver,
roleResolver,
tablesResolver,
columnsResolver,
viewsResolver,
Expand Down
17 changes: 14 additions & 3 deletions drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
applySqliteSnapshotsDiff,
} from '../../snapshotsDiffer';
import { prepareOutFolder } from '../../utils';
import { Entities } from '../validations/cli';
import type { Casing, Prefix } from '../validations/common';
import { LibSQLCredentials } from '../validations/libsql';
import type { MysqlCredentials } from '../validations/mysql';
Expand All @@ -31,6 +32,7 @@ import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
policyResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
Expand All @@ -47,6 +49,7 @@ export const introspectPostgres = async (
tablesFilter: string[],
schemasFilter: string[],
prefix: Prefix,
entities: Entities,
) => {
const { preparePostgresDB } = await import('../connections');
const db = await preparePostgresDB(credentials);
Expand Down Expand Up @@ -79,11 +82,18 @@ export const introspectPostgres = async (
};

const progress = new IntrospectProgress(true);

const res = await renderWithTask(
progress,
fromPostgresDatabase(db, filter, schemasFilter, (stage, count, status) => {
progress.update(stage, count, status);
}),
fromPostgresDatabase(
db,
filter,
schemasFilter,
entities,
(stage, count, status) => {
progress.update(stage, count, status);
},
),
);

const schema = { id: originUUID, prevId: '', ...res } as PgSchema;
Expand All @@ -106,6 +116,7 @@ export const introspectPostgres = async (
schemasResolver,
enumsResolver,
sequencesResolver,
policyResolver,
tablesResolver,
columnsResolver,
viewsResolver,
Expand Down
113 changes: 112 additions & 1 deletion drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import path, { join } from 'path';
import { TypeOf } from 'zod';
import type { CommonSchema } from '../../schemaValidator';
import { MySqlSchema, mysqlSchema, squashMysqlScheme, ViewSquashed } from '../../serializer/mysqlSchema';
import { PgSchema, pgSchema, squashPgScheme, View } from '../../serializer/pgSchema';
import { PgSchema, pgSchema, Policy, Role, squashPgScheme, View } from '../../serializer/pgSchema';
import { SQLiteSchema, sqliteSchema, squashSqliteScheme, View as SQLiteView } from '../../serializer/sqliteSchema';
import {
applyLibSQLSnapshotsDiff,
Expand All @@ -28,6 +28,8 @@ import {
ResolverInput,
ResolverOutput,
ResolverOutputWithMoved,
RolesResolverInput,
RolesResolverOutput,
Sequence,
Table,
} from '../../snapshotsDiffer';
Expand All @@ -41,6 +43,7 @@ import {
ResolveColumnSelect,
ResolveSchemasSelect,
ResolveSelect,
ResolveSelectNamed,
schema,
} from '../views';
import { GenerateConfig } from './utils';
Expand Down Expand Up @@ -180,6 +183,38 @@ export const sequencesResolver = async (
}
};

export const roleResolver = async (
input: RolesResolverInput<Role>,
): Promise<RolesResolverOutput<Role>> => {
const result = await promptNamedConflict(
input.created,
input.deleted,
'role',
);
return {
created: result.created,
deleted: result.deleted,
renamed: result.renamed,
};
};

export const policyResolver = async (
input: ColumnsResolverInput<Policy>,
): Promise<ColumnsResolverOutput<Policy>> => {
const result = await promptColumnsConflicts(
input.tableName,
input.created,
input.deleted,
);
return {
tableName: input.tableName,
schema: input.schema,
created: result.created,
deleted: result.deleted,
renamed: result.renamed,
};
};

export const enumsResolver = async (
input: ResolverInput<Enum>,
): Promise<ResolverOutputWithMoved<Enum>> => {
Expand Down Expand Up @@ -264,6 +299,8 @@ export const prepareAndMigratePg = async (config: GenerateConfig) => {
schemasResolver,
enumsResolver,
sequencesResolver,
policyResolver,
roleResolver,
tablesResolver,
columnsResolver,
viewsResolver,
Expand Down Expand Up @@ -310,6 +347,8 @@ export const preparePgPush = async (
schemasResolver,
enumsResolver,
sequencesResolver,
policyResolver,
roleResolver,
tablesResolver,
columnsResolver,
viewsResolver,
Expand Down Expand Up @@ -736,6 +775,78 @@ export const promptColumnsConflicts = async <T extends Named>(
return result;
};

export const promptNamedConflict = async <T extends Named>(
newItems: T[],
missingItems: T[],
entity: 'role',
): Promise<{
created: T[];
renamed: { from: T; to: T }[];
deleted: T[];
}> => {
if (missingItems.length === 0 || newItems.length === 0) {
return {
created: newItems,
renamed: [],
deleted: missingItems,
};
}

const result: {
created: T[];
renamed: { from: T; to: T }[];
deleted: T[];
} = { created: [], renamed: [], deleted: [] };
let index = 0;
let leftMissing = [...missingItems];
do {
const created = newItems[index];
const renames: RenamePropmtItem<T>[] = leftMissing.map((it) => {
return { from: it, to: created };
});

const promptData: (RenamePropmtItem<T> | T)[] = [created, ...renames];

const { status, data } = await render(
new ResolveSelectNamed<T>(created, promptData, entity),
);
if (status === 'aborted') {
console.error('ERROR');
process.exit(1);
}

if (isRenamePromptItem(data)) {
console.log(
`${chalk.yellow('~')} ${data.from.name} › ${data.to.name} ${
chalk.gray(
`${entity} will be renamed/moved`,
)
}`,
);

if (data.from.name !== data.to.name) {
result.renamed.push(data);
}

delete leftMissing[leftMissing.indexOf(data.from)];
leftMissing = leftMissing.filter(Boolean);
} else {
console.log(
`${chalk.green('+')} ${data.name} ${
chalk.gray(
`${entity} will be created`,
)
}`,
);
result.created.push(created);
}
index += 1;
} while (index < newItems.length);
console.log(chalk.gray(`--- all ${entity} conflicts resolved ---\n`));
result.deleted.push(...leftMissing);
return result;
};

export const promptNamedWithSchemasConflict = async <T extends NamedWithSchema>(
newItems: T[],
missingItems: T[],
Expand Down
4 changes: 3 additions & 1 deletion drizzle-kit/src/cli/commands/pgIntrospect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { originUUID } from '../../global';
import type { PgSchema } from '../../serializer/pgSchema';
import { fromDatabase } from '../../serializer/pgSerializer';
import type { DB } from '../../utils';
import { Entities } from '../validations/cli';
import { ProgressView } from '../views';

export const pgPushIntrospect = async (
db: DB,
filters: string[],
schemaFilters: string[],
entities: Entities = { roles: true },
) => {
const matchers = filters.map((it) => {
return new Minimatch(it);
Expand Down Expand Up @@ -43,7 +45,7 @@ export const pgPushIntrospect = async (
);
const res = await renderWithTask(
progress,
fromDatabase(db, filter, schemaFilters),
fromDatabase(db, filter, schemaFilters, entities),
);

const schema = { id: originUUID, prevId: '', ...res } as PgSchema;
Expand Down
10 changes: 2 additions & 8 deletions drizzle-kit/src/cli/commands/pgPushUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,12 @@ export const pgSuggestions = async (db: DB, statements: JsonStatement[]) => {
}
const stmnt = fromJson([statement], 'postgresql');
if (typeof stmnt !== 'undefined') {
if (statement.type === 'drop_table') {
statementsToExecute.push(
`DROP TABLE ${concatSchemaAndTableName(statement.schema, statement.tableName)} CASCADE;`,
);
} else {
statementsToExecute.push(...stmnt);
}
statementsToExecute.push(...stmnt);
}
}

return {
statementsToExecute,
statementsToExecute: [...new Set(statementsToExecute)],
shouldAskForApprove,
infoToPrint,
matViewsToRemove: [...new Set(matViewsToRemove)],
Expand Down
4 changes: 3 additions & 1 deletion drizzle-kit/src/cli/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk';
import { render } from 'hanji';
import { fromJson } from '../../sqlgenerator';
import { Select } from '../selector-ui';
import { Entities } from '../validations/cli';
import { CasingType } from '../validations/common';
import { LibSQLCredentials } from '../validations/libsql';
import type { MysqlCredentials } from '../validations/mysql';
Expand Down Expand Up @@ -160,14 +161,15 @@ export const pgPush = async (
credentials: PostgresCredentials,
tablesFilter: string[],
schemasFilter: string[],
entities: Entities,
force: boolean,
casing: CasingType | undefined,
) => {
const { preparePostgresDB } = await import('../connections');
const { pgPushIntrospect } = await import('./pgIntrospect');

const db = await preparePostgresDB(credentials);
const { schema } = await pgPushIntrospect(db, tablesFilter, schemasFilter);
const { schema } = await pgPushIntrospect(db, tablesFilter, schemasFilter, entities);

const { preparePgPush } = await import('./migrate');

Expand Down
6 changes: 5 additions & 1 deletion drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { object, string } from 'zod';
import { assertUnreachable } from '../../global';
import { type Dialect, dialect } from '../../schemaValidator';
import { prepareFilenames } from '../../serializer';
import { pullParams, pushParams } from '../validations/cli';
import { Entities, pullParams, pushParams } from '../validations/cli';
import {
Casing,
CasingType,
Expand Down Expand Up @@ -391,6 +391,7 @@ export const preparePullConfig = async (
tablesFilter: string[];
schemasFilter: string[];
prefix: Prefix;
entities: Entities;
}
> => {
const raw = flattenPull(
Expand Down Expand Up @@ -450,6 +451,7 @@ export const preparePullConfig = async (
tablesFilter,
schemasFilter,
prefix: config.migrations?.prefix || 'index',
entities: config.entities,
};
}

Expand All @@ -468,6 +470,7 @@ export const preparePullConfig = async (
tablesFilter,
schemasFilter,
prefix: config.migrations?.prefix || 'index',
entities: config.entities,
};
}

Expand All @@ -486,6 +489,7 @@ export const preparePullConfig = async (
tablesFilter,
schemasFilter,
prefix: config.migrations?.prefix || 'index',
entities: config.entities,
};
}

Expand Down
2 changes: 2 additions & 0 deletions drizzle-kit/src/cli/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ export const pull = command({
tablesFilter,
schemasFilter,
prefix,
entities,
} = config;
mkdirSync(out, { recursive: true });

Expand Down Expand Up @@ -496,6 +497,7 @@ export const pull = command({
tablesFilter,
schemasFilter,
prefix,
entities,
);
} else if (dialect === 'mysql') {
const { introspectMysql } = await import('./commands/introspect');
Expand Down
Loading
Loading