可以在 Symfony2 中使用不同的数据库凭据进行 Doctrine 迁移吗?

egg*_*yal 5 permissions doctrine symfony doctrine-migrations

如何配置 SymfonyDoctrineMigrationsBundle以对其使用不同的数据库身份验证凭据DoctrineBundle——或者至少,DoctrineBundle与应用程序中其他地方使用的连接不同?

我们希望应用程序以有限的权限连接到数据库,例如无法发出 DDL 命令,如 CREATE、ALTER或DROP。但是,迁移将需要执行此类 DDL 命令,因此应以具有提升权限的用户身份进行连接。这可能吗?

sca*_*del 1

我知道这是一篇非常老的帖子,但由于它是在 Google 搜索中显示的有关该主题的帖子,因此我添加了我的解决方案,使用 Symfony 4。

\n\n

首先,您只需定义一个新的数据库连接config/doctrine.yml(不需要新的实体管理器):

\n\n
doctrine:\n    dbal:\n        default_connection: default\n        connections:\n            default:\n                # This will be the connection used by the default entity manager\n                url: \'%env(resolve:DATABASE_URL)%\'\n                driver: \'pdo_pgsql\'\n                server_version: \'11.1\'\n                charset: UTF8\n            migrations:\n                # This will be the connection used for playing the migrations\n                url: \'%env(resolve:DATABASE_MIGRATIONS_URL)%\'\n                driver: \'pdo_pgsql\'\n                server_version: \'11.1\'\n                charset: UTF8\n\n    orm:\n        #\xc2\xa0As usual... \n
Run Code Online (Sandbox Code Playgroud)\n\n

您还必须DATABASE_MIGRATIONS_URL在文件或环境变量中使用管理员凭据进行定义.env:

\n\n
###> doctrine/doctrine-bundle ###\n# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url\nDATABASE_URL=postgresql://app_user:app_user_pass@localhost:5432/db\n#\xc2\xa0Database url used for migrations (elevated rights)\nDATABASE_MIGRATIONS_URL=postgresql://admin_user:admin_user_pass@localhost:5432/db\n###< doctrine/doctrine-bundle ###\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,只需使用以下命令执行迁移--db,并传递新连接的名称:

\n\n
php bin/console doctrine:migrations:migrate --db=migrations\n
Run Code Online (Sandbox Code Playgroud)\n