|
1 | 1 | import type { Payload } from 'payload'
|
2 | 2 | import type { Connect } from 'payload/database'
|
3 | 3 |
|
4 |
| -import { eq, sql } from 'drizzle-orm' |
| 4 | +import { sql } from 'drizzle-orm' |
5 | 5 | import { drizzle } from 'drizzle-orm/node-postgres'
|
6 |
| -import { numeric, timestamp, varchar } from 'drizzle-orm/pg-core' |
7 | 6 | import { Pool } from 'pg'
|
8 |
| -import prompts from 'prompts' |
9 | 7 |
|
10 | 8 | import type { PostgresAdapter } from './types'
|
11 | 9 |
|
| 10 | +import { pushDevSchema } from './utilities/pushDevSchema' |
| 11 | + |
12 | 12 | const connectWithReconnect = async function ({
|
13 | 13 | adapter,
|
14 | 14 | payload,
|
@@ -48,6 +48,7 @@ const connectWithReconnect = async function ({
|
48 | 48 |
|
49 | 49 | export const connect: Connect = async function connect(this: PostgresAdapter, payload) {
|
50 | 50 | this.schema = {
|
| 51 | + pgSchema: this.pgSchema, |
51 | 52 | ...this.tables,
|
52 | 53 | ...this.relations,
|
53 | 54 | ...this.enums,
|
@@ -77,76 +78,10 @@ export const connect: Connect = async function connect(this: PostgresAdapter, pa
|
77 | 78 |
|
78 | 79 | // Only push schema if not in production
|
79 | 80 | if (
|
80 |
| - process.env.NODE_ENV === 'production' || |
81 |
| - process.env.PAYLOAD_MIGRATING === 'true' || |
82 |
| - this.push === false |
83 |
| - ) |
84 |
| - return |
85 |
| - |
86 |
| - const { pushSchema } = require('drizzle-kit/payload') |
87 |
| - |
88 |
| - // This will prompt if clarifications are needed for Drizzle to push new schema |
89 |
| - const { apply, hasDataLoss, statementsToExecute, warnings } = await pushSchema( |
90 |
| - this.schema, |
91 |
| - this.drizzle, |
92 |
| - ) |
93 |
| - |
94 |
| - if (warnings.length) { |
95 |
| - let message = `Warnings detected during schema push: \n\n${warnings.join('\n')}\n\n` |
96 |
| - |
97 |
| - if (hasDataLoss) { |
98 |
| - message += `DATA LOSS WARNING: Possible data loss detected if schema is pushed.\n\n` |
99 |
| - } |
100 |
| - |
101 |
| - message += `Accept warnings and push schema to database?` |
102 |
| - |
103 |
| - const { confirm: acceptWarnings } = await prompts( |
104 |
| - { |
105 |
| - name: 'confirm', |
106 |
| - type: 'confirm', |
107 |
| - initial: false, |
108 |
| - message, |
109 |
| - }, |
110 |
| - { |
111 |
| - onCancel: () => { |
112 |
| - process.exit(0) |
113 |
| - }, |
114 |
| - }, |
115 |
| - ) |
116 |
| - |
117 |
| - // Exit if user does not accept warnings. |
118 |
| - // Q: Is this the right type of exit for this interaction? |
119 |
| - if (!acceptWarnings) { |
120 |
| - process.exit(0) |
121 |
| - } |
122 |
| - } |
123 |
| - |
124 |
| - await apply() |
125 |
| - |
126 |
| - // Migration table def in order to use query using drizzle |
127 |
| - const migrationsSchema = this.pgSchema.table('payload_migrations', { |
128 |
| - name: varchar('name'), |
129 |
| - batch: numeric('batch'), |
130 |
| - created_at: timestamp('created_at'), |
131 |
| - updated_at: timestamp('updated_at'), |
132 |
| - }) |
133 |
| - |
134 |
| - const devPush = await this.drizzle |
135 |
| - .select() |
136 |
| - .from(migrationsSchema) |
137 |
| - .where(eq(migrationsSchema.batch, '-1')) |
138 |
| - |
139 |
| - if (!devPush.length) { |
140 |
| - await this.drizzle.insert(migrationsSchema).values({ |
141 |
| - name: 'dev', |
142 |
| - batch: '-1', |
143 |
| - }) |
144 |
| - } else { |
145 |
| - await this.drizzle |
146 |
| - .update(migrationsSchema) |
147 |
| - .set({ |
148 |
| - updated_at: new Date(), |
149 |
| - }) |
150 |
| - .where(eq(migrationsSchema.batch, '-1')) |
| 81 | + process.env.NODE_ENV !== 'production' && |
| 82 | + process.env.PAYLOAD_MIGRATING !== 'true' && |
| 83 | + this.push !== false |
| 84 | + ) { |
| 85 | + await pushDevSchema(this) |
151 | 86 | }
|
152 | 87 | }
|
0 commit comments