dgrigg / Craft-Migration-Manager

Deprecated - Migration Manager for Craft CMS
MIT License
39 stars 15 forks source link

Migration fails when using Environment variables #54

Closed jordikroon closed 5 years ago

jordikroon commented 5 years ago

Execution of migrations fails when using environment variables. This is because they behave like PHP variables when surrounded with double quotes.

Possible solutions are to use single quotes instead, or escape variables with a backslash (\)

dgrigg commented 5 years ago

@jordikroon can you provide an example of where a migration is failing? That would help in fixing the issue.

jordikroon commented 5 years ago

We use Amazon S3 for storing our images. Our settings are stored in the .env file. Same can be applied for the (default) local image adapter.

screenshot 2019-02-21 at 10 40 20 am

The screenshot above generates this migration. Where as you can see the variables behave like normal PHP variables.


<?php

namespace craft\contentmigrations;

use craft\db\Migration;
//use craft\db\MigrationManager;
//use craft\helpers\MigrationHelper;

use firstborn\migrationmanager\MigrationManager;

/**
 * Generated migration
 */
class m190221_104420_migration_assetVolume_default extends Migration
{
    /**
    Migration manifest:

    ASSETVOLUME
        - default
            */

private $json = <<<JSON
{"settings":{"dependencies":{"assetVolumes":[{"name":"Default","handle":"default","type":"craft\\\\awss3\\\\Volume","sortOrder":null,"typesettings":{"subfolder":"","keyId":"$AWS_ACCESS_KEY_ID","secret":"$AWS_ACCESS_KEY_TOKEN","bucket":"racingnews-v2-dev","region":"eu-west-1","expires":"","storageClass":"","cfDistributionId":"","autoFocalPoint":""},"hasUrls":1,"url":"$S3_BASE_URL"}]},"elements":{"assetVolumes":[{"name":"Default","handle":"default","type":"craft\\\\awss3\\\\Volume","sortOrder":null,"typesettings":{"subfolder":"","keyId":"$AWS_ACCESS_KEY_ID","secret":"$AWS_ACCESS_KEY_TOKEN","bucket":"racingnews-v2-dev","region":"eu-west-1","expires":"","storageClass":"","cfDistributionId":"","autoFocalPoint":""},"hasUrls":1,"url":"$S3_BASE_URL","fieldLayout":{"Content":["subTitle"]}}]}}}
JSON;

    /**
     * Any migration code in here is wrapped inside of a transaction.
     * Returning false will rollback the migration
     *
     * @return bool
     */
    public function safeUp()
    {
        return MigrationManager::getInstance()->migrations->import($this->json);    }

    public function safeDown()
    {
        echo "m190221_104420_migration_assetVolume_default cannot be reverted.\n";
        return false;
    }
}
dgrigg commented 5 years ago

@jordikroon fixed in latest release