-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_database.php
More file actions
128 lines (109 loc) · 4.54 KB
/
migrate_database.php
File metadata and controls
128 lines (109 loc) · 4.54 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Advanced Stats Plugin - Database Migration Script
* Run this script to update the database schema for existing installations
*
* Usage: php migrate_database.php
*/
$dbPath = '/home/fpp/media/config/plugin.fpp-plugin-AdvancedStats.db';
if (!file_exists($dbPath)) {
echo "Error: Database not found at $dbPath\n";
echo "Please install the plugin first.\n";
exit(1);
}
try {
$db = new SQLite3($dbPath);
echo "Advanced Stats Plugin - Database Migration\n";
echo "==========================================\n\n";
$migrationsApplied = 0;
// Migration 1: Add description column to gpio_events
echo "Checking Migration 1: gpio_events.description column...\n";
$result = $db->query("PRAGMA table_info(gpio_events)");
$hasDescription = false;
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
if ($row['name'] === 'description') {
$hasDescription = true;
break;
}
}
if (!$hasDescription) {
echo " Applying: Adding description column to gpio_events...\n";
$db->exec('ALTER TABLE gpio_events ADD COLUMN description TEXT');
echo " ✓ Migration applied successfully\n";
$migrationsApplied++;
} else {
echo " ✓ Already applied (column exists)\n";
}
echo "\n";
// Migration 2: Create command_history table if it doesn't exist
echo "Checking Migration 2: command_history table...\n";
$result = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='command_history'");
$hasCommandHistory = $result->fetchArray() !== false;
if (!$hasCommandHistory) {
echo " Applying: Creating command_history table...\n";
$db->exec('
CREATE TABLE IF NOT EXISTS command_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER NOT NULL,
command TEXT NOT NULL,
args TEXT,
multisyncCommand INTEGER DEFAULT 0,
multisyncHosts TEXT,
trigger_source TEXT,
payload_json TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
');
$db->exec('CREATE INDEX IF NOT EXISTS idx_cmd_timestamp ON command_history(timestamp)');
$db->exec('CREATE INDEX IF NOT EXISTS idx_cmd_command ON command_history(command)');
echo " ✓ Migration applied successfully\n";
$migrationsApplied++;
} else {
echo " ✓ Already applied (table exists)\n";
}
echo "\n";
// Migration 3: Create command_preset_history table if it doesn't exist
echo "Checking Migration 3: command_preset_history table...\n";
$result = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='command_preset_history'");
$hasPresetHistory = $result->fetchArray() !== false;
if (!$hasPresetHistory) {
echo " Applying: Creating command_preset_history table...\n";
$db->exec('
CREATE TABLE IF NOT EXISTS command_preset_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER NOT NULL,
preset_name TEXT NOT NULL,
command_count INTEGER DEFAULT 0,
trigger_source TEXT,
payload_json TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
');
$db->exec('CREATE INDEX IF NOT EXISTS idx_preset_timestamp ON command_preset_history(timestamp)');
$db->exec('CREATE INDEX IF NOT EXISTS idx_preset_name ON command_preset_history(preset_name)');
echo " ✓ Migration applied successfully\n";
$migrationsApplied++;
} else {
echo " ✓ Already applied (table exists)\n";
}
echo "\n";
// Future migrations can be added here following the same pattern
// Example:
// echo "Checking Migration 2: new_feature...\n";
// ... check and apply migration ...
echo "==========================================\n";
echo "Migration Summary:\n";
echo " - Migrations applied: $migrationsApplied\n";
echo " - Database schema is up to date\n";
if ($migrationsApplied > 0) {
echo "\nDatabase migration completed successfully!\n";
echo "The plugin should now work correctly with the latest features.\n";
} else {
echo "\nNo migrations needed - database is already up to date.\n";
}
$db->close();
} catch (Exception $e) {
echo "Error during migration: " . $e->getMessage() . "\n";
exit(1);
}
?>