cakephp / phinx

PHP Database Migrations for Everyone
https://phinx.org
MIT License
4.45k stars 895 forks source link

Does Phinx support '%%PHINX_%%' variables for environment ports? #2247

Closed AshleyJackson closed 8 months ago

AshleyJackson commented 9 months ago

Hello,

I am currently trying run phinx with the below script in my composer.json

"phinx migrate --configuration \"${GITHUB_WORKSPACE}/build/configs/phinx.github.yml\" -e tests"

My phinx.github.yml looks similar to below

paths:
  migrations: "%%PHINX_CONFIG_DIR%%/../../Checker/migrations"
migration_base_class: \Checker\Phinx\AbstractCheckerMigration
templates:
  file: "%%PHINX_CONFIG_DIR%%/../../Checker/config/phinx/Migration.template.php.dist"
environments:
  default_migration_table: phinxlog
  default_database: tests
  tests:
    adapter: mysql
    host: 127.0.0.1
    name: checker_unittest
    user: root
    pass: ''
    port: '%%PHINX_MYSQL_PORT%%'
    charset: utf8mb4
    collation: utf8mb4_unicode_ci

The above .yml file does not work and complains with the following error:

PDOException: SQLSTATE[HY000] [2002] Connection refused in /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:91
Stack trace:
#0 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(91): PDO->__construct('...', '...', '', Array)
#1 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(157): Phinx\Db\Adapter\PdoAdapter->createPdoConnection('...', '...', '', Array)
#2 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(168): Phinx\Db\Adapter\MysqlAdapter->connect()
#3 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(252): Phinx\Db\Adapter\PdoAdapter->getConnection()
#4 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(273): Phinx\Db\Adapter\PdoAdapter->query('...')
#5 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(409): Phinx\Db\Adapter\PdoAdapter->fetchAll('...')
#6 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(381): Phinx\Db\Adapter\PdoAdapter->getVersionLog()
#7 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(205): Phinx\Db\Adapter\PdoAdapter->getVersions()
#8 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php(253): Phinx\Db\Adapter\AdapterWrapper->getVersions()
#9 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php(315): Phinx\Migration\Manager\Environment->getVersions()
#10 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(122): Phinx\Migration\Manager->migrate('...', NULL, false)
#11 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/symfony/console/Command/Command.php(298): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/symfony/console/Application.php(1040): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Phinx\Console\Command\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/src/Phinx/Console/PhinxApplication.php(70): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/symfony/console/Application.php(171): Phinx\Console\PhinxApplication->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/robmorgan/phinx/bin/phinx(28): Symfony\Component\Console\Application->run()
#17 /home/ubuntu/actions-runner2/_work/Checker/Checker/Checker/vendor/bin/phinx(119): include('...')
#18 {main}

When using a static number for port, the script works perfectly, and yes I can confirm that the environment variable "MYSQL_PORT" exists and has a number in it.

paths:
  migrations: "%%PHINX_CONFIG_DIR%%/../../Checker/migrations"
migration_base_class: \Checker\Phinx\AbstractCheckerMigration
templates:
  file: "%%PHINX_CONFIG_DIR%%/../../Checker/config/phinx/Migration.template.php.dist"
environments:
  default_migration_table: phinxlog
  default_database: tests
  tests:
    adapter: mysql
    host: 127.0.0.1
    name: checker_unittest
    user: root
    pass: ''
    port: 3091
    charset: utf8mb4
    collation: utf8mb4_unicode_ci

Is this expected behaviour or is something broken here?

Thanks.

MasterOdin commented 9 months ago

The values within the config are replaced recursively throughout (relevant code), so if this wasn't working, it'd be a bug.

When using a static number for port, the script works perfectly, and yes I can confirm that the environment variable "MYSQL_PORT" exists and has a number in it.

However, I think this is the issue? Per the docs, phinx only picks up and injects any variables from the environment that start with PHINX_. So you'd want to set PHINX_MYSQL_PORT within your environment to have %%PHINX_MYSQL_PORT%% get replaced within your config, while variable named MYSQL_PORT will get ignored and not be used.

MasterOdin commented 8 months ago

Closing this as I believe that phinx is working as expected/documented, and that the solution is to use PHINX_MYSQL_PORT VS MYSQL_PORT.

Feel free to re-open this issue if you still think there's a bug.