Skip to content

Commit fd63360

Browse files
committed
[Feat] Implement collaboration migration and service for shared content management
1 parent 8f6884d commit fd63360

4 files changed

Lines changed: 803 additions & 219 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
const mysql = require('mysql2/promise');
2+
const fs = require('fs');
3+
const path = require('path');
4+
require('dotenv').config();
5+
6+
async function runCollaborationMigration() {
7+
let connection;
8+
9+
try {
10+
console.log('[LOG collaboration_migration] ========= Starting collaboration tables migration');
11+
12+
// Create connection
13+
connection = await mysql.createConnection({
14+
host: process.env.DB_HOST || 'localhost',
15+
user: process.env.DB_USER || 'root',
16+
password: process.env.DB_PASSWORD || 'root',
17+
database: process.env.DB_NAME || 'unihub_db',
18+
multipleStatements: true
19+
});
20+
21+
console.log('[LOG collaboration_migration] ========= Connected to database');
22+
23+
// Check if users table exists
24+
const [tables] = await connection.execute("SHOW TABLES LIKE 'users'");
25+
if (tables.length === 0) {
26+
throw new Error('Users table does not exist. Please run basic migrations first.');
27+
}
28+
29+
// Get detailed information about users table
30+
const [createTable] = await connection.execute("SHOW CREATE TABLE users");
31+
console.log('[LOG collaboration_migration] ========= Users table structure:');
32+
console.log(createTable[0]['Create Table']);
33+
34+
// Try to create tables without foreign keys first
35+
console.log('[LOG collaboration_migration] ========= Creating tables without foreign keys...');
36+
37+
const createTablesSQL = `
38+
-- Create shared_content table without foreign keys
39+
CREATE TABLE IF NOT EXISTS shared_content (
40+
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
41+
user_id CHAR(36) NOT NULL,
42+
content_type ENUM('topic', 'quiz') NOT NULL,
43+
content_url VARCHAR(500) NOT NULL,
44+
title VARCHAR(255) NOT NULL,
45+
description TEXT,
46+
tags JSON,
47+
likes_count INT DEFAULT 0,
48+
comments_count INT DEFAULT 0,
49+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
50+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
51+
INDEX idx_user_id (user_id),
52+
INDEX idx_content_type (content_type),
53+
INDEX idx_created_at (created_at)
54+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
55+
56+
-- Create shared_content_likes table without foreign keys
57+
CREATE TABLE IF NOT EXISTS shared_content_likes (
58+
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
59+
shared_content_id CHAR(36) NOT NULL,
60+
user_id CHAR(36) NOT NULL,
61+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
62+
UNIQUE KEY unique_user_content (shared_content_id, user_id)
63+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
64+
65+
-- Create shared_content_comments table without foreign keys
66+
CREATE TABLE IF NOT EXISTS shared_content_comments (
67+
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
68+
shared_content_id CHAR(36) NOT NULL,
69+
user_id CHAR(36) NOT NULL,
70+
comment TEXT NOT NULL,
71+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
72+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
73+
INDEX idx_shared_content_id (shared_content_id),
74+
INDEX idx_user_id (user_id)
75+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
76+
`;
77+
78+
await connection.query(createTablesSQL);
79+
console.log('[LOG collaboration_migration] ========= Tables created successfully without foreign keys');
80+
81+
// Now try to add foreign keys one by one
82+
console.log('[LOG collaboration_migration] ========= Adding foreign key constraints...');
83+
84+
try {
85+
await connection.execute(`
86+
ALTER TABLE shared_content
87+
ADD CONSTRAINT fk_shared_content_user
88+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
89+
`);
90+
console.log('[LOG collaboration_migration] ========= Added foreign key for shared_content.user_id');
91+
} catch (error) {
92+
console.error('[LOG collaboration_migration] ========= Failed to add shared_content foreign key:', error.message);
93+
}
94+
95+
try {
96+
await connection.execute(`
97+
ALTER TABLE shared_content_likes
98+
ADD CONSTRAINT fk_shared_content_likes_content
99+
FOREIGN KEY (shared_content_id) REFERENCES shared_content(id) ON DELETE CASCADE
100+
`);
101+
console.log('[LOG collaboration_migration] ========= Added foreign key for shared_content_likes.shared_content_id');
102+
} catch (error) {
103+
console.error('[LOG collaboration_migration] ========= Failed to add shared_content_likes content foreign key:', error.message);
104+
}
105+
106+
try {
107+
await connection.execute(`
108+
ALTER TABLE shared_content_likes
109+
ADD CONSTRAINT fk_shared_content_likes_user
110+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
111+
`);
112+
console.log('[LOG collaboration_migration] ========= Added foreign key for shared_content_likes.user_id');
113+
} catch (error) {
114+
console.error('[LOG collaboration_migration] ========= Failed to add shared_content_likes user foreign key:', error.message);
115+
}
116+
117+
try {
118+
await connection.execute(`
119+
ALTER TABLE shared_content_comments
120+
ADD CONSTRAINT fk_shared_content_comments_content
121+
FOREIGN KEY (shared_content_id) REFERENCES shared_content(id) ON DELETE CASCADE
122+
`);
123+
console.log('[LOG collaboration_migration] ========= Added foreign key for shared_content_comments.shared_content_id');
124+
} catch (error) {
125+
console.error('[LOG collaboration_migration] ========= Failed to add shared_content_comments content foreign key:', error.message);
126+
}
127+
128+
try {
129+
await connection.execute(`
130+
ALTER TABLE shared_content_comments
131+
ADD CONSTRAINT fk_shared_content_comments_user
132+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
133+
`);
134+
console.log('[LOG collaboration_migration] ========= Added foreign key for shared_content_comments.user_id');
135+
} catch (error) {
136+
console.error('[LOG collaboration_migration] ========= Failed to add shared_content_comments user foreign key:', error.message);
137+
}
138+
139+
// Verify tables were created
140+
const [newTables] = await connection.execute("SHOW TABLES LIKE 'shared_content%'");
141+
console.log('[LOG collaboration_migration] ========= Created tables:', newTables.map(t => Object.values(t)[0]));
142+
143+
} catch (error) {
144+
console.error('[LOG collaboration_migration] ========= Migration failed:', error.message);
145+
throw error;
146+
} finally {
147+
if (connection) {
148+
await connection.end();
149+
}
150+
}
151+
}
152+
153+
// Run if called directly
154+
if (require.main === module) {
155+
runCollaborationMigration()
156+
.then(() => {
157+
console.log('[LOG collaboration_migration] ========= Migration completed successfully!');
158+
process.exit(0);
159+
})
160+
.catch((error) => {
161+
console.error('[LOG collaboration_migration] ========= Migration failed:', error);
162+
process.exit(1);
163+
});
164+
}
165+
166+
module.exports = { runCollaborationMigration };

0 commit comments

Comments
 (0)