Skip to content
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

Make doctrine role a database-level setting #2

Conversation

rinkp
Copy link

@rinkp rinkp commented Oct 29, 2024

No description provided.

Adds support for migrations while using Doctrine ORM. The initial migration is
based on the current `main`.

Normally, a `migrations:rollup` would be used to ensure that this migration is
successful. However, in our case it is necessary to export ALL data (but not
structure) from the database, drop everything in the database, and only then
perform the migration. Data can be easily exported from PostgreSQL using:

```sh
pg_dump -U gewisdb --column-inserts --data-only gewisdb
pg_dump -U gewisdb --column-inserts --data-only gewisdb_report
```

This ensures that _all_ FKs are the same for newer migrations preventing
headaches in the future.

---

Convenience methods have been added for `make` to make using the migrations
easier. However, in production everything must go directly through `./orm`.
Initially, I attempted to use the `wrapperClass` option to handle setting the
database role. However, I encountered issues because DBAL checks for the exact
`Connection` class type and not the interface, making it difficult to extend the
`Connection` class as needed.

I also considered overwriting the PgSQL-specific connection class (PDO variant)
to set the role upon connection. Unfortunately, this was not an option because
the class is declared as `final`, preventing me from extending it.

The next potential solution was to use an `EventSubscriber` to set the role
after the connection was established (using `postConnect`). However, this
approach is already deprecated in our version of DBAL and completely removed in
the next major release, rendering it unsuitable for us (maintainability).

Ultimately, I implemented the `SET ROLE` functionality using DBAL's middleware.
By wrapping the driver, and manually creating the `Connection` we can perform
the `SET ROLE` query before the connection is used by the application.

Runtime checks exist to ensure that the role (`DOCTRINE_ROLE`) is defined.
However, validation of the actual value is done by PostgreSQL itself (it will
complain if the role does not exist).
@rinkp rinkp force-pushed the pr/tomudding-doctrine-migrations branch from dcc9d4f to 3430fa5 Compare October 29, 2024 14:15
@tomudding tomudding force-pushed the chore/doctrine-migrations branch from fe02177 to f80419f Compare November 2, 2024 12:38
@tomudding
Copy link
Owner

f80419f

@tomudding tomudding closed this Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants