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

What is the -g option in spark migrate for? #7891

Closed
kenjis opened this issue Sep 4, 2023 · 7 comments
Closed

What is the -g option in spark migrate for? #7891

kenjis opened this issue Sep 4, 2023 · 7 comments
Labels
database Issues or pull requests that affect the database layer question

Comments

@kenjis
Copy link
Member

kenjis commented Sep 4, 2023

Migration of non-default group is also executed without the -g option.

-g - to chose database group, otherwise default database group will be used.
https://codeigniter4.github.io/CodeIgniter4/dbmgmt/migration.html#migrate

How to Reproduce

--- a/app/Config/Database.php
+++ b/app/Config/Database.php
@@ -45,6 +45,47 @@ class Database extends Config
         'numberNative' => false,
     ];
 
+    public array $test1 = [
+        'DSN'          => '',
+        'hostname'     => 'localhost',
+        'username'     => 'root',
+        'password'     => '',
+        'database'     => 'ci4_test1',
+        'DBDriver'     => 'MySQLi',
+        'DBPrefix'     => '',
+        'pConnect'     => false,
+        'DBDebug'      => true,
+        'charset'      => 'utf8',
+        'DBCollat'     => 'utf8_general_ci',
+        'swapPre'      => '',
+        'encrypt'      => false,
+        'compress'     => false,
+        'strictOn'     => false,
+        'failover'     => [],
+        'port'         => 3306,
+        'numberNative' => false,
+    ];
+    public array $test2 = [
+        'DSN'          => '',
+        'hostname'     => 'localhost',
+        'username'     => 'root',
+        'password'     => '',
+        'database'     => 'ci4_test2',
+        'DBDriver'     => 'MySQLi',
+        'DBPrefix'     => '',
+        'pConnect'     => false,
+        'DBDebug'      => true,
+        'charset'      => 'utf8',
+        'DBCollat'     => 'utf8_general_ci',
+        'swapPre'      => '',
+        'encrypt'      => false,
+        'compress'     => false,
+        'strictOn'     => false,
+        'failover'     => [],
+        'port'         => 3306,
+        'numberNative' => false,
+    ];
+
     /**
      * This database connection is used when
      * running PHPUnit database tests.
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddBlog extends Migration
{
    protected $DBGroup = 'test1';

    public function up()
    {
        $this->forge->addField([
            'blog_id' => [
                'type'           => 'INT',
                'constraint'     => 5,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'blog_title' => [
                'type'       => 'VARCHAR',
                'constraint' => '100',
            ],
            'blog_description' => [
                'type' => 'TEXT',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('blog_id', true);
        $this->forge->createTable('blog');
    }

    public function down()
    {
        $this->forge->dropTable('blog');
    }
}
$ php spark migrate

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 02:12:42 UTC+00:00

Running all new migrations...
	Running: (App) 2023-09-04-013453_App\Database\Migrations\AddBlog
Migrations complete.
$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 02:15:26 UTC+00:00

+-----------+-------------------+----------+-------+---------------------+-------+
| Namespace | Version           | Filename | Group | Migrated On         | Batch |
+-----------+-------------------+----------+-------+---------------------+-------+
| App       | 2023-09-04-013453 | AddBlog  | test1 | 2023-09-04 02:12:42 | 1     |
+-----------+-------------------+----------+-------+---------------------+-------+

References

@kenjis kenjis added the database Issues or pull requests that affect the database layer label Sep 4, 2023
@kenjis
Copy link
Member Author

kenjis commented Sep 4, 2023

The migration specifications are not clear, but my understanding is as follows:

  • If a non-default DB group is specified, the $DBGroup in the migration file must be specified.
  • Migration must manage the state of the entire databases, so the migration table is always created in the default database group.
  • It is not expected that a single migration file will be executed for multiple databases.

Is there something wrong? > @codeigniter4/database-team

@michalsn
Copy link
Member

michalsn commented Sep 4, 2023

@kenjis All 3 points are valid.

-g is for running migrations only for specified database.

I believe the lack of this parameter should execute all migrations.

@kenjis
Copy link
Member Author

kenjis commented Sep 4, 2023

Okay, then the description in the user guide is wrong?

-g - to chose database group, otherwise default database group will be used.

@michalsn
Copy link
Member

michalsn commented Sep 4, 2023

I think so.

@kenjis
Copy link
Member Author

kenjis commented Sep 4, 2023

Okay, it seems the current behavior is fine. And the bug is only in the docs.

If you don't specify -g, migrations for all groups run.

$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:47:49 UTC+00:00

+-----------+-------------------+----------------+-------+-------------+-------+
| Namespace | Version           | Filename       | Group | Migrated On | Batch |
+-----------+-------------------+----------------+-------+-------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | ---   | ---         | ---   |
| App       | 2023-09-04-013454 | Test1AddBlog   | ---   | ---         | ---   |
| App       | 2023-09-04-013455 | Test2AddBlog   | ---   | ---         | ---   |
+-----------+-------------------+----------------+-------+-------------+-------+
$ php spark migrate

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:48:00 UTC+00:00

Running all new migrations...
        Running: (App) 2023-09-04-013453_App\Database\Migrations\DefaultAddBlog
        Running: (App) 2023-09-04-013454_App\Database\Migrations\Test1AddBlog
        Running: (App) 2023-09-04-013455_App\Database\Migrations\Test2AddBlog
Migrations complete.
$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:48:04 UTC+00:00

+-----------+-------------------+----------------+---------+---------------------+-------+
| Namespace | Version           | Filename       | Group   | Migrated On         | Batch |
+-----------+-------------------+----------------+---------+---------------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | default | 2023-09-04 05:48:00 | 1     |
| App       | 2023-09-04-013454 | Test1AddBlog   | test1   | 2023-09-04 05:48:00 | 1     |
| App       | 2023-09-04-013455 | Test2AddBlog   | test2   | 2023-09-04 05:48:00 | 1     |
+-----------+-------------------+----------------+---------+---------------------+-------+

If you specify default:

$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:48:10 UTC+00:00

+-----------+-------------------+----------------+-------+-------------+-------+
| Namespace | Version           | Filename       | Group | Migrated On | Batch |
+-----------+-------------------+----------------+-------+-------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | ---   | ---         | ---   |
| App       | 2023-09-04-013454 | Test1AddBlog   | ---   | ---         | ---   |
| App       | 2023-09-04-013455 | Test2AddBlog   | ---   | ---         | ---   |
+-----------+-------------------+----------------+-------+-------------+-------+
$ php spark migrate -g default

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:02 UTC+00:00

Running all new migrations...
        Running: (App) 2023-09-04-013453_App\Database\Migrations\DefaultAddBlog
Migrations complete.
$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:05 UTC+00:00

+-----------+-------------------+----------------+---------+---------------------+-------+
| Namespace | Version           | Filename       | Group   | Migrated On         | Batch |
+-----------+-------------------+----------------+---------+---------------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | default | 2023-09-04 05:49:02 | 1     |
| App       | 2023-09-04-013454 | Test1AddBlog   | ---     | ---                 | ---   |
| App       | 2023-09-04-013455 | Test2AddBlog   | ---     | ---                 | ---   |
+-----------+-------------------+----------------+---------+---------------------+-------+

If you specify test2:

$ php spark migrate -g test2

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:21 UTC+00:00

Running all new migrations...
        Running: (App) 2023-09-04-013455_App\Database\Migrations\Test2AddBlog
Migrations complete.
$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:23 UTC+00:00

+-----------+-------------------+----------------+---------+---------------------+-------+
| Namespace | Version           | Filename       | Group   | Migrated On         | Batch |
+-----------+-------------------+----------------+---------+---------------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | default | 2023-09-04 05:49:02 | 1     |
| App       | 2023-09-04-013454 | Test1AddBlog   | ---     | ---                 | ---   |
| App       | 2023-09-04-013455 | Test2AddBlog   | test2   | 2023-09-04 05:49:21 | 2     |
+-----------+-------------------+----------------+---------+---------------------+-------+

If you specify test1:

$ php spark migrate -g test1

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:33 UTC+00:00

Running all new migrations...
        Running: (App) 2023-09-04-013454_App\Database\Migrations\Test1AddBlog
Migrations complete.
$ php spark migrate:status

CodeIgniter v4.4.0 Command Line Tool - Server Time: 2023-09-04 05:49:34 UTC+00:00

+-----------+-------------------+----------------+---------+---------------------+-------+
| Namespace | Version           | Filename       | Group   | Migrated On         | Batch |
+-----------+-------------------+----------------+---------+---------------------+-------+
| App       | 2023-09-04-013453 | DefaultAddBlog | default | 2023-09-04 05:49:02 | 1     |
| App       | 2023-09-04-013454 | Test1AddBlog   | test1   | 2023-09-04 05:49:33 | 3     |
| App       | 2023-09-04-013455 | Test2AddBlog   | test2   | 2023-09-04 05:49:21 | 2     |
+-----------+-------------------+----------------+---------+---------------------+-------+

@michalsn
Copy link
Member

michalsn commented Sep 4, 2023

Great, thanks!

@kenjis
Copy link
Member Author

kenjis commented Sep 11, 2023

Closed by #7894

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Issues or pull requests that affect the database layer question
Projects
None yet
Development

No branches or pull requests

2 participants