Skip to content

Commit

Permalink
Fix collection config not being updated for database restore
Browse files Browse the repository at this point in the history
  • Loading branch information
elliott-w committed Nov 12, 2024
1 parent 9d0f581 commit cf53c80
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 0 additions & 2 deletions dev/src/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export interface Page {
export interface Media {
id: string;
text?: string | null;
createdDuringDevelopment?: boolean | null;
updatedAt: string;
createdAt: string;
url?: string | null;
Expand Down Expand Up @@ -201,7 +200,6 @@ export interface PagesSelect<T extends boolean = true> {
*/
export interface MediaSelect<T extends boolean = true> {
text?: T;
createdDuringDevelopment?: T;
updatedAt?: T;
createdAt?: T;
url?: T;
Expand Down
30 changes: 20 additions & 10 deletions src/lib/api-endpoints/switch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DatabaseAdapter, Endpoint, PayloadRequest } from 'payload'
import type { DatabaseAdapter, Endpoint, PayloadRequest } from 'payload'
import { getEnv, setEnv } from '../env'
import { backup, restore } from '../db/mongo'
import { formatFileSize } from '../utils'
import { type GetDatabaseAdapter } from '../db/getDbaFunction'
import type { GetDatabaseAdapter } from '../db/getDbaFunction'
import {
addDevelopmentSettingsToUploadCollection,
removeDevelopmentSettingsFromUploadCollection,
Expand Down Expand Up @@ -53,6 +53,24 @@ export const switchEndpoint = ({ getDatabaseAdapter }: SwitchEndpointArgs): Endp
await req.payload.db.destroy()
}

req.payload.config.collections = Object.values(req.payload.config.collections || []).map(
(collection) => {
if (newEnv === 'development') {
return addDevelopmentSettingsToUploadCollection(collection)
} else {
return removeDevelopmentSettingsFromUploadCollection(collection)
}
},
)

Object.values(req.payload.collections || {}).forEach((collection) => {
if (newEnv === 'development') {
collection.config = addDevelopmentSettingsToUploadCollection(collection.config)
} else {
collection.config = removeDevelopmentSettingsFromUploadCollection(collection.config)
}
})

const newDb = getDatabaseAdapter().init({ payload: req.payload })
req.payload.db = newDb as unknown as DatabaseAdapter
req.payload.db.payload = req.payload
Expand All @@ -70,14 +88,6 @@ export const switchEndpoint = ({ getDatabaseAdapter }: SwitchEndpointArgs): Endp
await restore(req.payload.db.connection, backupString, req.payload.logger)
}

Object.entries(req.payload.collections || {}).forEach(([slug, collection]) => {
if (newEnv === 'development') {
collection.config = addDevelopmentSettingsToUploadCollection(collection.config)
} else {
collection.config = removeDevelopmentSettingsFromUploadCollection(collection.config)
}
})

logger.info('Switched to ' + newEnv + ' environment')

const res: SwitchEndpointOutput = {
Expand Down

0 comments on commit cf53c80

Please sign in to comment.