38451-vm/db/migrations/20260218_trading_tables.php
2026-02-18 05:09:56 +00:00

73 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/../config.php';
$db = db();
$tables = [
"CREATE TABLE IF NOT EXISTS contract_orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
symbol VARCHAR(20) NOT NULL,
type ENUM('limit', 'market') DEFAULT 'market',
direction ENUM('long', 'short') NOT NULL,
leverage INT DEFAULT 1,
amount DECIMAL(20,8) NOT NULL,
entry_price DECIMAL(20,8) DEFAULT NULL,
close_price DECIMAL(20,8) DEFAULT NULL,
status ENUM('open', 'closed', 'cancelled') DEFAULT 'open',
profit decimal(20,8) DEFAULT 0,
control_status TINYINT DEFAULT 0 COMMENT '0: normal, 1: force win, 2: force loss',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS spot_orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
symbol VARCHAR(20) NOT NULL,
side ENUM('buy', 'sell') NOT NULL,
price DECIMAL(20,8) DEFAULT NULL,
amount DECIMAL(20,8) NOT NULL,
filled DECIMAL(20,8) DEFAULT 0,
status ENUM('pending', 'filled', 'cancelled') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS exchange_records (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
from_symbol VARCHAR(10) NOT NULL,
to_symbol VARCHAR(10) NOT NULL,
from_amount DECIMAL(20,8) NOT NULL,
to_amount DECIMAL(20,8) NOT NULL,
rate DECIMAL(20,8) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS staking_records (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
plan_name VARCHAR(100) NOT NULL,
amount DECIMAL(20,8) NOT NULL,
symbol VARCHAR(10) DEFAULT 'USDT',
daily_profit DECIMAL(5,2) NOT NULL,
period INT NOT NULL COMMENT 'days',
status ENUM('running', 'ended') DEFAULT 'running',
start_date DATE NOT NULL,
end_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS price_controls (
id INT AUTO_INCREMENT PRIMARY KEY,
symbol VARCHAR(20) NOT NULL,
target_price DECIMAL(20,8) NOT NULL,
execution_time TIMESTAMP NOT NULL,
duration INT DEFAULT 60 COMMENT 'seconds',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)"
];
foreach ($tables as $sql) {
try {
$db->exec($sql);
echo "Table created/verified successfully.\n";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}