I recently had to run a couple of migrations to move entries from one structure to the other, but had problems when running the migration PHP script being able to get environment variables e.g. DB and stuff in .env. In the end I temporarily replaced my craft/config/db.php and hard coded my DB on local so I could run the script, however this isn't really ideal but did the job. It seemed the script couldn't get the DB environment variables.
I'm wondering what should I be including in my bootstrap migration scripts going forward in order to utilise the environment variables properly. I used a version of the script here:
Move entry from one structure to another with parenting
Slightly modified to take an array of ids, as well as mapping the old and new entry types as an arry, so it could be done in one go.
I essentially tried copying most of the beginning portion of the index.php like this and transplanting it into my migration script
// Composer: https://getcomposer.org/doc/ require_once('../vendor/autoload.php'); // PHP dotenv: https://github.com/vlucas/phpdotenv if (!getenv('APP_SECRETS')) { $root_dir = dirname(dirname(__FILE__)); if (file_exists($root_dir . '/.env')) { Dotenv::load($root_dir); Dotenv::required(array('APP_SECRETS', 'CRAFT_ENVIRONMENT')); } } define('CRAFT_ENVIRONMENT', getenv('CRAFT_ENVIRONMENT')); However, no matter what I just got a die() error from craft/local/db.php saying the path to the file containing the DB crendentials (secrets.json) couldn't be found, so it seems the environment variables weren't available at runtime. APP_SECRETS is the path to the json file.
Any ideas, what I'm missing for environment variables to be loaded in?
Thanks.
index.phpas a reference and remove the$app->run()part in order to not start Craft? I don't see any benefit in your way at all. You can even run Craft normally and create a migration file or start your script inside a module / plugin.bootstrap.phpwas the preferred mechanism to do migrations like this. Given it ships with Craft.index.phpto yourbootstrap.phpand removing the$app->runfrom yourindex.php. You need most parts of the bootstrap anyway so you can't remove that much from it. Thus you can just remove therunfunction from yourindex.phpand that's basically itbootstrap.phpwas preferred method, but if the same can be achieved through the content in theindex.phpand just removing the Craft app run part, I guess I can just use that!