How is it possible to use Phinx without migrations?
Asked Answered
M

1

6

I would like to synchronize the states of multiple databases. I would like to compare the tables in them, and create the missing tables and columns.

There is a library for Php, which has a lot of functions for database manipulation: Phinx Unfortunately there are only examples for creating migrations, but it does not explain, how to use it without that.

How is it possible to query and modify the database structure, without writing migrations?

Malvina answered 23/9, 2019 at 14:15 Comment(2)
Do you have a lot of these databases? If you only have a few, I personally would feel more comfortable doing this manually, database-by-database, instead of firing up an orchestration system. Even upwards of a dozen. I think I'd write a quick SQL statement that queries and runs DESCRIBE on each table and pipe that through a diff.Shandy
I have ~20 databases, and each database has 100+ tables. Comparing them by hand is not an option in my caseMalvina
D
2

The main problem here is sync all the databases, to do that by using phinx, you should decide which is the most complete database and do the following:

Option 1: Create a "master" migration with each table that should exist, "complete database, one per table.

<?PHP

    public function up()
    {
        $exists = $this->hasTable('xxx');
        if (!$exists) {
            $this->execute("CREATE TABLE `xxx` (`xxx_id` int(10) NOT NULL AUTO_INCREMENT);");
        }
    }
    public function down()
    {
        $exists = $this->hasTable('xxx');
        if ($exists) {
            $table = $this->table('xxx');
            if (!$table->hasColumn('name')) {
                $table->addColumn("name", "string", ["limit" => 255])->save();
            }
            $table->drop();
        }
    }

but creating one migration per table could be a hard job, then ...

Option 2:: You could create the database scripts dynamically by listing tables and adding them into an array:

<?PHP

    public function up()
    {
        $tables = ['xxx', 'yyy'];
        foreach ($tables as $curTable) {
             $exists = $this->hasTable($curTable);
             if (!$exists) {
                   $this->execute($this->getCreateTable($curTable));
             }
        }

    }

but then you should generate a method called getCreateTable that receives the table name and get the structure from the master database.

after that create a routine to do the same with fields but you can get them for each table and nested loop for each field.

Disaffirm answered 13/6, 2020 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.