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

php spark migrate, migrating table to wrong database group #5087

Closed
renmura opened this issue Sep 14, 2021 · 5 comments
Closed

php spark migrate, migrating table to wrong database group #5087

renmura opened this issue Sep 14, 2021 · 5 comments

Comments

@renmura
Copy link

renmura commented Sep 14, 2021

I'm facing this issue in the last few days. I have 3 databases to connect with. Well actually I just need to connect to 1 database since my task is on that 1 database. But, my other team members need to connect to other database, so we configured 3 database connections.

Note : the database I'm working on is api_mobile

A week ago, I created some migration files to migrate. At that time, I only made one database configuration. And executing php spark migrate has no problems.

Several days later, we (team members) merging our work, so we created 3 database connections.

When I execute php spark migrate -g api_mobile (hoping my new migration file is creating table into api_mobile), all of the migration files that I have created is migrating tables into wrong database (migrated to default group).

My laptop setup is:
Apache 2.4.48
PHP 8.0.9
PostgreSQL 13.3

My CLI response:

D:\xampp\htdocs\my-app> php spark migrate -g api_mobile

CodeIgniter v4.1.3 Command Line Tool - Server Time: 2021-09-14 22:27:38 UTC+07:00

Running all new migrations...
        Running: (App) 2021-09-04-141412_App\Database\Migrations\CreateRoleTable
        Running: (App) 2021-09-04-141513_App\Database\Migrations\CreateUserTable
        Running: (App) 2021-09-04-143719_App\Database\Migrations\CreateAuditTrailTable
        Running: (App) 2021-09-04-144446_App\Database\Migrations\CreateJenisSurveyTable
        Running: (App) 2021-09-04-144621_App\Database\Migrations\CreateSurveyTable
        Running: (App) 2021-09-04-145012_App\Database\Migrations\CreateSurveyDetailTable
        Running: (App) 2021-09-04-150024_App\Database\Migrations\CreateMenuTable
        Running: (App) 2021-09-04-150300_App\Database\Migrations\CreateHakAksesTable
        Running: (App) 2021-09-04-150450_App\Database\Migrations\CreateHeroTable
        Running: (App) 2021-09-04-150639_App\Database\Migrations\CreateJudulTable
        Running: (App) 2021-09-04-150753_App\Database\Migrations\CreateAboutTable
        Running: (App) 2021-09-04-151115_App\Database\Migrations\CreateTimTable
        Running: (App) 2021-09-04-151123_App\Database\Migrations\CreateTimSocmedTable
        Running: (App) 2021-09-04-151649_App\Database\Migrations\CreatePesanTable
        Running: (App) 2021-09-04-151905_App\Database\Migrations\CreateBalasanTable
        Running: (App) 2021-09-04-152100_App\Database\Migrations\CreateKontakTable
        Running: (App) 2021-09-04-152304_App\Database\Migrations\CreateFooterTable
        Running: (App) 2021-09-04-152348_App\Database\Migrations\CreateCiSessionsTable
        Running: (App) 2021-09-07-041929_App\Database\Migrations\CreateLaporanBugTable
        Running: (App) 2021-09-14-133002_App\Database\Migrations\CreateUploadTable
        Running: (App) 2021-09-14-134138_App\Database\Migrations\CreateUploadDetailTable
Done migrations.

My migrations table (created by CI 4, and yes in the wrong database):

id|version          |class                                          |group     |namespace|time      |batch|
--+-----------------+-----------------------------------------------+----------+---------+----------+-----+
 1|2021-09-04-141412|App\Database\Migrations\CreateRoleTable        |api_mobile|App      |1631633259|    1|
 2|2021-09-04-141513|App\Database\Migrations\CreateUserTable        |api_mobile|App      |1631633259|    1|
 3|2021-09-04-143719|App\Database\Migrations\CreateAuditTrailTable  |api_mobile|App      |1631633259|    1|
 4|2021-09-04-144446|App\Database\Migrations\CreateJenisSurveyTable |api_mobile|App      |1631633259|    1|
 5|2021-09-04-144621|App\Database\Migrations\CreateSurveyTable      |api_mobile|App      |1631633259|    1|
 6|2021-09-04-145012|App\Database\Migrations\CreateSurveyDetailTable|api_mobile|App      |1631633259|    1|
 7|2021-09-04-150024|App\Database\Migrations\CreateMenuTable        |api_mobile|App      |1631633259|    1|
 8|2021-09-04-150300|App\Database\Migrations\CreateHakAksesTable    |api_mobile|App      |1631633259|    1|
 9|2021-09-04-150450|App\Database\Migrations\CreateHeroTable        |api_mobile|App      |1631633259|    1|
10|2021-09-04-150639|App\Database\Migrations\CreateJudulTable       |api_mobile|App      |1631633259|    1|
11|2021-09-04-150753|App\Database\Migrations\CreateAboutTable       |api_mobile|App      |1631633259|    1|
12|2021-09-04-151115|App\Database\Migrations\CreateTimTable         |api_mobile|App      |1631633259|    1|
13|2021-09-04-151123|App\Database\Migrations\CreateTimSocmedTable   |api_mobile|App      |1631633259|    1|
14|2021-09-04-151649|App\Database\Migrations\CreatePesanTable       |api_mobile|App      |1631633259|    1|
15|2021-09-04-151905|App\Database\Migrations\CreateBalasanTable     |api_mobile|App      |1631633259|    1|
16|2021-09-04-152100|App\Database\Migrations\CreateKontakTable      |api_mobile|App      |1631633259|    1|
17|2021-09-04-152304|App\Database\Migrations\CreateFooterTable      |api_mobile|App      |1631633259|    1|
18|2021-09-04-152348|App\Database\Migrations\CreateCiSessionsTable  |api_mobile|App      |1631633259|    1|
19|2021-09-07-041929|App\Database\Migrations\CreateLaporanBugTable  |api_mobile|App      |1631633259|    1|
20|2021-09-14-133002|App\Database\Migrations\CreateUploadTable      |api_mobile|App      |1631633259|    1|
21|2021-09-14-134138|App\Database\Migrations\CreateUploadDetailTable|api_mobile|App      |1631633259|    1|

My Database.php file is:

public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'password',
    'database' => 'geonode_data',
    'DBDriver' => 'Postgre',
    ....
];

public $ppigis = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'password',
    'database' => 'ppigis',
    'DBDriver' => 'Postgre',
    ....
];

public $api_mobile = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'password',
    'database' => 'api_mobile',
    'DBDriver' => 'Postgre',
    ....
];

My .env file is:

database.default.hostname = localhost
database.default.database = geonode_data
database.default.username = postgres
database.default.password = password
database.default.DBDriver = Postgre

database.ppigis.hostname = localhost
database.ppigis.database = ppi_gis
database.ppigis.username = postgres
database.ppigis.password = password
database.ppigis.DBDriver = Postgre

database.api_mobile.hostname = localhost
database.api_mobile.database = api_mobile
database.api_mobile.username = postgres
database.api_mobile.password = password
database.api_mobile.DBDriver = Postgre

One of my migrations file:

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateRoleTable extends Migration
{
	protected $DBGroup = 'api_mobile';
	

Here's how to reproduce the error:

  1. Creating 1 database connection, api_mobile
  2. Creating 19 migration files via CLI
  3. Several days later, adding two new database connection
  4. Creating 2 new migration files via CLI
  5. Execute php spark migrate -g api_mobile hoping that will only migrate api_mobile group

Thank you for your times.. 😄

@renmura renmura added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 14, 2021
@paulbalandan
Copy link
Member

Hi and thanks for the bug report. Please give us a few more time to debug this issue (as we're just busy at work 😅).

Anyway, I want to clarify your example migrations table (which I assume is generated by php spark migrate status). You are saying it goes migrated to the default migration group (which is geonode_data) but your example shows the group all as api_mobile.

From that I assume it is working correctly, unless I am missing something here. Thanks.

@renmura
Copy link
Author

renmura commented Sep 15, 2021

Hi and thanks for the bug report. Please give us a few more time to debug this issue (as we're just busy at work 😅).

Anyway, I want to clarify your example migrations table (which I assume is generated by php spark migrate status). You are saying it goes migrated to the default migration group (which is geonode_data) but your example shows the group all as api_mobile.

From that I assume it is working correctly, unless I am missing something here. Thanks.

Yes, the tables migrated to the default database group, geonode_data.

After executing php spark migrate -g api_mobile, I check api_mobile database, there is no table in it.

And I check geonode_data database, all of my table that I needed in there.

When I open migrations table, in group column, its filled with api_mobile.

It's okay, I understand. For now I just need to change the database name from geonode_data to api_mobile in the default database group, and do the migrations. After the migration is done, I revert the database name. But I think this bug need to be fixed in the future..

Thanks...

@paulbalandan
Copy link
Member

Hi @renmura ! Thanks for the thorough explanation.

I think I get where the problem is, but it needs writing some testing. Can you check this for me if this "fix" will work?

  1. In system/Database/MigrationRunner.php in the method latest, change the lines as follows:
-      $this->ensureTable();
-
        if ($group !== null) {
            $this->groupFilter = $group;
            $this->setGroup($group);
        }
+
+      $this->ensureTable();
  1. In the same file, check the ensureTable() method and change as follows:
-    $forge = Database::forge($this->db);
+    $forge = Database::forge($this->group ?: null);

Save everything then re-run php spark migrate -g api_mobile. Thanks!

@kenjis kenjis added the waiting for info Issues or pull requests that need further clarification from the author label Oct 28, 2021
@paulbalandan paulbalandan removed the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 9, 2021
@kenjis
Copy link
Member

kenjis commented Nov 11, 2021

I can't reproduce.

Create a migration file:

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateCiSessionsTable extends Migration
{
    protected $DBGroup = 'tests';

    ...
}
$ php spark migrate:status 

CodeIgniter v4.1.5 Command Line Tool - Server Time: 2021-11-10 20:59:02 UTC-06:00

+-----------+-------------------+-----------------------+-------+-------------+-------+
| Namespace | Version           | Filename              | Group | Migrated On | Batch |
+-----------+-------------------+-----------------------+-------+-------------+-------+
| App       | 2021-11-11-022829 | CreateCiSessionsTable | ---   | ---         | ---   |
+-----------+-------------------+-----------------------+-------+-------------+-------+

↑ Not migrated yet.

$ php spark migrate 

CodeIgniter v4.1.5 Command Line Tool - Server Time: 2021-11-10 20:59:36 UTC-06:00

Running all new migrations...
Done migrations.

tests is not the default group, so do nothing.

$ php spark migrate -g tests

CodeIgniter v4.1.5 Command Line Tool - Server Time: 2021-11-10 21:04:03 UTC-06:00

Running all new migrations...
	Running: (App) 2021-11-11-022829_App\Database\Migrations\CreateCiSessionsTable
Done migrations.

↑ Migrated.

$ php spark migrate:status 

CodeIgniter v4.1.5 Command Line Tool - Server Time: 2021-11-10 21:04:07 UTC-06:00

+-----------+-------------------+-----------------------+-------+---------------------+-------+
| Namespace | Version           | Filename              | Group | Migrated On         | Batch |
+-----------+-------------------+-----------------------+-------+---------------------+-------+
| App       | 2021-11-11-022829 | CreateCiSessionsTable | tests | 2021-11-10 21:04:03 | 1     |
+-----------+-------------------+-----------------------+-------+---------------------+-------+

db_ci_sessions table was created in tests group database.

@paulbalandan paulbalandan changed the title Bug: php spark migrate, migrating table to wrong database group php spark migrate, migrating table to wrong database group Dec 23, 2021
@paulbalandan paulbalandan removed the waiting for info Issues or pull requests that need further clarification from the author label Dec 23, 2021
@paulbalandan
Copy link
Member

No feedback received. Closing..

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