diff --git a/app/consumer/initdb/01_schema.sql b/app/consumer/initdb/01_schema.sql new file mode 100644 index 0000000..364f1d2 --- /dev/null +++ b/app/consumer/initdb/01_schema.sql @@ -0,0 +1,62 @@ +CREATE TABLE IF NOT EXISTS "device" ( + "device_id" UUID NOT NULL DEFAULT uuidv4(), + "device_eui" VARCHAR(255) NOT NULL, + "site_id" UUID, + PRIMARY KEY("device_id") +); + +CREATE TABLE IF NOT EXISTS "site" ( + "site_id" UUID NOT NULL DEFAULT uuidv4(), + "pce" VARCHAR(255) NOT NULL, + "address_1" VARCHAR(255) NOT NULL, + "address_2" VARCHAR(255), + "postal_code" VARCHAR(255) NOT NULL, + "city" VARCHAR(255) NOT NULL, + PRIMARY KEY("site_id") +); + +CREATE TABLE IF NOT EXISTS "user" ( + "user_id" UUID NOT NULL DEFAULT uuidv4(), + "name" VARCHAR(255) NOT NULL, + "first_name" VARCHAR(255) NOT NULL, + "email" VARCHAR(255) NOT NULL, + "password_hash" VARCHAR(255), + "user_type_id" UUID NOT NULL, + PRIMARY KEY("user_id") +); + +CREATE TABLE IF NOT EXISTS "reading" ( + "reading_id" UUID NOT NULL DEFAULT uuidv7(), + "device_id" UUID NOT NULL, + "date" DATE NOT NULL, + PRIMARY KEY("reading_id") +); + +CREATE TABLE IF NOT EXISTS "subscription" ( + "subscription_id" UUID NOT NULL DEFAULT uuidv4(), + "site_id" UUID NOT NULL, + "user_id" UUID NOT NULL, + PRIMARY KEY("subscription_id") +); + +CREATE TABLE IF NOT EXISTS "user_type" ( + "user_type_id" UUID NOT NULL DEFAULT uuidv4(), + "label" VARCHAR(255) NOT NULL, + PRIMARY KEY("user_type_id") +); + +ALTER TABLE "device" +ADD FOREIGN KEY("site_id") REFERENCES "site"("site_id") +ON UPDATE NO ACTION ON DELETE NO ACTION; +ALTER TABLE "subscription" +ADD FOREIGN KEY("site_id") REFERENCES "site"("site_id") +ON UPDATE NO ACTION ON DELETE NO ACTION; +ALTER TABLE "subscription" +ADD FOREIGN KEY("user_id") REFERENCES "user"("user_id") +ON UPDATE NO ACTION ON DELETE NO ACTION; +ALTER TABLE "reading" +ADD FOREIGN KEY("device_id") REFERENCES "device"("device_id") +ON UPDATE NO ACTION ON DELETE NO ACTION; +ALTER TABLE "user" +ADD FOREIGN KEY("user_type_id") REFERENCES "user_type"("user_type_id") +ON UPDATE NO ACTION ON DELETE NO ACTION; \ No newline at end of file diff --git a/app/consumer/initdb/02_dummy_data.sql b/app/consumer/initdb/02_dummy_data.sql new file mode 100644 index 0000000..0243dc7 --- /dev/null +++ b/app/consumer/initdb/02_dummy_data.sql @@ -0,0 +1,37 @@ +INSERT INTO "user_type" ("label") VALUES + ('admin'), + ('subscriber'); + +INSERT INTO "site" ("pce", "address_1", "address_2", "postal_code", "city") VALUES + ('GI123456789', '12 Rue de la Paix', NULL, '75001', 'Paris'), + ('GI987654321', '5 Avenue Foch', 'Bât B', '69003', 'Lyon'), + ('GI111222333', '8 Rue du Moulin', NULL, '59000', 'Lille'), + ('GI444555666', '27 Boulevard Victor', 'Apt 12', '33000', 'Bordeaux'); + +-- Mots de passe d'exemple : (hash générés avec `mkpasswd --method=bcrypt --rounds=12 `) +-- AdminPass123! +-- BobPass456! +-- ClairePass789! +-- DavidPass012 + +INSERT INTO "user" ("name", "first_name", "email", "password_hash", "user_type_id") VALUES + ('Dupont', 'Alice', 'alice.dupont@example.com', '$2b$12$qdHcvSLkbflmHn45gokjX.zm27JxamMBplA/l4y4D2GuykDvjJll.', (SELECT "user_type_id" FROM "user_type" WHERE "label" = 'admin')), + ('Martin', 'Bernard', 'bernard.martin@example.com', '$2b$12$3ulCb.7b9LeQv2edkmju2uwtn8bA/1jpj4K5n51DxH6HYDme0Gbfq', (SELECT "user_type_id" FROM "user_type" WHERE "label" = 'subscriber')), + ('Durand', 'Claire', 'claire.durand@example.com', '$2b$12$nzYUQG/SHV9uvOxtYJ5XWOnUT1bgiUS0FejgFl.Y57Pz0LB9U5ia6', (SELECT "user_type_id" FROM "user_type" WHERE "label" = 'subscriber')), + ('Leroy', 'David', 'david.leroy@example.com', '$2b$12$Yy/K3.kghkrYtflPUGjuM.cj6pbCP/Bc4sasLlkbA7RgdlK1wzR2u', (SELECT "user_type_id" FROM "user_type" WHERE "label" = 'subscriber')); + +INSERT INTO "subscription" ("site_id", "user_id") VALUES + ( + (SELECT "site_id" FROM "site" WHERE "pce" = 'GI123456789'), + (SELECT "user_id" FROM "user" WHERE "email" = 'bernard.martin@example.com')), + + ( + (SELECT "site_id" FROM "site" WHERE "pce" = 'GI987654321'), + (SELECT "user_id" FROM "user" WHERE "email" = 'claire.durand@example.com')), + + ( + (SELECT "site_id" FROM "site" WHERE "pce" = 'GI111222333'), + (SELECT "user_id" FROM "user" WHERE "email" = 'david.leroy@example.com')), + + ((SELECT "site_id" FROM "site" WHERE "pce" = 'GI444555666'), + (SELECT "user_id" FROM "user" WHERE "email" = 'david.leroy@example.com')); diff --git a/app/docker-compose.yml b/app/docker-compose.yml index 289317a..9b6e15b 100644 --- a/app/docker-compose.yml +++ b/app/docker-compose.yml @@ -4,7 +4,41 @@ services: restart: unless-stopped networks: - lora-gateway_mqtt + - database + + db: + image: postgres:18-alpine + restart: unless-stopped + volumes: + - ./consumer/initdb:/docker-entrypoint-initdb.d:ro + - db:/var/lib/postgresql + networks: + - database + environment: + - POSTGRES_USER=simugaz + - POSTGRES_PASSWORD=simugaz + - POSTGRES_DB=simugaz + + pgadmin: + image: dpage/pgadmin4:latest + restart: unless-stopped + ports: + - 8081:80 + networks: + - public + - database + volumes: + - ./servers.json:/pgadmin4/servers.json + environment: + PGADMIN_DEFAULT_EMAIL: admin@example.com + PGADMIN_DEFAULT_PASSWORD: changeme + +volumes: + db: networks: + public: + database: + internal: true lora-gateway_mqtt: external: true \ No newline at end of file diff --git a/app/servers.json b/app/servers.json new file mode 100644 index 0000000..502f15a --- /dev/null +++ b/app/servers.json @@ -0,0 +1,16 @@ +{ + "Servers": { + "1": { + "Name": "SimuGaz", + "Group": "SimuGaz", + "Port": 5432, + "Username": "simugaz", + "Host": "db", + "MaintenanceDB": "postgres", + "ConnectionParameters": { + "sslmode": "prefer", + "connect_timeout": 10 + } + } + } +} \ No newline at end of file