1

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?

victorf
  • 978
  • 2
  • 17
  • 35
  • 2
    There is no way of doing it out-of-the-box in Yii 2 (you can only log queries prior to the execution) but it gives me a nice idea of a new feature in my package https://github.com/bizley/yii2-migration :) thanks! – Bizley Sep 21 '22 at 13:11
  • 1
    https://github.com/bizley/yii2-migration ready with that feature ;) – Bizley Oct 10 '22 at 17:21

2 Answers2

0

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.

victorf
  • 978
  • 2
  • 17
  • 35
0

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.

$
victorf
  • 978
  • 2
  • 17
  • 35