![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/migrations/ |
<?php
/**
* Migration: Add published_at column to music_tracks table
*
* This column stores the permanent first-publish timestamp.
* Once set, it never changes - prevents gaming the "newest" sorting.
*
* Run this migration once to add the column.
*/
require_once __DIR__ . '/../config/database.php';
echo "Starting migration: add_published_at_column\n";
echo "============================================\n\n";
try {
$pdo = getDBConnection();
if (!$pdo) {
throw new Exception("Failed to connect to database");
}
// Check if column already exists
$stmt = $pdo->query("SHOW COLUMNS FROM music_tracks LIKE 'published_at'");
$exists = $stmt->fetch();
if ($exists) {
echo "✓ Column 'published_at' already exists. Skipping.\n";
} else {
// Add the published_at column
$pdo->exec("
ALTER TABLE music_tracks
ADD COLUMN published_at TIMESTAMP NULL DEFAULT NULL
AFTER is_public
");
echo "✓ Added 'published_at' column to music_tracks table.\n";
}
// Backfill: Set published_at for tracks that are already public
// Use their created_at date as the published_at
$stmt = $pdo->prepare("
UPDATE music_tracks
SET published_at = created_at
WHERE is_public = 1
AND published_at IS NULL
");
$stmt->execute();
$affected = $stmt->rowCount();
if ($affected > 0) {
echo "✓ Backfilled published_at for $affected existing public tracks.\n";
} else {
echo "✓ No existing public tracks needed backfill.\n";
}
// Add index for sorting performance
try {
$pdo->exec("
CREATE INDEX idx_published_at ON music_tracks(published_at DESC)
");
echo "✓ Added index on published_at column.\n";
} catch (Exception $e) {
if (strpos($e->getMessage(), 'Duplicate key name') !== false) {
echo "✓ Index already exists. Skipping.\n";
} else {
throw $e;
}
}
echo "\n============================================\n";
echo "Migration completed successfully!\n";
} catch (Exception $e) {
echo "✗ Migration failed: " . $e->getMessage() . "\n";
exit(1);
}