Update table prefix from ogp_ to gsp_ in SQL files and config

Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-11-07 10:48:01 +00:00
parent 4e5bae68c4
commit 0be96da2a0
7 changed files with 48 additions and 42 deletions

View file

@ -2,17 +2,18 @@
-- This script upgrades existing billing installations to the new invoice-based system
-- Run this ONCE on existing installations (not needed for fresh installs)
-- Compatible with MySQL 5.7+ and MariaDB 10.2+
-- Table prefix is hardcoded to gsp_ for standalone billing module
-- Step 1: Add new columns to billing_orders (only if they don't exist)
SET @dbname = DATABASE();
SET @tablename = 'ogp_billing_orders';
SET @tablename = 'gsp_billing_orders';
-- Add order_date column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'order_date';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD COLUMN `order_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `status`',
'ALTER TABLE `gsp_billing_orders` ADD COLUMN `order_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `status`',
'SELECT "Column order_date already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -23,7 +24,7 @@ SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'payment_txid';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD COLUMN `payment_txid` VARCHAR(255) NULL AFTER `end_date`',
'ALTER TABLE `gsp_billing_orders` ADD COLUMN `payment_txid` VARCHAR(255) NULL AFTER `end_date`',
'SELECT "Column payment_txid already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -34,21 +35,21 @@ SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'paid_ts';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD COLUMN `paid_ts` DATETIME NULL AFTER `payment_txid`',
'ALTER TABLE `gsp_billing_orders` ADD COLUMN `paid_ts` DATETIME NULL AFTER `payment_txid`',
'SELECT "Column paid_ts already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Step 2: Modify existing columns to use proper data types
ALTER TABLE `ogp_billing_orders`
ALTER TABLE `gsp_billing_orders`
MODIFY COLUMN `status` VARCHAR(16) NOT NULL DEFAULT 'in-cart',
MODIFY COLUMN `remote_control_password` VARCHAR(255) NULL,
MODIFY COLUMN `ftp_password` VARCHAR(255) NULL;
-- Convert end_date from VARCHAR to DATETIME (handle existing data)
-- First, update any '0' values to NULL
UPDATE `ogp_billing_orders` SET `end_date` = NULL WHERE `end_date` = '0' OR `end_date` = '';
UPDATE `gsp_billing_orders` SET `end_date` = NULL WHERE `end_date` = '0' OR `end_date` = '';
-- Check current end_date type and convert if needed
SET @col_type = '';
@ -56,7 +57,7 @@ SELECT DATA_TYPE INTO @col_type FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'end_date';
SET @sql = IF(@col_type = 'varchar',
'ALTER TABLE `ogp_billing_orders` MODIFY COLUMN `end_date` DATETIME NULL',
'ALTER TABLE `gsp_billing_orders` MODIFY COLUMN `end_date` DATETIME NULL',
'SELECT "Column end_date already DATETIME" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -67,7 +68,7 @@ SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'cart_id';
SET @sql = IF(@col_exists > 0,
'ALTER TABLE `ogp_billing_orders` DROP COLUMN `cart_id`',
'ALTER TABLE `gsp_billing_orders` DROP COLUMN `cart_id`',
'SELECT "Column cart_id already removed" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -77,7 +78,7 @@ SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'extended';
SET @sql = IF(@col_exists > 0,
'ALTER TABLE `ogp_billing_orders` DROP COLUMN `extended`',
'ALTER TABLE `gsp_billing_orders` DROP COLUMN `extended`',
'SELECT "Column extended already removed" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -88,7 +89,7 @@ SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = 'idx_user_id';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD INDEX `idx_user_id` (`user_id`)',
'ALTER TABLE `gsp_billing_orders` ADD INDEX `idx_user_id` (`user_id`)',
'SELECT "Index idx_user_id already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -98,7 +99,7 @@ SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = 'idx_status';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD INDEX `idx_status` (`status`)',
'ALTER TABLE `gsp_billing_orders` ADD INDEX `idx_status` (`status`)',
'SELECT "Index idx_status already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -108,7 +109,7 @@ SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = 'idx_home_id';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE `ogp_billing_orders` ADD INDEX `idx_home_id` (`home_id`)',
'ALTER TABLE `gsp_billing_orders` ADD INDEX `idx_home_id` (`home_id`)',
'SELECT "Index idx_home_id already exists" AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
@ -116,7 +117,7 @@ DEALLOCATE PREPARE stmt;
-- Step 5: Create the new billing_invoices table
CREATE TABLE IF NOT EXISTS `ogp_billing_invoices` (
CREATE TABLE IF NOT EXISTS `gsp_billing_invoices` (
`invoice_id` INT(11) NOT NULL AUTO_INCREMENT,
`order_id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
@ -142,7 +143,7 @@ CREATE TABLE IF NOT EXISTS `ogp_billing_invoices` (
-- Step 6: Migrate existing paid orders to create initial invoices
-- This creates a historical invoice for each paid/installed order
INSERT INTO `ogp_billing_invoices`
INSERT INTO `gsp_billing_invoices`
(`order_id`, `user_id`, `customer_name`, `customer_email`, `amount`, `currency`, `status`, `invoice_date`, `paid_date`, `payment_txid`, `description`, `invoice_duration`, `qty`)
SELECT
o.order_id,
@ -158,18 +159,18 @@ SELECT
CONCAT('Initial invoice for ', o.home_name) AS description,
o.invoice_duration,
o.qty
FROM `ogp_billing_orders` o
FROM `gsp_billing_orders` o
LEFT JOIN `ogp_users` u ON o.user_id = u.user_id
WHERE o.status IN ('paid', 'installed')
AND NOT EXISTS (
SELECT 1 FROM `ogp_billing_invoices` i
SELECT 1 FROM `gsp_billing_invoices` i
WHERE i.order_id = o.order_id AND i.status = 'paid'
);
-- Step 7: Drop the obsolete billing_carts table (replaced by invoice system)
DROP TABLE IF EXISTS `ogp_billing_carts`;
DROP TABLE IF EXISTS `gsp_billing_carts`;
-- Step 8: Update billing_services charset for consistency
ALTER TABLE `ogp_billing_services` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE `gsp_billing_services` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
SELECT 'Migration completed successfully! Invoice-based billing system is now active.' AS Status;