-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_posts.php
More file actions
155 lines (134 loc) · 6.65 KB
/
Copy pathmigrate_posts.php
File metadata and controls
155 lines (134 loc) · 6.65 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
require_once 'config/db.php';
echo "<h1>🔄 Database Migration Tool</h1>";
echo "<p>This script will help identify and fix column name issues.</p>";
try {
// Check if posts table has both columns
echo "<h2>📋 Table Structure Analysis:</h2>";
$stmt = $pdo->query("DESCRIBE posts");
$columns = $stmt->fetchAll();
$hasAuthorId = false;
$hasUserId = false;
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>";
echo "<tr style='background: #f8f9fa;'><th>Column</th><th>Type</th><th>Null</th><th>Key</th></tr>";
foreach ($columns as $col) {
if ($col['Field'] == 'author_id') $hasAuthorId = true;
if ($col['Field'] == 'user_id') $hasUserId = true;
$highlight = ($col['Field'] == 'author_id' || $col['Field'] == 'user_id') ? 'background: #ffffcc;' : '';
echo "<tr style='$highlight'>";
echo "<td><strong>" . $col['Field'] . "</strong></td>";
echo "<td>" . $col['Type'] . "</td>";
echo "<td>" . $col['Null'] . "</td>";
echo "<td>" . $col['Key'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h2>🔍 Column Status:</h2>";
if ($hasUserId) echo "<p style='color: green;'>✅ <strong>user_id</strong> column exists</p>";
if ($hasAuthorId) echo "<p style='color: orange;'>⚠️ <strong>author_id</strong> column exists</p>";
if ($hasAuthorId && $hasUserId) {
echo "<div style='background: #fff3cd; padding: 15px; border-radius: 5px; margin: 15px 0;'>";
echo "<h3>⚠️ Both Columns Exist!</h3>";
echo "<p>Your table has both user_id and author_id columns. We need to migrate data.</p>";
// Check data in both columns
$stmt = $pdo->query("SELECT COUNT(*) as count FROM posts WHERE author_id IS NOT NULL");
$authorIdCount = $stmt->fetch()['count'];
$stmt = $pdo->query("SELECT COUNT(*) as count FROM posts WHERE user_id IS NOT NULL");
$userIdCount = $stmt->fetch()['count'];
echo "<p><strong>Posts with author_id:</strong> $authorIdCount</p>";
echo "<p><strong>Posts with user_id:</strong> $userIdCount</p>";
if ($authorIdCount > 0 && $userIdCount == 0) {
echo "<h4>🔧 Migration Needed</h4>";
echo "<p>All posts are using author_id. We need to copy data to user_id.</p>";
echo "<form method='post' style='margin: 10px 0;'>";
echo "<button type='submit' name='migrate' value='author_to_user' style='background: #28a745; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer;'>Migrate author_id → user_id</button>";
echo "</form>";
}
echo "</div>";
}
if (!$hasUserId && $hasAuthorId) {
echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px; margin: 15px 0;'>";
echo "<h3>❌ Missing user_id Column</h3>";
echo "<p>Your table only has author_id. We need to add user_id column or rename author_id.</p>";
echo "<form method='post' style='margin: 10px 0;'>";
echo "<button type='submit' name='add_user_id' value='1' style='background: #dc3545; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer;'>Add user_id Column</button>";
echo "</form>";
echo "</div>";
}
// Handle migration
if ($_POST['migrate'] ?? false) {
if ($_POST['migrate'] == 'author_to_user') {
echo "<h3>🔄 Migrating Data...</h3>";
$stmt = $pdo->prepare("UPDATE posts SET user_id = author_id WHERE user_id IS NULL AND author_id IS NOT NULL");
$affected = $stmt->execute();
$count = $stmt->rowCount();
echo "<p style='color: green;'>✅ Migrated $count posts from author_id to user_id</p>";
}
}
if ($_POST['add_user_id'] ?? false) {
echo "<h3>🔧 Adding user_id Column...</h3>";
$stmt = $pdo->exec("ALTER TABLE posts ADD COLUMN user_id INT");
echo "<p style='color: green;'>✅ Added user_id column</p>";
echo "<p>Now copying data from author_id to user_id...</p>";
$stmt = $pdo->prepare("UPDATE posts SET user_id = author_id");
$stmt->execute();
$count = $stmt->rowCount();
echo "<p style='color: green;'>✅ Copied data for $count posts</p>";
}
// Show current posts
echo "<h2>📝 Current Posts:</h2>";
$stmt = $pdo->query("SELECT p.id, p.title, p.user_id, p.author_id, p.is_deleted, p.created_at, u.username
FROM posts p
LEFT JOIN users u ON u.id = COALESCE(p.user_id, p.author_id)
ORDER BY p.created_at DESC LIMIT 10");
$posts = $stmt->fetchAll();
if (empty($posts)) {
echo "<p>No posts found in database.</p>";
} else {
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
echo "<tr style='background: #f8f9fa;'><th>ID</th><th>Title</th><th>user_id</th><th>author_id</th><th>Username</th><th>Status</th><th>Created</th></tr>";
foreach ($posts as $post) {
$status = $post['is_deleted'] ? 'DELETED' : 'ACTIVE';
$rowColor = $post['is_deleted'] ? 'background: #f8d7da;' : 'background: #d4edda;';
echo "<tr style='$rowColor'>";
echo "<td>" . $post['id'] . "</td>";
echo "<td>" . htmlspecialchars($post['title']) . "</td>";
echo "<td>" . ($post['user_id'] ?? 'NULL') . "</td>";
echo "<td>" . ($post['author_id'] ?? 'NULL') . "</td>";
echo "<td>" . htmlspecialchars($post['username'] ?? 'Unknown') . "</td>";
echo "<td>" . $status . "</td>";
echo "<td>" . $post['created_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
} catch (PDOException $e) {
echo "<h3 style='color: red;'>❌ Database Error:</h3>";
echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>";
}
echo "<hr>";
echo "<a href='debug_posts.php' style='background: #007bff; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px; margin: 5px;'>Debug Posts</a>";
echo "<a href='profile.php' style='background: #28a745; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px; margin: 5px;'>Go to Profile</a>";
?>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
}
table {
width: 100%;
margin: 10px 0;
}
th,
td {
text-align: left;
padding: 8px;
}
th {
background: #e9ecef;
font-weight: 600;
}
</style>