Skip to content

Commit d0ac808

Browse files
authored
Merge pull request #12 from replit/jackyzha0/fix-conns
thread through pool
2 parents 1245e09 + a80dd79 commit d0ac808

File tree

10 files changed

+208
-144
lines changed

10 files changed

+208
-144
lines changed

.envrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use flake
2+
dotenv_if_exists
3+
PATH_add ./node_modules/.bin

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ dist-dts
1212
rollup.config-*.mjs
1313
*.log
1414
.DS_Store
15-
drizzle-seed/src/dev
15+
drizzle-seed/src/dev
16+
.direnv

drizzle-kit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pnpm install && pnpm build
99
```
1010

1111
#### Development
12-
Make any changes you require to the `drizzle-kit/api` file ([see here](./drizzle-kit/src/api.ts)), then run:
12+
Make any changes you require to the `drizzle-kit/api` file ([see here](./drizzle-kit/src/api.ts)), then run (from `drizzle-kit`):
1313
```bash
1414
pnpm build
1515
```

drizzle-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@drizzle-team/drizzle-kit",
3-
"version": "0.31.11",
3+
"version": "0.32.0",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",

drizzle-kit/src/api.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,15 @@ export const introspectPgDB = async (
8484
};
8585

8686
export const preparePgDB = async (
87-
credentials: DrizzlePostgresCredentials,
87+
pool: import('pg').Pool | import('pg').PoolClient,
8888
): Promise<
8989
DrizzlePgDB
9090
> => {
91-
console.log(`Using 'pg' driver for database querying`);
9291
const { default: pg } = await import('pg');
92+
9393
const { drizzle } = await import('drizzle-orm/node-postgres');
9494
const { migrate } = await import('drizzle-orm/node-postgres/migrator');
9595

96-
const ssl = 'ssl' in credentials
97-
? credentials.ssl === 'prefer'
98-
|| credentials.ssl === 'require'
99-
|| credentials.ssl === 'allow'
100-
? { rejectUnauthorized: false }
101-
: credentials.ssl === 'verify-full'
102-
? {}
103-
: credentials.ssl
104-
: {};
105-
10696
// Override pg default date parsers
10797
const types: { getTypeParser: typeof pg.types.getTypeParser } = {
10898
// @ts-ignore
@@ -124,17 +114,13 @@ export const preparePgDB = async (
124114
},
125115
};
126116

127-
const client = 'url' in credentials
128-
? new pg.Pool({ connectionString: credentials.url, max: 1 })
129-
: new pg.Pool({ ...credentials, ssl, max: 1 });
130-
131-
const db = drizzle(client);
117+
const db = drizzle(pool);
132118
const migrateFn = async (config: MigrationConfig) => {
133119
return migrate(db, config);
134120
};
135121

136122
const query = async (sql: string, params?: any[]) => {
137-
const result = await client.query({
123+
const result = await pool.query({
138124
text: sql,
139125
values: params ?? [],
140126
types,
@@ -143,7 +129,7 @@ export const preparePgDB = async (
143129
};
144130

145131
const proxy: Proxy = async (params: ProxyParams) => {
146-
const result = await client.query({
132+
const result = await pool.query({
147133
text: params.sql,
148134
values: params.params,
149135
...(params.mode === 'array' && { rowMode: 'array' }),
@@ -155,17 +141,6 @@ export const preparePgDB = async (
155141
return { query, proxy, migrate: migrateFn };
156142
};
157143

158-
export const getPgClientPool = async (
159-
targetCredentials: DrizzlePostgresCredentials,
160-
) => {
161-
const { default: pg } = await import('pg');
162-
const pool = 'url' in targetCredentials
163-
? new pg.Pool({ connectionString: targetCredentials.url, max: 1 })
164-
: new pg.Pool({ ...targetCredentials, ssl: undefined, max: 1 });
165-
166-
return pool;
167-
};
168-
169144
export type { SelectResolverInput, SelectResolverOutput } from './cli/commands/pgPushUtils';
170145
export { applyPgSnapshotsDiff } from './snapshotsDiffer';
171146
export type {

drizzle-kit/src/serializer/pgSerializer.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
PgView,
2020
uniqueKeyName,
2121
} from 'drizzle-orm/pg-core';
22+
import { parse as parsePostgresArray } from 'postgres-array';
2223
import { CasingType } from 'src/cli/validations/common';
2324
import { vectorOps } from 'src/extensions/vector';
2425
import { withStyle } from '../cli/validations/outputs';
@@ -40,7 +41,6 @@ import type {
4041
UniqueConstraint,
4142
View,
4243
} from '../serializer/pgSchema';
43-
import { parse as parsePostgresArray } from 'postgres-array';
4444
import { type DB, escapeSingleQuotes, isPgArrayType } from '../utils';
4545
import { getColumnCasing, sqlToStr } from './utils';
4646

@@ -1942,21 +1942,25 @@ WHERE
19421942
*/
19431943
const formatArrayElement = (element: any, dataType: string): string | null => {
19441944
if (element === null) return element;
1945-
1945+
19461946
// Remove outer quotes from postgres-array parsed elements if present
19471947
// First trim spaces since postgres-array includes leading spaces in array elements
19481948
let cleanElement = typeof element === 'string' ? element.trim() : element;
1949-
if (typeof cleanElement === 'string' && ((cleanElement.startsWith("'") && cleanElement.endsWith("'")) || (cleanElement.startsWith('"') && cleanElement.endsWith('"')))) {
1949+
if (
1950+
typeof cleanElement === 'string'
1951+
&& ((cleanElement.startsWith("'") && cleanElement.endsWith("'"))
1952+
|| (cleanElement.startsWith('"') && cleanElement.endsWith('"')))
1953+
) {
19501954
cleanElement = cleanElement.slice(1, -1);
19511955
}
19521956

19531957
// remove [] from dataType if it exists
1954-
if (!dataType.endsWith("[]")) {
1958+
if (!dataType.endsWith('[]')) {
19551959
throw new Error(`array dataType ${dataType} does not end with '[]'`);
19561960
}
19571961

1958-
const baseDataType = dataType.slice(0, -"[]".length);
1959-
1962+
const baseDataType = dataType.slice(0, -'[]'.length);
1963+
19601964
if (['integer', 'smallint', 'bigint', 'double precision', 'real'].includes(baseDataType)) {
19611965
return cleanElement;
19621966
} else if (dataType.startsWith('timestamp')) {
@@ -1968,7 +1972,7 @@ const formatArrayElement = (element: any, dataType: string): string | null => {
19681972
} else if (['json', 'jsonb'].includes(baseDataType)) {
19691973
// For JSON/JSONB arrays, cleanElement is already a JSON string
19701974
// We just need to ensure it's properly quoted
1971-
1975+
19721976
// First, try to parse it to validate it's valid JSON
19731977
const parsed = JSON.parse(cleanElement);
19741978
// Then stringify it back to ensure consistent formatting
@@ -1986,36 +1990,38 @@ const handleArrayDefault = (columnDefaultAsString: string, dataType: string): st
19861990
// Handle common simple cases
19871991
if (columnDefaultAsString === '{}' || columnDefaultAsString === "'{}'") {
19881992
return "'{}'";
1989-
} else if (columnDefaultAsString === '{""}' || columnDefaultAsString === "'{\"\"}'") {
1990-
return "'{\"\"}'";
1993+
} else if (columnDefaultAsString === '{""}' || columnDefaultAsString === '\'{""}\'') {
1994+
return '\'{""}\'';
19911995
}
19921996

19931997
// Convert ARRAY constructor syntax to PostgreSQL bracket notation that postgres-array can parse
19941998
let normalizedArrayString = columnDefaultAsString;
1995-
1999+
19962000
if (columnDefaultAsString.startsWith('ARRAY[') && columnDefaultAsString.endsWith(']')) {
19972001
// Convert ARRAY['a'::text, 'b', 'c'::varchar] -> {'a', 'b', 'c'}
19982002
const content = columnDefaultAsString.slice(6, -1); // Remove 'ARRAY[' and ']'
1999-
2003+
20002004
// Remove type casting from individual elements (::text, ::varchar, etc.)
20012005
const cleanContent = content.replace(/::\w+/g, '');
20022006
normalizedArrayString = `{${cleanContent}}`;
20032007
}
2004-
2008+
20052009
// Handle various bracket notation formats to ensure compatibility with postgres-array
20062010
if (normalizedArrayString.startsWith("'{") && normalizedArrayString.endsWith("}'")) {
20072011
normalizedArrayString = normalizedArrayString.slice(1, -1); // Remove outer quotes
2008-
} else if (!normalizedArrayString.startsWith("{") && !normalizedArrayString.startsWith("'") && normalizedArrayString !== '{}') {
2012+
} else if (
2013+
!normalizedArrayString.startsWith('{') && !normalizedArrayString.startsWith("'") && normalizedArrayString !== '{}'
2014+
) {
20092015
// Handle cases where array string doesn't have proper brackets
20102016
normalizedArrayString = `{${normalizedArrayString}}`;
20112017
}
20122018

20132019
// Use postgres-array library to parse the normalized string
20142020
const parsedArray = [...parsePostgresArray(normalizedArrayString)];
2015-
2021+
20162022
// Format elements according to data type
20172023
const formattedElements = parsedArray.map((element) => formatArrayElement(element, dataType));
2018-
2024+
20192025
return `'{${formattedElements.join(',')}}'`;
20202026
};
20212027

0 commit comments

Comments
 (0)