mardi 27 février 2018

how to make a laravel command take --env option into account

Background

I created a Laravel command that interacts with the database:

    try {
        // Get Point coordinates for each user location vote in the database 
        $result = DB::select( // blah blah

My intention is to have this command available locally, but then being able to pass it the --env=prod option so that it can interact with the production database. This works perfectly fine for example with the orangehill/iseed library where I can run a command like so:

iseed <table_name> --env=prod

side note

my prod db is RDS, hiding behind a vpc firewall. So what I did is create an ssh tunnel like so:

ssh -N -L 6666:rds.host.com:5432 user@ec2ip -i pub_file.pub"

I can easily connect to the database using command line then like so:

PGPASSWORD=password psql productiondb -U productionuser -h 127.0.0.1 -p 6666;

likewise, I have the same credentials in my .env.prod file:

APP_ENV=prod
..
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_DATABASE=productiondb
DB_USERNAME=productionuser
DB_PASSWORD=password
DB_PORT=6666

so when i run iseed.. it reads just fine from the above and everyone is happy

problem

When I run my command on localhost like so:

$ php artisan toters:getUserVotesByLocation 63 --env=prod
Counting Votes Started @ 1519664089


  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[08006] [7] FATAL:  role "productionuser" does not exist

it blows up! the weird part is that it kind of reads from the prod env settings (it figured it that it's using productionuser user, but it seems that the other paramters are not being read.. ie if i try to login to local db using production user i'll get the same error message:

$ psql database -U productionuser // defaults to localhost
psql: FATAL:  role "productionuser" does not exist

How do I fix this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2EYJE3X
via IFTTT

Aucun commentaire:

Enregistrer un commentaire