recreate-database.js•2.77 kB
#!/usr/bin/env node
/**
* Script to recreate the database with the new schema
* WARNING: This will delete all existing data!
*/
require('dotenv').config();
const { DataSource } = require('typeorm');
const AppDataSource = new DataSource({
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5433'),
username: process.env.DB_USER || process.env.DB_USERNAME || 'mcp_user',
password: process.env.DB_PASSWORD || '123456',
database: process.env.DB_NAME || process.env.DB_DATABASE || 'mcp_rss',
synchronize: true, // THIS WILL DROP AND RECREATE TABLES
logging: true,
entities: ['dist/entities/**/*.js']
});
async function recreateDatabase() {
console.log('⚠️ WARNING: This will DROP all tables and recreate them!');
console.log('⚠️ All existing data will be LOST!');
console.log('');
console.log('Connecting to database...');
try {
// First, connect with synchronize: false to manually drop tables
const tempDataSource = new DataSource({
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5433'),
username: process.env.DB_USER || process.env.DB_USERNAME || 'mcp_user',
password: process.env.DB_PASSWORD || '123456',
database: process.env.DB_NAME || process.env.DB_DATABASE || 'mcp_rss',
synchronize: false,
logging: false
});
await tempDataSource.initialize();
console.log('✓ Connected to database');
console.log('');
console.log('Dropping existing tables...');
// Drop tables in correct order (child tables first)
await tempDataSource.query('DROP TABLE IF EXISTS "article" CASCADE');
await tempDataSource.query('DROP TABLE IF EXISTS "feed" CASCADE');
await tempDataSource.query('DROP TYPE IF EXISTS "article_status_enum" CASCADE');
console.log('✓ Tables dropped');
await tempDataSource.destroy();
// Now reconnect with synchronize: true to create new schema
await AppDataSource.initialize();
console.log('✓ Schema synchronized (new tables created)');
console.log('');
console.log('Database schema has been recreated with proper timestamp columns:');
console.log(' - pubDate: timestamp (publication date from RSS feed)');
console.log(' - fetchDate: timestamp (when article was fetched)');
console.log('');
console.log('Next steps:');
console.log(' 1. Restart your MCP server');
console.log(' 2. Wait for the cron job to fetch articles');
console.log(' 3. Articles will now have proper date filtering!');
await AppDataSource.destroy();
process.exit(0);
} catch (error) {
console.error('❌ Error:', error.message);
process.exit(1);
}
}
recreateDatabase();