Add JS migration script
This commit is contained in:
53
src/lib/migrateData.js
Normal file
53
src/lib/migrateData.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import dotenv from 'dotenv';
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const cloudUrl = process.env.CLOUD_SUPABASE_URL;
|
||||
const cloudKey = process.env.CLOUD_SUPABASE_SERVICE_ROLE_KEY || process.env.CLOUD_SUPABASE_ANON_KEY;
|
||||
const onpremUrl = process.env.ONPREM_SUPABASE_URL;
|
||||
const onpremKey = process.env.ONPREM_SUPABASE_SERVICE_ROLE_KEY || process.env.ONPREM_SUPABASE_ANON_KEY;
|
||||
|
||||
if (!cloudUrl || !cloudKey || !onpremUrl || !onpremKey) {
|
||||
console.error('환경 변수 설정 오류: CLOUD/ONPREM URL 또는 키가 누락되었습니다.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cloud = createClient(cloudUrl, cloudKey);
|
||||
const onprem = createClient(onpremUrl, onpremKey);
|
||||
const tables = ['users', 'accounts', 'transactions'];
|
||||
|
||||
async function migrateTable(table) {
|
||||
console.log(`Migrating table: ${table}`);
|
||||
const { data, error } = await cloud.from(table).select('*');
|
||||
if (error) {
|
||||
if (error.code === '42P01') {
|
||||
console.warn(`Table ${table} not found in Cloud DB, skipping.`);
|
||||
return;
|
||||
}
|
||||
console.error(`Error fetching ${table}:`, error);
|
||||
return;
|
||||
}
|
||||
if (!data.length) {
|
||||
console.log(`${table} has no data to migrate.`);
|
||||
return;
|
||||
}
|
||||
const { error: insertError } = await onprem.from(table).upsert(data);
|
||||
if (insertError) {
|
||||
console.error(`Error inserting into ${table}:`, insertError);
|
||||
} else {
|
||||
console.log(`Migrated ${data.length} rows into ${table}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
for (const table of tables) {
|
||||
await migrateTable(table);
|
||||
}
|
||||
console.log('Migration complete.');
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error('Migration failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user