initial
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Vrts\Tables;
|
||||
|
||||
class Alerts_Table {
|
||||
|
||||
const DB_VERSION = '1.2';
|
||||
const TABLE_NAME = 'vrts_alerts';
|
||||
|
||||
/**
|
||||
* Get the name of the table.
|
||||
*/
|
||||
public static function get_table_name() {
|
||||
global $wpdb;
|
||||
return $wpdb->prefix . self::TABLE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update the database table for tests.
|
||||
*/
|
||||
public static function install_table() {
|
||||
$option_name = self::TABLE_NAME . '_db_version';
|
||||
$installed_version = get_option( $option_name );
|
||||
|
||||
if ( self::DB_VERSION !== $installed_version ) {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
if ( $installed_version && version_compare( $installed_version, '1.1', '<' ) ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's OK.
|
||||
$wpdb->query(
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's OK.
|
||||
"ALTER TABLE {$table_name} MODIFY alert_state tinyint NOT NULL DEFAULT 0"
|
||||
);
|
||||
}
|
||||
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
title text,
|
||||
post_id bigint(20),
|
||||
test_run_id bigint(20),
|
||||
screenshot_test_id varchar(40),
|
||||
target_screenshot_url varchar(2048),
|
||||
target_screenshot_finish_date datetime,
|
||||
base_screenshot_url varchar(2048),
|
||||
base_screenshot_finish_date datetime,
|
||||
comparison_screenshot_url varchar(2048),
|
||||
comparison_id varchar(40),
|
||||
differences int(4),
|
||||
alert_state tinyint NOT NULL DEFAULT 0,
|
||||
is_false_positive tinyint NOT NULL DEFAULT 0,
|
||||
meta text,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
dbDelta( $sql );
|
||||
|
||||
if ( $installed_version && version_compare( $installed_version, '1.2', '<' ) ) {
|
||||
static::set_is_false_positive_from_alert_state();
|
||||
}
|
||||
|
||||
update_option( $option_name, self::DB_VERSION );
|
||||
}//end if
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the database table for tests.
|
||||
*/
|
||||
public static function uninstall_table() {
|
||||
global $wpdb;
|
||||
$table_name = self::get_table_name();
|
||||
$sql = "DROP TABLE IF EXISTS {$table_name};";
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
|
||||
$wpdb->query( $sql );
|
||||
|
||||
delete_option( self::TABLE_NAME . '_db_version' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set is_false_positive to 1 for all alerts that have alert_state set to 2.
|
||||
*/
|
||||
protected static function set_is_false_positive_from_alert_state() {
|
||||
global $wpdb;
|
||||
$table_name = self::get_table_name();
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$wpdb->query(
|
||||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
"UPDATE {$table_name} SET is_false_positive = 1, alert_state = 1 WHERE alert_state = 2"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace Vrts\Tables;
|
||||
|
||||
class Test_Runs_Table {
|
||||
|
||||
const DB_VERSION = '1.1';
|
||||
const TABLE_NAME = 'vrts_test_runs';
|
||||
|
||||
/**
|
||||
* Get the name of the table.
|
||||
*/
|
||||
public static function get_table_name() {
|
||||
global $wpdb;
|
||||
return $wpdb->prefix . self::TABLE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update the database table for tests.
|
||||
*/
|
||||
public static function install_table() {
|
||||
$option_name = self::TABLE_NAME . '_db_version';
|
||||
$installed_version = get_option( $option_name );
|
||||
|
||||
if ( self::DB_VERSION !== $installed_version ) {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
service_test_run_id varchar(40),
|
||||
tests text,
|
||||
`trigger` varchar(20),
|
||||
trigger_notes text,
|
||||
trigger_meta text default NULL,
|
||||
started_at datetime,
|
||||
scheduled_at datetime,
|
||||
finished_at datetime,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
dbDelta( $sql );
|
||||
|
||||
if ( ! $installed_version ) {
|
||||
if ( static::create_runs_from_alerts() ) {
|
||||
update_option( 'vrts_test_runs_has_migrated_alerts', true );
|
||||
}
|
||||
}
|
||||
|
||||
update_option( $option_name, self::DB_VERSION );
|
||||
}//end if
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the database table for tests.
|
||||
*/
|
||||
public static function uninstall_table() {
|
||||
global $wpdb;
|
||||
$table_name = self::get_table_name();
|
||||
$sql = "DROP TABLE IF EXISTS {$table_name};";
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
|
||||
$wpdb->query( $sql );
|
||||
|
||||
delete_option( self::TABLE_NAME . '_db_version' );
|
||||
delete_option( 'vrts_test_runs_has_migrated_alerts' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate alerts from the old table.
|
||||
*/
|
||||
public static function create_runs_from_alerts() {
|
||||
global $wpdb;
|
||||
$alerts_table = Alerts_Table::get_table_name();
|
||||
$tests_table = Tests_Table::get_table_name();
|
||||
$runs_table = self::get_table_name();
|
||||
|
||||
$sql = "SELECT
|
||||
a.id as id,
|
||||
a.target_screenshot_finish_date as finished_at,
|
||||
t.id as test_id,
|
||||
a.post_id as post_id
|
||||
FROM {$alerts_table} a
|
||||
JOIN {$tests_table} t
|
||||
ON t.post_id = a.post_id
|
||||
WHERE a.test_run_id IS NULL;
|
||||
";
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
|
||||
$alerts = $wpdb->get_results( $sql );
|
||||
|
||||
if ( empty( $alerts ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$test_runs = array_map(function ( $alert ) {
|
||||
return [
|
||||
'tests' => maybe_serialize( [
|
||||
[
|
||||
'id' => $alert->test_id,
|
||||
'post_id' => $alert->post_id,
|
||||
'post_title' => get_the_title( $alert->post_id ),
|
||||
'permalink' => get_permalink( $alert->post_id ),
|
||||
],
|
||||
] ),
|
||||
'alert_id' => $alert->id,
|
||||
'trigger' => 'legacy',
|
||||
'started_at' => $alert->finished_at,
|
||||
'finished_at' => $alert->finished_at,
|
||||
];
|
||||
}, $alerts);
|
||||
|
||||
$test_runs_values = implode( ',', array_map(function ( $run ) {
|
||||
return "('" . implode( "','", array_map( 'esc_sql', $run ) ) . "')";
|
||||
}, $test_runs));
|
||||
|
||||
// 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 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, 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 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.alert_id = a.id SET a.test_run_id = r.id;" );
|
||||
|
||||
// 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 alert_id;" );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Vrts\Tables;
|
||||
|
||||
class Tests_Table {
|
||||
|
||||
const DB_VERSION = '1.5';
|
||||
const TABLE_NAME = 'vrts_tests';
|
||||
|
||||
/**
|
||||
* Get the name of the table.
|
||||
*/
|
||||
public static function get_table_name() {
|
||||
global $wpdb;
|
||||
return $wpdb->prefix . self::TABLE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update the database table for tests.
|
||||
*/
|
||||
public static function install_table() {
|
||||
$option_name = self::TABLE_NAME . '_db_version';
|
||||
$installed_version = get_option( $option_name );
|
||||
if ( self::DB_VERSION !== $installed_version ) {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
if ( $installed_version && version_compare( $installed_version, '1.3', '<' ) ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's OK.
|
||||
$wpdb->query(
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's OK.
|
||||
"ALTER TABLE {$table_name} RENAME COLUMN target_screenshot_url TO base_screenshot_url;"
|
||||
);
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's OK.
|
||||
$wpdb->query(
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's OK.
|
||||
"ALTER TABLE {$table_name} RENAME COLUMN snapshot_date TO base_screenshot_date;"
|
||||
);
|
||||
}
|
||||
|
||||
if ( $installed_version && version_compare( $installed_version, '1.5', '<' ) ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's OK.
|
||||
$wpdb->query(
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's OK.
|
||||
"ALTER TABLE {$table_name} DROP COLUMN current_alert_id;"
|
||||
);
|
||||
}
|
||||
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
status boolean NOT NULL,
|
||||
post_id bigint(20),
|
||||
service_test_id varchar(40),
|
||||
base_screenshot_url varchar(2048),
|
||||
base_screenshot_date datetime,
|
||||
last_comparison_date datetime,
|
||||
next_run_date datetime,
|
||||
is_running boolean,
|
||||
hide_css_selectors longtext,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
dbDelta( $sql );
|
||||
|
||||
update_option( $option_name, self::DB_VERSION );
|
||||
}//end if
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the database table for tests.
|
||||
*/
|
||||
public static function uninstall_table() {
|
||||
global $wpdb;
|
||||
$table_name = self::get_table_name();
|
||||
$sql = "DROP TABLE IF EXISTS {$table_name};";
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
|
||||
$wpdb->query( $sql );
|
||||
|
||||
delete_option( self::TABLE_NAME . '_db_version' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user