Skip to content

Commit

Permalink
Merge branch 'fix-empty-array-generation' of https://github.com/L-Mar…
Browse files Browse the repository at this point in the history
…io564/drizzle-orm into fix-empty-array-generation
  • Loading branch information
L-Mario564 committed Aug 7, 2024
2 parents 39c1419 + c7b5d8f commit ca314f1
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 303 deletions.
4 changes: 4 additions & 0 deletions changelogs/drizzle-orm/0.32.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Fix AWS Data API type hints bugs in RQB
- Fix set transactions in MySQL bug - thanks @roguesherlock
- Add forwaring dependencies within useLiveQuery, fixes [#2651](https://github.com/drizzle-team/drizzle-orm/issues/2651) - thanks @anstapol
- Export additional types from SQLite package, like `AnySQLiteUpdate` - thanks @veloii
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.32.1",
"version": "0.32.2",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/expo-sqlite/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SQLiteRelationalQuery } from '~/sqlite-core/query-builders/query.ts';

export const useLiveQuery = <T extends Pick<AnySQLiteSelect, '_' | 'then'> | SQLiteRelationalQuery<'sync', unknown>>(
query: T,
deps: unknown[] = [],
) => {
const [data, setData] = useState<Awaited<T>>(
(is(query, SQLiteRelationalQuery) && query.mode === 'first' ? undefined : []) as Awaited<T>,
Expand Down Expand Up @@ -43,7 +44,7 @@ export const useLiveQuery = <T extends Pick<AnySQLiteSelect, '_' | 'then'> | SQL
return () => {
listener?.remove();
};
}, []);
}, deps);

return {
data,
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/mysql-core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export abstract class MySqlSession<
parts.push(`isolation level ${config.isolationLevel}`);
}

return parts.length ? sql.join(['set transaction ', parts.join(' ')]) : undefined;
return parts.length ? sql`set transaction ${sql.raw(parts.join(' '))}` : undefined;
}

protected getStartTransactionSQL(config: MySqlTransactionConfig): SQL | undefined {
Expand All @@ -112,7 +112,7 @@ export abstract class MySqlSession<
parts.push(config.accessMode);
}

return parts.length ? sql.join(['start transaction ', parts.join(' ')]) : undefined;
return parts.length ? sql`start transaction ${sql.raw(parts.join(' '))}` : undefined;
}
}

Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/sql/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class SQL<T = unknown> implements SQLWrapper {
return { sql: this.mapInlineParam(mappedValue, config), params: [] };
}

let typings: QueryTypingsValue[] | undefined;
let typings: QueryTypingsValue[] = ['none'];
if (prepareTyping) {
typings = [prepareTyping(chunk.encoder)];
}
Expand Down Expand Up @@ -263,7 +263,7 @@ export class SQL<T = unknown> implements SQLWrapper {
return { sql: this.mapInlineParam(chunk, config), params: [] };
}

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

Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/sqlite-core/query-builders/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type SQLiteUpdate<
TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined,
> = SQLiteUpdateBase<TTable, TResultType, TRunResult, TReturning, true, never>;

type AnySQLiteUpdate = SQLiteUpdateBase<any, any, any, any, any, any>;
export type AnySQLiteUpdate = SQLiteUpdateBase<any, any, any, any, any, any>;

export interface SQLiteUpdateBase<
TTable extends SQLiteTable = SQLiteTable,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "pnpm test:vitest",
"test:vitest": "vitest run",
"test:esm": "node tests/imports.test.mjs && node tests/imports.test.cjs",
"test:data-api": "sst shell vitest run tests/awsdatapi.test.ts"
"test:data-api": "sst shell vitest run tests/pg/awsdatapi.test.ts"
},
"keywords": [],
"author": "Drizzle Team",
Expand Down
39 changes: 39 additions & 0 deletions integration-tests/tests/mysql/mysql-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,45 @@ export function tests(driver?: string) {
await db.execute(sql`drop table ${products}`);
});

test('transaction with options (set isolationLevel)', async (ctx) => {
const { db } = ctx.mysql;

const users = mysqlTable('users_transactions', {
id: serial('id').primaryKey(),
balance: int('balance').notNull(),
});
const products = mysqlTable('products_transactions', {
id: serial('id').primaryKey(),
price: int('price').notNull(),
stock: int('stock').notNull(),
});

await db.execute(sql`drop table if exists ${users}`);
await db.execute(sql`drop table if exists ${products}`);

await db.execute(sql`create table users_transactions (id serial not null primary key, balance int not null)`);
await db.execute(
sql`create table products_transactions (id serial not null primary key, price int not null, stock int not null)`,
);

const [{ insertId: userId }] = await db.insert(users).values({ balance: 100 });
const user = await db.select().from(users).where(eq(users.id, userId)).then((rows) => rows[0]!);
const [{ insertId: productId }] = await db.insert(products).values({ price: 10, stock: 10 });
const product = await db.select().from(products).where(eq(products.id, productId)).then((rows) => rows[0]!);

await db.transaction(async (tx) => {
await tx.update(users).set({ balance: user.balance - product.price }).where(eq(users.id, user.id));
await tx.update(products).set({ stock: product.stock - 1 }).where(eq(products.id, product.id));
}, { isolationLevel: 'serializable' });

const result = await db.select().from(users);

expect(result).toEqual([{ id: 1, balance: 90 }]);

await db.execute(sql`drop table ${users}`);
await db.execute(sql`drop table ${products}`);
});

test('transaction rollback', async (ctx) => {
const { db } = ctx.mysql;

Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/mysql/mysql-planetscale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ skipTests([
// to redefine in this file
'utc config for datetime',
'transaction',
'transaction with options (set isolationLevel)',
'having',
'select count()',
'insert via db.execute w/ query builder',
Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/mysql/mysql-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ skipTests([
'nested transaction',
'transaction rollback',
'transaction',
'transaction with options (set isolationLevel)',
'migrator',
]);

Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/mysql/tidb-serverless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ skipTests([
'select iterator w/ prepared statement',
'select iterator',
'transaction',
'transaction with options (set isolationLevel)',
'Insert all defaults in multiple rows',
'Insert all defaults in 1 row',
'$default with empty array',
Expand Down
Loading

0 comments on commit ca314f1

Please sign in to comment.