Deploy: 2026-05-25 00:29:42
This commit is contained in:
54
backend/migrate_meta_sessions.php
Normal file
54
backend/migrate_meta_sessions.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->safeLoad();
|
||||
|
||||
try {
|
||||
$pdo = new PDO(
|
||||
"mysql:host=" . $_ENV['DB_HOST'] . ";dbname=" . $_ENV['DB_NAME'],
|
||||
$_ENV['DB_USER'],
|
||||
$_ENV['DB_PASS']
|
||||
);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
echo "=== Running Database Migrations: Meta Channel Integration ===\n";
|
||||
|
||||
// 1. Create meta_sessions table
|
||||
$createSessionsTableSql = "
|
||||
CREATE TABLE IF NOT EXISTS `meta_sessions` (
|
||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`company_id` INT NOT NULL,
|
||||
`channel_type` ENUM('messenger', 'instagram') NOT NULL,
|
||||
`page_id` VARCHAR(255) NOT NULL,
|
||||
`page_name` VARCHAR(255) NOT NULL,
|
||||
`page_access_token` TEXT NOT NULL,
|
||||
`status` ENUM('connected', 'disconnected') DEFAULT 'connected',
|
||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (`company_id`) REFERENCES `companies`(`id`) ON DELETE CASCADE,
|
||||
UNIQUE KEY `page_channel_unique` (`page_id`, `channel_type`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
";
|
||||
$pdo->exec($createSessionsTableSql);
|
||||
echo "✅ Table 'meta_sessions' verified/created.\n";
|
||||
|
||||
// 2. Make session_id column in messages_log nullable
|
||||
// First check current column definition or just modify it
|
||||
$pdo->exec("ALTER TABLE `messages_log` MODIFY COLUMN `session_id` INT NULL");
|
||||
echo "✅ Modified 'session_id' in 'messages_log' to be nullable.\n";
|
||||
|
||||
// 3. Add meta_session_id column if not exists
|
||||
$result = $pdo->query("SHOW COLUMNS FROM `messages_log` LIKE 'meta_session_id'");
|
||||
if ($result->rowCount() === 0) {
|
||||
$pdo->exec("ALTER TABLE `messages_log` ADD COLUMN `meta_session_id` INT NULL AFTER `session_id`");
|
||||
$pdo->exec("ALTER TABLE `messages_log` ADD CONSTRAINT `fk_msg_meta_session` FOREIGN KEY (`meta_session_id`) REFERENCES `meta_sessions`(`id`) ON DELETE CASCADE");
|
||||
echo "✅ Added 'meta_session_id' column and foreign key constraint to 'messages_log'.\n";
|
||||
} else {
|
||||
echo "ℹ️ Column 'meta_session_id' already exists in 'messages_log'. Skipping.\n";
|
||||
}
|
||||
|
||||
echo "Migration completed successfully!\n";
|
||||
} catch (PDOException $e) {
|
||||
echo "❌ Database error: " . $e->getMessage() . "\n";
|
||||
}
|
||||
Reference in New Issue
Block a user