Skip to content

Commit

Permalink
Merge pull request #13 from caiodearaujo/feature/flyway-stated
Browse files Browse the repository at this point in the history
🧑‍💻 Flyway migrations is creating all tables structure now
  • Loading branch information
caiodearaujo authored Oct 2, 2024
2 parents b834bdc + b543dcc commit c347aa5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
redis
api/.idea/
api/.idea/
pg
2 changes: 0 additions & 2 deletions db/migrations/V1__create_initial_schema.sql

This file was deleted.

95 changes: 95 additions & 0 deletions db/migrations/V1__create_initial_schema_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- This migration script creates the initial schema for the UATZ API database.
-- The schema includes tables for devices, device handlers, device webhooks, and webhook messages.
-- Version: 1
-- Author: @caiofabiodearaujo
-- Date: 2024-10-01

-- Schema 'whatsmeow' is used for the whatsmeow library.
-- Schema 'uatzapi' is used for the UATZ API.
create schema whatsmeow;
create schema uatzapi;

-- Create the sequence for the 'device' table if it does not exist
create sequence if not exists uatzapi.device_id_seq;

-- Table 'device'
-- This table stores information about each device. The device has a unique ID, WhatsApp ID,
-- push name, business name, active status, and a timestamp for when it was created.
create table if not exists uatzapi.device (
id bigint primary key not null default nextval('uatzapi.device_id_seq'::regclass),
whatsapp_id character varying not null, -- WhatsApp ID associated with the device
push_name character varying not null, -- Name pushed to the device
business_name character varying, -- Optional business name associated with the device
active boolean not null, -- Indicates if the device is currently active
created_at timestamp with time zone not null default now() -- Timestamp when the record was created
);

-- Creating a unique index on 'whatsapp_id' to ensure no two devices have the same WhatsApp ID
create unique index if not exists device_whatsapp_id_key
on uatzapi.device using btree (whatsapp_id);

-- Create the sequence for the 'device' table if it does not exist
create sequence if not exists uatzapi.device_handler_id_seq;

-- Table 'device_handler'
-- This table stores handlers associated with each device. It contains a reference to the device,
-- a boolean indicating whether the handler is active, and timestamps for activation and deactivation.
create table if not exists uatzapi.device_handler (
id bigint primary key not null default nextval('uatzapi.device_handler_id_seq'::regclass),
device_id bigint not null, -- Foreign key referencing the 'device' table
active boolean not null, -- Indicates if the handler is active
active_at timestamp with time zone not null, -- Timestamp when the handler was activated
inactive_at timestamp with time zone, -- Optional timestamp when the handler was deactivated
constraint fk_device_handler_device_id foreign key (device_id) references uatzapi.device (id) -- Foreign key constraint
);

-- Creating an index on 'device_id' in the 'device_handler' table to optimize queries by device reference
create index if not exists device_handler_device_id_idx
on uatzapi.device_handler (device_id);

-- Create the sequence for the 'device' table if it does not exist
create sequence if not exists uatzapi.device_webhook_id_seq;

-- Table 'device_webhook'
-- This table stores webhook information for each device, including the URL, status, and timestamp.
create table if not exists uatzapi.device_webhook (
id bigint primary key not null default nextval('uatzapi.device_webhook_id_seq'::regclass),
device_id bigint not null, -- Foreign key referencing the 'device' table
webhook_url character varying not null, -- Webhook URL associated with the device
active boolean not null, -- Indicates if the webhook is currently active
timestamp timestamp with time zone not null default now(), -- Timestamp when the webhook was created
constraint fk_device_webhook_device_id foreign key (device_id) references uatzapi.device (id) -- Foreign key constraint
);

-- Creating an index on 'device_id' in the 'device_webhook' table to optimize queries by device reference
create index if not exists device_webhook_device_id_idx
on uatzapi.device_webhook (device_id);

-- Create the sequence for the 'device' table if it does not exist
create sequence if not exists uatzapi.webhook_message_id_seq;

-- Table 'webhook_message'
-- This table stores information about messages sent to the webhook, including the message content,
-- the response, the webhook URL used, the response code, and a timestamp.
create table if not exists uatzapi.webhook_message (
id bigint primary key not null default nextval('uatzapi.webhook_message_id_seq'::regclass),
device_id bigint not null, -- Foreign key referencing the 'device' table
message character varying not null, -- The message sent to the webhook
response character varying not null, -- The response received from the webhook
webhook_url character varying not null, -- The webhook URL to which the message was sent
code_response bigint not null, -- HTTP status code or custom response code from the webhook
timestamp timestamp with time zone not null default now(), -- Timestamp when the message was logged
constraint fk_webhook_message_device_id foreign key (device_id) references uatzapi.device (id) -- Foreign key constraint
);

-- Creating an index on 'device_id' in the 'webhook_message' table to optimize queries by device reference
create index if not exists webhook_message_device_id_idx
on uatzapi.webhook_message (device_id);

-- Creating an index on 'webhook_url' in the 'webhook_message' table to optimize queries by webhook URL
create index if not exists webhook_message_webhook_url_idx
on uatzapi.webhook_message (webhook_url);

-- Creating an index on 'code_response' in the 'webhook_message' table to optimize queries by response code
create index if not exists webhook_message_code_response_idx
on uatzapi.webhook_message (code_response);
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ services:
- POSTGRES_DB=uatzapi
ports:
- "5432:5432"
volumes:
- ./pg/data:/var/lib/postgresql/data

flyway:
build:
Expand Down

0 comments on commit c347aa5

Please sign in to comment.