Essentially, running artisan --env=testing migrate does not actually choose the testing environment.
We have .env which is for development, but also .env.testing. PHPUnit picks up the settings in .env.testing, but artisan --env=testing migrate does not. It simply uses .env configuration.
I've seen some people solve this by adding a 'testing_*' option in config/database.php, then using artisan migrate --database=testing_*, but this implies you need to always have a testing option in your database.php file, and seems like it shouldn't be that way.
The only way I got migrate to use .env.testing is to run export APP_ENV=testing prior to running the command.
Using laravel 5.3, php7.0, homestead 0.4.4 on vagrant box.
Due to a subtlety in the implementation, this will only work if you run the config cache clear command first.
That doesn't seem to make a difference.
Content of directory
vagrant@homestead:~/book$ ll
total 596
drwxr-xr-x 1 vagrant vagrant 4096 Aug 22 19:22 ./
drwxr-xr-x 1 vagrant vagrant 4096 Aug 18 14:11 ../
drwxr-xr-x 1 vagrant vagrant 4096 Aug 22 13:37 app/
-rw-r--r-- 1 vagrant vagrant 1646 Aug 17 21:25 artisan
drwxr-xr-x 1 vagrant vagrant 4096 Aug 17 21:25 bootstrap/
-rw-r--r-- 1 vagrant vagrant 1418 Aug 18 20:38 composer.json
-rw-r--r-- 1 vagrant vagrant 129540 Aug 18 19:44 composer.lock
drwxr-xr-x 1 vagrant vagrant 4096 Aug 22 15:40 config/
drwxr-xr-x 1 vagrant vagrant 4096 Aug 17 21:25 database/
-rw-r--r-- 1 vagrant vagrant 723 Aug 22 15:36 .env
-rw-r--r-- 1 vagrant vagrant 723 Aug 22 15:40 .env.development
-rw-r--r-- 1 vagrant vagrant 719 Aug 22 19:22 .env.testing
drwxr-xr-x 1 vagrant vagrant 4096 Aug 22 19:22 .git/
-rw-r--r-- 1 vagrant vagrant 61 Aug 17 21:25 .gitattributes
-rw-r--r-- 1 vagrant vagrant 118 Aug 18 20:14 .gitignore
-rw-r--r-- 1 vagrant vagrant 556 Aug 17 21:25 gulpfile.js
drwxr-xr-x 1 vagrant vagrant 4096 Aug 22 19:22 .idea/
-rw-r--r-- 1 vagrant vagrant 684 Aug 18 19:55 _ide_helper_models.php
-rw-r--r-- 1 vagrant vagrant 366429 Aug 18 19:51 _ide_helper.php
-rw-r--r-- 1 vagrant vagrant 400 Aug 17 21:25 package.json
-rw-r--r-- 1 vagrant vagrant 989 Aug 22 19:22 phpunit.xml
drwxr-xr-x 1 vagrant vagrant 4096 Aug 17 21:25 public/
-rw-r--r-- 1 vagrant vagrant 1918 Aug 17 21:25 readme.md
drwxr-xr-x 1 vagrant vagrant 4096 Aug 17 21:25 resources/
drwxr-xr-x 1 vagrant vagrant 4096 Aug 19 15:21 routes/
-rw-r--r-- 1 vagrant vagrant 563 Aug 17 21:25 server.php
drwxr-xr-x 1 vagrant vagrant 4096 Aug 17 21:25 storage/
drwxr-xr-x 1 vagrant vagrant 4096 Aug 18 20:39 tests/
drwxr-xr-x 1 vagrant vagrant 4096 Aug 18 19:44 vendor/
Relevent content of .env.testing file.
vagrant@homestead:~/book$ cat .env.testing | grep DB_
DB_CONNECTION="mysql"
DB_HOST="127.0.0.1"
DB_PORT=3306
DB_DATABASE="book_test"
DB_USERNAME="homestead"
DB_PASSWORD="secret"
Ran artisan config:clear:
vagrant@homestead:~/book$ artisan config:clear
Configuration cache cleared!
Run migrate, and it still uses the settings in .env.
vagrant@homestead:~/book$ artisan --env=testing migrate
Nothing to migrate.
I dropped the tables in the normal db (.env has DB_DATABASE as "book"). and it runs on that db.
vagrant@homestead:~/book$ artisan --env=testing migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2016_08_18_154433_create_books_table
DB Contents
MariaDB [book]> show tables;
+-----------------+
| Tables_in_book |
+-----------------+
| books |
| migrations |
| password_resets |
| users |
+-----------------+
4 rows in set (0.00 sec)
MariaDB [book]> use book_test;
Database changed
MariaDB [book_test]> show tables;
Empty set (0.00 sec)
Should artisan db:seed --env=testing pick up on a .env.testing file as well? It doesn't for me but I thought maybe it just didn't support that. But if this is a problem maybe it's a similar issue.
I was able to replicate the same as reported by @nokios. I will look to add a PR for this.
@GrahamCampbell If i generate a PR for this. Should i do it for 5.1 or 5.3?
5.3 probably. we changed how this worked in 5.2.
Hello @srmklive :)
Have you ever got the chance to do a second PR for this with Taylor's feedback in mind?
@themsaid I tried to do it again. The console kernel doesn't use the loadEnvironment feature. I have to do $this->app->call every time i want to use a different environment file. Although i did not see any major performance issue while calling $this->app call multiple times in PR #15004.
@srmklive How about doing this in DetectEnvironment as Taylor suggested?
@themsaid DetectEnvironment to me seems a better place to do this. I will working on a PR to do this functionality there.
Still an issue in 5.7.
php artisan config:clear is required, as @GrahamCampbell said.
Still an issue in 5.7.
php artisan confgi:clearis required, as @GrahamCampbell said.
This helps but running php artisan config:cache will revert the weird bug. There are no room for testing env variable values in the cached config.
Most helpful comment
Still an issue in 5.7.
php artisan config:clearis required, as @GrahamCampbell said.