select * from pg_available_extensions;

create extension if not exists ltree;
create extension if not exists pgtap;
create extension if not exists postgis;
create extension if not exists pgrouting;
create extension if not exists pgcrypto;

create table adherent (
  id int primary key,
  nom text,
  prenom text,
  genre smallint,
  naissance date,
  codepostal text
);

create table famille (
  code text primary key,
  famille text,
  code_parent text, -- references famille(code)
  arborescence ltree
);

create table article (
  code text primary key,
  article text,
  famille_code text, -- references famille(code)
  factpoids boolean,
  unitevente int,
  prix decimal,
  suivistock int
);

create table ticket (
  id int primary key,
  date_ticket timestamp,
  adherent_id int,  -- references adherent(id)
  mode_rglt int
);

create table ligne (
  id int primary key,
  ticket_id int, --references ticket (id),
  article_code text, -- references article (code)
  prix_unitaire decimal,
  quantite decimal
);

alter table ligne
   add column total decimal generated always as (prix_unitaire * quantite) stored;

create table marque (
  id int primary key,
  marque text not null
);

create table fournisseur (
  id int primary key,
  fournisseur text not null
);

create table region (
  id int primary key,
  region text not null
);

insert into region values
  (1, 'Est'), (2, 'Ouest');
