|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +use Migrations\BaseMigration; |
| 5 | + |
| 6 | +/** |
| 7 | + * Example migration demonstrating views and triggers support. |
| 8 | + * |
| 9 | + * This migration shows how to create and drop database views and triggers |
| 10 | + * using the CakePHP Migrations plugin. |
| 11 | + */ |
| 12 | +class ViewsAndTriggersExample extends BaseMigration |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Change Method. |
| 16 | + * |
| 17 | + * Write your reversible migrations using this method. |
| 18 | + * |
| 19 | + * More information on writing migrations is available here: |
| 20 | + * https://book.cakephp.org/migrations/4/en/index.html |
| 21 | + */ |
| 22 | + public function change(): void |
| 23 | + { |
| 24 | + // Create a users table |
| 25 | + $users = $this->table('users'); |
| 26 | + $users->addColumn('username', 'string', ['limit' => 100]) |
| 27 | + ->addColumn('email', 'string', ['limit' => 255]) |
| 28 | + ->addColumn('status', 'string', ['limit' => 20, 'default' => 'active']) |
| 29 | + ->addColumn('created', 'datetime') |
| 30 | + ->create(); |
| 31 | + |
| 32 | + // Create a posts table |
| 33 | + $posts = $this->table('posts'); |
| 34 | + $posts->addColumn('user_id', 'integer') |
| 35 | + ->addColumn('title', 'string', ['limit' => 255]) |
| 36 | + ->addColumn('body', 'text') |
| 37 | + ->addColumn('published', 'boolean', ['default' => false]) |
| 38 | + ->addColumn('created', 'datetime') |
| 39 | + ->addForeignKey('user_id', 'users', 'id', ['delete' => 'CASCADE']) |
| 40 | + ->create(); |
| 41 | + |
| 42 | + // Create a view showing active users with their post counts |
| 43 | + // Note: Views are created through a dummy table object |
| 44 | + $this->createView( |
| 45 | + 'active_users_with_posts', |
| 46 | + 'SELECT u.id, u.username, u.email, COUNT(p.id) as post_count |
| 47 | + FROM users u |
| 48 | + LEFT JOIN posts p ON u.id = p.user_id |
| 49 | + WHERE u.status = \'active\' |
| 50 | + GROUP BY u.id, u.username, u.email' |
| 51 | + ); |
| 52 | + |
| 53 | + // Create a materialized view (PostgreSQL only) |
| 54 | + // On other databases, this will create a regular view |
| 55 | + $this->createView( |
| 56 | + 'published_posts_summary', |
| 57 | + 'SELECT user_id, COUNT(*) as published_count |
| 58 | + FROM posts |
| 59 | + WHERE published = 1 |
| 60 | + GROUP BY user_id', |
| 61 | + ['materialized' => true] |
| 62 | + ); |
| 63 | + |
| 64 | + // Create an audit log table for triggers |
| 65 | + $auditLog = $this->table('audit_log'); |
| 66 | + $auditLog->addColumn('table_name', 'string', ['limit' => 100]) |
| 67 | + ->addColumn('action', 'string', ['limit' => 20]) |
| 68 | + ->addColumn('record_id', 'integer') |
| 69 | + ->addColumn('created', 'datetime') |
| 70 | + ->create(); |
| 71 | + |
| 72 | + // Create a trigger to log user insertions |
| 73 | + // Note: The trigger definition syntax varies by database |
| 74 | + |
| 75 | + // For MySQL: |
| 76 | + $this->createTrigger( |
| 77 | + 'users', |
| 78 | + 'log_user_insert', |
| 79 | + 'INSERT', |
| 80 | + "INSERT INTO audit_log (table_name, action, record_id, created) |
| 81 | + VALUES ('users', 'INSERT', NEW.id, NOW())", |
| 82 | + ['timing' => 'AFTER'] |
| 83 | + ); |
| 84 | + |
| 85 | + // For PostgreSQL, you would need to create a function first: |
| 86 | + // $this->execute(" |
| 87 | + // CREATE OR REPLACE FUNCTION log_user_insert_func() |
| 88 | + // RETURNS TRIGGER AS $$ |
| 89 | + // BEGIN |
| 90 | + // INSERT INTO audit_log (table_name, action, record_id, created) |
| 91 | + // VALUES ('users', 'INSERT', NEW.id, NOW()); |
| 92 | + // RETURN NEW; |
| 93 | + // END; |
| 94 | + // $$ LANGUAGE plpgsql; |
| 95 | + // "); |
| 96 | + // |
| 97 | + // $this->createTrigger( |
| 98 | + // 'users', |
| 99 | + // 'log_user_insert', |
| 100 | + // 'INSERT', |
| 101 | + // 'log_user_insert_func()', // Function name for PostgreSQL |
| 102 | + // ['timing' => 'AFTER'] |
| 103 | + // ); |
| 104 | + |
| 105 | + // Create a trigger for updates with multiple events |
| 106 | + $this->createTrigger( |
| 107 | + 'posts', |
| 108 | + 'log_post_changes', |
| 109 | + ['UPDATE', 'DELETE'], |
| 110 | + "INSERT INTO audit_log (table_name, action, record_id, created) |
| 111 | + VALUES ('posts', 'CHANGE', OLD.id, NOW())", |
| 112 | + ['timing' => 'BEFORE'] |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Migrate Up. |
| 118 | + * |
| 119 | + * If you need more control, you can use up() and down() methods instead. |
| 120 | + */ |
| 121 | + public function up(): void |
| 122 | + { |
| 123 | + // Example of creating a view in up() method |
| 124 | + $this->createView( |
| 125 | + 'simple_user_list', |
| 126 | + 'SELECT id, username FROM users' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Migrate Down. |
| 132 | + */ |
| 133 | + public function down(): void |
| 134 | + { |
| 135 | + // Drop views |
| 136 | + $this->dropView('simple_user_list'); |
| 137 | + $this->dropView('active_users_with_posts'); |
| 138 | + $this->dropView('published_posts_summary', ['materialized' => true]); |
| 139 | + |
| 140 | + // Drop triggers |
| 141 | + $this->dropTrigger('users', 'log_user_insert'); |
| 142 | + $this->dropTrigger('posts', 'log_post_changes'); |
| 143 | + |
| 144 | + // Drop tables |
| 145 | + $this->table('audit_log')->drop()->save(); |
| 146 | + $this->table('posts')->drop()->save(); |
| 147 | + $this->table('users')->drop()->save(); |
| 148 | + } |
| 149 | +} |
0 commit comments