Skip to content

Commit

Permalink
finish formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jan 21, 2024
1 parent 223b6f0 commit f2950eb
Showing 1 changed file with 130 additions and 72 deletions.
202 changes: 130 additions & 72 deletions crates/kitsune-db/migrations/2023-12-16-192941_initial/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CREATE TABLE accounts
);

ALTER TABLE accounts
ADD CONSTRAINT "constr-unique-username-domain"
ADD CONSTRAINT "constr-unique-accounts-username-domain"
UNIQUE (username, domain);

CREATE INDEX "idx-accounts-account_ts" ON accounts USING GIN (account_ts);
Expand All @@ -82,16 +82,16 @@ CREATE TABLE accounts_follows

-- UNIQUE constraints
ALTER TABLE accounts_follows
ADD CONSTRAINT "constr-unique-account_id-follower_id"
ADD CONSTRAINT "constr-unique-accounts_follows-account_id-follower_id"
UNIQUE (account_id, follower_id);

-- Foreign key constraints
ALTER TABLE accounts_follows
ADD CONSTRAINT "const-foreign-account_id"
ADD CONSTRAINT "const-foreign-accounts_follows-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE accounts_follows
ADD CONSTRAINT "constr-foreign-follower_id"
ADD CONSTRAINT "constr-foreign-accounts_follows-follower_id"
FOREIGN KEY (follower_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE INDEX "idx-accounts_follows-account_id" ON accounts_follows (account_id);
Expand All @@ -110,7 +110,7 @@ CREATE TABLE accounts_preferences

-- Foreign key constraints
ALTER TABLE accounts_preferences
ADD CONSTRAINT "constr-foreign-account_id"
ADD CONSTRAINT "constr-foreign-accounts_preferences-account_id"
FOREIGN KEY (account_id)
REFERENCES accounts (id)
ON DELETE CASCADE
Expand Down Expand Up @@ -138,16 +138,16 @@ CREATE TABLE users

-- UNIQUE constraints
ALTER TABLE users
ADD CONSTRAINT "constr-unique-username-domain"
ADD CONSTRAINT "constr-unique-users-username-domain"
UNIQUE (username, domain);

ALTER TABLE users
ADD CONSTRAINT "constr-unique-confirmation_token"
ADD CONSTRAINT "constr-unique-users-confirmation_token"
UNIQUE (confirmation_token);

-- Foreign key constraints
ALTER TABLE users
ADD CONSTRAINT "constr-foreign-account_id"
ADD CONSTRAINT "constr-foreign-users-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id);

CREATE TABLE posts
Expand Down Expand Up @@ -180,15 +180,15 @@ CREATE TABLE posts

-- Foreign key constraints
ALTER TABLE posts
ADD CONSTRAINT "constr-foreign-account_id"
ADD CONSTRAINT "constr-foreign-posts-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE posts
ADD CONSTRAINT "constr-foreign-in_reply_to_id"
ADD CONSTRAINT "constr-foreign-posts-in_reply_to_id"
FOREIGN KEY (in_reply_to_id) REFERENCES posts (id) ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE posts
ADD CONSTRAINT "constr-foreign-reposted_post_id"
ADD CONSTRAINT "constr-foreign-posts-reposted_post_id"
FOREIGN KEY (reposted_post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE INDEX "idx-posts-account_id" ON posts (account_id);
Expand All @@ -203,28 +203,40 @@ CREATE TABLE posts_favourites
account_id UUID NOT NULL,
post_id UUID NOT NULL,
url TEXT NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- UNIQUE constraints
UNIQUE (account_id, post_id),
-- UNIQUE constraints
ALTER TABLE posts_favourites
ADD CONSTRAINT "constr-unique-posts_favourites-account_id-post_id"
UNIQUE (account_id, post_id);

-- Foreign key constraints
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Foreign key constraints
ALTER TABLE posts_favourites
ADD CONSTRAINT "constr-foreign-posts_favourites-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE posts_favourites
ADD CONSTRAINT "constr-foreign-posts_favourites-post_id"
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE posts_mentions
(
post_id UUID NOT NULL,
account_id UUID NOT NULL,
mention_text TEXT NOT NULL,
PRIMARY KEY (post_id, account_id),

-- Foreign key constraints
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE
PRIMARY KEY (post_id, account_id)
);

-- Foreign key constraints
ALTER TABLE posts_mentions
ADD CONSTRAINT "constr-foreign-posts_mentions-post_id"
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE posts_mentions
ADD CONSTRAINT "constr-foreign-posts_mentions-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE job_context
(
id UUID PRIMARY KEY,
Expand Down Expand Up @@ -252,39 +264,54 @@ CREATE TABLE oauth2_authorization_codes
user_id UUID NOT NULL,
scopes TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

-- Foreign key constraints
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
expires_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Foreign key constraints
ALTER TABLE oauth2_authorization_codes
ADD CONSTRAINT "constr-foreign-oauth2_authorization_codes-application_id"
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE oauth2_authorization_codes
ADD CONSTRAINT "constr-foreign-oauth2_authorization_codes-user_id"
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE oauth2_access_tokens
(
token TEXT PRIMARY KEY,
user_id UUID,
application_id UUID,
scopes TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,

-- Foreign key constraints
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE
expires_at TIMESTAMPTZ NOT NULL
);

-- Foreign key constraints
ALTER TABLE oauth2_access_tokens
ADD CONSTRAINT "constr-foreign-oauth2_access_tokens-user_id"
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE oauth2_access_tokens
ADD CONSTRAINT "constr-foreign-oauth2_access_tokens-application_id"
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE oauth2_refresh_tokens
(
token TEXT PRIMARY KEY,
access_token TEXT NOT NULL UNIQUE,
application_id UUID NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

-- Foreign key constraint
FOREIGN KEY (access_token) REFERENCES oauth2_access_tokens (token) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Foreign key constraint
ALTER TABLE oauth2_refresh_tokens
ADD CONSTRAINT "constr-foreign-oauth2_refresh_tokens-access_token"
FOREIGN KEY (access_token) REFERENCES oauth2_access_tokens (token) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE oauth2_refresh_tokens
ADD CONSTRAINT "constr-foreign-oauth2_refresh_tokens-application_id"
FOREIGN KEY (application_id) REFERENCES oauth2_applications (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE media_attachments
(
id UUID PRIMARY KEY,
Expand All @@ -295,23 +322,31 @@ CREATE TABLE media_attachments
file_path TEXT,
remote_url TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

-- Foreign key constraints
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Foreign key constraints
ALTER TABLE media_attachments
ADD CONSTRAINT "constr-foreign-media_attachments-account_id"
FOREIGN KEY (account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE posts_media_attachments
(
post_id UUID NOT NULL,
media_attachment_id UUID NOT NULL,
PRIMARY KEY (post_id, media_attachment_id),

-- Foreign key constraints
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (media_attachment_id) REFERENCES media_attachments (id) ON DELETE CASCADE ON UPDATE CASCADE
PRIMARY KEY (post_id, media_attachment_id)
);

-- Foreign key constraints
ALTER TABLE posts_media_attachments
ADD CONSTRAINT "constr-foreign-posts_media_attachments-post_id"
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE posts_media_attachments
ADD CONSTRAINT "constr-foreign-posts_media_attachments-media_attachment_id"
FOREIGN KEY (media_attachment_id) REFERENCES media_attachments (id) ON DELETE CASCADE ON UPDATE CASCADE;

-- Add columns for avatars and headers to the "accounts" table
ALTER TABLE accounts
ADD avatar_id UUID,
ADD FOREIGN KEY (avatar_id) REFERENCES media_attachments (id)
Expand All @@ -329,14 +364,18 @@ CREATE TABLE users_roles
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
role INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- UNIQUE constraints
UNIQUE (user_id, role),
-- UNIQUE constraints
ALTER TABLE users_roles
ADD CONSTRAINT "constr-unique-users_roles-user_id-role"
UNIQUE (user_id, role);

-- Foreign key constraints
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Foreign key constraints
ALTER TABLE users_roles
ADD CONSTRAINT "constr-foreign-users_roles-user_id"
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE link_previews
(
Expand All @@ -358,16 +397,26 @@ CREATE TABLE notifications
triggering_account_id UUID,
post_id UUID,
notification_type SMALLINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- UNIQUE constraints
UNIQUE (receiving_account_id, triggering_account_id, post_id, notification_type),
-- UNIQUE constraints
ALTER TABLE notifications
ADD CONSTRAINT "constr-unique-notifications-ra_id-tr_id-post_id-notif_type"
UNIQUE (receiving_account_id, triggering_account_id, post_id, notification_type);

-- Foreign key constraints
FOREIGN KEY (receiving_account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (triggering_account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Foreign key constraints
ALTER TABLE notifications
ADD CONSTRAINT "constr-foreign-notifications-receiving_account_id"
FOREIGN KEY (receiving_account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE notifications
ADD CONSTRAINT "constr-foreign-notifications-triggering_account_id"
FOREIGN KEY (triggering_account_id) REFERENCES accounts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE notifications
ADD CONSTRAINT "constr-foreign-notifications-post_id"
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE INDEX "idx-notifications-receiving_account_id" ON notifications (receiving_account_id);

Expand All @@ -381,27 +430,36 @@ CREATE TABLE custom_emojis
endorsed BOOLEAN NOT NULL DEFAULT FALSE,

created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- UNIQUE constraints
UNIQUE (shortcode, domain),
-- Unique constraints
ALTER TABLE custom_emojis
ADD CONSTRAINT "constr-unique-custom_emojis-shortcode-domain"
UNIQUE (shortcode, domain);

-- Foreign key constraints
FOREIGN KEY (media_attachment_id) REFERENCES media_attachments (id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Foreign key constraints
ALTER TABLE custom_emojis
ADD CONSTRAINT "constr-foreign-custom_emojis-media_attachment_id"
FOREIGN KEY (media_attachment_id) REFERENCES media_attachments (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE posts_custom_emojis
(
post_id UUID NOT NULL,
custom_emoji_id UUID NOT NULL,
emoji_text TEXT NOT NULL,
PRIMARY KEY (post_id, custom_emoji_id),

-- Foreign key constraints
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (custom_emoji_id) REFERENCES custom_emojis (id) ON DELETE CASCADE ON UPDATE CASCADE
PRIMARY KEY (post_id, custom_emoji_id)
);

-- Foreign key constraints
ALTER TABLE posts_custom_emojis
ADD CONSTRAINT "constr-foreign-posts_custom_emojis-post_id"
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE posts_custom_emojis
ADD CONSTRAINT "constr-foreign-posts_custom_emojis-custom_emoji_id"
FOREIGN KEY (custom_emoji_id) REFERENCES custom_emojis (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE INDEX "idx-custom_emojis-remote_id" ON custom_emojis (remote_id);
CREATE INDEX "idx-custom_emojis-shortcode" ON custom_emojis (shortcode);
CREATE INDEX "idx-custom_emojis-domain" ON custom_emojis (domain);
Expand Down

0 comments on commit f2950eb

Please sign in to comment.