From 77e4429f6a7d4eeacc44e5913bedf0431d5e2c10 Mon Sep 17 00:00:00 2001 From: John Hooks Date: Thu, 20 Apr 2023 21:01:05 -0700 Subject: [PATCH] refactor: extract db table sql to Schema class --- includes/class-activator.php | 46 +++++---------------- includes/class-uninstaller.php | 7 +++- includes/database/class-schema.php | 66 ++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 includes/database/class-schema.php diff --git a/includes/class-activator.php b/includes/class-activator.php index 19041da2..bb7b215f 100644 --- a/includes/class-activator.php +++ b/includes/class-activator.php @@ -1,13 +1,18 @@ get_charset_collate(); + require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/database/class-schema.php'; $tables = $wpdb->get_results( 'SHOW TABLES' ); // Create the messages table if ( ! in_array( $wpdb->prefix . 'notifications_messages', $tables, true ) ) { - $messages_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_messages` ( - `id` BIGINT(20) NOT NULL, - `channel_name` VARCHAR(50) NOT NULL, - `channel_title` TINYTEXT NOT NULL, - `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(), - `expires_at` DATETIME NULL, - `severity` VARCHAR(16) NULL, - `title` TINYTEXT NULL, - `message` TINYTEXT NULL, - `meta` TEXT NULL, - PRIMARY KEY (`id`), - KEY `channel_name` (`channel_name`) - ) $charset_collate;\n"; - + $messages_sql = Database\Schema::messages_table_v1(); dbDelta( $messages_sql ); } // Create the subscriptions table if ( ! in_array( $wpdb->prefix . 'notifications_subscriptions', $tables, true ) ) { - $subscriptions_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_subscriptions` ( - `user_id` BIGINT(20) NOT NULL, - `channel_name` VARCHAR(50) NOT NULL, - `snoozed_until` DATETIME NULL, - KEY `user_id` (`user_id`), - KEY `channel_name` (`channel_name`) - ) $charset_collate;\n"; - + $subscriptions_sql = Database\Schema::subscriptions_table_v1(); dbDelta( $subscriptions_sql ); } // Create the queue table if ( ! in_array( $wpdb->prefix . 'notifications_queue', $tables, true ) ) { - $queue_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_queue` ( - `message_id` BIGINT(20) NOT NULL, - `user_id` BIGINT(20) NOT NULL, - `dismissed_at` DATETIME NULL, - `displayed_at` DATETIME NULL, - KEY `message_id` (`message_id`), - KEY `user_id` (`user_id`) - ) $charset_collate;\n"; - + $queue_sql = Database\Schema::queue_table_v1(); dbDelta( $queue_sql ); } } diff --git a/includes/class-uninstaller.php b/includes/class-uninstaller.php index ac8e51cf..19289993 100644 --- a/includes/class-uninstaller.php +++ b/includes/class-uninstaller.php @@ -1,4 +1,9 @@ get_charset_collate(); + + return 'CREATE TABLE `' . $wpdb->prefix . "notifications_messages` ( + `id` BIGINT(20) NOT NULL, + `channel_name` VARCHAR(50) NOT NULL, + `channel_title` TINYTEXT NOT NULL, + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(), + `expires_at` DATETIME NULL, + `severity` VARCHAR(16) NULL, + `title` TINYTEXT NULL, + `message` TINYTEXT NULL, + `meta` TEXT NULL, + PRIMARY KEY (`id`), + KEY `channel_name` (`channel_name`) + ) $charset_collate;\n"; + } + + + public static function subscriptions_table_v1() { + global $wpdb; + + $charset_collate = $wpdb->get_charset_collate(); + + return 'CREATE TABLE `' . $wpdb->prefix . "notifications_subscriptions` ( + `user_id` BIGINT(20) NOT NULL, + `channel_name` VARCHAR(50) NOT NULL, + `snoozed_until` DATETIME NULL, + KEY `user_id` (`user_id`), + KEY `channel_name` (`channel_name`) + ) $charset_collate;\n"; + } + + public static function queue_table_v1() { + global $wpdb; + + $charset_collate = $wpdb->get_charset_collate(); + + return 'CREATE TABLE `' . $wpdb->prefix . "notifications_queue` ( + `message_id` BIGINT(20) NOT NULL, + `user_id` BIGINT(20) NOT NULL, + `dismissed_at` DATETIME NULL, + `displayed_at` DATETIME NULL, + KEY `message_id` (`message_id`), + KEY `user_id` (`user_id`) + ) $charset_collate;\n"; + } +}