174 lines
6.2 KiB
JavaScript
174 lines
6.2 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
const sqlite3 = require('sqlite3').verbose();
|
||
const path = require('path');
|
||
|
||
console.log('🔄 Starting Instagram to Facebook schema migration...');
|
||
|
||
// Open database
|
||
const dbPath = path.join(__dirname, 'water_stations.db');
|
||
const db = new sqlite3.Database(dbPath);
|
||
|
||
db.serialize(() => {
|
||
console.log('📋 Checking current database schema...');
|
||
|
||
// Check if instagram_id column exists
|
||
db.all("PRAGMA table_info(users)", (err, columns) => {
|
||
if (err) {
|
||
console.error('❌ Error checking table schema:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
const hasInstagramId = columns.some(col => col.name === 'instagram_id');
|
||
const hasFacebookId = columns.some(col => col.name === 'facebook_id');
|
||
|
||
console.log(`📊 Current schema status:`);
|
||
console.log(` - instagram_id column exists: ${hasInstagramId}`);
|
||
console.log(` - facebook_id column exists: ${hasFacebookId}`);
|
||
|
||
if (!hasInstagramId && hasFacebookId) {
|
||
console.log('✅ Migration already completed! Database is up to date.');
|
||
db.close();
|
||
return;
|
||
}
|
||
|
||
if (hasInstagramId) {
|
||
// Count Instagram users before migration
|
||
db.get('SELECT COUNT(*) as count FROM users WHERE instagram_id IS NOT NULL', (err, row) => {
|
||
if (err) {
|
||
console.error('❌ Error counting Instagram users:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
const instagramUserCount = row.count;
|
||
console.log(`📊 Found ${instagramUserCount} Instagram users in database`);
|
||
|
||
if (instagramUserCount > 0) {
|
||
console.log('⚠️ WARNING: Instagram OAuth is no longer supported.');
|
||
console.log(' These users will need to register again using Facebook or Google OAuth,');
|
||
console.log(' or create a username/password account.');
|
||
}
|
||
|
||
performMigration(hasFacebookId, instagramUserCount);
|
||
});
|
||
} else {
|
||
console.log('📄 No instagram_id column found, performing clean schema update...');
|
||
performMigration(hasFacebookId, 0);
|
||
}
|
||
});
|
||
|
||
function performMigration(hasFacebookId, instagramUserCount) {
|
||
console.log('\n🔧 Performing database migration...');
|
||
|
||
// Add facebook_id column if it doesn't exist
|
||
if (!hasFacebookId) {
|
||
console.log('➕ Adding facebook_id column...');
|
||
db.run('ALTER TABLE users ADD COLUMN facebook_id TEXT', (err) => {
|
||
if (err) {
|
||
console.error('❌ Error adding facebook_id column:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log('✅ facebook_id column added successfully');
|
||
dropInstagramColumn(instagramUserCount);
|
||
});
|
||
} else {
|
||
dropInstagramColumn(instagramUserCount);
|
||
}
|
||
}
|
||
|
||
function dropInstagramColumn(instagramUserCount) {
|
||
// Check if instagram_id column exists before trying to drop it
|
||
db.all("PRAGMA table_info(users)", (err, columns) => {
|
||
if (err) {
|
||
console.error('❌ Error checking table schema:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
const hasInstagramId = columns.some(col => col.name === 'instagram_id');
|
||
|
||
if (hasInstagramId) {
|
||
console.log('🗑️ Removing deprecated instagram_id column...');
|
||
|
||
// SQLite doesn't support DROP COLUMN directly, so we need to recreate the table
|
||
const createNewTableSQL = `
|
||
CREATE TABLE users_new (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
username TEXT UNIQUE,
|
||
email TEXT UNIQUE,
|
||
password_hash TEXT,
|
||
google_id TEXT,
|
||
facebook_id TEXT,
|
||
display_name TEXT,
|
||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||
)
|
||
`;
|
||
|
||
db.run(createNewTableSQL, (err) => {
|
||
if (err) {
|
||
console.error('❌ Error creating new users table:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
// Copy data from old table to new table (excluding instagram_id)
|
||
const copyDataSQL = `
|
||
INSERT INTO users_new (id, username, email, password_hash, google_id, facebook_id, display_name, created_at)
|
||
SELECT id, username, email, password_hash, google_id, NULL, display_name, created_at
|
||
FROM users
|
||
`;
|
||
|
||
db.run(copyDataSQL, (err) => {
|
||
if (err) {
|
||
console.error('❌ Error copying data to new table:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
// Drop old table and rename new table
|
||
db.run('DROP TABLE users', (err) => {
|
||
if (err) {
|
||
console.error('❌ Error dropping old users table:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
db.run('ALTER TABLE users_new RENAME TO users', (err) => {
|
||
if (err) {
|
||
console.error('❌ Error renaming new users table:', err);
|
||
process.exit(1);
|
||
}
|
||
|
||
completeMigration(instagramUserCount);
|
||
});
|
||
});
|
||
});
|
||
});
|
||
} else {
|
||
completeMigration(instagramUserCount);
|
||
}
|
||
});
|
||
}
|
||
|
||
function completeMigration(instagramUserCount) {
|
||
console.log('\n✅ Database migration completed successfully!');
|
||
console.log('\n📊 Migration Summary:');
|
||
console.log(' ✅ facebook_id column added');
|
||
console.log(' ✅ instagram_id column removed (deprecated)');
|
||
|
||
if (instagramUserCount > 0) {
|
||
console.log(` ⚠️ ${instagramUserCount} Instagram users will need to re-register`);
|
||
console.log('\n📝 Next Steps:');
|
||
console.log(' 1. Set up Facebook OAuth credentials in your .env file');
|
||
console.log(' 2. Notify existing Instagram users to register with Facebook/Google OAuth');
|
||
console.log(' 3. Consider adding a migration notice in your app UI');
|
||
}
|
||
|
||
console.log('\n🎉 Your database is now ready for Facebook OAuth!');
|
||
|
||
db.close((err) => {
|
||
if (err) {
|
||
console.error('❌ Error closing database:', err);
|
||
process.exit(1);
|
||
}
|
||
process.exit(0);
|
||
});
|
||
}
|
||
}); |