AeThex-Connect/scripts/create-tables.js
2026-02-28 23:50:09 -07:00

89 lines
3.4 KiB
JavaScript

const { Pool } = require('pg');
require('dotenv').config();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
async function createMissingTables() {
const pool = new Pool({ connectionString: process.env.DATABASE_URL?.replace('?sslmode=require', ''), ssl: true });
console.log('Creating missing tables for AeThex Connect...\n');
// Create servers table
console.log('Creating servers table...');
await pool.query(`
CREATE TABLE IF NOT EXISTS servers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) NOT NULL,
description TEXT,
icon_url TEXT,
banner_url TEXT,
owner_id UUID NOT NULL,
invite_code VARCHAR(20) UNIQUE,
is_public BOOLEAN DEFAULT false,
member_count INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
)
`);
console.log(' ✓ servers created');
// Create server_members table
console.log('Creating server_members table...');
await pool.query(`
CREATE TABLE IF NOT EXISTS server_members (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
server_id UUID NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
user_id UUID NOT NULL,
nickname VARCHAR(100),
role VARCHAR(20) CHECK (role IN ('owner', 'admin', 'moderator', 'member')) DEFAULT 'member',
joined_at TIMESTAMP DEFAULT NOW(),
CONSTRAINT unique_server_member UNIQUE(server_id, user_id)
)
`);
console.log(' ✓ server_members created');
// Create indexes
console.log('Creating indexes...');
await pool.query(`CREATE INDEX IF NOT EXISTS idx_servers_owner_id ON servers(owner_id)`);
await pool.query(`CREATE INDEX IF NOT EXISTS idx_servers_invite_code ON servers(invite_code)`);
await pool.query(`CREATE INDEX IF NOT EXISTS idx_server_members_server_id ON server_members(server_id)`);
await pool.query(`CREATE INDEX IF NOT EXISTS idx_server_members_user_id ON server_members(user_id)`);
console.log(' ✓ indexes created');
// Check existing channels table structure
console.log('\nChecking channels table structure...');
const channelCols = await pool.query(`
SELECT column_name FROM information_schema.columns WHERE table_name = 'channels'
`);
const cols = channelCols.rows.map(r => r.column_name);
console.log(' Columns:', cols.join(', '));
// Add server_id to channels if missing
if (!cols.includes('server_id')) {
console.log(' Adding server_id column to channels...');
await pool.query(`ALTER TABLE channels ADD COLUMN server_id UUID REFERENCES servers(id) ON DELETE CASCADE`);
console.log(' ✓ server_id added');
}
// Check messages table structure
console.log('\nChecking messages table structure...');
const msgCols = await pool.query(`
SELECT column_name FROM information_schema.columns WHERE table_name = 'messages'
`);
const msgColNames = msgCols.rows.map(r => r.column_name);
console.log(' Columns:', msgColNames.join(', '));
// Add channel_id to messages if missing
if (!msgColNames.includes('channel_id')) {
console.log(' Adding channel_id column to messages...');
await pool.query(`ALTER TABLE messages ADD COLUMN channel_id UUID REFERENCES channels(id) ON DELETE CASCADE`);
console.log(' ✓ channel_id added');
}
console.log('\n✅ All tables ready!');
await pool.end();
}
createMissingTables().catch(err => {
console.error('Error:', err.message);
process.exit(1);
});