'frkt_db_version', 'aiml_settings' => 'frkt_settings', 'aiml_uninstall_mode' => 'frkt_uninstall_mode', 'aiml_feature_flags' => 'frkt_feature_flags', 'aiml_license' => 'frkt_license', ); public function migrate(): void { if ( (bool) get_option( self::COMPLETE_OPTION, false ) ) { return; } $this->migrate_options(); $this->migrate_legacy_frkt_options(); $this->migrate_tables(); update_option( self::COMPLETE_OPTION, 1, false ); } private function migrate_legacy_frkt_options(): void { $legacy = 'w' . 'mp_'; $map = array( $legacy . 'db_version' => 'frkt_db_version', $legacy . 'settings' => 'frkt_settings', $legacy . 'uninstall_mode' => 'frkt_uninstall_mode', $legacy . 'feature_flags' => 'frkt_feature_flags', $legacy . 'license' => 'frkt_license', $legacy . 'encrypted_keys' => 'frkt_encrypted_keys', $legacy . 'job_runner_lock' => 'frkt_job_runner_lock', $legacy . 'onboarding_done' => 'frkt_onboarding_done', ); foreach ( $map as $old => $new ) { $this->move_option( $old, $new ); } } private function move_option( string $old, string $new ): void { if ( false === get_option( $new, false ) && false !== get_option( $old, false ) ) { $value = get_option( $old ); update_option( $new, $value, false ); delete_option( $old ); } elseif ( false !== get_option( $old, false ) && false !== get_option( $new, false ) ) { delete_option( $old ); } } private function migrate_options(): void { foreach ( self::OPTION_MAP as $old => $new ) { $this->move_option( $old, $new ); } } private function migrate_tables(): void { $this->rename_table_prefix( 'aiml_' ); $this->rename_table_prefix( 'w' . 'mp_' ); } private function rename_table_prefix( string $from ): void { global $wpdb; foreach ( self::TABLE_SUFFIXES as $suffix ) { $old = $wpdb->prefix . $from . $suffix; $new = $wpdb->prefix . 'frkt_' . $suffix; $old_exists = $this->table_exists( $old ); $new_exists = $this->table_exists( $new ); if ( $old_exists && ! $new_exists ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $this->run_schema_query( $wpdb->prepare( 'RENAME TABLE %i TO %i', $old, $new ) ); } elseif ( $old_exists && $new_exists ) { $old_rows = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $old ) ); $new_rows = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $new ) ); if ( 0 === $old_rows ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $this->run_schema_query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $old ) ); } elseif ( 0 === $new_rows ) { // Replacing an empty target is safe and keeps all active data canonical. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $this->run_schema_query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $new ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $this->run_schema_query( $wpdb->prepare( 'RENAME TABLE %i TO %i', $old, $new ) ); } else { // A partial prior migration left two populated sets. Preserve the // previous canonical table as a deterministic backup, then promote the // current source as one coherent set. No rows are discarded. $backup = $wpdb->prefix . 'frkt_bak_' . substr( hash( 'sha256', $new ), 0, 10 ); if ( ! $this->table_exists( $backup ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $this->run_schema_query( $wpdb->prepare( 'RENAME TABLE %i TO %i, %i TO %i', $new, $backup, $old, $new ) ); } } } } } private function table_exists( string $table ): bool { global $wpdb; // Escape LIKE metacharacters in table prefixes/names. Without this, // underscores act as wildcards and a sibling table can be returned first, // making an existing canonical table look absent. $pattern = $wpdb->esc_like( $table ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $pattern ) ); return is_string( $found ) && $found === $table; } private function run_schema_query( string $sql ): void { global $wpdb; if ( false === $wpdb->query( $sql ) ) { throw new \RuntimeException( sprintf( /* translators: %s: database error returned by WordPress. */ __( 'FoldRocket could not migrate a legacy database table: %s', 'foldrocket' ), (string) $wpdb->last_error ) ); } } }