2020-06-14 03:50:31 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
use console\db\Migration;
|
|
|
|
|
|
|
|
class m200613_204832_remove_webhooks_events_table extends Migration {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function safeUp(): void {
|
2020-06-14 03:50:31 +05:30
|
|
|
$this->addColumn('webhooks', 'events', $this->json()->toString('events') . ' AFTER `secret`');
|
|
|
|
$webhooksIds = $this->db->createCommand('SELECT id FROM webhooks')->queryColumn();
|
|
|
|
foreach ($webhooksIds as $webhookId) {
|
|
|
|
$events = $this->db->createCommand("SELECT event_type FROM webhooks_events WHERE webhook_id = {$webhookId}")->queryColumn();
|
|
|
|
if (empty($events)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->execute('UPDATE webhooks SET events = JSON_ARRAY("' . implode('","', $events) . '")');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dropTable('webhooks_events');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function safeDown(): void {
|
2020-06-14 03:50:31 +05:30
|
|
|
$this->createTable('webhooks_events', [
|
|
|
|
'webhook_id' => $this->db->getTableSchema('webhooks')->getColumn('id')->dbType . ' NOT NULL',
|
|
|
|
'event_type' => $this->string()->notNull(),
|
|
|
|
$this->primary('webhook_id', 'event_type'),
|
|
|
|
]);
|
|
|
|
$this->addForeignKey('FK_webhook_event_to_webhook', 'webhooks_events', 'webhook_id', 'webhooks', 'id', 'CASCADE', 'CASCADE');
|
|
|
|
|
|
|
|
$webhooks = $this->db->createCommand('SELECT id, `events` FROM webhooks')->queryAll();
|
|
|
|
foreach ($webhooks as $webhook) {
|
|
|
|
if (empty($webhook['events'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
$events = json_decode((string)$webhook['events'], true);
|
2020-06-14 03:50:31 +05:30
|
|
|
if (empty($events)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->batchInsert(
|
|
|
|
'webhooks_events',
|
|
|
|
['webhook_id', 'event_type'],
|
2024-12-02 15:40:55 +05:30
|
|
|
array_map(fn($event): array => [$webhook['id'], $event], $events),
|
2020-06-14 03:50:31 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dropColumn('webhooks', 'events');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|