Skip to content

Conversation

@lucasramallo
Copy link

@lucasramallo lucasramallo commented Oct 21, 2025

Descrição

Este PR refatora a estrutura do banco de dados para remover a tabela historico_consultas e introduz a view Agendamentos_Gerados, responsável por gerar dinamicamente as datas de consultas com base nas informações da tabela agendamento.

Também foram realizados ajustes nas tabelas agendamento e faltas para refletir o novo modelo proposto.

Related to: #313


Seed para facilitar testes

-- 1. Inserir um paciente (Maria Oliveira)
INSERT INTO pacientes (
    id, 
    nome_completo, 
    data_de_nascimento, 
    data_de_cadastro, 
    is_aluno
) VALUES (
    gen_random_uuid(), 
    'Maria Oliveira', 
    '1995-05-15', 
    '2025-10-22', 
    TRUE
);

-- 2. Inserir outro paciente (João Silva)
INSERT INTO pacientes (
    id, 
    nome_completo, 
    data_de_nascimento, 
    data_de_cadastro, 
    is_aluno
) VALUES (
    gen_random_uuid(), 
    'João Silva', 
    '1988-03-10', 
    '2025-10-22', 
    FALSE
);

-- 3. Inserir um profissional da saúde
INSERT INTO profissionais_da_saude (
    id, 
    nome, 
    area_da_saude
) VALUES (
    gen_random_uuid(), 
    'Dr. Carlos Souza', 
    'Medicina Geral'
);

-- 4. Inserir um atendimento
INSERT INTO atendimento (
    id
) VALUES (
    gen_random_uuid()
);

-- 5. Inserir cadastros anuais para 2025 (um por paciente, ativo)
INSERT INTO cadastro_anual (
    id, 
    ano, 
    ativo, 
    paciente_id
) VALUES (
    gen_random_uuid(), 
    2025, 
    TRUE, 
    (SELECT id FROM pacientes WHERE nome_completo = 'Maria Oliveira' LIMIT 1)
);

INSERT INTO cadastro_anual (
    id, 
    ano, 
    ativo, 
    paciente_id
) VALUES (
    gen_random_uuid(), 
    2025, 
    TRUE, 
    (SELECT id FROM pacientes WHERE nome_completo = 'João Silva' LIMIT 1)
);

-- 6. Inserir agendamentos para cada cadastro anual
-- Agendamento para Maria Oliveira (mensal em 2025, começando em 2025-07-22 até 2025-12-31)
INSERT INTO agendamentos (
    id, 
    frequencia_dias, 
    data_criacao, 
    profissional_id, 
    data_inicial, 
    data_final, 
    atendimento_id, 
    cadastro_anual_id, 
    hora
) VALUES (
    gen_random_uuid(), 
    30, 
    CURRENT_TIMESTAMP, 
    (SELECT id FROM profissionais_da_saude LIMIT 1), 
    '2025-07-22', 
    '2025-12-31', 
    (SELECT id FROM atendimento LIMIT 1), 
    (SELECT id FROM cadastro_anual WHERE paciente_id = (SELECT id FROM pacientes WHERE nome_completo = 'Maria Oliveira' LIMIT 1) LIMIT 1), 
    '10:00:00'
);

-- Agendamento para João Silva (bimensal em 2025, começando em 2025-01-01 até 2025-12-31)
INSERT INTO agendamentos (
    id, 
    frequencia_dias, 
    data_criacao, 
    profissional_id, 
    data_inicial, 
    data_final, 
    atendimento_id, 
    cadastro_anual_id, 
    hora
) VALUES (
    gen_random_uuid(), 
    14, 
    CURRENT_TIMESTAMP, 
    (SELECT id FROM profissionais_da_saude LIMIT 1), 
    '2025-01-01', 
    '2025-12-31', 
    (SELECT id FROM atendimento LIMIT 1), 
    (SELECT id FROM cadastro_anual WHERE paciente_id = (SELECT id FROM pacientes WHERE nome_completo = 'João Silva' LIMIT 1) LIMIT 1), 
    '14:00:00'
);

-- 7. Verificar os dados inseridos
SELECT * FROM pacientes;
SELECT * FROM profissionais_da_saude;
SELECT * FROM atendimento;
SELECT * FROM cadastro_anual;
SELECT * FROM agendamentos;

Consulte os dados da view

SELECT * FROM "Agendamentos_Gerados" ORDER BY data_consulta, paciente_nome;

Evidencias

image

@lucasramallo lucasramallo linked an issue Oct 21, 2025 that may be closed by this pull request
9 tasks
@lucasramallo lucasramallo changed the base branch from main to backend-refactor October 21, 2025 23:16
@coderabbitai
Copy link

coderabbitai bot commented Oct 21, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 299-database-refatorar-banco-de-dados-agendamento

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lucasramallo lucasramallo marked this pull request as draft October 21, 2025 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DATABASE] - Refatorar Banco de Dados

3 participants