Skip to content

Commit

Permalink
feat(Runs): convert temporary alerts column to bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 7, 2024
1 parent 6c3c3bc commit 4672230
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions includes/tables/class-test-runs-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function create_runs_from_alerts() {
'permalink' => get_permalink( $alert->post_id ),
],
] ),
'alerts' => maybe_serialize( [ $alert->id ] ),
'alert_id' => $alert->id,
'trigger' => 'legacy',
'started_at' => $alert->finished_at,
'finished_at' => $alert->finished_at,
Expand All @@ -116,21 +116,21 @@ public static function create_runs_from_alerts() {
return "('" . implode( "','", array_map( 'esc_sql', $run ) ) . "')";
}, $test_runs));

// add alerts column to test_runs table.
// add alert_id column to test_runs table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "ALTER TABLE {$runs_table} ADD COLUMN alerts text;" );
$wpdb->query( "ALTER TABLE {$runs_table} ADD COLUMN alert_id bigint(20) unsigned;" );

// insert all test runs with single query.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "INSERT INTO {$runs_table} (tests, alerts, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );
$wpdb->query( "INSERT INTO {$runs_table} (tests, alert_id, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );

// update test_run_id in alerts table from newly created test runs based on alerts column.
// update test_run_id in alerts table from newly created test runs based on alert_id column.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alerts LIKE CONCAT('%\"', a.id, '\"%') SET a.test_run_id = r.id;" );
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alert_id = a.id SET a.test_run_id = r.id;" );

// remove alerts column from test_runs table.
// remove alert_id column from test_runs table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "ALTER TABLE {$runs_table} DROP COLUMN alerts;" );
$wpdb->query( "ALTER TABLE {$runs_table} DROP COLUMN alert_id;" );

return true;
}
Expand Down

0 comments on commit 4672230

Please sign in to comment.