Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: summerize all migration files #4148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

13 changes: 12 additions & 1 deletion server/src/database/migrations/sql/1_initial_schema.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ drop type if exists session_role cascade;
drop type if exists voting_status cascade;

drop table if exists users cascade;
drop table if exists github_users cascade;
drop table if exists google_users cascade;
drop table if exists microsoft_users cascade;
drop table if exists azure_ad_users cascade;
drop table if exists apple_users cascade;
drop table if exists boards cascade;
drop table if exists "columns" cascade;
drop table if exists board_session_requests cascade;
drop table if exists board_sessions cascade;
drop table if exists notes cascade;
drop table if exists votings cascade;
drop table if exists votes cascade;
drop table if exists votes cascade;
drop table if exists ractions cascade;
drop table if exists deleted_boards cascade;

DROP FUNCTION record_deleted_board_statistics();

DROP TRIGGER before_delete_board ON boards;
242 changes: 168 additions & 74 deletions server/src/database/migrations/sql/1_initial_schema.up.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create type account_type as enum ('ANONYMOUS', 'GOOGLE', 'GITHUB', 'MICROSOFT', 'APPLE');
create type account_type as enum ('ANONYMOUS', 'GOOGLE', 'GITHUB', 'MICROSOFT', 'APPLE', 'AZURE_AD');
create type access_policy as enum ('PUBLIC', 'BY_PASSPHRASE', 'BY_INVITE');
create type board_session_request_status as enum ('PENDING', 'ACCEPTED', 'REJECTED');
create type session_role as enum ('OWNER', 'PARTICIPANT', 'MODERATOR');
Expand All @@ -7,135 +7,229 @@ create type color as enum ('backlog-blue' , 'grooming-green' , 'lean-lilac' , 'o

create table users
(
id uuid default gen_random_uuid() not null primary key,
"created_at" timestamptz default now(),
name varchar(64) not null,
account_type account_type not null
id uuid default gen_random_uuid() not null primary key,
"created_at" timestamptz default now(),
name varchar(64) not null,
account_type account_type not null,
"avatar" JSONB,
key_migration date
);

create table github_users
(
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
);

create table google_users
(
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(2048)
);

create table microsoft_users
(
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
);

create table azure_ad_users
(
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
);

create table apple_users
(
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
"user" uuid not null references users ON DELETE CASCADE,
id varchar(64) not null unique,
name varchar(64) not null,
avatar_url varchar(256)
);

create table boards
(
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz default now(),
"name" varchar(128),
access_policy access_policy not null DEFAULT 'PUBLIC',
passphrase varchar(128),
salt varchar(32),
show_authors boolean not null DEFAULT true,
show_notes_of_other_users boolean not null DEFAULT true,
allow_stacking boolean not null DEFAULT true,
timer_end timestamptz
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz default now(),
"name" varchar(128),
description varchar(300),
access_policy access_policy not null DEFAULT 'PUBLIC',
passphrase varchar(128),
salt varchar(32),
show_authors boolean not null DEFAULT true,
show_notes_of_other_users boolean not null DEFAULT true,
allow_stacking boolean not null DEFAULT true,
timer_start timestamptz,
timer_end timestamptz,
show_note_reactions boolean not null DEFAULT true,
allow_editing boolean not null DEFAULT true
);

create table columns
(
id uuid default gen_random_uuid() not null primary key,
"board" uuid not null references boards ON DELETE CASCADE,
name varchar(128) not null,
check (name <> ''),
color color not null default 'backlog-blue',
"visible" boolean DEFAULT false,
"index" int not null DEFAULT 0
id uuid default gen_random_uuid() not null primary key,
"board" uuid not null references boards ON DELETE CASCADE,
name varchar(128) not null,
check (name <> ''),
color color not null default 'backlog-blue',
"visible" boolean DEFAULT false,
"index" int not null DEFAULT 0
);
create index columns_board_index on columns (board);

create table board_session_requests
(
"user" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"status" board_session_request_status not null DEFAULT 'PENDING',
"created_at" timestamptz not null DEFAULT now(),
PRIMARY KEY ("user", board)
"user" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"status" board_session_request_status not null DEFAULT 'PENDING',
"created_at" timestamptz not null DEFAULT now(),
PRIMARY KEY ("user", board)
);
create index board_session_requests_board_index on board_session_requests (board);

create table board_sessions
(
"user" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"show_hidden_columns" boolean not null DEFAULT false,
"connected" boolean not null DEFAULT false,
"ready" boolean not null DEFAULT false,
raised_hand boolean not null DEFAULT false,
"role" session_role not null,
"created_at" timestamptz not null DEFAULT now(),
PRIMARY KEY ("user", board)
"user" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"show_hidden_columns" boolean not null DEFAULT true,
"connected" boolean not null DEFAULT false,
"ready" boolean not null DEFAULT false,
raised_hand boolean not null DEFAULT false,
"role" session_role not null,
"created_at" timestamptz not null DEFAULT now(),
banned boolean not null DEFAULT false,
PRIMARY KEY ("user", board)
);
create index board_sessions_board_index on board_sessions (board);

create table notes
(
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz not null DEFAULT now(),
"author" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"column" uuid not null references columns ON DELETE CASCADE,
text varchar(2048) not null,
check (text <> ''),
"rank" int not null DEFAULT 0
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz not null DEFAULT now(),
"author" uuid not null references users ON DELETE CASCADE,
"board" uuid not null references boards ON DELETE CASCADE,
"column" uuid not null references columns ON DELETE CASCADE,
text varchar(2048) not null,
check (text <> ''),
"rank" int not null DEFAULT 0
);
create index notes_board_index on notes (board);
create index notes_column_index on notes ("column");

alter table notes
add "stack" uuid references notes ON DELETE CASCADE;
add "stack" uuid references notes ON DELETE CASCADE;
create index notes_stack_index on notes (stack);

alter table boards
add shared_note uuid references notes;
add shared_note uuid references notes;


create table votings
(
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz not null default now(),
"board" uuid not null references boards ON DELETE CASCADE,
vote_limit int not null,
allow_multiple_votes boolean not null DEFAULT true,
show_votes_of_others boolean not null DEFAULT false,
"status" voting_status not null DEFAULT 'OPEN'
id uuid default gen_random_uuid() not null primary key,
created_at timestamptz not null default now(),
"board" uuid not null references boards ON DELETE CASCADE,
vote_limit int not null,
allow_multiple_votes boolean not null DEFAULT true,
show_votes_of_others boolean not null DEFAULT false,
"status" voting_status not null DEFAULT 'OPEN'
);

alter table boards
add show_voting uuid references votings;
add show_voting uuid references votings;


create table votes
(
"board" uuid not null references boards ON DELETE CASCADE,
"voting" uuid not null references votings ON DELETE CASCADE,
"user" uuid not null references users ON DELETE CASCADE,
"note" uuid not null references notes ON DELETE CASCADE
"board" uuid not null references boards ON DELETE CASCADE,
"voting" uuid not null references votings ON DELETE CASCADE,
"user" uuid not null references users ON DELETE CASCADE,
"note" uuid not null references notes ON DELETE CASCADE
);

CREATE TABLE reactions
(
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"note" UUID NOT NULL REFERENCES notes ON DELETE CASCADE,
"user" UUID NOT NULL REFERENCES users ON DELETE CASCADE,
/* CLDR short name in order to allow custom emojis */
"reaction_type" VARCHAR(50) NOT NULL
);
create index reactions_note_index on reactions (note);

create table deleted_boards
(
id uuid not null primary key,
access_policy access_policy,
total_columns int,
hidden_columns int,
total_notes int,
hidden_notes int,
first_note_created timestamptz,
last_note_created timestamptz,
avg_chars_per_note int,
total_participants int,
total_moderators int,
total_votes int,
total_votings int,
created_at timestamptz,
deleted_at timestamptz default now()
);

CREATE FUNCTION record_deleted_board_statistics() RETURNS TRIGGER AS $$
DECLARE
total_columns int;
hidden_columns int;
total_notes int;
hidden_notes int;
first_note_created timestamptz;
last_note_created timestamptz;
avg_chars_per_note int;
total_participants int;
total_moderators int;
total_votes int;
total_votings int;

BEGIN
-- Retrieve all needed stats
SELECT count(id) INTO total_columns FROM columns WHERE board = OLD.id;
SELECT count(id) INTO hidden_columns FROM columns c WHERE board = OLD.id AND c."visible" = false;

SELECT count(id) INTO total_notes FROM notes WHERE board = OLD.id;
SELECT count(n.id) INTO hidden_notes FROM notes n INNER JOIN columns c ON n."column" = c.id
WHERE c."visible" = false AND n."board" = OLD.id;
SELECT min(created_at) INTO first_note_created FROM notes WHERE board = OLD.id;
SELECT max(created_at) INTO last_note_created FROM notes WHERE board = OLD.id;
SELECT AVG(LENGTH(text)) INTO avg_chars_per_note FROM notes WHERE board = OLD.id;

SELECT count(user) INTO total_participants FROM board_sessions s WHERE s.board = OLD.id AND s.role = 'PARTICIPANT';
SELECT count(user) INTO total_moderators FROM board_sessions s WHERE s.board = OLD.id AND s.role = 'OWNER'OR s.role = 'MODERATOR';

SELECT count(board) INTO total_votes FROM votes WHERE board = OLD.id;
SELECT count(id) INTO total_votings FROM votings WHERE board = OLD.id;

-- Insert data
INSERT INTO deleted_boards (id, access_policy, total_columns, hidden_columns, total_notes, hidden_notes,
avg_chars_per_note, first_note_created, last_note_created, total_participants,
total_moderators, total_votes, total_votings, created_at)
VALUES (OLD.id, OLD.access_policy, total_columns, hidden_columns, total_notes, hidden_notes, avg_chars_per_note,
first_note_created, last_note_created, total_participants, total_moderators, total_votes, total_votings,
OLD.created_at);
RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER before_delete_board
BEFORE DELETE
ON boards
FOR EACH ROW
EXECUTE FUNCTION record_deleted_board_statistics();

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions server/src/database/migrations/sql/3_add_avatar_column.up.sql

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions server/src/database/migrations/sql/5_add_key_rotation.up.sql

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading