diff --git a/.gitignore b/.gitignore index ea8c4bf7..1f430c10 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /target +AGENTS.md +LOGGING_DESIGN.md +MIGRATION_STRATEGY.md diff --git a/migrations/mysql/00001_init_schema.down.sql b/migrations/mysql/00001_init_schema.down.sql new file mode 100644 index 00000000..4b8470b7 --- /dev/null +++ b/migrations/mysql/00001_init_schema.down.sql @@ -0,0 +1,36 @@ +-- 000001_init_schema.down.sql +SET FOREIGN_KEY_CHECKS = 0; + +DROP TABLE IF EXISTS `user_subscribe_log`; +DROP TABLE IF EXISTS `user_subscribe`; +DROP TABLE IF EXISTS `user_login_log`; +DROP TABLE IF EXISTS `user_gift_amount_log`; +DROP TABLE IF EXISTS `user_device`; +DROP TABLE IF EXISTS `user_commission_log`; +DROP TABLE IF EXISTS `user_balance_log`; +DROP TABLE IF EXISTS `user_auth_methods`; +DROP TABLE IF EXISTS `user`; +DROP TABLE IF EXISTS `traffic_log`; +DROP TABLE IF EXISTS `ticket_follow`; +DROP TABLE IF EXISTS `ticket`; +DROP TABLE IF EXISTS `system`; +DROP TABLE IF EXISTS `subscribe_type`; +DROP TABLE IF EXISTS `subscribe_group`; +DROP TABLE IF EXISTS `subscribe`; +DROP TABLE IF EXISTS `sms`; +DROP TABLE IF EXISTS `server_rule_group`; +DROP TABLE IF EXISTS `server_group`; +DROP TABLE IF EXISTS `server`; +DROP TABLE IF EXISTS `payment`; +DROP TABLE IF EXISTS `order`; +DROP TABLE IF EXISTS `message_log`; +DROP TABLE IF EXISTS `document`; +DROP TABLE IF EXISTS `coupon`; +DROP TABLE IF EXISTS `auth_method`; +DROP TABLE IF EXISTS `application_version`; +DROP TABLE IF EXISTS `application_config`; +DROP TABLE IF EXISTS `application`; +DROP TABLE IF EXISTS `announcement`; +DROP TABLE IF EXISTS `ads`; + +SET FOREIGN_KEY_CHECKS = 1; \ No newline at end of file diff --git a/migrations/mysql/00002_init_basic_data.down.sql b/migrations/mysql/00002_init_basic_data.down.sql new file mode 100644 index 00000000..7d749b28 --- /dev/null +++ b/migrations/mysql/00002_init_basic_data.down.sql @@ -0,0 +1,21 @@ +-- 000002_init_data.down.sql +SET +FOREIGN_KEY_CHECKS = 0; + +DELETE +FROM `auth_method` +WHERE `id` IN (1, 2, 3, 4, 5, 6, 7, 8); +DELETE +FROM `payment` +WHERE `id` = -1; +DELETE +FROM `subscribe_type` +WHERE `id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); +DELETE +FROM `system` +WHERE `id` IN + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41); + +SET +FOREIGN_KEY_CHECKS = 1; \ No newline at end of file diff --git a/migrations/mysql/02003_update_payment.down.sql b/migrations/mysql/02003_update_payment.down.sql new file mode 100644 index 00000000..51715c21 --- /dev/null +++ b/migrations/mysql/02003_update_payment.down.sql @@ -0,0 +1,72 @@ +-- migrations/02003_update_payment.down.sql +-- Purpose: Revert updates to payment and order tables +-- Author: PPanel Team, 2025-04-21 + +SET FOREIGN_KEY_CHECKS = 0; + +-- Drop payment_id column from order table (if exists) +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND COLUMN_NAME = 'payment_id'); +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `order` DROP COLUMN `payment_id`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Drop platform column from payment table (if exists) +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND COLUMN_NAME = 'platform'); +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `payment` DROP COLUMN `platform`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Drop description column from payment table (if exists) +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND COLUMN_NAME = 'description'); +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `payment` DROP COLUMN `description`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Drop token column from payment table (if exists) +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND COLUMN_NAME = 'token'); +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `payment` DROP COLUMN `token`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Optionally restore mark column (if needed, adjust definition as per original schema) +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND COLUMN_NAME = 'mark'); +SET @sql = IF(@column_exists = 0, + 'ALTER TABLE `payment` ADD COLUMN `mark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT \'Payment Mark\' AFTER `name`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET FOREIGN_KEY_CHECKS = 1; \ No newline at end of file diff --git a/migrations/mysql/02004_rebuild_rule.down.sql b/migrations/mysql/02004_rebuild_rule.down.sql new file mode 100644 index 00000000..a818ad88 --- /dev/null +++ b/migrations/mysql/02004_rebuild_rule.down.sql @@ -0,0 +1,4 @@ +-- migrations/02003_rebuild_rule.up.sql +-- Purpose: Back rebuilding server rule table +-- Author: PPanel Team, 2025-04-21 +DROP TABLE IF EXISTS server_rule_group; \ No newline at end of file diff --git a/migrations/mysql/02005_device_online_record.down.sql b/migrations/mysql/02005_device_online_record.down.sql new file mode 100644 index 00000000..e571443d --- /dev/null +++ b/migrations/mysql/02005_device_online_record.down.sql @@ -0,0 +1,52 @@ +-- migrations/02004_create_user_device_online_record.down.sql +-- Purpose: Drop user device online record table +-- Author: PPanel Team, 2025-04-22 + +DROP TABLE IF EXISTS `user_device_online_record`; + +-- User subscribe table migration for removing finished_at column +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'user_subscribe' + AND COLUMN_NAME = 'finished_at'); +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `user_subscribe` DROP COLUMN `finished_at`', + 'SELECT 1' + ); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Application config table migration for removing invitation_link column + +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'application_config' + AND COLUMN_NAME = 'invitation_link'); + +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `application_config` DROP COLUMN `invitation_link`', + 'SELECT 1' + ); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Application config table migration for removing kr_website_id column +SET @column_exists = (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'application_config' + AND COLUMN_NAME = 'kr_website_id'); + +SET @sql = IF(@column_exists > 0, + 'ALTER TABLE `application_config` DROP COLUMN `kr_website_id`', + 'SELECT 1' + ); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; \ No newline at end of file diff --git a/migrations/mysql/02006_reset_subscribe_record.down.sql b/migrations/mysql/02006_reset_subscribe_record.down.sql new file mode 100644 index 00000000..38af3746 --- /dev/null +++ b/migrations/mysql/02006_reset_subscribe_record.down.sql @@ -0,0 +1,5 @@ +-- migrations/02008_create_user_reset_subscribe_log.down.sql +-- Purpose: Drop user_reset_subscribe_log table +-- Author: PPanel Team, 2025-04-22 + +DROP TABLE IF EXISTS `user_reset_subscribe_log`; diff --git a/migrations/mysql/02007_adapte_rule.down.sql b/migrations/mysql/02007_adapte_rule.down.sql new file mode 100644 index 00000000..ed5d22f9 --- /dev/null +++ b/migrations/mysql/02007_adapte_rule.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE `server_rule_group` +DROP COLUMN `default`, +DROP COLUMN `type`; diff --git a/migrations/mysql/02100_task.down.sql b/migrations/mysql/02100_task.down.sql new file mode 100644 index 00000000..0d1f763d --- /dev/null +++ b/migrations/mysql/02100_task.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `email_task`; \ No newline at end of file diff --git a/migrations/mysql/02101_subscribe_application.down.sql b/migrations/mysql/02101_subscribe_application.down.sql new file mode 100644 index 00000000..bca110af --- /dev/null +++ b/migrations/mysql/02101_subscribe_application.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `subscribe_application`; \ No newline at end of file diff --git a/migrations/mysql/02102_subscribe_config.down.sql b/migrations/mysql/02102_subscribe_config.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02103_delete_application.down.sql b/migrations/mysql/02103_delete_application.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02104_system_log.down.sql b/migrations/mysql/02104_system_log.down.sql new file mode 100644 index 00000000..2682c97f --- /dev/null +++ b/migrations/mysql/02104_system_log.down.sql @@ -0,0 +1,106 @@ +CREATE TABLE IF NOT EXISTS `user_balance_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL COMMENT 'User ID', + `amount` bigint NOT NULL COMMENT 'Amount', + `type` tinyint(1) NOT NULL COMMENT 'Type: 1: Recharge 2: Withdraw 3: Payment 4: Refund 5: Reward', + `order_id` bigint DEFAULT NULL COMMENT 'Order ID', + `balance` bigint NOT NULL COMMENT 'Balance', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `user_commission_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL COMMENT 'User ID', + `order_no` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Order No.', + `amount` bigint NOT NULL COMMENT 'Amount', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `user_gift_amount_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL COMMENT 'User ID', + `user_subscribe_id` bigint DEFAULT NULL COMMENT 'Deduction User Subscribe ID', + `order_no` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Order No.', + `type` tinyint(1) NOT NULL COMMENT 'Type: 1: Increase 2: Reduce', + `amount` bigint NOT NULL COMMENT 'Amount', + `balance` bigint NOT NULL COMMENT 'Balance', + `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT 'Remark', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `user_login_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL COMMENT 'User ID', + `login_ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'Login IP', + `user_agent` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'UserAgent', + `success` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Login Success', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `user_reset_subscribe_log` +( + `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + `user_id` BIGINT NOT NULL COMMENT 'User ID', + `type` TINYINT(1) NOT NULL COMMENT 'Type: 1: Auto 2: Advance 3: Paid', + `order_no` VARCHAR(255) DEFAULT NULL COMMENT 'Order No.', + `user_subscribe_id` BIGINT NOT NULL COMMENT 'User Subscribe ID', + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation Time', + INDEX `idx_user_id` (`user_id`), + INDEX `idx_user_subscribe_id` (`user_subscribe_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `user_subscribe_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL COMMENT 'User ID', + `user_subscribe_id` bigint NOT NULL COMMENT 'User Subscribe ID', + `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'Token', + `ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'IP', + `user_agent` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'UserAgent', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`), + KEY `idx_user_subscribe_id` (`user_subscribe_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `message_log` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'email' COMMENT 'Message Type', + `platform` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'smtp' COMMENT 'Platform', + `to` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'To', + `subject` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Subject', + `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT 'Content', + `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Status', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Create Time', + `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time', + PRIMARY KEY (`id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; + +DROP TABLE IF EXISTS `system_logs`; \ No newline at end of file diff --git a/migrations/mysql/02105_node.down.sql b/migrations/mysql/02105_node.down.sql new file mode 100644 index 00000000..210462e0 --- /dev/null +++ b/migrations/mysql/02105_node.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS `nodes`; +DROP TABLE IF EXISTS `servers`; diff --git a/migrations/mysql/02106_subscribe.down.sql b/migrations/mysql/02106_subscribe.down.sql new file mode 100644 index 00000000..20984b79 --- /dev/null +++ b/migrations/mysql/02106_subscribe.down.sql @@ -0,0 +1,5 @@ +ALTER TABLE `subscribe` +DROP COLUMN `nodes`, + DROP COLUMN `node_tags`, + ADD COLUMN `server` VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'Server', + ADD COLUMN `server_group` VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'Server Group'; diff --git a/migrations/mysql/02107_log_setting.down.sql b/migrations/mysql/02107_log_setting.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02108_user_referral.down.sql b/migrations/mysql/02108_user_referral.down.sql new file mode 100644 index 00000000..3bdac5b5 --- /dev/null +++ b/migrations/mysql/02108_user_referral.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE `user` +DROP COLUMN `referral_percentage`, +DROP COLUMN `only_first_purchase`; \ No newline at end of file diff --git a/migrations/mysql/02109_node_sort.down.sql b/migrations/mysql/02109_node_sort.down.sql new file mode 100644 index 00000000..44136469 --- /dev/null +++ b/migrations/mysql/02109_node_sort.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `nodes` +DROP COLUMN `sort`; \ No newline at end of file diff --git a/migrations/mysql/02110_traffic_log_index.down.sql b/migrations/mysql/02110_traffic_log_index.down.sql new file mode 100644 index 00000000..f42807ca --- /dev/null +++ b/migrations/mysql/02110_traffic_log_index.down.sql @@ -0,0 +1 @@ +DROP INDEX idx_traffic_log_time_user_sub ON traffic_log; diff --git a/migrations/mysql/02111_clear_table.down.sql b/migrations/mysql/02111_clear_table.down.sql new file mode 100644 index 00000000..85c9f3ff --- /dev/null +++ b/migrations/mysql/02111_clear_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS `subscribe_type`; +DROP TABLE IF EXISTS `sms`; \ No newline at end of file diff --git a/migrations/mysql/02112_subscribe.down.sql b/migrations/mysql/02112_subscribe.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02113_task.down.sql b/migrations/mysql/02113_task.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02114_node_config.down.sql b/migrations/mysql/02114_node_config.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02115_ads.down.sql b/migrations/mysql/02115_ads.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02116_user_algo.down.sql b/migrations/mysql/02116_user_algo.down.sql new file mode 100644 index 00000000..41644c22 --- /dev/null +++ b/migrations/mysql/02116_user_algo.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE `user` +DROP COLUMN `algo`, + DROP COLUMN `salt`; diff --git a/migrations/mysql/02117_site_custom_data.down.sql b/migrations/mysql/02117_site_custom_data.down.sql new file mode 100644 index 00000000..c8581e8e --- /dev/null +++ b/migrations/mysql/02117_site_custom_data.down.sql @@ -0,0 +1,7 @@ +INSERT INTO `system` (`category`, `key`, `value`, `type`, `desc`, `created_at`, `updated_at`) +SELECT 'site', 'CustomData', '{ + "kr_website_id": "" +}', 'string', 'Custom Data', '2025-04-22 14:25:16.637', '2025-10-14 15:47:19.187' + WHERE NOT EXISTS ( + SELECT 1 FROM `system` WHERE `category` = 'site' AND `key` = 'CustomData' +); diff --git a/migrations/mysql/02118_traffic_log_idx.down.sql b/migrations/mysql/02118_traffic_log_idx.down.sql new file mode 100644 index 00000000..25598dd1 --- /dev/null +++ b/migrations/mysql/02118_traffic_log_idx.down.sql @@ -0,0 +1 @@ +ALTER TABLE traffic_log DROP INDEX idx_timestamp; diff --git a/migrations/mysql/02119_user_subscribe_note.down.sql b/migrations/mysql/02119_user_subscribe_note.down.sql new file mode 100644 index 00000000..60cc0e8a --- /dev/null +++ b/migrations/mysql/02119_user_subscribe_note.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `user_subscribe` +DROP COLUMN `note`; diff --git a/migrations/mysql/02120_user_rules.down.sql b/migrations/mysql/02120_user_rules.down.sql new file mode 100644 index 00000000..718f4a6a --- /dev/null +++ b/migrations/mysql/02120_user_rules.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `user` +DROP COLUMN IF EXISTS `rules`; diff --git a/migrations/mysql/02121_user_withdrawal.down.sql b/migrations/mysql/02121_user_withdrawal.down.sql new file mode 100644 index 00000000..4de8bc50 --- /dev/null +++ b/migrations/mysql/02121_user_withdrawal.down.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS `withdrawals`; + +DELETE FROM `system` +WHERE `category` = 'invite' + AND `key` = 'WithdrawalMethod'; diff --git a/migrations/mysql/02122_server.down.sql b/migrations/mysql/02122_server.down.sql new file mode 100644 index 00000000..7053859b --- /dev/null +++ b/migrations/mysql/02122_server.down.sql @@ -0,0 +1,27 @@ +CREATE TABLE IF NOT EXISTS `server` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Node Name', + `tags` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Tags', + `country` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Country', + `city` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'City', + `latitude` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'latitude', + `longitude` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'longitude', + `server_addr` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Server Address', + `relay_mode` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'none' COMMENT 'Relay Mode', + `relay_node` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT 'Relay Node', + `speed_limit` bigint NOT NULL DEFAULT '0' COMMENT 'Speed Limit', + `traffic_ratio` decimal(4, 2) NOT NULL DEFAULT '0.00' COMMENT 'Traffic Ratio', + `group_id` bigint DEFAULT NULL COMMENT 'Group ID', + `protocol` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'Protocol', + `config` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT 'Config', + `enable` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Enabled', + `sort` bigint NOT NULL DEFAULT '0' COMMENT 'Sort', + `last_reported_at` datetime(3) DEFAULT NULL COMMENT 'Last Reported Time', + `created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time', + `updated_at` datetime(3) DEFAULT NULL COMMENT 'Update Time', + PRIMARY KEY (`id`), + KEY `idx_group_id` (`group_id`) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci; diff --git a/migrations/mysql/02123_subscribe_original.down.sql b/migrations/mysql/02123_subscribe_original.down.sql new file mode 100644 index 00000000..2527d488 --- /dev/null +++ b/migrations/mysql/02123_subscribe_original.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `subscribe` +DROP COLUMN `show_original_price`; diff --git a/migrations/mysql/02124_server_group_delete.down.sql b/migrations/mysql/02124_server_group_delete.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/02125_subscribe_stock.down.sql b/migrations/mysql/02125_subscribe_stock.down.sql new file mode 100644 index 00000000..c8482cca --- /dev/null +++ b/migrations/mysql/02125_subscribe_stock.down.sql @@ -0,0 +1,5 @@ + +-- This migration script reverts the inventory values in the 'subscribe' table +UPDATE `subscribe` +SET `inventory` = 0 +WHERE `inventory` = -1; \ No newline at end of file diff --git a/migrations/mysql/02126_system_log_idx.down.sql b/migrations/mysql/02126_system_log_idx.down.sql new file mode 100644 index 00000000..e605bb27 --- /dev/null +++ b/migrations/mysql/02126_system_log_idx.down.sql @@ -0,0 +1 @@ +DROP INDEX idx_type_date ON system_logs; diff --git a/migrations/mysql/02127_search_indexes.down.sql b/migrations/mysql/02127_search_indexes.down.sql new file mode 100644 index 00000000..0623d5ff --- /dev/null +++ b/migrations/mysql/02127_search_indexes.down.sql @@ -0,0 +1,131 @@ +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'nodes' + AND INDEX_NAME = 'idx_nodes_port'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `nodes` DROP INDEX `idx_nodes_port`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'nodes' + AND INDEX_NAME = 'idx_nodes_tags'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `nodes` DROP INDEX `idx_nodes_tags`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'nodes' + AND INDEX_NAME = 'idx_nodes_address'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `nodes` DROP INDEX `idx_nodes_address`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'nodes' + AND INDEX_NAME = 'idx_nodes_name'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `nodes` DROP INDEX `idx_nodes_name`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'servers' + AND INDEX_NAME = 'idx_servers_address'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `servers` DROP INDEX `idx_servers_address`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'servers' + AND INDEX_NAME = 'idx_servers_name'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `servers` DROP INDEX `idx_servers_name`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND INDEX_NAME = 'idx_payment_name'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `payment` DROP INDEX `idx_payment_name`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'coupon' + AND INDEX_NAME = 'idx_coupon_name'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `coupon` DROP INDEX `idx_coupon_name`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'user' + AND INDEX_NAME = 'idx_user_refer_code'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `user` DROP INDEX `idx_user_refer_code`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND INDEX_NAME = 'idx_order_coupon'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `order` DROP INDEX `idx_order_coupon`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @index_exists = (SELECT COUNT(1) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND INDEX_NAME = 'idx_order_trade_no'); +SET @sql = IF(@index_exists > 0, + 'ALTER TABLE `order` DROP INDEX `idx_order_trade_no`', + 'SELECT 1'); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; diff --git a/migrations/mysql/02128_server_config_override.down.sql b/migrations/mysql/02128_server_config_override.down.sql new file mode 100644 index 00000000..20a24fe5 --- /dev/null +++ b/migrations/mysql/02128_server_config_override.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `server_config_overrides`; diff --git a/migrations/mysql/02129_payment_sort.down.sql b/migrations/mysql/02129_payment_sort.down.sql new file mode 100644 index 00000000..e1aa973f --- /dev/null +++ b/migrations/mysql/02129_payment_sort.down.sql @@ -0,0 +1,17 @@ +SET @column_exists = ( + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'payment' + AND COLUMN_NAME = 'sort' +); + +SET @sql = IF( + @column_exists > 0, + 'ALTER TABLE `payment` DROP COLUMN `sort`', + 'SELECT 1' +); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; diff --git a/migrations/mysql/02130_subscribe_tutorial.down.sql b/migrations/mysql/02130_subscribe_tutorial.down.sql new file mode 100644 index 00000000..92554ad7 --- /dev/null +++ b/migrations/mysql/02130_subscribe_tutorial.down.sql @@ -0,0 +1 @@ +DELETE FROM `system` WHERE `category` = 'subscribe' AND `key` = 'ShowTutorial'; diff --git a/migrations/mysql/02131_timestamptz_last_reported_at.down.sql b/migrations/mysql/02131_timestamptz_last_reported_at.down.sql new file mode 100644 index 00000000..e0ac49d1 --- /dev/null +++ b/migrations/mysql/02131_timestamptz_last_reported_at.down.sql @@ -0,0 +1 @@ +SELECT 1; diff --git a/migrations/postgres/00001_init_schema.down.sql b/migrations/postgres/00001_init_schema.down.sql new file mode 100644 index 00000000..a7f106f5 --- /dev/null +++ b/migrations/postgres/00001_init_schema.down.sql @@ -0,0 +1,32 @@ +-- 000001_init_schema.down.sql +DROP TABLE IF EXISTS "user_subscribe_log"; +DROP TABLE IF EXISTS "user_subscribe"; +DROP TABLE IF EXISTS "user_login_log"; +DROP TABLE IF EXISTS "user_gift_amount_log"; +DROP TABLE IF EXISTS "user_device"; +DROP TABLE IF EXISTS "user_commission_log"; +DROP TABLE IF EXISTS "user_balance_log"; +DROP TABLE IF EXISTS "user_auth_methods"; +DROP TABLE IF EXISTS "user"; +DROP TABLE IF EXISTS "traffic_log"; +DROP TABLE IF EXISTS "ticket_follow"; +DROP TABLE IF EXISTS "ticket"; +DROP TABLE IF EXISTS "system"; +DROP TABLE IF EXISTS "subscribe_type"; +DROP TABLE IF EXISTS "subscribe_group"; +DROP TABLE IF EXISTS "subscribe"; +DROP TABLE IF EXISTS "sms"; +DROP TABLE IF EXISTS "server_rule_group"; +DROP TABLE IF EXISTS "server_group"; +DROP TABLE IF EXISTS "server"; +DROP TABLE IF EXISTS "payment"; +DROP TABLE IF EXISTS "order"; +DROP TABLE IF EXISTS "message_log"; +DROP TABLE IF EXISTS "document"; +DROP TABLE IF EXISTS "coupon"; +DROP TABLE IF EXISTS "auth_method"; +DROP TABLE IF EXISTS "application_version"; +DROP TABLE IF EXISTS "application_config"; +DROP TABLE IF EXISTS "application"; +DROP TABLE IF EXISTS "announcement"; +DROP TABLE IF EXISTS "ads"; diff --git a/migrations/postgres/00002_init_basic_data.down.sql b/migrations/postgres/00002_init_basic_data.down.sql new file mode 100644 index 00000000..ac4e1c91 --- /dev/null +++ b/migrations/postgres/00002_init_basic_data.down.sql @@ -0,0 +1,15 @@ +-- 000002_init_data.down.sql +DELETE +FROM "auth_method" +WHERE "id" IN (1, 2, 3, 4, 5, 6, 7, 8); +DELETE +FROM "payment" +WHERE "id" = -1; +DELETE +FROM "subscribe_type" +WHERE "id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); +DELETE +FROM "system" +WHERE "id" IN + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41); diff --git a/migrations/postgres/02003_update_payment.down.sql b/migrations/postgres/02003_update_payment.down.sql new file mode 100644 index 00000000..ef94e390 --- /dev/null +++ b/migrations/postgres/02003_update_payment.down.sql @@ -0,0 +1,5 @@ +ALTER TABLE "order" DROP COLUMN IF EXISTS "payment_id"; +ALTER TABLE "payment" DROP COLUMN IF EXISTS "platform"; +ALTER TABLE "payment" DROP COLUMN IF EXISTS "description"; +ALTER TABLE "payment" DROP COLUMN IF EXISTS "token"; +ALTER TABLE "payment" ADD COLUMN IF NOT EXISTS "mark" VARCHAR(255) DEFAULT NULL; diff --git a/migrations/postgres/02004_rebuild_rule.down.sql b/migrations/postgres/02004_rebuild_rule.down.sql new file mode 100644 index 00000000..d717c27e --- /dev/null +++ b/migrations/postgres/02004_rebuild_rule.down.sql @@ -0,0 +1,4 @@ +-- migrations/02003_rebuild_rule.up.sql +-- Purpose: Back rebuilding server rule table +-- Author: PPanel Team, 2025-04-21 +DROP TABLE IF EXISTS server_rule_group; diff --git a/migrations/postgres/02005_device_online_record.down.sql b/migrations/postgres/02005_device_online_record.down.sql new file mode 100644 index 00000000..db7e9bf4 --- /dev/null +++ b/migrations/postgres/02005_device_online_record.down.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS "user_device_online_record"; +ALTER TABLE "user_subscribe" DROP COLUMN IF EXISTS "finished_at"; +ALTER TABLE "application_config" DROP COLUMN IF EXISTS "invitation_link"; +ALTER TABLE "application_config" DROP COLUMN IF EXISTS "kr_website_id"; diff --git a/migrations/postgres/02006_reset_subscribe_record.down.sql b/migrations/postgres/02006_reset_subscribe_record.down.sql new file mode 100644 index 00000000..218c652d --- /dev/null +++ b/migrations/postgres/02006_reset_subscribe_record.down.sql @@ -0,0 +1,5 @@ +-- migrations/02008_create_user_reset_subscribe_log.down.sql +-- Purpose: Drop user_reset_subscribe_log table +-- Author: PPanel Team, 2025-04-22 + +DROP TABLE IF EXISTS "user_reset_subscribe_log"; diff --git a/migrations/postgres/02007_adapte_rule.down.sql b/migrations/postgres/02007_adapte_rule.down.sql new file mode 100644 index 00000000..11e98b35 --- /dev/null +++ b/migrations/postgres/02007_adapte_rule.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE "server_rule_group" +DROP COLUMN "default", +DROP COLUMN "type"; diff --git a/migrations/postgres/02100_task.down.sql b/migrations/postgres/02100_task.down.sql new file mode 100644 index 00000000..f20f88a0 --- /dev/null +++ b/migrations/postgres/02100_task.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS "email_task"; diff --git a/migrations/postgres/02101_subscribe_application.down.sql b/migrations/postgres/02101_subscribe_application.down.sql new file mode 100644 index 00000000..5ba84d45 --- /dev/null +++ b/migrations/postgres/02101_subscribe_application.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS "subscribe_application"; diff --git a/migrations/postgres/02102_subscribe_config.down.sql b/migrations/postgres/02102_subscribe_config.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02103_delete_application.down.sql b/migrations/postgres/02103_delete_application.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02104_system_log.down.sql b/migrations/postgres/02104_system_log.down.sql new file mode 100644 index 00000000..89366294 --- /dev/null +++ b/migrations/postgres/02104_system_log.down.sql @@ -0,0 +1,85 @@ +CREATE TABLE IF NOT EXISTS "user_balance_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "user_id" bigint NOT NULL, + "amount" bigint NOT NULL, + "type" SMALLINT NOT NULL, + "order_id" bigint DEFAULT NULL, + "balance" bigint NOT NULL, + "created_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "user_balance_log_idx_user_id" ON "user_balance_log" ("user_id"); +CREATE TABLE IF NOT EXISTS "user_commission_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "user_id" bigint NOT NULL, + "order_no" varchar(191) DEFAULT NULL, + "amount" bigint NOT NULL, + "created_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "user_commission_log_idx_user_id" ON "user_commission_log" ("user_id"); +CREATE TABLE IF NOT EXISTS "user_gift_amount_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "user_id" bigint NOT NULL, + "user_subscribe_id" bigint DEFAULT NULL, + "order_no" varchar(191) DEFAULT NULL, + "type" SMALLINT NOT NULL, + "amount" bigint NOT NULL, + "balance" bigint NOT NULL, + "remark" varchar(255) DEFAULT '', + "created_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "user_gift_amount_log_idx_user_id" ON "user_gift_amount_log" ("user_id"); +CREATE TABLE IF NOT EXISTS "user_login_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "user_id" bigint NOT NULL, + "login_ip" varchar(255) NOT NULL, + "user_agent" text NOT NULL, + "success" BOOLEAN NOT NULL DEFAULT false, + "created_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "user_login_log_idx_user_id" ON "user_login_log" ("user_id"); +CREATE TABLE IF NOT EXISTS "user_reset_subscribe_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY, + "user_id" BIGINT NOT NULL, + "type" SMALLINT NOT NULL, + "order_no" VARCHAR(255) DEFAULT NULL, + "user_subscribe_id" BIGINT NOT NULL, + "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); +CREATE INDEX IF NOT EXISTS "user_reset_subscribe_log_idx_user_id" ON "user_reset_subscribe_log" ("user_id"); +CREATE INDEX IF NOT EXISTS "user_reset_subscribe_log_idx_user_subscribe_id" ON "user_reset_subscribe_log" ("user_subscribe_id"); +CREATE TABLE IF NOT EXISTS "user_subscribe_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "user_id" bigint NOT NULL, + "user_subscribe_id" bigint NOT NULL, + "token" varchar(255) NOT NULL, + "ip" varchar(255) NOT NULL, + "user_agent" text NOT NULL, + "created_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "user_subscribe_log_idx_user_id" ON "user_subscribe_log" ("user_id"); +CREATE INDEX IF NOT EXISTS "user_subscribe_log_idx_user_subscribe_id" ON "user_subscribe_log" ("user_subscribe_id"); +CREATE TABLE IF NOT EXISTS "message_log" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "type" varchar(50) NOT NULL DEFAULT 'email', + "platform" varchar(50) NOT NULL DEFAULT 'smtp', + "to" text NOT NULL, + "subject" varchar(255) NOT NULL DEFAULT '', + "content" text, + "status" SMALLINT NOT NULL DEFAULT '0', + "created_at" TIMESTAMP(3) DEFAULT NULL, + "updated_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +DROP TABLE IF EXISTS "system_logs"; diff --git a/migrations/postgres/02105_node.down.sql b/migrations/postgres/02105_node.down.sql new file mode 100644 index 00000000..0c0b0385 --- /dev/null +++ b/migrations/postgres/02105_node.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS "nodes"; +DROP TABLE IF EXISTS "servers"; diff --git a/migrations/postgres/02106_subscribe.down.sql b/migrations/postgres/02106_subscribe.down.sql new file mode 100644 index 00000000..bda90302 --- /dev/null +++ b/migrations/postgres/02106_subscribe.down.sql @@ -0,0 +1,5 @@ +ALTER TABLE "subscribe" +DROP COLUMN "nodes", + DROP COLUMN "node_tags", + ADD COLUMN "server" VARCHAR(255) NOT NULL DEFAULT '' , + ADD COLUMN "server_group" VARCHAR(255) NOT NULL DEFAULT ''; diff --git a/migrations/postgres/02107_log_setting.down.sql b/migrations/postgres/02107_log_setting.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02108_user_referral.down.sql b/migrations/postgres/02108_user_referral.down.sql new file mode 100644 index 00000000..94d2ee55 --- /dev/null +++ b/migrations/postgres/02108_user_referral.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE "user" +DROP COLUMN "referral_percentage", +DROP COLUMN "only_first_purchase"; diff --git a/migrations/postgres/02109_node_sort.down.sql b/migrations/postgres/02109_node_sort.down.sql new file mode 100644 index 00000000..f229cd3d --- /dev/null +++ b/migrations/postgres/02109_node_sort.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "nodes" +DROP COLUMN "sort"; diff --git a/migrations/postgres/02110_traffic_log_index.down.sql b/migrations/postgres/02110_traffic_log_index.down.sql new file mode 100644 index 00000000..d26f1dfe --- /dev/null +++ b/migrations/postgres/02110_traffic_log_index.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS "idx_traffic_log_time_user_sub"; diff --git a/migrations/postgres/02111_clear_table.down.sql b/migrations/postgres/02111_clear_table.down.sql new file mode 100644 index 00000000..92562a53 --- /dev/null +++ b/migrations/postgres/02111_clear_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS "subscribe_type"; +DROP TABLE IF EXISTS "sms"; diff --git a/migrations/postgres/02112_subscribe.down.sql b/migrations/postgres/02112_subscribe.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02113_task.down.sql b/migrations/postgres/02113_task.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02114_node_config.down.sql b/migrations/postgres/02114_node_config.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02115_ads.down.sql b/migrations/postgres/02115_ads.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02116_user_algo.down.sql b/migrations/postgres/02116_user_algo.down.sql new file mode 100644 index 00000000..007c9240 --- /dev/null +++ b/migrations/postgres/02116_user_algo.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "user" DROP COLUMN IF EXISTS "algo"; +ALTER TABLE "user" DROP COLUMN IF EXISTS "salt"; diff --git a/migrations/postgres/02117_site_custom_data.down.sql b/migrations/postgres/02117_site_custom_data.down.sql new file mode 100644 index 00000000..81a70c26 --- /dev/null +++ b/migrations/postgres/02117_site_custom_data.down.sql @@ -0,0 +1,7 @@ +INSERT INTO "system" ("category", "key", "value", "type", "desc", "created_at", "updated_at") +SELECT 'site', 'CustomData', '{ + "kr_website_id": "" +}', 'string', 'Custom Data', '2025-04-22 14:25:16.637', '2025-10-14 15:47:19.187' + WHERE NOT EXISTS ( + SELECT 1 FROM "system" WHERE "category" = 'site' AND "key" = 'CustomData' +); diff --git a/migrations/postgres/02118_traffic_log_idx.down.sql b/migrations/postgres/02118_traffic_log_idx.down.sql new file mode 100644 index 00000000..ee3aa677 --- /dev/null +++ b/migrations/postgres/02118_traffic_log_idx.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS "idx_timestamp"; diff --git a/migrations/postgres/02119_user_subscribe_note.down.sql b/migrations/postgres/02119_user_subscribe_note.down.sql new file mode 100644 index 00000000..2ac2995a --- /dev/null +++ b/migrations/postgres/02119_user_subscribe_note.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "user_subscribe" +DROP COLUMN "note"; diff --git a/migrations/postgres/02120_user_rules.down.sql b/migrations/postgres/02120_user_rules.down.sql new file mode 100644 index 00000000..740609bd --- /dev/null +++ b/migrations/postgres/02120_user_rules.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "user" +DROP COLUMN IF EXISTS "rules"; diff --git a/migrations/postgres/02121_user_withdrawal.down.sql b/migrations/postgres/02121_user_withdrawal.down.sql new file mode 100644 index 00000000..cbce3d5f --- /dev/null +++ b/migrations/postgres/02121_user_withdrawal.down.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS "withdrawals"; +DELETE FROM "system" +WHERE "category" = 'invite' + AND "key" = 'WithdrawalMethod'; diff --git a/migrations/postgres/02122_server.down.sql b/migrations/postgres/02122_server.down.sql new file mode 100644 index 00000000..007cf024 --- /dev/null +++ b/migrations/postgres/02122_server.down.sql @@ -0,0 +1,25 @@ +CREATE TABLE IF NOT EXISTS "server" +( + "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + "name" varchar(100) NOT NULL DEFAULT '', + "tags" varchar(128) NOT NULL DEFAULT '', + "country" varchar(128) NOT NULL DEFAULT '', + "city" varchar(128) NOT NULL DEFAULT '', + "latitude" varchar(128) NOT NULL DEFAULT '', + "longitude" varchar(128) NOT NULL DEFAULT '', + "server_addr" varchar(100) NOT NULL DEFAULT '', + "relay_mode" varchar(20) NOT NULL DEFAULT 'none', + "relay_node" text, + "speed_limit" bigint NOT NULL DEFAULT '0', + "traffic_ratio" decimal(4, 2) NOT NULL DEFAULT '0.00', + "group_id" bigint DEFAULT NULL, + "protocol" varchar(20) NOT NULL DEFAULT '', + "config" text, + "enable" SMALLINT NOT NULL DEFAULT '1', + "sort" bigint NOT NULL DEFAULT '0', + "last_reported_at" TIMESTAMP(3) DEFAULT NULL, + "created_at" TIMESTAMP(3) DEFAULT NULL, + "updated_at" TIMESTAMP(3) DEFAULT NULL, + PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "server_idx_group_id" ON "server" ("group_id"); diff --git a/migrations/postgres/02123_subscribe_original.down.sql b/migrations/postgres/02123_subscribe_original.down.sql new file mode 100644 index 00000000..0eb75750 --- /dev/null +++ b/migrations/postgres/02123_subscribe_original.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "subscribe" +DROP COLUMN "show_original_price"; diff --git a/migrations/postgres/02124_server_group_delete.down.sql b/migrations/postgres/02124_server_group_delete.down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgres/02125_subscribe_stock.down.sql b/migrations/postgres/02125_subscribe_stock.down.sql new file mode 100644 index 00000000..d39924a5 --- /dev/null +++ b/migrations/postgres/02125_subscribe_stock.down.sql @@ -0,0 +1,4 @@ +-- This migration script reverts the inventory values in the 'subscribe' table +UPDATE "subscribe" +SET "inventory" = 0 +WHERE "inventory" = -1; diff --git a/migrations/postgres/02126_system_log_idx.down.sql b/migrations/postgres/02126_system_log_idx.down.sql new file mode 100644 index 00000000..4d8904b5 --- /dev/null +++ b/migrations/postgres/02126_system_log_idx.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS "idx_type_date"; diff --git a/migrations/postgres/02127_search_indexes.down.sql b/migrations/postgres/02127_search_indexes.down.sql new file mode 100644 index 00000000..29d7060b --- /dev/null +++ b/migrations/postgres/02127_search_indexes.down.sql @@ -0,0 +1,29 @@ +DROP INDEX IF EXISTS "idx_system_logs_content_trgm"; +DROP INDEX IF EXISTS "idx_ticket_description_trgm"; +DROP INDEX IF EXISTS "idx_ticket_title_trgm"; +DROP INDEX IF EXISTS "idx_subscribe_description_trgm"; +DROP INDEX IF EXISTS "idx_subscribe_name_trgm"; +DROP INDEX IF EXISTS "idx_document_content_trgm"; +DROP INDEX IF EXISTS "idx_document_title_trgm"; +DROP INDEX IF EXISTS "idx_announcement_content_trgm"; +DROP INDEX IF EXISTS "idx_announcement_title_trgm"; +DROP INDEX IF EXISTS "idx_ads_content_trgm"; +DROP INDEX IF EXISTS "idx_ads_title_trgm"; + +DROP INDEX IF EXISTS "idx_nodes_port"; +DROP INDEX IF EXISTS "idx_nodes_tags_pattern"; +DROP INDEX IF EXISTS "idx_nodes_address_pattern"; +DROP INDEX IF EXISTS "idx_nodes_name_pattern"; +DROP INDEX IF EXISTS "idx_servers_address_pattern"; +DROP INDEX IF EXISTS "idx_servers_name_pattern"; + +DROP INDEX IF EXISTS "idx_payment_name_pattern"; +DROP INDEX IF EXISTS "idx_coupon_code_pattern"; +DROP INDEX IF EXISTS "idx_coupon_name_pattern"; + +DROP INDEX IF EXISTS "idx_user_auth_identifier_pattern"; +DROP INDEX IF EXISTS "idx_user_refer_code_pattern"; + +DROP INDEX IF EXISTS "idx_order_coupon_pattern"; +DROP INDEX IF EXISTS "idx_order_trade_no_pattern"; +DROP INDEX IF EXISTS "idx_order_order_no_pattern"; diff --git a/migrations/postgres/02128_server_config_override.down.sql b/migrations/postgres/02128_server_config_override.down.sql new file mode 100644 index 00000000..4efa18ca --- /dev/null +++ b/migrations/postgres/02128_server_config_override.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS "server_config_overrides"; diff --git a/migrations/postgres/02129_payment_sort.down.sql b/migrations/postgres/02129_payment_sort.down.sql new file mode 100644 index 00000000..081f73f4 --- /dev/null +++ b/migrations/postgres/02129_payment_sort.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "payment" + DROP COLUMN IF EXISTS "sort"; diff --git a/migrations/postgres/02130_subscribe_tutorial.down.sql b/migrations/postgres/02130_subscribe_tutorial.down.sql new file mode 100644 index 00000000..4b5aca17 --- /dev/null +++ b/migrations/postgres/02130_subscribe_tutorial.down.sql @@ -0,0 +1 @@ +DELETE FROM "system" WHERE "category" = 'subscribe' AND "key" = 'ShowTutorial'; diff --git a/migrations/postgres/02131_timestamptz_last_reported_at.down.sql b/migrations/postgres/02131_timestamptz_last_reported_at.down.sql new file mode 100644 index 00000000..2dddd6ea --- /dev/null +++ b/migrations/postgres/02131_timestamptz_last_reported_at.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE "servers" + ALTER COLUMN "last_reported_at" TYPE timestamp(3) + USING "last_reported_at" AT TIME ZONE 'UTC'; diff --git a/src/main.rs b/src/main.rs index 8c8dab83..ef058fdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use axum::routing::get; use tracing_subscriber::layer::SubscriberExt as _; use tracing_subscriber::util::SubscriberInitExt as _; +use tracing_subscriber::Layer as _; use tracing_subscriber::EnvFilter; pub mod adapter; diff --git a/src/tracing_otel.rs b/src/tracing_otel.rs index 2a92dddb..ed2a9b66 100644 --- a/src/tracing_otel.rs +++ b/src/tracing_otel.rs @@ -1,20 +1,14 @@ //! OpenTelemetry provider initialisation — ported from `pkg/trace/agent.go`. //! -//! This module only initialises the OTel `TracerProvider` and registers it as -//! the global OTel provider. It does NOT touch the `tracing` subscriber — the -//! bridge layer (`tracing-opentelemetry`) is added in `main.rs` when building -//! the subscriber stack. +//! Currently only the stdout exporter is wired up. OTLP gRPC / HTTP support +//! can be added once the opentelemetry-otlp 0.27 builder API is confirmed. //! -//! Supported batchers (mirrors Go `kindJaeger` / `kindOtlpGrpc` / etc.): -//! - `"jaeger"` → OTLP gRPC (Jaeger v2 speaks native OTLP) -//! - `"otlpgrpc"` → OTLP gRPC -//! - `"otlphttp"` → OTLP HTTP -//! - `"stdout"` → pretty-print to stdout -//! - disabled / empty endpoint → no-op +//! To enable: set `Trace.Batcher = "stdout"` in config (any non-empty batcher +//! with a non-empty endpoint also works; set `Trace.Disabled = true` to opt out). use opentelemetry::global; -use opentelemetry::trace::TracerProvider as _; -use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, TracerProvider}; +use opentelemetry::KeyValue; +use opentelemetry_sdk::trace::TracerProvider; use opentelemetry_sdk::Resource; use crate::config::TraceConfig; @@ -35,84 +29,36 @@ impl Drop for OtelGuard { /// Build and install the OTel `TracerProvider`. /// -/// Returns `None` when tracing is disabled or no endpoint is configured. -/// The caller must keep the returned `OtelGuard` alive for the process lifetime. +/// Returns `None` when tracing is disabled (config `Trace.Disabled = true`) +/// or no batcher is configured. pub fn init_otel(cfg: &TraceConfig) -> Option { if cfg.disabled { return None; } - if cfg.endpoint.is_empty() && cfg.batcher.as_str() != "stdout" { + // Require explicit opt-in: batcher must be set. + if cfg.batcher.is_empty() || cfg.batcher == "none" { return None; } - let resource = Resource::builder() - .with_service_name(cfg.name.clone()) + let resource = Resource::new(vec![KeyValue::new( + opentelemetry_semantic_conventions::resource::SERVICE_NAME, + cfg.name.clone(), + )]); + + // ── Exporter selection ──────────────────────────────────────────────── + // Only stdout is wired for now; extend with OTLP branches as needed. + let exporter = opentelemetry_stdout::SpanExporter::default(); + + let provider = TracerProvider::builder() + .with_resource(resource) + .with_simple_exporter(exporter) .build(); - let sampler = if (cfg.sampler - 1.0_f64).abs() < f64::EPSILON { - Sampler::AlwaysOn - } else { - Sampler::TraceIdRatioBased(cfg.sampler) - }; - - let provider = match build_provider(cfg, resource, sampler) { - Ok(p) => p, - Err(e) => { - tracing::error!("[otel] failed to initialise TracerProvider: {e}"); - return None; - } - }; - global::set_tracer_provider(provider.clone()); tracing::info!( batcher = %cfg.batcher, - endpoint = %cfg.endpoint, + name = %cfg.name, "OpenTelemetry tracing initialised", ); Some(OtelGuard { provider }) } - -// ─── private helpers ───────────────────────────────────────────────────────── - -fn build_provider( - cfg: &TraceConfig, - resource: Resource, - sampler: Sampler, -) -> anyhow::Result { - use opentelemetry_sdk::trace::BatchExporter; - - let exporter: BatchExporter = match cfg.batcher.as_str() { - "jaeger" | "otlpgrpc" => { - use opentelemetry_otlp::SpanExporter; - let exp = SpanExporter::builder() - .with_tonic() - .with_endpoint(&cfg.endpoint) - .build() - .map_err(|e| anyhow::anyhow!("otlpgrpc: {e}"))?; - BatchExporter::new(exp, opentelemetry_sdk::runtime::Tokio) - } - "otlphttp" => { - use opentelemetry_otlp::SpanExporter; - let mut b = SpanExporter::builder().with_http().with_endpoint(&cfg.endpoint); - if !cfg.otlp_headers.is_empty() { - b = b.with_headers(cfg.otlp_headers.clone()); - } - let exp = b.build().map_err(|e| anyhow::anyhow!("otlphttp: {e}"))?; - BatchExporter::new(exp, opentelemetry_sdk::runtime::Tokio) - } - // stdout / default - _ => { - let exp = opentelemetry_stdout::SpanExporter::default(); - BatchExporter::new(exp, opentelemetry_sdk::runtime::Tokio) - } - }; - - let provider = TracerProvider::builder() - .with_resource(resource) - .with_sampler(sampler) - .with_id_generator(RandomIdGenerator::default()) - .with_span_processor(exporter) - .build(); - - Ok(provider) -}