Skip to content

Commit

Permalink
Merge pull request #2363 from drizzle-team/fix-rqb-tables-with-schema
Browse files Browse the repository at this point in the history
Fix RQB behavior for tables with same names in different schemas
  • Loading branch information
AndriiSherman authored Jul 8, 2024
2 parents b59e0a5 + ee089d9 commit b59b8f5
Show file tree
Hide file tree
Showing 94 changed files with 19,435 additions and 45,376 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
permissions:
actions: read
contents: read
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
- 55432:5432
mysql:
image: mysql:8
env:
Expand All @@ -73,7 +73,7 @@ jobs:
--health-timeout 5s
--health-retries 5
ports:
- 3306:3306
- 33306:3306
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -146,10 +146,10 @@ jobs:
- name: Run tests
if: steps.checks.outputs.has_new_release == 'true'
env:
PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:5432/drizzle
PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55432/drizzle
PG_VECTOR_CONNECTION_STRING: postgres://postgres:postgres@localhost:54321/drizzle
PG_POSTGIS_CONNECTION_STRING: postgres://postgres:postgres@localhost:54322/drizzle
MYSQL_CONNECTION_STRING: mysql://root:root@localhost:3306/drizzle
MYSQL_CONNECTION_STRING: mysql://root:root@localhost:33306/drizzle
PLANETSCALE_CONNECTION_STRING: ${{ secrets.PLANETSCALE_CONNECTION_STRING }}
NEON_CONNECTION_STRING: ${{ secrets.NEON_CONNECTION_STRING }}
TIDB_CONNECTION_STRING: ${{ secrets.TIDB_CONNECTION_STRING }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
- 55432:5432
mysql:
image: mysql:8
env:
Expand All @@ -66,7 +66,7 @@ jobs:
--health-timeout 5s
--health-retries 5
ports:
- 3306:3306
- 33306:3306
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -149,10 +149,10 @@ jobs:
- name: Run tests
if: steps.checks.outputs.has_new_release == 'true'
env:
PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:5432/drizzle
PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55432/drizzle
PG_VECTOR_CONNECTION_STRING: postgres://postgres:postgres@localhost:54321/drizzle
PG_POSTGIS_CONNECTION_STRING: postgres://postgres:postgres@localhost:54322/drizzle
MYSQL_CONNECTION_STRING: mysql://root:root@localhost:3306/drizzle
MYSQL_CONNECTION_STRING: mysql://root:root@localhost:33306/drizzle
PLANETSCALE_CONNECTION_STRING: ${{ secrets.PLANETSCALE_CONNECTION_STRING }}
NEON_CONNECTION_STRING: ${{ secrets.NEON_CONNECTION_STRING }}
TIDB_CONNECTION_STRING: ${{ secrets.TIDB_CONNECTION_STRING }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unpublish-release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version: 18
node-version: '18.18'
registry-url: 'https://registry.npmjs.org'

- name: Unpublish
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dist.new
.rollup.cache
dist-dts
rollup.config-*.mjs
*.log
.DS_Store
2 changes: 2 additions & 0 deletions changelogs/drizzle-orm/0.31.0-beta.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ db.select({ innerProduct: sql`(${maxInnerProduct(items.embedding, [3,1,2])}) * -

// and more!
```

- 🛠️ Fixed RQB behavior for tables with same names in different schemas
8 changes: 4 additions & 4 deletions drizzle-orm/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ await fs.remove('dist.new');

await Promise.all([
(async () => {
await $`tsup`;
await $`tsup`.stdio('pipe', 'pipe', 'pipe');
})(),
(async () => {
await $`tsc -p tsconfig.dts.json`;
await $`tsc -p tsconfig.dts.json`.stdio('pipe', 'pipe', 'pipe');
await cpy('dist-dts/**/*.d.ts', 'dist.new', {
rename: (basename) => basename.replace(/\.d\.ts$/, '.d.cts'),
});
Expand All @@ -64,8 +64,8 @@ await Promise.all([
]);

await Promise.all([
$`tsup src/version.ts --no-config --dts --format esm --outDir dist.new`,
$`tsup src/version.ts --no-config --dts --format cjs --outDir dist.new`,
$`tsup src/version.ts --no-config --dts --format esm --outDir dist.new`.stdio('pipe', 'pipe', 'pipe'),
$`tsup src/version.ts --no-config --dts --format cjs --outDir dist.new`.stdio('pipe', 'pipe', 'pipe'),
]);

await $`scripts/fix-imports.ts`;
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/d1/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function d1ToRawMapping(results: any) {
}

export class D1PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<
{ type: 'async'; run: D1Result; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
{ type: 'async'; run: D1Response; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'D1PreparedQuery';

Expand Down Expand Up @@ -177,7 +177,7 @@ export class D1PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig
this.stmt = stmt;
}

run(placeholderValues?: Record<string, unknown>): Promise<D1Result> {
run(placeholderValues?: Record<string, unknown>): Promise<D1Response> {
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
this.logger.logQuery(this.query.sql, params);
return this.stmt.bind(...params).run();
Expand Down
6 changes: 3 additions & 3 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { Param, SQL, sql, View } from '~/sql/sql.ts';
import type { Name, QueryWithTypings, SQLChunk } from '~/sql/sql.ts';
import { Subquery } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { getTableName, getTableUniqueName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import { MySqlColumn } from './columns/common.ts';
Expand Down Expand Up @@ -616,7 +616,7 @@ export class MySqlDialect {
} of selectedRelations
) {
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
const relationTableName = relation.referencedTable[Table.Symbol.Name];
const relationTableName = getTableUniqueName(relation.referencedTable);
const relationTableTsName = tableNamesMap[relationTableName]!;
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
const joinOn = and(
Expand Down Expand Up @@ -913,7 +913,7 @@ export class MySqlDialect {
} of selectedRelations
) {
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
const relationTableName = relation.referencedTable[Table.Symbol.Name];
const relationTableName = getTableUniqueName(relation.referencedTable);
const relationTableTsName = tableNamesMap[relationTableName]!;
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
const joinOn = and(
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/neon-http/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class NeonHttpDriver {
types.setTypeParser(types.builtins.TIMESTAMPTZ, (val) => val);
types.setTypeParser(types.builtins.TIMESTAMP, (val) => val);
types.setTypeParser(types.builtins.DATE, (val) => val);
types.setTypeParser(types.builtins.INTERVAL, (val) => val);
}
}

Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
type SQLChunk,
} from '~/sql/sql.ts';
import { Subquery } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { getTableName, getTableUniqueName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import type { PgSession } from './session.ts';
Expand Down Expand Up @@ -1219,7 +1219,7 @@ export class PgDialect {
} of selectedRelations
) {
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
const relationTableName = relation.referencedTable[Table.Symbol.Name];
const relationTableName = getTableUniqueName(relation.referencedTable);
const relationTableTsName = tableNamesMap[relationTableName]!;
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
const joinOn = and(
Expand Down
11 changes: 10 additions & 1 deletion drizzle-orm/src/pg-core/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { entityKind, is } from '~/entity.ts';
import { SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import type { pgEnum } from './columns/enum.ts';
import { pgEnumWithSchema } from './columns/enum.ts';
import { type PgTableFn, pgTableWithSchema } from './table.ts';
import { type pgMaterializedView, pgMaterializedViewWithSchema, type pgView, pgViewWithSchema } from './view.ts';

export class PgSchema<TName extends string = string> {
export class PgSchema<TName extends string = string> implements SQLWrapper {
static readonly [entityKind]: string = 'PgSchema';
constructor(
public readonly schemaName: TName,
Expand All @@ -25,6 +26,14 @@ export class PgSchema<TName extends string = string> {
enum: typeof pgEnum = ((name, values) => {
return pgEnumWithSchema(name, values, this.schemaName);
});

getSQL(): SQL {
return new SQL([sql.identifier(this.schemaName)]);
}

shouldOmitSQLParens(): boolean {
return true;
}
}

export function isPgSchema(obj: unknown): obj is PgSchema {
Expand Down
10 changes: 5 additions & 5 deletions drizzle-orm/src/relations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AnyTable, type InferModelFromColumns, Table } from '~/table.ts';
import { type AnyTable, getTableUniqueName, type InferModelFromColumns, Table } from '~/table.ts';
import { type AnyColumn, Column } from './column.ts';
import { entityKind, is } from './entity.ts';
import { PrimaryKeyBuilder } from './pg-core/primary-keys.ts';
Expand Down Expand Up @@ -430,7 +430,7 @@ export function extractTablesRelationalConfig<
const tablesConfig: TablesRelationalConfig = {};
for (const [key, value] of Object.entries(schema)) {
if (is(value, Table)) {
const dbName = value[Table.Symbol.Name];
const dbName = getTableUniqueName(value);
const bufferedRelations = relationsBuffer[dbName];
tableNamesMap[dbName] = key;
tablesConfig[key] = {
Expand Down Expand Up @@ -462,7 +462,7 @@ export function extractTablesRelationalConfig<
}
}
} else if (is(value, Relations)) {
const dbName: string = value.table[Table.Symbol.Name];
const dbName = getTableUniqueName(value.table);
const tableName = tableNamesMap[dbName];
const relations: Record<string, Relation> = value.config(
configHelpers(value.table),
Expand Down Expand Up @@ -561,7 +561,7 @@ export function normalizeRelation(
};
}

const referencedTableTsName = tableNamesMap[relation.referencedTable[Table.Symbol.Name]];
const referencedTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
if (!referencedTableTsName) {
throw new Error(
`Table "${relation.referencedTable[Table.Symbol.Name]}" not found in schema`,
Expand All @@ -574,7 +574,7 @@ export function normalizeRelation(
}

const sourceTable = relation.sourceTable;
const sourceTableTsName = tableNamesMap[sourceTable[Table.Symbol.Name]];
const sourceTableTsName = tableNamesMap[getTableUniqueName(sourceTable)];
if (!sourceTableTsName) {
throw new Error(
`Table "${sourceTable[Table.Symbol.Name]}" not found in schema`,
Expand Down
15 changes: 9 additions & 6 deletions drizzle-orm/src/sql/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface QueryWithTypings extends Query {
*/
export interface SQLWrapper {
getSQL(): SQL;
shouldOmitSQLParens?(): boolean;
}

export function isSQLWrapper(value: unknown): value is SQLWrapper {
Expand Down Expand Up @@ -213,15 +214,15 @@ export class SQL<T = unknown> implements SQLWrapper {
}

let typings: QueryTypingsValue[] | undefined;
if (prepareTyping !== undefined) {
if (prepareTyping) {
typings = [prepareTyping(chunk.encoder)];
}

return { sql: escapeParam(paramStartIndex.value++, mappedValue), params: [mappedValue], typings };
}

if (is(chunk, Placeholder)) {
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk] };
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ['none'] };
}

if (is(chunk, SQL.Aliased) && chunk.fieldAlias !== undefined) {
Expand All @@ -248,6 +249,9 @@ export class SQL<T = unknown> implements SQLWrapper {
}

if (isSQLWrapper(chunk)) {
if (chunk.shouldOmitSQLParens?.()) {
return this.buildQueryFromSourceParams([chunk.getSQL()], config);
}
return this.buildQueryFromSourceParams([
new StringChunk('('),
chunk.getSQL(),
Expand Down Expand Up @@ -441,11 +445,10 @@ export type SQLChunk =

export function sql<T>(strings: TemplateStringsArray, ...params: any[]): SQL<T>;
/*
The type of `params` is specified as `SQLSourceParam[]`, but that's slightly incorrect -
The type of `params` is specified as `SQLChunk[]`, but that's slightly incorrect -
in runtime, users won't pass `FakePrimitiveParam` instances as `params` - they will pass primitive values
which will be wrapped in `Param` using `buildChunksFromParam(...)`. That's why the overload
specify `params` as `any[]` and not as `SQLSourceParam[]`. This type is used to make our lives easier and
the type checker happy.
which will be wrapped in `Param`. That's why the overload specifies `params` as `any[]` and not as `SQLSourceParam[]`.
This type is used to make our lives easier and the type checker happy.
*/
export function sql(strings: TemplateStringsArray, ...params: SQLChunk[]): SQL {
const queryChunks: SQLChunk[] = [];
Expand Down
6 changes: 3 additions & 3 deletions drizzle-orm/src/sqlite-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { SQLiteColumn } from '~/sqlite-core/columns/index.ts';
import type { SQLiteDeleteConfig, SQLiteInsertConfig, SQLiteUpdateConfig } from '~/sqlite-core/query-builders/index.ts';
import { SQLiteTable } from '~/sqlite-core/table.ts';
import { Subquery } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { getTableName, getTableUniqueName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import type {
Expand Down Expand Up @@ -585,7 +585,7 @@ export abstract class SQLiteDialect {
} of selectedRelations
) {
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
const relationTableName = relation.referencedTable[Table.Symbol.Name];
const relationTableName = getTableUniqueName(relation.referencedTable);
const relationTableTsName = tableNamesMap[relationTableName]!;
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
// const relationTable = schema[relationTableTsName]!;
Expand Down Expand Up @@ -779,7 +779,7 @@ export class SQLiteAsyncDialect extends SQLiteDialect {

async migrate(
migrations: MigrationMeta[],
session: SQLiteSession<'async', unknown, any, TablesRelationalConfig>,
session: SQLiteSession<'async', any, any, any>,
config?: string | MigrationConfig,
): Promise<void> {
const migrationsTable = config === undefined
Expand Down
10 changes: 10 additions & 0 deletions drizzle-orm/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const IsAlias = Symbol.for('drizzle:IsAlias');
/** @internal */
export const ExtraConfigBuilder = Symbol.for('drizzle:ExtraConfigBuilder');

const IsDrizzleTable = Symbol.for('drizzle:IsDrizzleTable');

export interface Table<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
T extends TableConfig = TableConfig,
Expand Down Expand Up @@ -115,6 +117,10 @@ export class Table<T extends TableConfig = TableConfig> implements SQLWrapper {
}
}

export function isTable(table: unknown): table is Table {
return typeof table === 'object' && table !== null && IsDrizzleTable in table;
}

/**
* Any table with a specified boundary.
*
Expand All @@ -138,6 +144,10 @@ export function getTableName<T extends Table>(table: T): T['_']['name'] {
return table[TableName];
}

export function getTableUniqueName<T extends Table>(table: T): `${T['_']['schema']}.${T['_']['name']}` {
return `${table[Schema] ?? 'public'}.${table[TableName]}`;
}

export type MapColumnName<TName extends string, TColumn extends Column, TDBColumNames extends boolean> =
TDBColumNames extends true ? TColumn['_']['name']
: TName;
Expand Down
5 changes: 2 additions & 3 deletions drizzle-orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,11 @@ export type UpdateSet = Record<string, SQL | Param | null | undefined>;

export type OneOrMany<T> = T | T[];

export type Update<T, TUpdate> = Simplify<
export type Update<T, TUpdate> =
& {
[K in Exclude<keyof T, keyof TUpdate>]: T[K];
}
& TUpdate
>;
& TUpdate;

export type Simplify<T> =
& {
Expand Down
Loading

0 comments on commit b59b8f5

Please sign in to comment.