skip to Main Content

Is there any tool or programming technique to capture the output from Yii2 migrations to up/down SQL scripts, that is, print the migration corresponding raw SQL before finally running php yii migrate?

For example, in Laravel you can add a flag --pretend to output the queries to the terminal, like in php artisan migrate --pretend. Is there anything similar in Yii Framework?

2

Answers


  1. Chosen as BEST ANSWER

    As @Bizley said when commenting the question, there is not a native way of doing it for Yii2 until the moment this question was answered.

    For my case I just tracked my MySQL DB log and picked the queries I was looking for, since I had to set the current DB into a new one through migrations.


  2. Just install Bizley’s migration package and try php yii migration/sql m220914_000000_create_clients_table for example, and the corresponding SQL statements can be extracted, like:

    $ php yii migration/sql m220914_000001_create_client_member_levels_table
    Yii 2 Migration Generator Tool v4.4.0
     > SQL statements of the m220914_000001_create_client_member_levels_table file (UP method):
    
    CREATE TABLE `client_member_levels` (
        `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        `level_identifier` varchar(255) NOT NULL DEFAULT 'employee',
        `display_name` varchar(255) NOT NULL DEFAULT 'Employee',
        `client_id` int(11) NOT NULL,
        `created_at` datetime(0) NOT NULL DEFAULT NOW(),
        `updated_at` datetime(0),
        `deleted_at` datetime(0)
    );
    ALTER TABLE `client_member_levels` ADD INDEX `idx-client_member_levels-client_id` (`client_id`);
    ALTER TABLE `client_member_levels` ADD CONSTRAINT `fk-client_member_levels-client_id` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) ON DELETE CASCADE;
    
     (!) Note that the above statements were not executed.
    
    $
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search