symfony 4 创建测试数据库
symfony 4 create testing DB
我的 .env
和 .env.test
文件具有不同的 APP_ENV
和 DATABASE_URL
值。
在 .env
我有 DATABASE_URL=pgsql://postgres:asdfasdf@db:5432/real_db
在 .env.test
我有 DATABASE_URL=pgsql://postgres:asdfasdf@db:5432/test_db
但是当我 运行 命令 bin/console doctrine:database:create --if-not-exists --env=test
时,我可以看到名为 default 的连接的消息 数据库 "real_db" 已经存在。已跳过
所以看起来由于某种原因 Symfony cli 无法正确识别环境。
此外,我在 运行ning bin/console doctrine:schema:update --end=dev
时遇到了非常相似的问题。当我 运行 它时,我看到警告消息 [CAUTION] This operation should not be executed in a production environment! 我需要使用 --force
来更新架构.
我该如何解决这个问题,尤其是无法创建测试数据库的问题?谢谢!
默认情况下 .env
文件仅适用于 dev
环境。所以 .env.xxx
与任何环境无关,你不能像那样拆分 env
文件,但你需要为每个环境手动 export
环境变量。阅读更多 link https://symfony.com/doc/current/configuration/environments.html and http://fabien.potencier.org/symfony4-best-practices.html
用于测试的变量可以在phpunit.xml
中定义——参见https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests
而 [CAUTION] This operation should not be executed in a production environment!
是一个很好的警告,不用担心,使用 --force
可以防止你犯一些错误。
我的 .env
和 .env.test
文件具有不同的 APP_ENV
和 DATABASE_URL
值。
在 .env
我有 DATABASE_URL=pgsql://postgres:asdfasdf@db:5432/real_db
在 .env.test
我有 DATABASE_URL=pgsql://postgres:asdfasdf@db:5432/test_db
但是当我 运行 命令 bin/console doctrine:database:create --if-not-exists --env=test
时,我可以看到名为 default 的连接的消息 数据库 "real_db" 已经存在。已跳过
所以看起来由于某种原因 Symfony cli 无法正确识别环境。
此外,我在 运行ning bin/console doctrine:schema:update --end=dev
时遇到了非常相似的问题。当我 运行 它时,我看到警告消息 [CAUTION] This operation should not be executed in a production environment! 我需要使用 --force
来更新架构.
我该如何解决这个问题,尤其是无法创建测试数据库的问题?谢谢!
默认情况下 .env
文件仅适用于 dev
环境。所以 .env.xxx
与任何环境无关,你不能像那样拆分 env
文件,但你需要为每个环境手动 export
环境变量。阅读更多 link https://symfony.com/doc/current/configuration/environments.html and http://fabien.potencier.org/symfony4-best-practices.html
用于测试的变量可以在phpunit.xml
中定义——参见https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests
而 [CAUTION] This operation should not be executed in a production environment!
是一个很好的警告,不用担心,使用 --force
可以防止你犯一些错误。