Initial Commit
This commit is contained in:
106
database/migrate.php
Normal file
106
database/migrate.php
Normal file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
// Load environment variables
|
||||
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
try {
|
||||
$host = $_ENV['DB_HOST'];
|
||||
$port = $_ENV['DB_PORT'];
|
||||
$database = $_ENV['DB_DATABASE'];
|
||||
$username = $_ENV['DB_USERNAME'];
|
||||
$password = $_ENV['DB_PASSWORD'];
|
||||
|
||||
// Connect to database
|
||||
$dsn = "mysql:host=$host;port=$port;dbname=$database;charset=utf8mb4";
|
||||
$pdo = new PDO($dsn, $username, $password, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
]);
|
||||
|
||||
echo "Connected to database successfully!\n\n";
|
||||
|
||||
// Generate random admin password
|
||||
$adminPassword = bin2hex(random_bytes(8)); // 16 character random password
|
||||
$adminPasswordHash = password_hash($adminPassword, PASSWORD_BCRYPT);
|
||||
|
||||
// Get all migration files
|
||||
$migrationFiles = [
|
||||
__DIR__ . '/migrations/001_create_tables.sql',
|
||||
__DIR__ . '/migrations/002_create_users_table.sql',
|
||||
__DIR__ . '/migrations/003_add_whois_fields.sql',
|
||||
__DIR__ . '/migrations/004_create_tld_registry_table.sql',
|
||||
__DIR__ . '/migrations/005_update_tld_import_logs.sql',
|
||||
__DIR__ . '/migrations/006_add_complete_workflow_import_type.sql',
|
||||
];
|
||||
|
||||
foreach ($migrationFiles as $migrationFile) {
|
||||
if (!file_exists($migrationFile)) {
|
||||
echo "⚠ Migration file not found: " . basename($migrationFile) . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
echo "Running migration: " . basename($migrationFile) . "\n";
|
||||
|
||||
$sql = file_get_contents($migrationFile);
|
||||
|
||||
// Replace password placeholder in users migration
|
||||
if (basename($migrationFile) === '002_create_users_table.sql') {
|
||||
$sql = str_replace('{{ADMIN_PASSWORD_HASH}}', $adminPasswordHash, $sql);
|
||||
}
|
||||
|
||||
// Split by semicolon and execute each statement
|
||||
$statements = array_filter(array_map('trim', explode(';', $sql)));
|
||||
|
||||
foreach ($statements as $statement) {
|
||||
if (!empty($statement)) {
|
||||
try {
|
||||
$pdo->exec($statement);
|
||||
} catch (PDOException $e) {
|
||||
// Check if it's a "column already exists" error for migrations 003 and 005
|
||||
if (strpos($e->getMessage(), 'Duplicate column name') !== false &&
|
||||
(basename($migrationFile) === '003_add_whois_fields.sql' ||
|
||||
basename($migrationFile) === '005_update_tld_import_logs.sql')) {
|
||||
echo " ⚠ Column already exists, skipping: " . $e->getMessage() . "\n";
|
||||
continue;
|
||||
}
|
||||
// Check if it's an enum modification error for migrations 005 and 006
|
||||
if (strpos($e->getMessage(), 'Duplicate entry') !== false &&
|
||||
(basename($migrationFile) === '005_update_tld_import_logs.sql' ||
|
||||
basename($migrationFile) === '006_add_complete_workflow_import_type.sql')) {
|
||||
echo " ⚠ Enum already updated, skipping: " . $e->getMessage() . "\n";
|
||||
continue;
|
||||
}
|
||||
// Re-throw other errors
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo "✓ " . basename($migrationFile) . " completed\n";
|
||||
}
|
||||
|
||||
echo "\n✓ All migrations completed successfully!\n";
|
||||
echo "✓ All tables created.\n";
|
||||
echo "\n🔑 Admin credentials (SAVE THESE!):\n";
|
||||
echo " ═══════════════════════════════════════\n";
|
||||
echo " Username: admin\n";
|
||||
echo " Password: $adminPassword\n";
|
||||
echo " ═══════════════════════════════════════\n";
|
||||
echo " ⚠️ This password will not be shown again!\n";
|
||||
echo " 💾 Save it to a secure password manager.\n\n";
|
||||
echo "🌐 TLD Registry System:\n";
|
||||
echo " • Import RDAP data: php cron/import_tld_registry.php --rdap-only\n";
|
||||
echo " • Import WHOIS data: php cron/import_tld_registry.php --whois-only\n";
|
||||
echo " • Check for updates: php cron/import_tld_registry.php --check-updates\n";
|
||||
echo " • Full import: php cron/import_tld_registry.php\n\n";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "✗ Migration failed: " . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
72
database/migrations/001_create_tables.sql
Normal file
72
database/migrations/001_create_tables.sql
Normal file
@@ -0,0 +1,72 @@
|
||||
-- Create notification_groups table
|
||||
CREATE TABLE IF NOT EXISTS notification_groups (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Create notification_channels table
|
||||
CREATE TABLE IF NOT EXISTS notification_channels (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
notification_group_id INT NOT NULL,
|
||||
channel_type ENUM('email', 'telegram', 'discord', 'slack') NOT NULL,
|
||||
channel_config JSON NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (notification_group_id) REFERENCES notification_groups(id) ON DELETE CASCADE,
|
||||
INDEX idx_notification_group_id (notification_group_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Create domains table
|
||||
CREATE TABLE IF NOT EXISTS domains (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
domain_name VARCHAR(255) NOT NULL UNIQUE,
|
||||
notification_group_id INT,
|
||||
registrar VARCHAR(255),
|
||||
expiration_date DATE,
|
||||
last_checked TIMESTAMP NULL,
|
||||
status ENUM('active', 'expiring_soon', 'expired', 'error') DEFAULT 'active',
|
||||
whois_data JSON,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (notification_group_id) REFERENCES notification_groups(id) ON DELETE SET NULL,
|
||||
INDEX idx_notification_group_id (notification_group_id),
|
||||
INDEX idx_expiration_date (expiration_date),
|
||||
INDEX idx_status (status)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Create notification_logs table
|
||||
CREATE TABLE IF NOT EXISTS notification_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
domain_id INT NOT NULL,
|
||||
notification_type VARCHAR(50) NOT NULL,
|
||||
channel_type VARCHAR(50) NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
status ENUM('sent', 'failed') DEFAULT 'sent',
|
||||
error_message TEXT,
|
||||
FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE,
|
||||
INDEX idx_domain_id (domain_id),
|
||||
INDEX idx_sent_at (sent_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Create settings table
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
setting_key VARCHAR(255) NOT NULL UNIQUE,
|
||||
setting_value TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Insert default settings
|
||||
INSERT INTO settings (setting_key, setting_value) VALUES
|
||||
('notification_days_before', '30,15,7,3,1'),
|
||||
('check_interval_hours', '24'),
|
||||
('last_check_run', NULL)
|
||||
ON DUPLICATE KEY UPDATE setting_key=setting_key;
|
||||
|
||||
22
database/migrations/002_create_users_table.sql
Normal file
22
database/migrations/002_create_users_table.sql
Normal file
@@ -0,0 +1,22 @@
|
||||
-- Create users table
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255),
|
||||
full_name VARCHAR(255),
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
last_login TIMESTAMP NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_username (username),
|
||||
INDEX idx_email (email)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Insert default admin user
|
||||
-- Password is randomly generated during migration and displayed in output
|
||||
-- Hash placeholder will be replaced by migrate.php
|
||||
INSERT INTO users (username, password, email, full_name, is_active) VALUES
|
||||
('admin', '{{ADMIN_PASSWORD_HASH}}', 'admin@domainmonitor.local', 'Administrator', 1)
|
||||
ON DUPLICATE KEY UPDATE username=username;
|
||||
|
||||
13
database/migrations/003_add_whois_fields.sql
Normal file
13
database/migrations/003_add_whois_fields.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
-- Add WHOIS-related columns to domains table
|
||||
-- Note: These statements may show warnings if columns already exist, but won't fail
|
||||
|
||||
-- Add registrar_url column
|
||||
ALTER TABLE domains ADD COLUMN registrar_url VARCHAR(255) AFTER registrar;
|
||||
|
||||
-- Add updated_date column
|
||||
ALTER TABLE domains ADD COLUMN updated_date DATE AFTER expiration_date;
|
||||
|
||||
-- Add abuse_email column
|
||||
ALTER TABLE domains ADD COLUMN abuse_email VARCHAR(255) AFTER updated_date;
|
||||
|
||||
37
database/migrations/004_create_tld_registry_table.sql
Normal file
37
database/migrations/004_create_tld_registry_table.sql
Normal file
@@ -0,0 +1,37 @@
|
||||
-- Create tld_registry table for storing TLD registry information
|
||||
CREATE TABLE IF NOT EXISTS tld_registry (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
tld VARCHAR(63) NOT NULL UNIQUE,
|
||||
rdap_servers JSON,
|
||||
whois_server VARCHAR(255),
|
||||
registry_url VARCHAR(500),
|
||||
iana_publication_date TIMESTAMP NULL,
|
||||
iana_last_updated TIMESTAMP NULL,
|
||||
record_last_updated TIMESTAMP NULL,
|
||||
registration_date DATE NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_tld (tld),
|
||||
INDEX idx_is_active (is_active),
|
||||
INDEX idx_iana_publication_date (iana_publication_date)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- Create tld_import_logs table for tracking import operations
|
||||
CREATE TABLE IF NOT EXISTS tld_import_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
import_type ENUM('tld_list', 'rdap', 'whois', 'manual') NOT NULL,
|
||||
total_tlds INT DEFAULT 0,
|
||||
new_tlds INT DEFAULT 0,
|
||||
updated_tlds INT DEFAULT 0,
|
||||
failed_tlds INT DEFAULT 0,
|
||||
iana_publication_date TIMESTAMP NULL,
|
||||
version VARCHAR(50) NULL,
|
||||
started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP NULL,
|
||||
status ENUM('running', 'completed', 'failed') DEFAULT 'running',
|
||||
error_message TEXT,
|
||||
details JSON,
|
||||
INDEX idx_started_at (started_at),
|
||||
INDEX idx_status (status)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
11
database/migrations/005_update_tld_import_logs.sql
Normal file
11
database/migrations/005_update_tld_import_logs.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- Update tld_import_logs table to support TLD list imports
|
||||
-- Add version field and update import_type enum
|
||||
|
||||
-- Add version column (will fail gracefully if column already exists)
|
||||
ALTER TABLE tld_import_logs
|
||||
ADD COLUMN version VARCHAR(50) NULL AFTER iana_publication_date;
|
||||
|
||||
-- Update import_type enum to include 'tld_list'
|
||||
-- Note: This will fail gracefully if the enum already includes 'tld_list'
|
||||
ALTER TABLE tld_import_logs
|
||||
MODIFY COLUMN import_type ENUM('tld_list', 'rdap', 'whois', 'manual') NOT NULL;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Add complete_workflow to import_type enum
|
||||
ALTER TABLE tld_import_logs
|
||||
MODIFY COLUMN import_type ENUM('tld_list', 'rdap', 'whois', 'manual', 'complete_workflow', 'check_updates') NOT NULL;
|
||||
Reference in New Issue
Block a user