symfony / recipes

Symfony Recipes Repository
https://github.com/symfony/recipes/blob/flex/main/RECIPES.md
MIT License
980 stars 482 forks source link

doctrine 2.13 broken after `composer recipes:update doctrine/doctrine-bundle` #1363

Closed RafaelKr closed 1 week ago

RafaelKr commented 1 week ago

Issue

We started a new project with the Sulu CMS (https://github.com/sulu/sulu) using Postgres as our database.

Some time later we ran composer recipes:update and updated doctrine/doctrine-bundle. This update added those 2 lines to the config/packages/doctrine.yaml:

https://github.com/symfony/recipes/blob/b06f1ce2f7c3f7d5cb18fcce68c08a02cc1b271c/doctrine/doctrine-bundle/2.13/config/packages/doctrine.yaml#L17-L18

Now when trying to save anything which requires the generation of an identifier (creating a new entity, etc) we get errors like: An exception occurred while executing a query: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" of relation "the_table" violates not-null constraint

It seems like setting this config is only required after migrating to doctrine/dbal version 4: https://www.doctrine-project.org/projects/doctrine-dbal/en/stable/how-to/postgresql-identity-migration.html#migration-to-identity-columns-on-postgresql

The Sulu project still uses doctrine/dbal version 3.93

Reproduction

This can be quickly (~5 min) reproduced with the following steps:

# Open a terminal, I call it _Postgres Terminal_
# Create Postgres database, conveniently with docker. Any other way should do it also
docker run --name sulu-postgres -e POSTGRES_PASSWORD=mysecretpassword -p127.0.0.1:54321:5432 -it postgres:16

# Open a new terminal (_Sulu Terminal_)
# Install Sulu from the getting started guide: https://docs.sulu.io/en/2.6/book/getting-started.html
# I added COMPOSER_MEMORY_LIMIT=-1 because I got `Allowed memory size of 134217728 bytes exhausted` (128 MiB)
COMPOSER_MEMORY_LIMIT=-1 composer create-project sulu/skeleton sulu-doctrine-recipe-failure -n
cd sulu-doctrine-recipe-failure

# Setup our database connection
echo 'DATABASE_URL="postgresql://postgres:mysecretpassword@127.0.0.1:54321/postgres?serverVersion=16&charset=utf8"' >> .env.local

# Initialize Sulu and migrate our database
# Press enter when it asks "Look good?"
bin/adminconsole sulu:build dev

# Open a new terminal (_Server Terminal_) to serve the application
php -S localhost:8000 -t public/ config/router.php

If we now go back via the toolbar button the Snippet "Foo" should appear in the list.

To be able to do a recipe update we need to have a git repository. Initialize via inside the Sulu Terminal

git init
git add .
git commit -m "Initial commit"

Now we run (in the Sulu Terminal)

composer recipes:update doctrine/doctrine-bundle
bin/console cache:clear

git diff --staged config/packages/doctrine.yaml

The output of git diff should show the new key identity_generation_preferences.

If we now repeat the process of creating a snippet from above with the new name "Bar" Sulu will show "Error - There was an error when trying to save the form".

The network tab and the docker database logs (in the Postgres Terminal) will show a not-null constraint error: ERROR: null value in column "id" of relation "ac_activities" violates not-null constraint

Clean up

Clean up docker container and its data (volume)

docker rm -v sulu-postgres

Delete postgres docker image

docker rmi postgres:16


- In the _Sulu Terminal_
```bash
cd ..
rm -rf sulu-doctrine-recipe-failure
RafaelKr commented 1 week ago

The sulu developers cleared this up to me. As we're just starting out we're able to clear our database and start it from scratch with the new identity default setting. https://github.com/sulu/sulu/issues/7663#issuecomment-2490358163