phinx: --dry-run is not fully working

Running phinx migrate --dry-run will execute queries if they’re written via queryBuilder. For example here’s code from docs:

<?php
$builder = $this->getQueryBuilder();
$builder
->update('users')
->set('fname', 'Snow')
->where(['fname' => 'Jon'])
->execute()

So phinx migrate --dry-run will not work with that code.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 19 (8 by maintainers)

Most upvoted comments

Kludge-solutions Inc.

    /**
     * Execute queries build using the CakePHP QueryBuilder but respect Phinx --dry-run option
     *
     * @param  Query $queryBuilder
     */
    public function executeFromQueryBuilder(Query $queryBuilder)
    {
        $sql = $queryBuilder->sql();
        $bindings = $queryBuilder->getValueBinder()->bindings();

        if ($this->getInput()->getOption('dry-run')) {
            if (!empty($bindings)) {
                $values = array_column($bindings, 'value');

                foreach ($values as &$value) {
                    $value = is_string($value) ? "'" . $value . "'" : $value;
                }

                $sql = str_replace(array_keys($bindings), $values, $sql);
            }

            $this->getAdapter()->getOutput()->writeln($sql);
        } else {
            $queryBuilder->execute();
        }
    }

+1 For this. Query Builder has to be able to output raw SQL that interprets and sends to server. We’ve had a major problem where something got interpreted in an unintended way and entire database of suppliers got updated instead of a few intended records. Showing what was the interpreted SQL to be sent to server would have helped at code review stage. The issue we had was someone writing ->where(['id >', 1]) instead of ->where(['id >' => 1]) - that quietly passed for WHERE id SQL dropping the second bit, no error, and entire table updated. We need to see whats been run against the server.