prefix.DB::$namespace.'_whatsapp_conversations'; } public function __construct() { parent::__construct(); $this->table_name = $this->prefix.'_whatsapp_conversations'; $this->primary_key = 'id'; $this->version = '1.0'; } public function sql_create() { return "CREATE TABLE {$this->table_name} ( id bigint(20) NOT NULL AUTO_INCREMENT, restaurant_id bigint(20) NOT NULL, customer_id bigint(20) DEFAULT NULL, active_booking_id bigint(20) DEFAULT NULL, phone varchar(50) NOT NULL, phone_normalized varchar(20) NOT NULL, profile_name varchar(255) DEFAULT NULL, wa_id varchar(50) DEFAULT NULL, bsuid varchar(150) DEFAULT NULL, status varchar(20) DEFAULT 'open', customer_status varchar(30) DEFAULT 'IDLE', window_expires_at datetime DEFAULT NULL, last_message_at datetime DEFAULT NULL, last_inbound_at datetime DEFAULT NULL, last_outbound_at datetime DEFAULT NULL, unread_count int unsigned DEFAULT 0, message_count int unsigned DEFAULT 0, date_created datetime NOT NULL, date_modified datetime NOT NULL, PRIMARY KEY (id), KEY `wa_conv_restaurant_id_foreign` (`restaurant_id`), CONSTRAINT `wa_conv_restaurant_id_foreign` FOREIGN KEY (`restaurant_id`) REFERENCES `{$this->prefix}_restaurants` (`id`) ON DELETE CASCADE, KEY `wa_conv_customer_id_foreign` (`customer_id`), CONSTRAINT `wa_conv_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `{$this->prefix}_customers` (`id`) ON DELETE CASCADE, KEY `wa_conv_phone_normalized` (`phone_normalized`), UNIQUE KEY `wa_conv_unique_phone` (`restaurant_id`, `phone_normalized`), KEY `wa_conv_bsuid` (`bsuid`), KEY `wa_conv_status` (`status`), KEY `wa_conv_customer_status` (`customer_status`), KEY `wa_conv_window_expires` (`window_expires_at`) )"; //" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; } public function sql_update_columns_releases() { //ray('ACTUALIZANDO LA TABLA de conversations'); // No incremental migrations needed — table is always recreated from scratch via sql_create(). global $wpdb; $col_exists = $wpdb->get_var(" SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{$this->table_name}' AND COLUMN_NAME = 'active_booking_id' "); // Si no existe la columna es que es antigua tabla y hay que borrarla if ( ! $col_exists ) { //ray('BORRANDO LA TABLA de conversations'); $wpdb->query("SET FOREIGN_KEY_CHECKS=0"); $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}"); $wpdb->query("SET FOREIGN_KEY_CHECKS=1"); $this->sql_create(); } } } SRR_DB_Whatsapp_Conversations::init();