Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Add accounts, users, groups tables in database
Browse files Browse the repository at this point in the history
As well as tables binding these together.

See #16.
  • Loading branch information
markuswustenberg committed Aug 21, 2020
1 parent 9e65f5d commit e10eccb
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 4 deletions.
146 changes: 142 additions & 4 deletions storage/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions storage/migrations/000002_accounts.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table accounts;
7 changes: 7 additions & 0 deletions storage/migrations/000002_accounts.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- accounts have names and will be used primarily for billing.
create table accounts (
id uuid primary key default gen_random_uuid(),
name string not null,
created_at timestamp not null default current_timestamp(),
updated_at timestamp not null default current_timestamp()
);
2 changes: 2 additions & 0 deletions storage/migrations/000003_users.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
drop table account_membership;
drop table users;
21 changes: 21 additions & 0 deletions storage/migrations/000003_users.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- users has user information and preferences.
create table users (
id uuid primary key default gen_random_uuid(),
name string not null,
email string unique not null,
password string not null,
language string not null default 'en-us',
timezone string not null default 'CET',
preferences jsonb not null default '{}',
created_at timestamp not null default current_timestamp(),
updated_at timestamp not null default current_timestamp()
);

-- account_membership defines who is part of an account.
-- Note that it's a many-to-many relationship, so one user can be a part of multiple accounts,
-- and an account can (obviously) have many users.
create table account_membership (
account_id uuid references accounts(id) on delete cascade,
user_id uuid references users(id) on delete cascade,
primary key (account_id, user_id)
);
3 changes: 3 additions & 0 deletions storage/migrations/000004_groups.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
drop table group_membership;
drop table group_ownership;
drop table groups;
23 changes: 23 additions & 0 deletions storage/migrations/000004_groups.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- groups of users are named and have a secret that's used to join the group.
create table groups (
id uuid primary key default gen_random_uuid(),
name string not null,
invitation_secret bytes not null default uuid_v4(),
created_at timestamp not null default current_timestamp(),
updated_at timestamp not null default current_timestamp()
);

-- group_membership defines who's part of a group.
create table group_membership (
group_id uuid references groups(id) on delete cascade,
user_id uuid references users(id) on delete cascade,
primary key (group_id, user_id)
);

-- group_ownership defines who owns a group.
-- Note that a user cannot be deleted if the user is a group owner.
create table group_ownership (
group_id uuid references groups(id) on delete cascade,
user_id uuid references users(id),
primary key (group_id, user_id)
);

0 comments on commit e10eccb

Please sign in to comment.