Skip to content

Commit 0758199

Browse files
authored
fix(db-postgres, db-sqlite): enum schema (#7453)
- updates drizzle-kit and drizzle-orm - fix enum creation to fully support custom schemas - sqlite by default will not use transactions
1 parent 1ec78a1 commit 0758199

File tree

23 files changed

+173
-266
lines changed

23 files changed

+173
-266
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ jobs:
196196
- postgres-custom-schema
197197
- postgres-uuid
198198
- supabase
199+
- sqlite
199200
env:
200201
POSTGRES_USER: postgres
201202
POSTGRES_PASSWORD: postgres

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"create-payload-app": "workspace:*",
120120
"cross-env": "7.0.3",
121121
"dotenv": "16.4.5",
122-
"drizzle-orm": "0.29.4",
122+
"drizzle-orm": "0.32.1",
123123
"escape-html": "^1.0.3",
124124
"execa": "5.1.1",
125125
"form-data": "3.0.1",

packages/db-postgres/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"dependencies": {
4848
"@payloadcms/drizzle": "workspace:*",
4949
"console-table-printer": "2.11.2",
50-
"drizzle-kit": "0.20.14-1f2c838",
51-
"drizzle-orm": "0.29.4",
50+
"drizzle-kit": "0.23.1-7816536",
51+
"drizzle-orm": "0.32.1",
5252
"pg": "8.11.3",
5353
"prompts": "2.4.2",
5454
"to-snake-case": "1.0.0",

packages/db-postgres/src/connect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const connect: Connect = async function connect(
5353
const { hotReload } = options
5454

5555
this.schema = {
56+
pgSchema: this.pgSchema,
5657
...this.tables,
5758
...this.relations,
5859
...this.enums,

packages/db-postgres/src/createMigration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export const createMigration: CreateMigration = async function createMigration(
4949

5050
let drizzleJsonBefore = defaultDrizzleSnapshot
5151

52+
if (this.schemaName) {
53+
drizzleJsonBefore.schemas = {
54+
[this.schemaName]: this.schemaName,
55+
}
56+
}
57+
5258
if (!upSQL) {
5359
// Get latest migration snapshot
5460
const latestSnapshot = fs

packages/db-postgres/src/defaultSnapshot.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ export const defaultDrizzleSnapshot: DrizzleSnapshotJSON = {
77
schemas: {},
88
tables: {},
99
},
10-
dialect: 'pg',
10+
dialect: 'postgresql',
1111
enums: {},
1212
prevId: '00000000-0000-0000-0000-00000000000',
1313
schemas: {},
14+
sequences: {},
1415
tables: {},
15-
version: '5',
16+
version: '7',
1617
}

packages/db-postgres/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
updateOne,
3333
updateVersion,
3434
} from '@payloadcms/drizzle'
35+
import { type PgSchema, pgEnum, pgSchema, pgTable } from 'drizzle-orm/pg-core'
3536
import { createDatabaseAdapter } from 'payload'
3637

3738
import type { Args, PostgresAdapter } from './types.js'
@@ -62,12 +63,19 @@ export function postgresAdapter(args: Args): DatabaseAdapterObj<PostgresAdapter>
6263
const migrationDir = findMigrationDir(args.migrationDir)
6364
let resolveInitializing
6465
let rejectInitializing
66+
let adapterSchema: PostgresAdapter['pgSchema']
6567

6668
const initializing = new Promise<void>((res, rej) => {
6769
resolveInitializing = res
6870
rejectInitializing = rej
6971
})
7072

73+
if (args.schemaName) {
74+
adapterSchema = pgSchema(args.schemaName)
75+
} else {
76+
adapterSchema = { enum: pgEnum, table: pgTable }
77+
}
78+
7179
return createDatabaseAdapter<PostgresAdapter>({
7280
name: 'postgres',
7381
defaultDrizzleSnapshot,
@@ -83,7 +91,7 @@ export function postgresAdapter(args: Args): DatabaseAdapterObj<PostgresAdapter>
8391
localesSuffix: args.localesSuffix || '_locales',
8492
logger: args.logger,
8593
operators: operatorMap,
86-
pgSchema: undefined,
94+
pgSchema: adapterSchema,
8795
pool: undefined,
8896
poolOptions: args.pool,
8997
push: args.push,

packages/db-postgres/src/init.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Init, SanitizedCollectionConfig } from 'payload'
22

33
import { createTableName } from '@payloadcms/drizzle'
4-
import { pgEnum, pgSchema, pgTable } from 'drizzle-orm/pg-core'
54
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'
65
import toSnakeCase from 'to-snake-case'
76

@@ -10,13 +9,8 @@ import type { PostgresAdapter } from './types.js'
109
import { buildTable } from './schema/build.js'
1110

1211
export const init: Init = function init(this: PostgresAdapter) {
13-
if (this.schemaName) {
14-
this.pgSchema = pgSchema(this.schemaName)
15-
} else {
16-
this.pgSchema = { table: pgTable }
17-
}
1812
if (this.payload.config.localization) {
19-
this.enums.enum__locales = pgEnum(
13+
this.enums.enum__locales = this.pgSchema.enum(
2014
'_locales',
2115
this.payload.config.localization.locales.map(({ code }) => code) as [string, ...string[]],
2216
)

packages/db-postgres/src/schema/createIndex.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-param-reassign */
21
import { index, uniqueIndex } from 'drizzle-orm/pg-core'
32

43
import type { GenericColumn } from '../types.js'

packages/db-postgres/src/schema/traverseFields.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
integer,
1919
jsonb,
2020
numeric,
21-
pgEnum,
2221
text,
2322
timestamp,
2423
varchar,
@@ -237,7 +236,7 @@ export const traverseFields = ({
237236
throwValidationError,
238237
})
239238

240-
adapter.enums[enumName] = pgEnum(
239+
adapter.enums[enumName] = adapter.pgSchema.enum(
241240
enumName,
242241
field.options.map((option) => {
243242
if (optionIsObject(option)) {

0 commit comments

Comments
 (0)