Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artisan Migrate env param has no effect #14941

Closed
nokios opened this issue Aug 22, 2016 · 11 comments
Closed

Artisan Migrate env param has no effect #14941

nokios opened this issue Aug 22, 2016 · 11 comments

Comments

@nokios
Copy link

nokios commented Aug 22, 2016

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.

@nokios nokios changed the title Artisan Migrate env param has no affect Artisan Migrate env param has no effect Aug 22, 2016
@GrahamCampbell
Copy link
Member

Due to a subtlety in the implementation, this will only work if you run the config cache clear command first.

@nokios
Copy link
Author

nokios commented Aug 22, 2016

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)

@jtallant
Copy link

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.

@srmklive
Copy link
Contributor

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?

@GrahamCampbell
Copy link
Member

5.3 probably. we changed how this worked in 5.2.

@themsaid
Copy link
Member

Hello @srmklive :)

Have you ever got the chance to do a second PR for this with Taylor's feedback in mind?

@srmklive
Copy link
Contributor

@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.

@themsaid
Copy link
Member

@srmklive How about doing this in DetectEnvironment as Taylor suggested?

@srmklive
Copy link
Contributor

@themsaid DetectEnvironment to me seems a better place to do this. I will working on a PR to do this functionality there.

@Tarasovych
Copy link

Tarasovych commented Jan 3, 2019

Still an issue in 5.7.

php artisan config:clear is required, as @GrahamCampbell said.

@claw68
Copy link

claw68 commented Jan 8, 2020

Still an issue in 5.7.

php artisan confgi:clear is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants