cakephp / phinx

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

No migrations to rollback #2004

Open designermonkey opened 3 years ago

designermonkey commented 3 years ago

I am tearing my hair out here. Given I have the following Symfony commands:

<?php declare(strict_types=1);

namespace MicronResearch\Tenancy\Command;

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateLandlord extends Command
{
    protected Application $phinx;

    public function __construct(Application $phinx = null)
    {
        parent::__construct();
        $this->phinx = $phinx ?? new PhinxApplication();
    }

    protected function configure(): void
    {
        $this->setName("landlord:migrate")
            ->setDescription("Migrate the Landlord database");
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->phinx = new PhinxApplication();

        $arguments = [
            'command' => 'migrate',
            '--environment' => 'landlord',
            '--configuration' => dirname(dirname(__DIR__)) . '/phinx.php',
            '-vvv'
        ];

        return $this->phinx->run(new ArrayInput($arguments), $output);
    }
}
<?php declare(strict_types=1);

namespace MicronResearch\Tenancy\Command;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ResetLandlord extends MigrateLandlord
{
    protected function configure(): void
    {
        $this->setName("landlord:reset")
            ->setDescription("Reset the Landlord database to a clean migration");
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $arguments = [
            'command' => 'rollback',
            '--target' => 0,
            '--environment' => 'landlord',
            '--configuration' => dirname(dirname(__DIR__)) . '/phinx.php',
            '-vvv'
        ];

        $this->phinx->run(new ArrayInput($arguments), $output);

        return parent::execute($input, $output);
    }
}

I cannot figure out why phinx refuses to see the migrations that have been applied to roll them back!

$ ./tenancy landlord:migrate
Phinx by CakePHP - https://phinx.org.

using config file phinx.php
using config parser php
using migration paths 
 - /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations
using seed paths 
 - /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Seeds
using environment landlord
using adapter mysql
using database landlord
ordering by execution time
Migration file
    /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php
    /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php
Valid migration file /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php.
Loading class MicronResearch\Tenancy\Migrations\CreateTenantsTable from /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php.
Running MicronResearch\Tenancy\Migrations\CreateTenantsTable.
Valid migration file /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php.
Loading class MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable from /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php.
Running MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable.
CREATE TABLE `migrations` (`version` BIGINT(20) NOT NULL, `migration_name` VARCHAR(100) NULL, `start_time` TIMESTAMP NULL, `end_time` TIMESTAMP NULL, `breakpoint` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`version`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

 == 00000000000001 MicronResearch\Tenancy\Migrations\CreateTenantsTable: migrating
START TRANSACTION;
 -- createTable('tenants')
CREATE TABLE `tenants` (`id` INT(11) NOT NULL AUTO_INCREMENT, `uuid_bin` BINARY(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, `code` VARCHAR(127) NOT NULL, `name` VARCHAR(255) NOT NULL, `is_active` INT(1) NOT NULL DEFAULT 1, PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
    -> 0.0082s
COMMIT;
INSERT INTO `migrations` (`version`, `migration_name`, `start_time`, `end_time`, `breakpoint`) VALUES ('00000000000001', 'MicronResearch\Tenancy\Migrations\CreateTenantsTable', '2021-07-23 17:13:39', '2021-07-23 17:13:39', 0);
 == 00000000000001 MicronResearch\Tenancy\Migrations\CreateTenantsTable: migrated 0.0140s

 == 00000000000002 MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable: migrating
START TRANSACTION;
 -- createTable('tenant_connections')
CREATE TABLE `tenant_connections` (`id` INT(11) NOT NULL AUTO_INCREMENT, `tenant_id` INT(11) NOT NULL, `name` VARCHAR(255) NOT NULL, `driver` VARCHAR(255) NOT NULL DEFAULT 'mysql', `host` VARCHAR(255) NOT NULL, `database` VARCHAR(255) NOT NULL, `username` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `port` INT(12) NOT NULL DEFAULT 3306, `options` TEXT NOT NULL DEFAULT 'charset=utf8mb4', PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
    -> 0.0114s
 -- Altering table tenant_connections
ALTER TABLE `tenant_connections` ADD  FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
    -> 0.0207s
 -- Altering table tenant_connections
    -> 0.0000s
COMMIT;
INSERT INTO `migrations` (`version`, `migration_name`, `start_time`, `end_time`, `breakpoint`) VALUES ('00000000000002', 'MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable', '2021-07-23 17:13:39', '2021-07-23 17:13:39', 0);
 == 00000000000002 MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable: migrated 0.0418s

All Done. Took 0.0822s

Works fine and creates the correct tables and migrations in the right table.

$ ./tenancy landlord:reset  
Phinx by CakePHP - https://phinx.org.

using config file phinx.php
using config parser php
using migration paths 
 - /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations
using seed paths 
 - /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Seeds
using environment landlord
using adapter mysql
using database landlord
ordering by execution time
Migration file
    /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php
    /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php
Valid migration file /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php.
Loading class MicronResearch\Tenancy\Migrations\CreateTenantsTable from /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000001_create_tenants_table.php.
Running MicronResearch\Tenancy\Migrations\CreateTenantsTable.
Valid migration file /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php.
Loading class MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable from /Volumes/Micron Research/Projects/mars-three/api/libs/micron/tenancy/src/Migrations/00000000000002_create_tenant_connections_table.php.
Running MicronResearch\Tenancy\Migrations\CreateTenantConnectionsTable.
No migrations to rollback

All Done. Took 0.0171s

This command never works and always tells me there are no migrations to rollback, even though it is the exact same config and the records are present in the database.

Even if I run rollback as a phinxcommand on the cli with the right config and settings, I still get 'no migrations to rollback'.

If anyone has any insight I would greatly appreciate it.

designermonkey commented 3 years ago

I will point out that my migrations use up and down methods as I have custom sql (if it's MySQL) to run that isn't possible to run in the change method.

designermonkey commented 3 years ago

I had my migrations simply named as they are not date specific, so the files are not using real dates as shown above. Technically speaking they are real dates, so why would this fail?

halfinhalfout commented 7 months ago

:( I'm seeing this behavior as well with phinx 0.9.2.