0

I'm trying to setup environment in which several Symfony REST API projects (microservices actually) live in single Postgres database but occupy different schemas in DB.

There is no problem to use @ORM\Table to define schema but I found out that migration_versions table is still living in public schema. And blindly hitting 'Yes' while applying migration in the second project just removes the table for the first one.

Of course, I could manually trim generated migration class not to allow execute DROP TABLE statement. But is it possible to setup Doctrine to use custom schema for storing migration_versions table thus completely isolating one project from the others within a single database?

1

2 Answers 2

2

According to symfony Documentation, you can name migrations table on each of your project, maybe using the same prefix you use to separate your tables.

 # config/packages/doctrine_migrations.yaml
doctrine_migrations:
    dir_name: '%kernel.project_dir%/src/Migrations'
    namespace: DoctrineMigrations
    table_name: 'migration_versions' // Use a custom name for each application ? 
    column_name: 'version'
    column_length: 14
    executed_at_column_name: 'executed_at'
    name: 'Application Migrations'
    # available in version >= 1.2. Possible values: "BY_YEAR", "BY_YEAR_AND_MONTH", false
    organize_migrations: false
    # available in version >= 1.3. Path to your custom migrations template
    custom_template: ~
    all_or_nothing: false

https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html

Let's say your project tables start with myproject_ prefix. To prevent migrations from removing your other tables, you can use doctrine dbal schema_filter property in your config.yml/doctrine.yml

config.yml

doctrine:
    dbal:
        schema_filter: "/^myproject_/" 

According to symfony documentation : # If set to "/^sf2_/" all tables not prefixed with "sf2_" will be ignored by the schema tool. This is for custom tables which should not be altered automatically.

So that should do the trick for your use case

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! I'll try to use dot-notation in table_name property to move migration_versions in desired schema. Thanks again!
Hmm... It may be strange by Doctrine generates DROP TABLE statements for neighbor project. Both for entity table and migraion_versions_<project_name>. Again, it could be wiped out manually. But why does Doctrine care too much about tables not related to current project?
Look at the last section of the documentation about the doctrine dbal filter property. It may do the trick if you think of a regular expression that says : any table name not starting with my project prefix ...
Looks like it works well only for the first time but when I switch back to first project and start adding properties to an entity and ./bin/console make:migration it could not generate it because Duplicate table: 7 ERROR: relation "migration_versions_first" already exists . "first" is a postfix for migration_versions table and prefix for entity tables created in "first" schema. Need some debugging trying to find out the actual reason.
Same situation with the second experiment project: while make:migration it wants to re-create migration_versions_<project_postfix> table despite of table_name properties set in doctrine-migrations.yaml
|
0

For Doctrine Migration v3.x take care configuration has changed !

table_name: your_table_name

as been replaced by

table_storage:
    table_name: your_table_name

Doctrine migration documentation here : https://www.doctrine-project.org/projects/doctrine-migrations/en/3.3/reference/configuration.html

If you use doctrine-migration-bundle it will looks like

doctrine_migrations:
    storage:
        table_storage:
            table_name: your_table_name

Doctrine migration bundle documentation here : https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html#configuration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.