From 0c2eeaf954842fb00b3371eb6c1d438e1d9609ea Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 16 Aug 2023 03:30:34 +0200 Subject: [PATCH] chore: add down migrations --- scripts/migrate/001_init.sql | 17 +++++++++-- .../migrate/002_add_member_display_name.sql | 7 ++++- scripts/migrate/003_add_tokens.sql | 9 +++++- scripts/migrate/004_field_arrays.sql | 30 +++++++++++++++++-- scripts/migrate/005_delete_users.sql | 10 +++++-- scripts/migrate/006_jsonb_arrays.sql | 26 ++++++++++++++-- scripts/migrate/007_hashed_avatars.sql | 12 ++++++-- scripts/migrate/008_data_exports.sql | 8 +++-- scripts/migrate/009_fediverse_oauth.sql | 12 ++++++-- scripts/migrate/010_reports.sql | 10 +++++-- scripts/migrate/011_token_info.sql | 9 ++++-- scripts/migrate/012_custom_options.sql | 11 +++++-- scripts/migrate/013_tumblr_oauth.sql | 9 ++++-- scripts/migrate/014_google_oauth.sql | 9 ++++-- scripts/migrate/015_custom_preferences.sql | 8 +++-- scripts/migrate/016_user_activity.sql | 8 +++-- scripts/migrate/017_pride_flags.sql | 10 +++++-- scripts/migrate/018_short_ids.sql | 14 +++++++-- scripts/migrate/019_timezones.sql | 8 +++-- 19 files changed, 191 insertions(+), 36 deletions(-) diff --git a/scripts/migrate/001_init.sql b/scripts/migrate/001_init.sql index 2e83fb1..6708441 100644 --- a/scripts/migrate/001_init.sql +++ b/scripts/migrate/001_init.sql @@ -1,7 +1,7 @@ --- +migrate Up - -- 2022-05-02: initial schema +-- +migrate Up + create table users ( id text primary key, username text not null unique, @@ -89,3 +89,16 @@ create table invites ( created timestamp not null default (current_timestamp at time zone 'utc'), used boolean not null default false ); + +-- +migrate Down + +drop table invites; +drop table member_fields; +drop table member_pronouns; +drop table member_names; +drop index members_user_name_idx; +drop table members; +drop table user_fields; +drop table user_pronouns; +drop table user_names; +drop table users; diff --git a/scripts/migrate/002_add_member_display_name.sql b/scripts/migrate/002_add_member_display_name.sql index 6bce517..64ea9b7 100644 --- a/scripts/migrate/002_add_member_display_name.sql +++ b/scripts/migrate/002_add_member_display_name.sql @@ -1,4 +1,9 @@ +-- 2022-11-20: add display name to members + -- +migrate Up --- 2022-11-20: add display name to members alter table members add column display_name text; + +-- +migrate Down + +alter table members drop column display_name; diff --git a/scripts/migrate/003_add_tokens.sql b/scripts/migrate/003_add_tokens.sql index a6d81ef..8d19ac2 100644 --- a/scripts/migrate/003_add_tokens.sql +++ b/scripts/migrate/003_add_tokens.sql @@ -1,6 +1,7 @@ +-- 2022-12-23: Add database-backed tokens + -- +migrate Up --- 2022-12-23: Add database-backed tokens create table tokens ( user_id text not null references users (id) on delete cascade, token_id text primary key, @@ -13,3 +14,9 @@ create table tokens ( -- This does not change anything code-wise, but it's recommended over plain timestamp because plain timestamp does not handle timezones correctly alter table invites alter column created type timestamptz; alter table invites alter column created set default now(); + +-- +migrate Down + +drop table tokens; +alter table invites alter column created type timestamp; +alter table invites alter column created set default (current_timestamp at time zone 'utc'); diff --git a/scripts/migrate/004_field_arrays.sql b/scripts/migrate/004_field_arrays.sql index 0606cb4..ce5821e 100644 --- a/scripts/migrate/004_field_arrays.sql +++ b/scripts/migrate/004_field_arrays.sql @@ -1,7 +1,7 @@ --- +migrate Up - -- 2023-01-03: change names, pronouns, and fields to be columns instead of separate tables +-- +migrate Up + create type field_entry as ( value text, status int @@ -33,3 +33,29 @@ alter table member_fields drop column okay; alter table member_fields drop column jokingly; alter table member_fields drop column friends_only; alter table member_fields drop column avoid; + +-- +migrate Down + +alter table user_fields add column favourite text[]; +alter table user_fields add column okay text[]; +alter table user_fields add column jokingly text[]; +alter table user_fields add column friends_only text[]; +alter table user_fields add column avoid text[]; + +alter table member_fields add column favourite text[]; +alter table member_fields add column okay text[]; +alter table member_fields add column jokingly text[]; +alter table member_fields add column friends_only text[]; +alter table member_fields add column avoid text[]; + +alter table users drop column names; +alter table users drop column pronouns; + +alter table members drop column names; +alter table members drop column pronouns; + +alter table user_fields drop column entries; +alter table member_fields drop column entries; + +drop type field_entry; +drop type pronoun_entry; diff --git a/scripts/migrate/005_delete_users.sql b/scripts/migrate/005_delete_users.sql index 3bb2ac1..f6a0c45 100644 --- a/scripts/migrate/005_delete_users.sql +++ b/scripts/migrate/005_delete_users.sql @@ -1,10 +1,16 @@ --- +migrate Up - -- 2023-03-07: add delete functionality +-- +migrate Up + -- if not null, the user is soft deleted alter table users add column deleted_at timestamptz; -- if true, the user deleted their account themselves + should have option to reactivate; should also be deleted after 30 days alter table users add column self_delete boolean; -- delete reason if the user was deleted by a moderator alter table users add column delete_reason text; + +-- +migrate Down + +alter table users drop column deleted_at; +alter table users drop column self_delete; +alter table users drop column delete_reason; diff --git a/scripts/migrate/006_jsonb_arrays.sql b/scripts/migrate/006_jsonb_arrays.sql index c9ad3d1..9f72465 100644 --- a/scripts/migrate/006_jsonb_arrays.sql +++ b/scripts/migrate/006_jsonb_arrays.sql @@ -1,8 +1,8 @@ --- +migrate Up - -- 2023-03-11: Change composite type arrays to use jsonb columns -- Composite types aren't actually supported by pgx and this allows us to drop pggen as a dev dependency. +-- +migrate Up + -- Delete old columns alter table users drop column names; alter table users drop column pronouns; @@ -22,3 +22,25 @@ alter table members add column pronouns jsonb not null default '[]'; alter table user_fields add column entries jsonb not null default '[]'; alter table member_fields add column entries jsonb not null default '[]'; + +-- +migrate Down + +-- Delete old columns +alter table users drop column names; +alter table users drop column pronouns; + +alter table members drop column names; +alter table members drop column pronouns; + +alter table user_fields drop column entries; +alter table member_fields drop column entries; + +-- Create new columns +alter table users add column names field_entry; +alter table users add column pronouns pronoun_entry; + +alter table members add column names field_entry; +alter table members add column pronouns pronoun_entry; + +alter table user_fields add column entries field_entry; +alter table member_fields add column entries field_entry; diff --git a/scripts/migrate/007_hashed_avatars.sql b/scripts/migrate/007_hashed_avatars.sql index 547e433..4ad24b2 100644 --- a/scripts/migrate/007_hashed_avatars.sql +++ b/scripts/migrate/007_hashed_avatars.sql @@ -1,9 +1,17 @@ --- +migrate Up - -- 2023-03-13: Change avatar URLs to hashes +-- +migrate Up + alter table users drop column avatar_urls; alter table members drop column avatar_urls; alter table users add column avatar text; alter table members add column avatar text; + +-- +migrate Down + +alter table users drop column avatar; +alter table members drop column avatar; + +alter table users add column avatar_urls text[]; +alter table members add column avatar_urls text[]; diff --git a/scripts/migrate/008_data_exports.sql b/scripts/migrate/008_data_exports.sql index 937a56c..f4fbbf7 100644 --- a/scripts/migrate/008_data_exports.sql +++ b/scripts/migrate/008_data_exports.sql @@ -1,10 +1,14 @@ --- +migrate Up - -- 2023-03-15: Add data export +-- +migrate Up + create table data_exports ( id serial primary key, user_id text not null references users (id) on delete cascade, filename text not null, created_at timestamptz not null default now() ); + +-- +migrate Down + +drop table data_exports; diff --git a/scripts/migrate/009_fediverse_oauth.sql b/scripts/migrate/009_fediverse_oauth.sql index f1265cc..27ef875 100644 --- a/scripts/migrate/009_fediverse_oauth.sql +++ b/scripts/migrate/009_fediverse_oauth.sql @@ -1,7 +1,7 @@ --- +migrate Up - -- 2023-03-16: Add fediverse (Mastodon/Pleroma/Misskey) OAuth +-- +migrate Up + create table fediverse_apps ( id serial primary key, instance text not null unique, @@ -13,3 +13,11 @@ create table fediverse_apps ( alter table users add column fediverse text null; alter table users add column fediverse_username text null; alter table users add column fediverse_app_id integer null references fediverse_apps (id) on delete set null; + +-- +migrate Down + +alter table users drop column fediverse; +alter table users drop column fediverse_username; +alter table users drop column fediverse_app_id; + +drop table fediverse_apps; diff --git a/scripts/migrate/010_reports.sql b/scripts/migrate/010_reports.sql index 888f601..4cabb14 100644 --- a/scripts/migrate/010_reports.sql +++ b/scripts/migrate/010_reports.sql @@ -1,7 +1,7 @@ --- +migrate Up - -- 2023-03-19: Add moderation-related tables +-- +migrate Up + alter table users add column is_admin boolean not null default false; create table reports ( @@ -25,3 +25,9 @@ create table warnings ( created_at timestamptz not null default now(), read_at timestamptz ); + +-- +migrate Down + +drop table warnings; +drop table reports; +alter table users drop column is_admin; diff --git a/scripts/migrate/011_token_info.sql b/scripts/migrate/011_token_info.sql index c9958ac..019b2f4 100644 --- a/scripts/migrate/011_token_info.sql +++ b/scripts/migrate/011_token_info.sql @@ -1,6 +1,11 @@ --- +migrate Up - -- 2023-03-30: Add token information to database +-- +migrate Up + alter table tokens add column api_only boolean not null default false; alter table tokens add column read_only boolean not null default false; + +-- +migrate Down + +alter table tokens drop column api_only; +alter table tokens drop column read_only; diff --git a/scripts/migrate/012_custom_options.sql b/scripts/migrate/012_custom_options.sql index 31c2212..501940f 100644 --- a/scripts/migrate/012_custom_options.sql +++ b/scripts/migrate/012_custom_options.sql @@ -1,8 +1,15 @@ --- +migrate Up - -- 2023-04-01: Add a couple customization options to users and members +-- +migrate Up + alter table users add column member_title text; alter table users add column list_private boolean not null default false; alter table members add column unlisted boolean not null default false; + +-- +migrate Down + +alter table users drop column member_title; +alter table users drop column list_private; + +alter table members drop column unlisted; diff --git a/scripts/migrate/013_tumblr_oauth.sql b/scripts/migrate/013_tumblr_oauth.sql index 0a04c62..3ce37c7 100644 --- a/scripts/migrate/013_tumblr_oauth.sql +++ b/scripts/migrate/013_tumblr_oauth.sql @@ -1,6 +1,11 @@ --- +migrate Up - -- 2023-04-18: Add tumblr oauth +-- +migrate Up + alter table users add column tumblr text null; alter table users add column tumblr_username text null; + +-- +migrate Down + +alter table users drop column tumblr; +alter table users drop column tumblr_username; diff --git a/scripts/migrate/014_google_oauth.sql b/scripts/migrate/014_google_oauth.sql index 2fdb0fb..5ce1043 100644 --- a/scripts/migrate/014_google_oauth.sql +++ b/scripts/migrate/014_google_oauth.sql @@ -1,6 +1,11 @@ --- +migrate Up - -- 2023-04-18: Add Google oauth +-- +migrate Up + alter table users add column google text null; alter table users add column google_username text null; + +-- +migrate Down + +alter table users drop column google; +alter table users drop column google_username; diff --git a/scripts/migrate/015_custom_preferences.sql b/scripts/migrate/015_custom_preferences.sql index 90f112b..d05803c 100644 --- a/scripts/migrate/015_custom_preferences.sql +++ b/scripts/migrate/015_custom_preferences.sql @@ -1,5 +1,9 @@ --- +migrate Up - -- 2023-04-19: Add custom preferences +-- +migrate Up + alter table users add column custom_preferences jsonb not null default '{}'; + +-- +migrate Down + +alter table users drop column custom_preferences; diff --git a/scripts/migrate/016_user_activity.sql b/scripts/migrate/016_user_activity.sql index e306e93..9400643 100644 --- a/scripts/migrate/016_user_activity.sql +++ b/scripts/migrate/016_user_activity.sql @@ -1,7 +1,11 @@ --- +migrate Up - -- 2023-05-02: Add a last_active column to users, updated whenever the user modifies their profile or members. -- This is not directly exposed in the API. -- Potential future use cases: showing total number of active users, pruning completely empty users if they don't log in? +-- +migrate Up + alter table users add column last_active timestamptz not null default now(); + +-- +migrate Down + +alter table users drop column last_active; diff --git a/scripts/migrate/017_pride_flags.sql b/scripts/migrate/017_pride_flags.sql index 9c9b622..11add61 100644 --- a/scripts/migrate/017_pride_flags.sql +++ b/scripts/migrate/017_pride_flags.sql @@ -1,8 +1,8 @@ --- +migrate Up - -- 2023-05-09: Add pride flags -- Hashes are a separate table so we can deduplicate flags. +-- +migrate Up + create table pride_flags ( id text primary key, user_id text not null references users (id) on delete cascade, @@ -22,3 +22,9 @@ create table member_flags ( member_id text not null references members (id) on delete cascade, flag_id text not null references pride_flags (id) on delete cascade ); + +-- +migrate Down + +drop table member_flags; +drop table user_flags; +drop table pride_flags; diff --git a/scripts/migrate/018_short_ids.sql b/scripts/migrate/018_short_ids.sql index 09b9af6..d591c13 100644 --- a/scripts/migrate/018_short_ids.sql +++ b/scripts/migrate/018_short_ids.sql @@ -1,7 +1,7 @@ --- +migrate Up - -- 2023-06-03: Add short IDs for the prns.cc domain. +-- +migrate Up + -- add the columns alter table users add column sid text unique check(length(sid)=5); alter table members add column sid text unique check(length(sid)=6); @@ -48,3 +48,13 @@ update members set sid = find_free_member_sid(); -- finally, make the values non-nullable alter table users alter column sid set not null; alter table members alter column sid set not null; + +-- +migrate Down + +drop function find_free_member_sid; +drop function find_free_user_sid; +drop function generate_sid; + +alter table users drop column sid; +alter table users drop column last_sid_reroll; +alter table members drop column sid; diff --git a/scripts/migrate/019_timezones.sql b/scripts/migrate/019_timezones.sql index f45c69c..369300f 100644 --- a/scripts/migrate/019_timezones.sql +++ b/scripts/migrate/019_timezones.sql @@ -1,5 +1,9 @@ --- +migrate Up - -- 2023-07-30: Add user timezones +-- +migrate Up + alter table users add column timezone text null; + +-- +migrate Down + +alter table users drop column timezone;