可以在 Symfony2 中使用不同的数据库凭据进行 Doctrine 迁移吗?
Can one use different database credentials for Doctrine migrations in Symfony2?
如何配置 Symfony 的 DoctrineMigrationsBundle
以对其 DoctrineBundle
使用不同的数据库身份验证凭据——或者至少,与应用程序中其他地方使用的连接不同的 DoctrineBundle
连接?
我们希望应用仅以有限的权限连接到数据库,例如无法发出 DDL 命令,例如 CREATE
、ALTER
或 DROP
。但是,迁移将需要执行此类 DDL 命令,因此应以具有提升权限的用户身份进行连接。这可能吗?
是的。只需 define a new entity manager 使用正确的连接详细信息,然后在 运行 迁移命令
时使用该实体管理器
$ php app/console doctrine:migrations:version --em=new_entity_manager
我知道这是一个非常古老的 post,但由于它是在 Google 搜索该主题时显示的那个,我添加了我的解决方案,使用 Symfony 4。
首先,您只需在 config/doctrine.yml
中定义一个新的数据库连接(不需要新的实体管理器):
doctrine:
dbal:
default_connection: default
connections:
default:
# This will be the connection used by the default entity manager
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
migrations:
# This will be the connection used for playing the migrations
url: '%env(resolve:DATABASE_MIGRATIONS_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
orm:
# As usual...
您还必须在 .env
文件或环境变量中使用管理员凭据定义 DATABASE_MIGRATIONS_URL
:
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
DATABASE_URL=postgresql://app_user:app_user_pass@localhost:5432/db
# Database url used for migrations (elevated rights)
DATABASE_MIGRATIONS_URL=postgresql://admin_user:admin_user_pass@localhost:5432/db
###< doctrine/doctrine-bundle ###
然后,只需使用 --db
选项执行迁移,传递新连接的名称:
php bin/console doctrine:migrations:migrate --db=migrations
如何配置 Symfony 的 DoctrineMigrationsBundle
以对其 DoctrineBundle
使用不同的数据库身份验证凭据——或者至少,与应用程序中其他地方使用的连接不同的 DoctrineBundle
连接?
我们希望应用仅以有限的权限连接到数据库,例如无法发出 DDL 命令,例如 CREATE
、ALTER
或 DROP
。但是,迁移将需要执行此类 DDL 命令,因此应以具有提升权限的用户身份进行连接。这可能吗?
是的。只需 define a new entity manager 使用正确的连接详细信息,然后在 运行 迁移命令
时使用该实体管理器$ php app/console doctrine:migrations:version --em=new_entity_manager
我知道这是一个非常古老的 post,但由于它是在 Google 搜索该主题时显示的那个,我添加了我的解决方案,使用 Symfony 4。
首先,您只需在 config/doctrine.yml
中定义一个新的数据库连接(不需要新的实体管理器):
doctrine:
dbal:
default_connection: default
connections:
default:
# This will be the connection used by the default entity manager
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
migrations:
# This will be the connection used for playing the migrations
url: '%env(resolve:DATABASE_MIGRATIONS_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
orm:
# As usual...
您还必须在 .env
文件或环境变量中使用管理员凭据定义 DATABASE_MIGRATIONS_URL
:
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
DATABASE_URL=postgresql://app_user:app_user_pass@localhost:5432/db
# Database url used for migrations (elevated rights)
DATABASE_MIGRATIONS_URL=postgresql://admin_user:admin_user_pass@localhost:5432/db
###< doctrine/doctrine-bundle ###
然后,只需使用 --db
选项执行迁移,传递新连接的名称:
php bin/console doctrine:migrations:migrate --db=migrations