banco reestabelecido

This commit is contained in:
2026-04-03 18:09:28 +00:00
parent 96ea8e21ef
commit 5c24783320
5 changed files with 463 additions and 20 deletions

View File

@@ -1,7 +1,18 @@
-- Criar schema gpi
CREATE SCHEMA IF NOT EXISTS gpi;
-- Tabela users (já existe em public, mas replicamos em gpi)
-- Tabela organizations
CREATE TABLE IF NOT EXISTS gpi.organizations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Inserir organização padrão
INSERT INTO gpi.organizations (id, name)
VALUES ('e47e6210-4879-4e5b-bf21-9285d2713123', 'Organização Migrada')
ON CONFLICT (id) DO NOTHING;
CREATE TABLE IF NOT EXISTS gpi.users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
logto_id TEXT UNIQUE,
@@ -14,9 +25,9 @@ CREATE TABLE IF NOT EXISTS gpi.users (
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Tabela projects
CREATE TABLE IF NOT EXISTS gpi.projects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
client TEXT,
start_date DATE,
@@ -24,8 +35,7 @@ CREATE TABLE IF NOT EXISTS gpi.projects (
environment TEXT,
technician TEXT,
weight_kg DECIMAL(10,2),
painted_weight DECIMAL(10,2),
created_by TEXT,
status TEXT DEFAULT 'active' CHECK (status IN ('active', 'archived')),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -33,7 +43,8 @@ CREATE TABLE IF NOT EXISTS gpi.projects (
-- Tabela parts
CREATE TABLE IF NOT EXISTS gpi.parts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project_id UUID REFERENCES gpi/projects(id) ON DELETE CASCADE,
organization_id UUID NOT NULL,
project_id UUID REFERENCES gpi.projects(id) ON DELETE CASCADE,
description TEXT,
dimensions TEXT,
weight DECIMAL(10,3),
@@ -49,7 +60,8 @@ CREATE TABLE IF NOT EXISTS gpi.parts (
-- Tabela painting_schemes
CREATE TABLE IF NOT EXISTS gpi.painting_schemes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project_id UUID REFERENCES gpi/projects(id) ON DELETE CASCADE,
organization_id UUID NOT NULL,
project_id UUID REFERENCES gpi.projects(id) ON DELETE CASCADE,
name TEXT NOT NULL,
type TEXT,
coat TEXT,
@@ -67,7 +79,6 @@ CREATE TABLE IF NOT EXISTS gpi.painting_schemes (
color_hex TEXT,
thinner_symbol TEXT,
notes TEXT,
created_by TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -75,7 +86,8 @@ CREATE TABLE IF NOT EXISTS gpi.painting_schemes (
-- Tabela application_records
CREATE TABLE IF NOT EXISTS gpi.application_records (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project_id UUID REFERENCES gpi/projects(id) ON DELETE CASCADE,
organization_id UUID NOT NULL,
project_id UUID REFERENCES gpi.projects(id) ON DELETE CASCADE,
coat_stage TEXT NOT NULL,
piece_description TEXT,
date DATE,
@@ -90,7 +102,6 @@ CREATE TABLE IF NOT EXISTS gpi.application_records (
diluent_used DECIMAL(10,3),
items JSONB,
notes TEXT,
created_by TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -98,8 +109,9 @@ CREATE TABLE IF NOT EXISTS gpi.application_records (
-- Tabela inspections
CREATE TABLE IF NOT EXISTS gpi.inspections (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
project_id UUID REFERENCES gpi/projects(id) ON DELETE CASCADE,
application_record_id UUID REFERENCES gpi/application_records(id),
organization_id UUID NOT NULL,
project_id UUID REFERENCES gpi.projects(id) ON DELETE CASCADE,
application_record_id UUID REFERENCES gpi.application_records(id),
stock_item_id TEXT,
instrument_id TEXT,
type TEXT CHECK (type IN ('painting', 'surface_treatment')),
@@ -122,7 +134,6 @@ CREATE TABLE IF NOT EXISTS gpi.inspections (
temperature DECIMAL(6,2),
relative_humidity DECIMAL(5,2),
period TEXT CHECK (period IN ('morning', 'afternoon', 'night')),
created_by TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -130,11 +141,12 @@ CREATE TABLE IF NOT EXISTS gpi.inspections (
-- Tabela technical_data_sheets
CREATE TABLE IF NOT EXISTS gpi.technical_data_sheets (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
manufacturer TEXT,
type TEXT,
file_url TEXT NOT NULL,
upload_date DATE NOT NULL,
file_url TEXT,
upload_date DATE,
solids_volume DECIMAL(5,2),
density DECIMAL(6,3),
mixing_ratio TEXT,
@@ -160,6 +172,7 @@ CREATE TABLE IF NOT EXISTS gpi.technical_data_sheets (
-- Tabela yield_studies
CREATE TABLE IF NOT EXISTS gpi.yield_studies (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
data_sheet_id TEXT NOT NULL,
target_dft DECIMAL(6,2) NOT NULL,
@@ -178,16 +191,16 @@ CREATE TABLE IF NOT EXISTS gpi.yield_studies (
-- Tabela instruments
CREATE TABLE IF NOT EXISTS gpi.instruments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
serial_number TEXT UNIQUE,
serial_number TEXT,
manufacturer TEXT,
model TEXT,
last_calibration DATE,
next_calibration DATE,
status TEXT DEFAULT 'active',
notes TEXT,
organization_id TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -195,6 +208,7 @@ CREATE TABLE IF NOT EXISTS gpi.instruments (
-- Tabela stock_items
CREATE TABLE IF NOT EXISTS gpi.stock_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
batch_number TEXT,
@@ -205,7 +219,6 @@ CREATE TABLE IF NOT EXISTS gpi.stock_items (
expiration_date DATE,
status TEXT DEFAULT 'available',
notes TEXT,
organization_id TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
@@ -213,7 +226,8 @@ CREATE TABLE IF NOT EXISTS gpi.stock_items (
-- Tabela stock_movements
CREATE TABLE IF NOT EXISTS gpi.stock_movements (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
stock_item_id UUID REFERENCES gpi/stock_items(id) ON DELETE CASCADE,
organization_id UUID NOT NULL,
stock_item_id UUID REFERENCES gpi.stock_items(id) ON DELETE CASCADE,
type TEXT NOT NULL CHECK (type IN ('in', 'out', 'adjustment')),
quantity DECIMAL(10,3) NOT NULL,
reason TEXT,
@@ -225,6 +239,7 @@ CREATE TABLE IF NOT EXISTS gpi.stock_movements (
-- Tabela notifications
CREATE TABLE IF NOT EXISTS gpi.notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
title TEXT NOT NULL,
message TEXT NOT NULL,
type TEXT DEFAULT 'info' CHECK (type IN ('info', 'warning', 'error', 'success')),
@@ -239,13 +254,13 @@ CREATE TABLE IF NOT EXISTS gpi.notifications (
-- Tabela geometry_types
CREATE TABLE IF NOT EXISTS gpi.geometry_types (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
name TEXT NOT NULL,
efficiency_loss DECIMAL(5,2),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Habilitar PostgREST para o schema gpi
ALTER SCHEMA gpi ENABLE VALUE;
GRANT USAGE ON SCHEMA gpi TO postgres, anon, authenticated, service_role;
-- Grant permissions em todas as tabelas