-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(db-postgres)!: relationship column #6339
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DanRibbens
force-pushed
the
feat/postgres-relationship-column
branch
from
May 28, 2024 15:34
61051b2
to
d97bacf
Compare
denolfe
changed the title
feat: postgres relationship column
feat(db-postgres): relationship column
May 30, 2024
denolfe
changed the title
feat(db-postgres): relationship column
feat(db-postgres)!: relationship column
May 30, 2024
DanRibbens
pushed a commit
that referenced
this pull request
Nov 13, 2024
### What? This command from here: #6339 ```sh payload migrate:create --file @payloadcms/db-postgres/relationships-v2-v3 ``` stopped working after db-postgers and drizzle packages were separated ### How? Passes correct `dirname` to `getPredefinedMigration` Additionally, adds support for `.js` files in `getPredefinedMigration`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BREAKING CHANGE:
Moves
upload
field andrelationship
fields withhasMany: 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
andrelationTo: 'categories'
field would have aposts_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.
upload
fieldhasMany: false
(default) andrelationTo
to a single category (has one) relationsWe 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 versionStep 3 - if you're migrating a dev DB, delete the dev
push
row from yourpayload_migrations
tableIf 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:Step 4 - update Payload and Postgres versions to most recent
Update packages, making sure you have matching versions across all
@payloadcms/*
andpayload
packages (including@payloadcms/db-postgres
)Step 5 - create the predefined migration
Run the following command to create the predefined migration we've provided:
Step 6 - migrate!
Run migrations with the following command:
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