fully prefixed name. * * @param string $name One of runs, run_items, run_events, exports. * @return string */ public static function table( $name ) { global $wpdb; return $wpdb->prefix . 'recast_' . $name; // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Recast-owned table, prepared query. } /** * All table short names. * * @return string[] */ public static function tables() { return array( 'runs', 'run_items', 'run_events', 'exports' ); } /** * CREATE TABLE statements in dbDelta-compatible format. * * @return string[] */ public static function statements() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Recast-owned table, prepared query. $runs = self::table( 'runs' ); $run_items = self::table( 'run_items' ); $run_events = self::table( 'run_events' ); $exports = self::table( 'exports' ); return array( "CREATE TABLE {$runs} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, uuid char(36) NOT NULL, relation varchar(20) NOT NULL DEFAULT 'operation', parent_run_id bigint(20) unsigned DEFAULT NULL, status varchar(24) NOT NULL, operation_id varchar(64) NOT NULL, operation_params longtext, cohort_spec longtext, cohort_hash char(64) NOT NULL DEFAULT '', fingerprint_fields text, fp_version tinyint(3) unsigned NOT NULL DEFAULT 1, summary longtext, resolve_offset int(10) unsigned NOT NULL DEFAULT 0, cursor_item_id bigint(20) unsigned NOT NULL DEFAULT 0, chunk_size smallint(5) unsigned NOT NULL DEFAULT 25, lanes tinyint(3) unsigned NOT NULL DEFAULT 1, created_by bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, previewed_at datetime DEFAULT NULL, expires_at datetime DEFAULT NULL, started_at datetime DEFAULT NULL, finished_at datetime DEFAULT NULL, last_error text, PRIMARY KEY (id), UNIQUE KEY uuid (uuid), KEY status_created (status,created_at), KEY parent_run (parent_run_id), KEY expires (expires_at) ) {$charset_collate};", "CREATE TABLE {$run_items} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, run_id bigint(20) unsigned NOT NULL, subscription_id bigint(20) unsigned NOT NULL, position int(10) unsigned NOT NULL DEFAULT 0, idem_key char(40) NOT NULL, status varchar(16) NOT NULL DEFAULT 'pending', before_hash char(64) DEFAULT NULL, after_hash char(64) DEFAULT NULL, before_state longtext, after_state longtext, diff longtext, warnings text, error text, attempts tinyint(3) unsigned NOT NULL DEFAULT 0, worker_token char(36) DEFAULT NULL, claimed_at datetime DEFAULT NULL, applied_at datetime DEFAULT NULL, reverted_by_run_id bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY run_sub (run_id,subscription_id), UNIQUE KEY idem (idem_key), KEY run_status_id (run_id,status,id), KEY run_worker (run_id,worker_token), KEY subscription (subscription_id,applied_at) ) {$charset_collate};", "CREATE TABLE {$run_events} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, run_id bigint(20) unsigned NOT NULL, level varchar(8) NOT NULL DEFAULT 'info', code varchar(48) NOT NULL, message text, context text, created_at datetime NOT NULL, PRIMARY KEY (id), KEY run_created (run_id,created_at) ) {$charset_collate};", "CREATE TABLE {$exports} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, spec_json longtext, spec_hash char(40) NOT NULL DEFAULT '', mode varchar(12) NOT NULL DEFAULT 'stream', status varchar(12) NOT NULL DEFAULT 'pending', rows_written int(10) unsigned NOT NULL DEFAULT 0, candidate_total int(10) unsigned DEFAULT NULL, file_name varchar(190) DEFAULT NULL, file_size bigint(20) unsigned DEFAULT NULL, created_by bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, finished_at datetime DEFAULT NULL, expires_at datetime DEFAULT NULL, error text, resume_cursor longtext, PRIMARY KEY (id), KEY status_expires (status,expires_at), KEY created_by (created_by,created_at) ) {$charset_collate};", ); } /** * Creates or updates all tables. * * @return void */ public static function install() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( self::statements() as $sql ) { dbDelta( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- plugin tables. } } /** * Drops all tables. Only called by the uninstaller when purging is enabled. * * @return void */ public static function drop() { global $wpdb; foreach ( self::tables() as $name ) { $table = self::table( $name ); $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Recast-owned table, prepared query. } } /** * Whether every table exists. * * @return bool */ public static function exists() { global $wpdb; foreach ( self::tables() as $name ) { $table = self::table( $name ); if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Recast-owned table, prepared query. return false; } } return true; } }