Skip to content

Commit edfa85b

Browse files
DanRibbensjmikrutPatrikKozak
authored
feat(db-postgres)!: relationship column (#6339)
BREAKING CHANGE: Moves `upload` field and `relationship` fields with `hasMany: false` & `relationTo: string` from the many-to-many `_rels` join table to simple columns. This only affects Postgres database users. ## TL;DR We have dramatically simplified the storage of simple relationships in relational databases to boost performance and align with more expected relational paradigms. If you are using the beta Postgres adapter, and you need to keep simple relationship data, you'll need to run a migration script that we provide you. ### Background For example, prior to this update, a collection of "posts" with a simple `hasMany: false` and `relationTo: 'categories'` field would have a `posts_rels` table where the category relations would be stored. This was somewhat unnecessary as simple relations like this can be expressed with a `category_id` column which is configured as a foreign key. This also introduced added complexity for dealing directly with the database if all you have are simple relations. ### Who needs to migrate You need to migrate if you are using the beta Postgres database adapter and any of the following applies to you. - If you have versions enabled on any collection / global - If you use the `upload` field - If you have relationship fields that are `hasMany: false` (default) and `relationTo` to a single category ([has one](https://payloadcms.com/docs/fields/relationship#has-one)) relations ### We have a migration for you Even though the Postgres adapter is in beta, we've prepared a predefined migration that will work out of the box for you to migrate from an earlier version of the adapter to the most recent version easily. It makes the schema changes in step with actually moving the data from the old locations to the new before adding any null constraints and dropping the old columns and tables. ### How to migrate The steps to preserve your data while making this update are as follows. These steps are the same whether you are moving from Payload v2 to v3 or a previous version of v3 beta to the most recent v3 beta. **Important: during these steps, don't start the dev server unless you have `push: false` set on your Postgres adapter.** #### Step 1 - backup Always back up your database before performing big changes, especially in production cases. #### Step 2 - create a pre-update migration Before updating to new Payload and Postgres adapter versions, run `payload migrate:create` without any other config changes to have a prior snapshot of the schema from the previous adapter version #### Step 3 - if you're migrating a dev DB, delete the dev `push` row from your `payload_migrations` table If you're migrating a dev database where you have the default setting to push database changes directly to your DB, and you need to preserve data in your development database, then you need to delete a `dev` migration record from your database. Connect directly to your database in any tool you'd like and delete the dev push record from the `payload_migrations` table using the following SQL statement: ```sql DELETE FROM payload_migrations where batch = -1` ``` #### Step 4 - update Payload and Postgres versions to most recent Update packages, making sure you have matching versions across all `@payloadcms/*` and `payload` packages (including `@payloadcms/db-postgres`) #### Step 5 - create the predefined migration Run the following command to create the predefined migration we've provided: ``` payload migrate:create --file @payloadcms/db-postgres/relationships-v2-v3 ``` #### Step 6 - migrate! Run migrations with the following command: ``` payload migrate ``` Assuming the migration worked, you can proceed to commit this change and distribute it to be run on all other environments. Note that if two servers connect to the same database, only one should be running migrations to avoid transaction conflicts. Related discussion: #4163 --------- Co-authored-by: James <james@trbl.design> Co-authored-by: PatrikKozak <patrik@payloadcms.com>
1 parent b86d4c6 commit edfa85b

File tree

163 files changed

+4364
-621
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+4364
-621
lines changed

examples/form-builder/next-app/components/Blocks/Form/Country/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CountryField } from 'payload-plugin-form-builder/dist/types'
2-
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
2+
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'
33

44
import React from 'react'
55
import { Controller } from 'react-hook-form'

examples/form-builder/next-app/components/Blocks/Form/Select/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SelectField } from 'payload-plugin-form-builder/dist/types'
2-
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
2+
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'
33

44
import React from 'react'
55
import { Controller } from 'react-hook-form'

examples/form-builder/next-app/components/Blocks/Form/State/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StateField } from 'payload-plugin-form-builder/dist/types'
2-
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
2+
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'
33

44
import React from 'react'
55
import { Controller } from 'react-hook-form'

examples/form-builder/next-app/components/CloseModalOnRouteChange/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client'
2-
import type React from 'react';
2+
import type React from 'react'
33

44
import { useModal } from '@faceless-ui/modal'
55
import { usePathname } from 'next/navigation'

examples/form-builder/next-app/components/Gutter/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Ref } from 'react';
1+
import type { Ref } from 'react'
22

33
import React, { forwardRef } from 'react'
44

examples/live-preview/next-app/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"dev": "next dev -p 3001",
76
"build": "next build",
8-
"start": "next start -p 3001",
9-
"lint": "next lint"
7+
"dev": "next dev -p 3001",
8+
"lint": "next lint",
9+
"start": "next start -p 3001"
1010
},
1111
"dependencies": {
1212
"@payloadcms/live-preview-react": "3.0.0-beta.28",

examples/live-preview/next-pages/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"dev": "next dev -p 3001",
76
"build": "next build",
8-
"start": "next start -p 3001",
9-
"lint": "next lint"
7+
"dev": "next dev -p 3001",
8+
"lint": "next lint",
9+
"start": "next start -p 3001"
1010
},
1111
"dependencies": {
1212
"@payloadcms/live-preview-react": "3.0.0-beta.28",

examples/live-preview/payload/src/app/(app)/_components/Button/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ElementType } from 'react';
1+
import type { ElementType } from 'react'
22

33
import Link from 'next/link'
44
import React from 'react'

examples/live-preview/payload/src/app/(app)/_components/Gutter/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Ref } from 'react';
1+
import type { Ref } from 'react'
22

33
import React, { forwardRef } from 'react'
44

examples/live-preview/payload/src/fields/link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const link: LinkType = ({ appearances, disableLabel = false, overrides = {} } =
125125
]
126126

127127
if (appearances) {
128-
appearanceOptionsToUse = appearances.map(appearance => appearanceOptions[appearance])
128+
appearanceOptionsToUse = appearances.map((appearance) => appearanceOptions[appearance])
129129
}
130130

131131
linkResult.fields.push({

0 commit comments

Comments
 (0)