98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
/**
|
|
* Script de Migração: Criação das tabelas de Pré-Matrícula
|
|
* Execução: node scratch/create_prematricula_tables.cjs
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
const pool = new Pool({
|
|
connectionString: 'postgresql://edumanager:EduManager2026!Seguro@150.230.87.131:5432/edumanager'
|
|
});
|
|
|
|
async function migrate() {
|
|
const client = await pool.connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
// 1. Tabela de configuração do formulário de pré-matrícula
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS prematricula_config (
|
|
id SERIAL PRIMARY KEY,
|
|
titulo TEXT NOT NULL DEFAULT 'Pré-Matrícula Online',
|
|
descricao TEXT DEFAULT '',
|
|
slug TEXT NOT NULL DEFAULT 'pre-matricula',
|
|
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'published')),
|
|
cor_primaria TEXT DEFAULT '#4f46e5',
|
|
logo_url TEXT DEFAULT '',
|
|
mensagem_sucesso TEXT DEFAULT 'Sua pré-matrícula foi enviada com sucesso! Entraremos em contato em breve.',
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
);
|
|
`);
|
|
|
|
// Inserir registro default se não existir
|
|
await client.query(`
|
|
INSERT INTO prematricula_config (id, titulo, descricao, slug, status)
|
|
VALUES (1, 'Pré-Matrícula Online', 'Preencha o formulário abaixo para realizar sua pré-matrícula.', 'pre-matricula', 'draft')
|
|
ON CONFLICT (id) DO NOTHING;
|
|
`);
|
|
|
|
// 2. Tabela de campos personalizáveis do formulário
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS prematricula_campos (
|
|
id TEXT PRIMARY KEY,
|
|
label TEXT NOT NULL,
|
|
tipo TEXT NOT NULL DEFAULT 'text' CHECK (tipo IN ('text', 'email', 'phone', 'cpf', 'date', 'select', 'textarea', 'number')),
|
|
placeholder TEXT DEFAULT '',
|
|
obrigatorio BOOLEAN DEFAULT false,
|
|
opcoes TEXT[] DEFAULT '{}',
|
|
ordem INTEGER NOT NULL DEFAULT 0,
|
|
ativo BOOLEAN DEFAULT true,
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
);
|
|
`);
|
|
|
|
// 3. Tabela de inscrições de pré-matrícula
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS prematriculas (
|
|
id TEXT PRIMARY KEY,
|
|
nome TEXT NOT NULL,
|
|
email TEXT DEFAULT '',
|
|
telefone TEXT DEFAULT '',
|
|
turma_id TEXT,
|
|
turma_nome TEXT DEFAULT '',
|
|
respostas JSONB DEFAULT '{}',
|
|
status TEXT NOT NULL DEFAULT 'pendente' CHECK (status IN ('pendente', 'aprovado', 'rejeitado', 'matriculado')),
|
|
observacoes TEXT DEFAULT '',
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
);
|
|
`);
|
|
|
|
// Índices
|
|
await client.query(`
|
|
CREATE INDEX IF NOT EXISTS idx_prematriculas_turma ON prematriculas(turma_id);
|
|
CREATE INDEX IF NOT EXISTS idx_prematriculas_status ON prematriculas(status);
|
|
CREATE INDEX IF NOT EXISTS idx_prematriculas_created ON prematriculas(created_at DESC);
|
|
`);
|
|
|
|
await client.query('COMMIT');
|
|
console.log('✅ Tabelas de pré-matrícula criadas com sucesso!');
|
|
|
|
// Verificar
|
|
const { rows } = await client.query(`
|
|
SELECT table_name FROM information_schema.tables
|
|
WHERE table_schema = 'public' AND table_name LIKE 'prematricula%'
|
|
ORDER BY table_name;
|
|
`);
|
|
console.log('📊 Tabelas criadas:', rows.map(r => r.table_name));
|
|
|
|
} catch (error) {
|
|
await client.query('ROLLBACK');
|
|
console.error('❌ Erro na migração:', error.message);
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
migrate();
|