Skip to content

Commit 498f7d4

Browse files
Beta21 (#5614)
* commutative fixes * feat feat * fix env * fix * Fixed import tests * Fixed migrator --------- Co-authored-by: Sergey Reka <71607800+Sukairo-02@users.noreply.github.com> Co-authored-by: Sukairo-02 <sreka9056@gmail.com>
1 parent 1a01082 commit 498f7d4

37 files changed

Lines changed: 2421 additions & 214 deletions

.github/workflows/release-feature-branch.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ on:
2525
required: true
2626
TURBO_TOKEN:
2727
required: true
28+
NETLIFY_DB_URL:
29+
required: true
2830

2931
concurrency:
3032
group: feature-${{ github.workflow }}-${{ github.ref }}
@@ -234,6 +236,7 @@ jobs:
234236
TEST_CONFIG_PATH_PREFIX: ./tests/cli/
235237
SQLITE_CLOUD_CONNECTION_STRING: ${{ secrets.SQLITE_CLOUD_CONNECTION_STRING }}
236238
SQLITE_MANY_CLOUD_CONNECTION_STRING: ${{ secrets.SQLITE_MANY_CLOUD_CONNECTION_STRING }}
239+
NETLIFY_DB_URL: ${{ secrets.NETLIFY_DB_URL }}
237240
working-directory: integration-tests
238241
shell: bash
239242
run: |

.github/workflows/release-latest.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ jobs:
468468

469469
release:
470470
permissions:
471-
contents: read
472-
id-token: write
471+
contents: write # for creating GitHub releases
472+
id-token: write # for OIDC
473473
needs:
474474
- test
475475
- attw

.github/workflows/router.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
SQLITE_CLOUD_CONNECTION_STRING: ${{ secrets.SQLITE_CLOUD_CONNECTION_STRING }}
4444
SQLITE_MANY_CLOUD_CONNECTION_STRING: ${{ secrets.SQLITE_MANY_CLOUD_CONNECTION_STRING }}
4545
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
46+
NETLIFY_DB_URL: ${{ secrets.NETLIFY_DB_URL }}
4647

4748
run-latest:
4849
needs: switch

changelogs/drizzle-kit/0.31.8.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
### Bug fixes
22

3-
- Fixed `algorythm` => `algorithm` typo
3+
- Fixed `algorythm` => `algorithm` typo.
4+
- Fixed external dependencies in build configuration.

changelogs/drizzle-kit/0.31.9.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- drizzle-kit api improvements for D1 connections
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
More fixes for the drizzle-kit migration process and commutativity checks
2+
3+
- Adding a value to a PostgreSQL enum will no longer be treated as commutative
4+
- Enum values added in different leaves will now be merged properly

changelogs/drizzle-orm/0.45.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed pg-native Pool detection in node-postgres transactions breaking in environments with forbidden `require()` ([#5107](https://github.com/drizzle-team/drizzle-orm/issues/5107))

drizzle-kit/package.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-kit",
3-
"version": "1.0.0-beta.20",
3+
"version": "1.0.0-beta.21",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",
@@ -148,6 +148,30 @@
148148
},
149149
"types": "./api-postgres.d.mts",
150150
"default": "./api-postgres.mjs"
151+
},
152+
"./api-mysql": {
153+
"import": {
154+
"types": "./api-mysql.d.mts",
155+
"default": "./api-mysql.mjs"
156+
},
157+
"require": {
158+
"types": "./api-mysql.d.ts",
159+
"default": "./api-mysql.js"
160+
},
161+
"types": "./api-mysql.d.mts",
162+
"default": "./api-mysql.mjs"
163+
},
164+
"./api-sqlite": {
165+
"import": {
166+
"types": "./api-sqlite.d.mts",
167+
"default": "./api-sqlite.mjs"
168+
},
169+
"require": {
170+
"types": "./api-sqlite.d.ts",
171+
"default": "./api-sqlite.js"
172+
},
173+
"types": "./api-sqlite.d.mts",
174+
"default": "./api-sqlite.mjs"
151175
}
152176
}
153177
}

drizzle-kit/src/cli/commands/studio.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="@cloudflare/workers-types" />
12
import type { PGlite } from '@electric-sql/pglite';
23
import { serve } from '@hono/node-server';
34
import { zValidator } from '@hono/zod-validator';
@@ -68,6 +69,7 @@ export type Setup = {
6869
| 'mysql2'
6970
| '@planetscale/database'
7071
| 'd1-http'
72+
| 'd1'
7173
| '@libsql/client'
7274
| 'better-sqlite3'
7375
| '@sqlitecloud/drivers'
@@ -76,7 +78,7 @@ export type Setup = {
7678
| 'duckdb'
7779
| '@duckdb/node-api'
7880
| 'node:sqlite';
79-
driver?: 'aws-data-api' | 'd1-http' | 'turso' | 'pglite' | 'sqlite-cloud';
81+
driver?: 'aws-data-api' | 'd1-http' | 'd1' | 'turso' | 'pglite' | 'sqlite-cloud';
8082
databaseName?: string; // for planetscale (driver remove database name from connection string)
8183
proxy: Proxy;
8284
transactionProxy: TransactionProxy;
@@ -457,17 +459,45 @@ export const drizzleForMySQL = async (
457459
// };
458460
// };
459461

462+
// D1 binding credentials type (mirrors the one in connections.ts)
463+
type D1BindingCredentials = {
464+
driver: 'd1';
465+
binding: D1Database;
466+
};
467+
460468
export const drizzleForSQLite = async (
461-
credentials: SqliteCredentials,
469+
credentials: SqliteCredentials | D1BindingCredentials,
462470
sqliteSchema: Record<string, Record<string, AnySQLiteTable>>,
463471
relations: Record<string, Relations>,
464472
schemaFiles?: SchemaFile[],
465473
casing?: CasingType,
466474
): Promise<Setup> => {
467-
const { connectToSQLite } = await import('../connections');
475+
const customDefaults = getCustomDefaults(sqliteSchema, casing);
468476

477+
if ('driver' in credentials && credentials.driver === 'd1') {
478+
const { connectToD1 } = await import('../connections');
479+
const sqliteDB = await connectToD1(credentials.binding);
480+
481+
const dbUrl = 'd1://binding';
482+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
483+
484+
return {
485+
dbHash,
486+
dialect: 'sqlite',
487+
driver: 'd1',
488+
packageName: 'd1',
489+
proxy: sqliteDB.proxy,
490+
transactionProxy: sqliteDB.transactionProxy,
491+
customDefaults,
492+
schema: sqliteSchema,
493+
relations,
494+
schemaFiles,
495+
casing,
496+
};
497+
}
498+
499+
const { connectToSQLite } = await import('../connections');
469500
const sqliteDB = await connectToSQLite(credentials);
470-
const customDefaults = getCustomDefaults(sqliteSchema, casing);
471501

472502
let dbUrl: string;
473503

drizzle-kit/src/cli/connections.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="@cloudflare/workers-types" />
12
import type { PGlite } from '@electric-sql/pglite';
23
import type { SQLiteCloudRowset } from '@sqlitecloud/drivers';
34
import type { AwsDataApiPgQueryResult, AwsDataApiSessionOptions } from 'drizzle-orm/aws-data-api/pg';
@@ -1702,7 +1703,7 @@ const prepareSqliteParams = (params: any[], driver?: string) => {
17021703
? JSON.stringify(param.value)
17031704
: (param.value as string);
17041705

1705-
if (driver === 'd1-http') {
1706+
if (driver === 'd1-http' || driver === 'd1') {
17061707
return value;
17071708
}
17081709

@@ -1731,6 +1732,84 @@ const preparePGliteParams = (params: any[]) => {
17311732
});
17321733
};
17331734

1735+
export type D1Credentials = {
1736+
driver: 'd1';
1737+
binding: D1Database;
1738+
};
1739+
1740+
export const connectToD1 = async (
1741+
d1: D1Database,
1742+
): Promise<
1743+
& SQLiteDB
1744+
& {
1745+
packageName: 'd1';
1746+
migrate: (config: MigrationConfig) => Promise<void | MigratorInitFailResponse>;
1747+
proxy: Proxy;
1748+
transactionProxy: TransactionProxy;
1749+
}
1750+
> => {
1751+
const db: SQLiteDB = {
1752+
query: async <T>(sql: string, params?: any[]) => {
1753+
const stmt = d1.prepare(sql);
1754+
const boundStmt = params && params.length > 0 ? stmt.bind(...params) : stmt;
1755+
const result = await boundStmt.all<T>();
1756+
return (result.results ?? []) as T[];
1757+
},
1758+
run: async (query: string) => {
1759+
const stmt = d1.prepare(query);
1760+
await stmt.run();
1761+
},
1762+
batch: async (statements: string[]) => {
1763+
await d1.batch(statements.map((s) => d1.prepare(s)));
1764+
},
1765+
};
1766+
1767+
const proxy: Proxy = async (params) => {
1768+
const preparedParams = prepareSqliteParams(params.params || [], 'd1');
1769+
const stmt = d1.prepare(params.sql);
1770+
const boundStmt = preparedParams.length > 0 ? stmt.bind(...preparedParams) : stmt;
1771+
1772+
try {
1773+
if (params.mode === 'array') {
1774+
return await boundStmt.raw();
1775+
}
1776+
const result = await boundStmt.all();
1777+
return result.results ?? [];
1778+
} catch (error: any) {
1779+
// D1 doesn't allow certain introspection queries (sqlite_master with pragma functions)
1780+
// Return empty array for SQLITE_AUTH errors on these system queries
1781+
if (error?.message?.includes('SQLITE_AUTH') || error?.message?.includes('not authorized')) {
1782+
return [];
1783+
}
1784+
throw error;
1785+
}
1786+
};
1787+
1788+
const transactionProxy: TransactionProxy = async (queries) => {
1789+
const results: any[] = [];
1790+
try {
1791+
// D1 doesn't support true transactions via binding, use batch instead
1792+
const statements = queries.map((q) => d1.prepare(q.sql));
1793+
const batchResults = await d1.batch(statements);
1794+
for (const result of batchResults) {
1795+
results.push(result.results ?? []);
1796+
}
1797+
} catch (error) {
1798+
results.push(error as Error);
1799+
}
1800+
return results;
1801+
};
1802+
1803+
const { drizzle } = await import('drizzle-orm/d1');
1804+
const { migrate } = await import('drizzle-orm/d1/migrator');
1805+
const drzl = drizzle(d1);
1806+
const migrateFn = async (config: MigrationConfig) => {
1807+
return migrate(drzl, config);
1808+
};
1809+
1810+
return { ...db, packageName: 'd1', proxy, transactionProxy, migrate: migrateFn };
1811+
};
1812+
17341813
export const connectToSQLite = async (
17351814
credentials: SqliteCredentials,
17361815
): Promise<

0 commit comments

Comments
 (0)