diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..112814f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +_build +_checkouts +.git +.github +.claude +*.crashdump diff --git a/docker-compose.yml b/docker-compose.yml index 08304e8..e03c232 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,10 +5,9 @@ services: command: -c 'max_connections=2000' environment: POSTGRES_PASSWORD: root + POSTGRES_DB: chatli ports: - 5555:5432 - volumes: - - ./sql:/docker-entrypoint-initdb.d adminer: image: adminer ports: diff --git a/sql/10_ldf.sql b/sql/10_ldf.sql deleted file mode 100755 index 05efc8d..0000000 --- a/sql/10_ldf.sql +++ /dev/null @@ -1,23 +0,0 @@ -CREATE DATABASE ldf; - -\c ldf; - -CREATE TABLE li -( - id SERIAL PRIMARY KEY, - type VARCHAR NOT NULL, - value VARCHAR NOT NULL, - callback_id uuid NOT NULL, - user_id uuid NOT NULL, - username VARCHAR, - phone_number VARCHAR, - email VARCHAR -); - -CREATE TABLE ldf_message -( - id SERIAL PRIMARY KEY, - message_id UUID NOT NULL, - payload VARCHAR, - content_length VARCHAR -); \ No newline at end of file diff --git a/sql/20_chatli.sql b/sql/20_chatli.sql deleted file mode 100755 index 318fbe6..0000000 --- a/sql/20_chatli.sql +++ /dev/null @@ -1,70 +0,0 @@ -CREATE DATABASE chatli; - -\c chatli; - -CREATE EXTENSION pgcrypto; - -CREATE TABLE chatli_user -( - id uuid PRIMARY KEY, - username varchar NOT NULL UNIQUE, - phone_number varchar UNIQUE, - email varchar UNIQUE, - avatar varchar, - password varchar NOT NULL -); - -CREATE TABLE message -( - id uuid PRIMARY KEY, - chat_id uuid NOT NULL, - payload jsonb, - sender uuid NOT NULL, - type varchar, - action varchar, - timestamp bigint, - sender_info jsonb -); - -CREATE TABLE chat -( - id uuid PRIMARY KEY, - name varchar NOT NULL, - description varchar, - type varchar -); - -CREATE TABLE participant -( - id SERIAL PRIMARY KEY, - chat_id uuid, - user_id uuid, - UNIQUE(chat_id, user_id) -); - -CREATE TABLE callback -( - id UUID PRIMARY KEY, - user_id UUID NOT NULL, - url VARCHAR NOT NULL -); - - -CREATE TABLE device -( - id UUID PRIMARY KEY, - user_id UUID NOT NULL, - name VARCHAR -); - -CREATE TABLE attachment -( - id UUID PRIMARY KEY, - chat_id UUID NOT NULL, - mime VARCHAR NOT NULL, - length INTEGER -); - -INSERT INTO chatli_user (id, username, phone_number, email, password) VALUES (gen_random_uuid(), 'alice', '461234', 'alice@example.com', 'alice'); -INSERT INTO chatli_user (id, username, phone_number, email, password) VALUES (gen_random_uuid(), 'bob', '462345', 'bob@example.com', 'bob'); -INSERT INTO chatli_user (id, username, phone_number, email, password) VALUES (gen_random_uuid(), 'ceasar', '463456', 'ceasar@example.com', 'ceasar'); \ No newline at end of file diff --git a/sql/seed.sql b/sql/seed.sql new file mode 100644 index 0000000..f8a57d3 --- /dev/null +++ b/sql/seed.sql @@ -0,0 +1,12 @@ +-- Sample users for local dev and tests. Apply AFTER the app has booted +-- and run its kura migrations (tables are created on boot, not here): +-- docker exec chatli-db-1 psql -U postgres -d chatli -f /seed/seed.sql +INSERT INTO chatli_user (id, username, phone_number, email, password) +VALUES (gen_random_uuid(), 'alice', '461234', 'alice@example.com', 'alice') +ON CONFLICT DO NOTHING; +INSERT INTO chatli_user (id, username, phone_number, email, password) +VALUES (gen_random_uuid(), 'bob', '462345', 'bob@example.com', 'bob') +ON CONFLICT DO NOTHING; +INSERT INTO chatli_user (id, username, phone_number, email, password) +VALUES (gen_random_uuid(), 'ceasar', '463456', 'ceasar@example.com', 'ceasar') +ON CONFLICT DO NOTHING; diff --git a/src/chatli_app.erl b/src/chatli_app.erl index 9b10fe1..ef05dfa 100644 --- a/src/chatli_app.erl +++ b/src/chatli_app.erl @@ -15,6 +15,7 @@ %%==================================================================== start(_StartType, _StartArgs) -> + {ok, _} = kura_migrator:migrate(chatli_repo), chatli_sup:start_link(). %%-------------------------------------------------------------------- diff --git a/src/migrations/m20260624090455_update_schema.erl b/src/migrations/m20260624090455_update_schema.erl new file mode 100644 index 0000000..056daa8 --- /dev/null +++ b/src/migrations/m20260624090455_update_schema.erl @@ -0,0 +1,60 @@ +-module(m20260624090455_update_schema). +-behaviour(kura_migration). +-include_lib("kura/include/kura.hrl"). +-export([up/0, down/0]). + +up() -> + [{create_table, <<"attachment">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = chat_id, type = uuid, nullable = false}, + #kura_column{name = mime, type = string, nullable = false}, + #kura_column{name = length, type = integer} + ]}, + {create_table, <<"callback">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = user_id, type = uuid, nullable = false}, + #kura_column{name = url, type = string, nullable = false} + ]}, + {create_table, <<"chat">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = name, type = string, nullable = false}, + #kura_column{name = description, type = string}, + #kura_column{name = type, type = string} + ]}, + {create_table, <<"chatli_user">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = username, type = string, nullable = false}, + #kura_column{name = phone_number, type = string}, + #kura_column{name = email, type = string}, + #kura_column{name = avatar, type = string}, + #kura_column{name = password, type = string, nullable = false} + ]}, + {create_table, <<"device">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = user_id, type = uuid, nullable = false}, + #kura_column{name = name, type = string} + ]}, + {create_table, <<"message">>, [ + #kura_column{name = id, type = uuid, primary_key = true}, + #kura_column{name = chat_id, type = uuid, nullable = false}, + #kura_column{name = payload, type = jsonb}, + #kura_column{name = sender, type = uuid, nullable = false}, + #kura_column{name = type, type = string}, + #kura_column{name = action, type = string}, + #kura_column{name = timestamp, type = integer}, + #kura_column{name = sender_info, type = jsonb} + ]}, + {create_table, <<"participant">>, [ + #kura_column{name = id, type = id, primary_key = true}, + #kura_column{name = chat_id, type = uuid}, + #kura_column{name = user_id, type = uuid} + ]}]. + +down() -> + [{drop_table, <<"attachment">>}, + {drop_table, <<"callback">>}, + {drop_table, <<"chat">>}, + {drop_table, <<"chatli_user">>}, + {drop_table, <<"device">>}, + {drop_table, <<"message">>}, + {drop_table, <<"participant">>}]. diff --git a/src/schemas/ldf_message.erl b/src/schemas/ldf_message.erl deleted file mode 100644 index 4defab8..0000000 --- a/src/schemas/ldf_message.erl +++ /dev/null @@ -1,15 +0,0 @@ --module(ldf_message). --behaviour(kura_schema). - --include_lib("kura/include/kura.hrl"). - --export([table/0, fields/0]). - -table() -> ~"ldf_message". - -fields() -> [ - #kura_field{name = id, type = id, primary_key = true}, - #kura_field{name = message_id, type = uuid, nullable = false}, - #kura_field{name = payload, type = string}, - #kura_field{name = content_length, type = string} -]. diff --git a/src/schemas/li.erl b/src/schemas/li.erl deleted file mode 100644 index 4a2c0b9..0000000 --- a/src/schemas/li.erl +++ /dev/null @@ -1,19 +0,0 @@ --module(li). --behaviour(kura_schema). - --include_lib("kura/include/kura.hrl"). - --export([table/0, fields/0]). - -table() -> ~"li". - -fields() -> [ - #kura_field{name = id, type = id, primary_key = true}, - #kura_field{name = type, type = string, nullable = false}, - #kura_field{name = value, type = string, nullable = false}, - #kura_field{name = callback_id, type = uuid, nullable = false}, - #kura_field{name = user_id, type = uuid, nullable = false}, - #kura_field{name = username, type = string}, - #kura_field{name = phone_number, type = string}, - #kura_field{name = email, type = string} -].