Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration:rollback does not work in 'development' environment #8026

Closed
xonipatino opened this issue Oct 10, 2023 · 6 comments
Closed

migration:rollback does not work in 'development' environment #8026

xonipatino opened this issue Oct 10, 2023 · 6 comments

Comments

@xonipatino
Copy link

xonipatino commented Oct 10, 2023

PHP Version

8.2

CodeIgniter4 Version

4.4.1

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MySQL 8

What happened?

Currently my project has two database groups, "default" and "test", I am currently developing and using the "test" database, and I configure in my .env file the following:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

...

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.defaultGroup = tests

database.tests.hostname = localhost
database.tests.database = macc_tests
database.tests.username = root
database.tests.password = 
database.tests.DBDriver = MySQLi
database.tests.DBPrefix = 
database.tests.charset  = utf8mb4
database.tests.DBCollat = utf8mb4_general_ci
database.tests.port     = 3306

Now when I run migration from command I have to do it like this:

php spark migrate -g test

The process runs normally, the problem is when I rollback, because when I run

php spark migrate:rollback -g tests

Since it does not make any changes, it does not delete or modify the tables

Steps to Reproduce

In .env:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

...

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.defaultGroup = tests

database.tests.hostname = localhost
database.tests.database = macc_tests
database.tests.username = root
database.tests.password = 
database.tests.DBDriver = MySQLi
database.tests.DBPrefix = 
database.tests.charset  = utf8mb4
database.tests.DBCollat = utf8mb4_general_ci
database.tests.port     = 3306

in app\Database\Migrations\2023-10-09-190224_TipoUsuarioMigration.php, for all "Migration" files I don't add "DBGroup" property:

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class TipoUsuarioMigration extends Migration
{
    // protected $DBGroup = 'default';

    public function up()
    {
        $this->forge
            ->addField(
                [
                    'id' => [
                        'type'           => 'INT',
                        'auto_increment' => true,
                    ],
                    'nombre' => [
                        'type'       => 'VARCHAR',
                        'constraint' => 50,
                    ],
                ]
            )
            ->addPrimaryKey('id')
            ->createTable('tipo_usuario', true);
    }

    public function down()
    {
        $this->forge->dropTable('tipo_usuario', true);
    }
}

In CLI:

  1. php spark migrate -g tests
  2. php spark migrate:rollback -g tests <--- doesn't work, doesn't delete the table.

Expected Output

when you run the command php spark migrate:rollback -g tests, it should modify or delete tables.

Anything else?

When I change the CI_ENVIRONMENT variable in .env like this CI_ENVIRONMENT = testing, the rollback works correctly.

@xonipatino xonipatino added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 10, 2023
@michalsn
Copy link
Member

Please see: #7894

@xonipatino
Copy link
Author

xonipatino commented Oct 10, 2023

Hello, I have reviewed the code of the file system\Database\MigrationRunner.php and I came across the following code starting from lines 849 -855

if (ENVIRONMENT !== 'testing' && $group === 'tests' && $this->groupFilter !== 'tests') {
    // @codeCoverageIgnoreStart
    $this->groupSkip = true;

    return true;
    // @codeCoverageIgnoreEnd
}

I have commented on these lines and it has solved the problem, I hope they make the decision to integrate the -g option into the rollback again, and eliminate the mentioned lines.

It should be allowed that regardless of whether your CI_ENVIRONMENT is "production", "testing" or "development" you can rollback without any problem.

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 10, 2023
@kenjis
Copy link
Member

kenjis commented Oct 10, 2023

The migration you expect and the one provided by CI behave differently.
So you need to customize your migration to fit your needs.

CI's migration is kind of version control system for database state, and it manages the state of multiple databases as a whole.

For example, when migrations are executed as follows,

1. db_a createTableA
2. db_b createTableB
3. db_a createTableC
4. db_b createTableD

If we roll back only db_b, only the following tables will remain, but the entire database has never been in this state. This is an inconsistent state that has never existed.

1. db_a createTableA
3. db_a createTableC

If the tables in each database were completely independent, there might be no problem with the above state.
However, CI's migration is not based on such an assumption and, therefore, provides no way to roll back to the inconsistent state.

@kenjis
Copy link
Member

kenjis commented Oct 10, 2023

@xonipatino Please change the group name tests. It is a special group for PHPUnit testing.
Then, you can use php spark migrate and php spark migrate:rollback without -g.

* This database connection is used when
* running PHPUnit database tests.
*/
public array $tests = [

@kenjis
Copy link
Member

kenjis commented Oct 10, 2023

Please do not use CI_ENVIRONMENT = testing.

Important
The environment testing is reserved for PHPUnit testing. It has special conditions built into the framework at various places to assist with that. You can’t use it for your development.
https://codeigniter4.github.io/CodeIgniter4/general/environments.html#the-defined-environments

@xonipatino
Copy link
Author

Hola

@xonipatino Please change the group name tests. It is a special group for PHPUnit testing. Then, you can use php spark migrate and php spark migrate:rollback without -g.

* This database connection is used when
* running PHPUnit database tests.
*/
public array $tests = [

Hello @kenjis, thank you for your answer, I thought that the "tests" group could be used without a problem, but since you clarified it, then I will create a new group in app\config\Database.php with the same "Default" options and then from the " .env" I modify it to my needs.

thanks you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants