Skip to content

Modelo de dados SQL, parte 1, doadores e doações

Peter edited this page Oct 16, 2021 · 4 revisions

Os dados das tabelas abaixo tem sido mantidos por planilhas e sua atualização, já que a interface não era prioritária e os dados estavam sendo constantemente revisados. Como os dados estão estáveis e o PostgREST tem recursos CRUD, convém a partir de agora realizar atualização por API, e vincular essa API a pelo menos uma interface simples de fomulário. Cada arquivo doado é um origin.fname vinculado ao doador. Como as doações muitas fazes são realizadas "por pacote", fica primeiramente registrada como donatedPack.

Os arquivos comuns também precisam ficar desmembrados de tal maneira que cada arquivo seja relativo a uma cidade. Arquivos excepcionais podem requerer unidade admistrativa maior (ver casos como o Planet-BR ou IBGE Rural).

Ver também Doadores e doações, workflow na Eclusa.

CREATE TABLE optim.donor (
  id serial NOT NULL primary key,
  scope text, -- city code or country code
  vat_id text,
  shortname text,
  legalName text NOT NULL,
  wikidata_id bigint,
  url text,
  info JSONb,
  UNIQUE(vat_id),
  UNIQUE(scope,legalName)
);
CREATE TABLE optim.donatedPack(
  pack_id int NOT NULL PRIMARY KEY,
  donor_id int NOT NULL REFERENCES ingest.donor(id),
  accepted_date date,
  about text,
  info jsonb,
  UNIQUE(pack_id)
); 

CREATE TABLE optim.city ( --  de http://datasets.ok.org.br/city-codes
  ibge_id int   NOT NULL PRIMARY KEY, -- BR official id, control
  name    text  NOT NULL CHECK(length(name)<60), -- admin-level3
  state   text  NOT NULL CHECK(length(state)=2), -- UF, admin-level2
  abbrev3 text  CHECK(length(abbrev3)=3),
  wikidata_id  bigint,  --  from '^Q\d+'
  lexlabel     text NOT NULL,  -- cache from name. 'sao.paulo'
  isolabel_ext text NOT NULL,  -- cache from name and state. BR-SP-SaoPaulo
  ddd          integer,
  info JSONb, -- postalCode_ranges, notes,   creation, extinction
  UNIQUE(state,name),
  UNIQUE(state,lexlabel)
);

CREATE TABLE IF NOT EXISTS optim.origin(
   id serial     NOT NULL PRIMARY KEY,
   city_id int NOT NULL REFERENCES ingest.city(ibge_id), -- escopo dos dados, desmembrando se possível.
   pack_id int NOT NULL REFERENCES ingest.donatedPack(pack_id), -- um ou mais origins no mesmo paxck.
   fhash text    NOT NULL, -- sha256 is a finger print
   cityname text NOT NULL, -- city name
   fname text    NOT NULL,  -- filename
   fversion smallint NOT NULL DEFAULT 1, -- version counter
   ctype text, -- content type
   config jsonb, -- example '{"staging_db":"ingest1"}' ou tmplixo1 para nao poluir std.
   cmds text[],  -- conforme config; uso posterior para guardar sequencia de comandos.
   is_valid boolean NOT NULL DEFAULT false,
   is_open boolean NOT NULL DEFAULT true,
   fmeta jsonb,
   ingest_instant timestamp DEFAULT now(),
   UNIQUE(fhash),
   UNIQUE(cityname,fname,fversion) -- ,kx_ingest_date=ingest_instant::date
);

Diagramas

Ver https://github.com/digital-guard/preserv/blob/main/docs/assets-src.md

Clone this wiki locally