From 3604c2cfb4393d4b9a03a18eac629204e63fe6b3 Mon Sep 17 00:00:00 2001 From: bielvigna Date: Sat, 8 Aug 2026 12:52:43 -0300 Subject: [PATCH 1/2] adicionando tabela de anexos dentro do supabase --- README.md | 3 ++- sql/script.sql | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ca57a0..5aef3a6 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# acta-template-repository \ No newline at end of file +# ACTA DATABASE - Postgresql + diff --git a/sql/script.sql b/sql/script.sql index a29b961..5a1684a 100644 --- a/sql/script.sql +++ b/sql/script.sql @@ -367,4 +367,76 @@ CREATE TABLE IF NOT EXISTS auditoria.log_tarefa ( data_log TIMESTAMPTZ NOT NULL DEFAULT NOW(), operacao VARCHAR(40) NOT NULL CHECK (operacao IN ('INSERT', 'UPDATE', 'DELETE', 'LOGIN', 'LOGOUT', 'READ', 'EXPORT')), CONSTRAINT log_tarefa_json_check CHECK (jsonb_typeof(dados_antes) = 'object' AND jsonb_typeof(dados_depois) = 'object') +); + + + +-----------Anexos (alterar no modelo lógico) + +CREATE TABLE IF NOT EXISTS pdca.anexo ( + id BIGSERIAL PRIMARY KEY, + + id_storage TEXT UNIQUE NOT NULL, + + id_empresa BIGINT NOT NULL + REFERENCES empresa(id) ON DELETE RESTRICT, + + id_ciclo BIGINT NOT NULL + REFERENCES pdca.ciclo(id) ON DELETE RESTRICT, + + criado_por BIGINT + REFERENCES usuario_sistema(id) ON DELETE SET NULL, + + categoria VARCHAR(40) NOT NULL CHECK ( + categoria IN ( + 'TREINAMENTO', + 'PLANO_ACAO', + 'CAUSA_RAIZ', + 'PROBLEMA', + 'META', + 'RELATORIO', + 'LICAO_APRENDIDA', + 'FORMULARIO', + 'EVIDENCIA', + 'OUTRO' + ) + ), + + referencia_origem VARCHAR(100), --> campo para armazenar o id da origem -> categoria = 'PLANO_ACAO' -> id_plano_acao + nome_arquivo VARCHAR(255) NOT NULL, + + tipo_arquivo VARCHAR(150) NOT NULL, + + tamanho_arquivo BIGINT NOT NULL + CHECK (tamanho_arquivo >= 0), + + bucket_arquivo VARCHAR(100) NOT NULL + DEFAULT 'acta-arquivos', + + caminho_arquivo TEXT NOT NULL UNIQUE, + + status VARCHAR(30) NOT NULL DEFAULT 'ATIVO' CHECK ( + status IN ( + 'PROCESSANDO', + 'ATIVO', + 'ERRO', + 'EXCLUIDO' + ) + ), + + descricao TEXT, + + criado_em TIMESTAMPTZ NOT NULL DEFAULT NOW(), + atualizado_em TIMESTAMPTZ, + excluido_em TIMESTAMPTZ, + + CONSTRAINT anexo_tipo_arquivo_check CHECK ( + tipo_arquivo = 'application/pdf' + OR tipo_arquivo LIKE 'image/%' + OR tipo_arquivo = + 'application/vnd.openxmlformats-officedocument.presentationml.presentation' + OR tipo_arquivo = + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ), + ); \ No newline at end of file From 6dc2ae2c0704b02796a9e9520b8ac33d4c29f503 Mon Sep 17 00:00:00 2001 From: bielvigna Date: Sat, 8 Aug 2026 13:00:00 -0300 Subject: [PATCH 2/2] hotfix/ ajustando referencias" --- sql/script.sql | 86 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/sql/script.sql b/sql/script.sql index 5a1684a..e30d5b9 100644 --- a/sql/script.sql +++ b/sql/script.sql @@ -376,16 +376,12 @@ CREATE TABLE IF NOT EXISTS auditoria.log_tarefa ( CREATE TABLE IF NOT EXISTS pdca.anexo ( id BIGSERIAL PRIMARY KEY, - id_storage TEXT UNIQUE NOT NULL, - - id_empresa BIGINT NOT NULL - REFERENCES empresa(id) ON DELETE RESTRICT, - - id_ciclo BIGINT NOT NULL - REFERENCES pdca.ciclo(id) ON DELETE RESTRICT, + id_empresa BIGINT NOT NULL, + id_ciclo BIGINT NOT NULL, criado_por BIGINT - REFERENCES usuario_sistema(id) ON DELETE SET NULL, + REFERENCES public.usuario_sistema(id) + ON DELETE SET NULL, categoria VARCHAR(40) NOT NULL CHECK ( categoria IN ( @@ -402,41 +398,75 @@ CREATE TABLE IF NOT EXISTS pdca.anexo ( ) ), - referencia_origem VARCHAR(100), --> campo para armazenar o id da origem -> categoria = 'PLANO_ACAO' -> id_plano_acao + -- ID do registro relacionado à categoria. + -- Exemplo: categoria = 'PLANO_ACAO', id_origem = id do plano. + id_origem BIGINT, + nome_arquivo VARCHAR(255) NOT NULL, tipo_arquivo VARCHAR(150) NOT NULL, tamanho_arquivo BIGINT NOT NULL - CHECK (tamanho_arquivo >= 0), + CHECK (tamanho_arquivo > 0), bucket_arquivo VARCHAR(100) NOT NULL DEFAULT 'acta-arquivos', - caminho_arquivo TEXT NOT NULL UNIQUE, + caminho_arquivo TEXT NOT NULL, - status VARCHAR(30) NOT NULL DEFAULT 'ATIVO' CHECK ( - status IN ( - 'PROCESSANDO', - 'ATIVO', - 'ERRO', - 'EXCLUIDO' - ) - ), + status VARCHAR(30) NOT NULL + DEFAULT 'PROCESSANDO' + CHECK ( + status IN ( + 'PROCESSANDO', + 'ATIVO', + 'ERRO', + 'EXCLUIDO' + ) + ), descricao TEXT, criado_em TIMESTAMPTZ NOT NULL DEFAULT NOW(), - atualizado_em TIMESTAMPTZ, + atualizado_em TIMESTAMPTZ NOT NULL DEFAULT NOW(), excluido_em TIMESTAMPTZ, - CONSTRAINT anexo_tipo_arquivo_check CHECK ( - tipo_arquivo = 'application/pdf' - OR tipo_arquivo LIKE 'image/%' - OR tipo_arquivo = - 'application/vnd.openxmlformats-officedocument.presentationml.presentation' - OR tipo_arquivo = - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' - ), + CONSTRAINT fk_anexo_empresa + FOREIGN KEY (id_empresa) + REFERENCES public.empresa(id) + ON DELETE RESTRICT, + + CONSTRAINT fk_anexo_ciclo + FOREIGN KEY (id_ciclo) + REFERENCES pdca.ciclo(id) + ON DELETE RESTRICT, + + CONSTRAINT uq_anexo_storage + UNIQUE (bucket_arquivo, caminho_arquivo), + CONSTRAINT ck_anexo_tipo_arquivo + CHECK ( + tipo_arquivo IN ( + 'application/pdf', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'text/csv', + 'text/plain' + ) + OR tipo_arquivo LIKE 'image/%' + ), + + CONSTRAINT ck_anexo_exclusao + CHECK ( + (status = 'EXCLUIDO' AND excluido_em IS NOT NULL) + OR + (status <> 'EXCLUIDO' AND excluido_em IS NULL) + ), + + CONSTRAINT ck_anexo_origem + CHECK ( + categoria = 'OUTRO' + OR id_origem IS NOT NULL + ) ); \ No newline at end of file