cakephp / cakephp/phinx

Drop table doesn't work as expected

Open
#2,211 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

The documentation says to use the following code when deleting a table:

$this->table('table')->drop()->save();

https://book.cakephp.org/phinx/0/en/migrations.html#dropping-a-table

But in my opinion it's better using:

$this->table('table')->drop()->update();

...because if the table doesn't exists and when executing Table::executeActions(), the actions will contain DropTable and a dynamically added CreateTable action. That makes no sense.

This is due to the following code:

/**
 * Executes all the pending actions for this table
 *
 * @param bool $exists Whether or not the table existed prior to executing this method
 * @return void
 */
protected function executeActions(bool $exists): void
{
    /* ... */

    // If the table does not exist, the last command in the chain needs to be
    // a CreateTable action.
    if (! $exists) {
        $this->actions->addAction(new CreateTable($this->table));
    }

    /* ... */
}

https://github.com/cakephp/phinx/blob/b2eef811863e6c4e96bac0136263f502424a170f/src/Phinx/Db/Table.php#L716

So am I using the save and update methods in a wrong way and then the documentation needs to be changed or is it a bug?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/Phinx/Db/Table.php at executeActions(bool $exists), then trace how the documented save() and suggested update() paths build and execute actions. Reproduce dropping a nonexistent table and determine whether the action sequence or the migration documentation should change; done means the behavior and documentation agree.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.