From ae576111a2611d39c96963f98cb38fd1c3561340 Mon Sep 17 00:00:00 2001 From: Dave Earley Date: Fri, 29 Mar 2024 08:43:49 -0700 Subject: [PATCH] update schema.sql --- backend/database/migrations/schema.sql | 738 ++++++++++++++----------- 1 file changed, 427 insertions(+), 311 deletions(-) diff --git a/backend/database/migrations/schema.sql b/backend/database/migrations/schema.sql index 1ea346fc..69f47232 100644 --- a/backend/database/migrations/schema.sql +++ b/backend/database/migrations/schema.sql @@ -1,32 +1,38 @@ -create table if not exists migrations +create table migrations ( - id serial, + id serial + primary key, migration varchar(255) not null, - batch integer not null, - primary key (id) + batch integer not null ); -create table if not exists personal_access_tokens +alter table migrations + owner to username; + +create table personal_access_tokens ( - id bigserial, + id bigserial + primary key, tokenable_type varchar(255) not null, tokenable_id bigint not null, name varchar(255) not null, - token varchar(64) not null, + token varchar(64) not null + constraint personal_access_tokens_token_unique + unique, abilities text, last_used_at timestamp(0), expires_at timestamp(0), created_at timestamp(0), - updated_at timestamp(0), - primary key (id), - constraint personal_access_tokens_token_unique - unique (token) + updated_at timestamp(0) ); -create index if not exists personal_access_tokens_tokenable_type_tokenable_id_index +alter table personal_access_tokens + owner to username; + +create index personal_access_tokens_tokenable_type_tokenable_id_index on personal_access_tokens (tokenable_type, tokenable_id); -create table if not exists password_reset_tokens +create table password_reset_tokens ( email varchar(255) not null, token varchar(255) not null, @@ -36,29 +42,36 @@ create table if not exists password_reset_tokens id bigint generated always as identity ); -create index if not exists password_reset_tokens_email_index +alter table password_reset_tokens + owner to username; + +create index password_reset_tokens_email_index on password_reset_tokens (email); -create index if not exists password_reset_tokens_token_index +create index password_reset_tokens_token_index on password_reset_tokens (token); -create table if not exists failed_jobs +create table failed_jobs ( - id bigint generated always as identity, - uuid varchar(255) not null, + id bigint generated always as identity + primary key, + uuid varchar(255) not null + constraint failed_jobs_uuid_unique + unique, connection text not null, queue text not null, payload text not null, exception text not null, - failed_at timestamp(0) default CURRENT_TIMESTAMP not null, - primary key (id), - constraint failed_jobs_uuid_unique - unique (uuid) + failed_at timestamp(0) default CURRENT_TIMESTAMP not null ); -create table if not exists accounts +alter table failed_jobs + owner to username; + +create table accounts ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, currency_code varchar(3) default 'USD'::character varying not null, timezone varchar(255), created_at timestamp, @@ -69,11 +82,13 @@ create table if not exists accounts stripe_account_id varchar(50), short_id varchar(20) not null, stripe_connect_setup_complete boolean default false, - account_verified_at timestamp, - primary key (id) + account_verified_at timestamp ); -create table if not exists password_resets +alter table accounts + owner to username; + +create table password_resets ( email varchar(200), token varchar(200), @@ -85,29 +100,39 @@ create table if not exists password_resets user_agent varchar ); -create table if not exists timezones +alter table password_resets + owner to username; + +create table timezones ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, name varchar(64) not null, - deleted_at timestamp, - primary key (id) + deleted_at timestamp ); -create table if not exists roles +alter table timezones + owner to username; + +create table roles ( - id bigint generated always as identity, + id bigint generated always as identity + constraint roles_pk + primary key, name varchar, permissions jsonb not null, - account_id bigint, - constraint roles_pk - primary key (id), - constraint roles_accounts_id_fk - foreign key (account_id) references accounts + account_id bigint + constraint roles_accounts_id_fk + references accounts ); -create table if not exists users +alter table roles + owner to username; + +create table users ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, email varchar(255) not null, email_verified_at timestamp(0), password varchar(255) not null, @@ -118,14 +143,19 @@ create table if not exists users first_name varchar not null, last_name varchar, pending_email varchar, - timezone varchar not null, - primary key (id) + timezone varchar not null ); -create table if not exists event_logs +alter table users + owner to username; + +create table event_logs ( - id bigint generated always as identity, - user_id bigint not null, + id bigint generated always as identity + primary key, + user_id bigint not null + constraint event_logs_users_id_fk + references users, type varchar(255) not null, entity_id bigint not null, entity_type bigint not null, @@ -134,17 +164,22 @@ create table if not exists event_logs data jsonb, created_at timestamp not null, updated_at timestamp, - deleted_at timestamp, - primary key (id), - constraint event_logs_users_id_fk - foreign key (user_id) references users + deleted_at timestamp ); -create table if not exists taxes_and_fees +alter table event_logs + owner to username; + +create table taxes_and_fees ( - id bigint generated always as identity, + id bigint generated always as identity + constraint tax_and_fee_types_pkey + primary key, name varchar(255) not null, - calculation_type varchar(20) not null, + calculation_type varchar(20) not null + constraint calculation_method_check + check ((calculation_type)::text = ANY + (ARRAY [('FIXED'::character varying)::text, ('PERCENTAGE'::character varying)::text])), rate numeric(10, 3) not null, is_active boolean default true, description text, @@ -153,24 +188,23 @@ create table if not exists taxes_and_fees updated_at timestamp, account_id bigint not null, is_default boolean default false not null, - type varchar(20) not null, - constraint tax_and_fee_types_pkey - primary key (id), - constraint calculation_method_check - check ((calculation_type)::text = ANY - (ARRAY [('FIXED'::character varying)::text, ('PERCENTAGE'::character varying)::text])), - constraint type_check - check ((type)::text = ANY (ARRAY [('TAX'::character varying)::text, ('FEE'::character varying)::text])) + type varchar(20) not null + constraint type_check + check ((type)::text = ANY (ARRAY [('TAX'::character varying)::text, ('FEE'::character varying)::text])) ); comment on column taxes_and_fees.is_default is 'Whether to apply to all new tickets automatically'; -create index if not exists tax_and_fees_account_id_index +alter table taxes_and_fees + owner to username; + +create index tax_and_fees_account_id_index on taxes_and_fees (account_id); -create table if not exists images +create table images ( - id bigserial, + id bigserial + primary key, entity_id bigint, entity_type varchar(120), type varchar(40), @@ -181,23 +215,27 @@ create table if not exists images mime_type varchar(50), created_at timestamp default CURRENT_TIMESTAMP, updated_at timestamp default CURRENT_TIMESTAMP, - deleted_at timestamp, - primary key (id) + deleted_at timestamp ); -create index if not exists idx_images_entity_id +alter table images + owner to username; + +create index idx_images_entity_id on images (entity_id); -create index if not exists idx_images_type +create index idx_images_type on images (type); -create index if not exists idx_images_entity_type +create index idx_images_entity_type on images (entity_type); -create table if not exists organizers +create table organizers ( - id bigint generated always as identity, - account_id integer not null, + id bigint generated always as identity + primary key, + account_id integer not null + references accounts, name varchar(255) not null, email varchar(255) not null, phone varchar(20), @@ -207,17 +245,25 @@ create table if not exists organizers updated_at timestamp not null, deleted_at timestamp, currency varchar(3) default 'USD'::character varying not null, - timezone varchar(255) not null, - primary key (id), - foreign key (account_id) references accounts + timezone varchar(255) not null ); -create table if not exists events +alter table organizers + owner to username; + +create table events ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, title varchar(255) not null, - account_id integer not null, - user_id integer not null, + account_id integer not null + constraint fk_events_account_id + references accounts + on update cascade on delete cascade, + user_id integer not null + constraint fk_events_user_id + references users + on delete cascade, start_date timestamp, end_date timestamp, description text, @@ -230,34 +276,34 @@ create table if not exists events updated_at timestamp not null, deleted_at timestamp, location varchar(255), - organizer_id bigint, + organizer_id bigint + constraint events_organizers_id_fk + references organizers, short_id varchar(32) not null, - ticket_quantity_available integer, - primary key (id), - constraint fk_events_account_id - foreign key (account_id) references accounts - on update cascade on delete cascade, - constraint fk_events_user_id - foreign key (user_id) references users - on delete cascade, - constraint events_organizers_id_fk - foreign key (organizer_id) references organizers + ticket_quantity_available integer ); -create index if not exists events_account_id_index +alter table events + owner to username; + +create index events_account_id_index on events (account_id); -create index if not exists events_user_id_index +create index events_user_id_index on events (user_id); -create index if not exists events_organizer_id_index +create index events_organizer_id_index on events (organizer_id); -create table if not exists tickets +create table tickets ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, title varchar(255) not null, - event_id integer not null, + event_id integer not null + constraint fk_tickets_event_id + references events + on delete cascade, sale_start_date timestamp, sale_end_date timestamp, max_per_order integer, @@ -275,47 +321,52 @@ create table if not exists tickets updated_at timestamp, deleted_at timestamp, type varchar(20) default 'PAID'::character varying not null, - is_hidden boolean default false, - primary key (id), - constraint fk_tickets_event_id - foreign key (event_id) references events - on delete cascade + is_hidden boolean default false ); -create table if not exists promo_codes +alter table tickets + owner to username; + +create table promo_codes ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, code varchar(50) not null, discount numeric(14, 2) default 0.00 not null, applicable_ticket_ids jsonb, expiry_date timestamp with time zone, - event_id bigint not null, + event_id bigint not null + constraint promo_codes_events_id_fk + references events, discount_type varchar, attendee_usage_count integer default 0 not null, order_usage_count integer default 0 not null, max_allowed_usages integer, created_at timestamp not null, updated_at timestamp, - deleted_at timestamp, - primary key (id), - constraint promo_codes_events_id_fk - foreign key (event_id) references events + deleted_at timestamp ); -create index if not exists promo_codes_code_index +alter table promo_codes + owner to username; + +create index promo_codes_code_index on promo_codes (code); -create index if not exists promo_codes_event_id_index +create index promo_codes_event_id_index on promo_codes (event_id); -create index if not exists promo_codes_applicable_ticket_ids_index +create index promo_codes_applicable_ticket_ids_index on promo_codes (applicable_ticket_ids); -create table if not exists orders +create table orders ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, short_id varchar(20) not null, - event_id integer not null, + event_id integer not null + constraint fk_orders_event_id + references events, total_before_additions numeric(14, 2) default 0.00 not null, total_refunded numeric(14, 2) default 0.00 not null, total_gross numeric(14, 2) default 0.00 not null, @@ -329,10 +380,14 @@ create table if not exists orders reserved_until timestamp(0), is_manually_created boolean default false not null, session_id varchar(40), - public_id varchar not null, + public_id varchar not null + constraint orders_pk + unique, point_in_time_data jsonb, payment_gateway varchar, - promo_code_id integer, + promo_code_id integer + constraint orders_promo_codes_id_fk + references promo_codes, promo_code varchar, address jsonb, created_at timestamp not null, @@ -340,35 +395,34 @@ create table if not exists orders deleted_at timestamp, taxes_and_fees_rollup jsonb, total_tax numeric(14, 2) default 0.00 not null, - total_fee numeric(14, 2) default 0.00 not null, - primary key (id), - constraint orders_pk - unique (public_id), - constraint fk_orders_event_id - foreign key (event_id) references events, - constraint orders_promo_codes_id_fk - foreign key (promo_code_id) references promo_codes + total_fee numeric(14, 2) default 0.00 not null ); -create index if not exists orders_promo_code_id_index +alter table orders + owner to username; + +create index orders_promo_code_id_index on orders (promo_code_id); -create index if not exists idx_orders_first_name_trgm +create index idx_orders_first_name_trgm on orders using gin (first_name gin_trgm_ops); -create index if not exists idx_orders_last_name_trgm +create index idx_orders_last_name_trgm on orders using gin (last_name gin_trgm_ops); -create index if not exists idx_orders_email_trgm +create index idx_orders_email_trgm on orders using gin (email gin_trgm_ops); -create index if not exists idx_orders_public_id_trgm +create index idx_orders_public_id_trgm on orders using gin (public_id gin_trgm_ops); -create table if not exists questions +create table questions ( - id bigint generated always as identity, - event_id bigint not null, + id bigint generated always as identity + primary key, + event_id bigint not null + constraint questions_event_id_fk + references events, title text not null, required boolean default false not null, type varchar, @@ -378,16 +432,19 @@ create table if not exists questions updated_at timestamp not null, deleted_at timestamp, "order" integer default 1 not null, - is_hidden boolean default false not null, - primary key (id), - constraint questions_event_id_fk - foreign key (event_id) references events + is_hidden boolean default false not null ); -create table if not exists stripe_payments +alter table questions + owner to username; + +create table stripe_payments ( - id bigint generated always as identity, - order_id bigint not null, + id bigint generated always as identity + primary key, + order_id bigint not null + constraint stripe_payments_orders_id_fk + references orders, payment_intent_id varchar not null, charge_id varchar, payment_method_id varchar, @@ -396,22 +453,27 @@ create table if not exists stripe_payments updated_at timestamp, deleted_at timestamp, last_error json, - connected_account_id varchar(50), - primary key (id), - constraint stripe_payments_orders_id_fk - foreign key (order_id) references orders + connected_account_id varchar(50) ); -create table if not exists messages +alter table stripe_payments + owner to username; + +create table messages ( - id bigint generated always as identity, - event_id bigint not null, + id bigint generated always as identity + primary key, + event_id bigint not null + constraint messages_events_id_fk + references events, subject varchar(255) not null, message text not null, type varchar(40) not null, recipient_ids jsonb, sent_at timestamp, - sent_by_user_id bigint not null, + sent_by_user_id bigint not null + constraint messages_users_id_fk + references users, attendee_ids jsonb, ticket_ids jsonb, order_id bigint, @@ -419,39 +481,43 @@ create table if not exists messages send_data jsonb, created_at timestamp not null, updated_at timestamp, - deleted_at timestamp, - primary key (id), - constraint messages_events_id_fk - foreign key (event_id) references events, - constraint messages_users_id_fk - foreign key (sent_by_user_id) references users + deleted_at timestamp ); -create table if not exists affiliates +alter table messages + owner to username; + +create table affiliates ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, code varchar not null, - event_id bigint, + event_id bigint + constraint affiliates_events_id_fk + references events, sales_volume numeric(14, 2), unique_visitors integer default 0 not null, created_at timestamp not null, updated_at timestamp, - deleted_at timestamp, - primary key (id), - constraint affiliates_events_id_fk - foreign key (event_id) references events + deleted_at timestamp ); -create unique index if not exists affiliates_code_uindex +alter table affiliates + owner to username; + +create unique index affiliates_code_uindex on affiliates (code); -create index if not exists affiliates_event_id_index +create index affiliates_event_id_index on affiliates (event_id); -create table if not exists event_statistics +create table event_statistics ( - id bigint generated always as identity, - event_id bigint not null, + id bigint generated always as identity + primary key, + event_id bigint not null + constraint event_statistics_events_id_fk + references events, unique_views bigint default 0 not null, total_views bigint default 0 not null, sales_total_gross numeric(14, 2) default 0.00 not null, @@ -464,18 +530,19 @@ create table if not exists event_statistics tickets_sold integer default 0 not null, version integer default 0 not null, orders_created integer default 0 not null, - total_refunded numeric(14, 2) default 0 not null, - primary key (id), - constraint event_statistics_events_id_fk - foreign key (event_id) references events + total_refunded numeric(14, 2) default 0 not null ); -create index if not exists event_statistics_event_id_index +alter table event_statistics + owner to username; + +create index event_statistics_event_id_index on event_statistics (event_id); -create table if not exists event_daily_statistics +create table event_daily_statistics ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, sales_total_gross numeric(14, 2) default 0.00 not null, total_tax numeric(14, 2) default 0.00 not null, sales_total_before_additions numeric(14, 2) default 0.00 not null, @@ -486,44 +553,56 @@ create table if not exists event_daily_statistics deleted_at timestamp, updated_at timestamp, total_fee numeric(14, 2) default 0 not null, - event_id bigint not null, + event_id bigint not null + constraint event_daily_statistics_events_id_fk + references events, version integer default 0 not null, total_refunded numeric(14, 2) default 0 not null, - total_views bigint default 0 not null, - primary key (id), - constraint event_daily_statistics_events_id_fk - foreign key (event_id) references events + total_views bigint default 0 not null ); -create index if not exists event_daily_statistics_event_id_index +alter table event_daily_statistics + owner to username; + +create index event_daily_statistics_event_id_index on event_daily_statistics (event_id); -create table if not exists ticket_taxes_and_fees +create table ticket_taxes_and_fees ( - id integer generated always as identity, - ticket_id bigint not null, - tax_and_fee_id bigint not null, - constraint ticket_tax_and_fees_pk - primary key (id), - constraint ticket_tax_and_fees_tickets_id_fk - foreign key (ticket_id) references tickets + id integer generated always as identity + constraint ticket_tax_and_fees_pk + primary key, + ticket_id bigint not null + constraint ticket_tax_and_fees_tickets_id_fk + references tickets on delete cascade, - constraint ticket_tax_and_fees_tax_and_fees_id_fk - foreign key (tax_and_fee_id) references taxes_and_fees + tax_and_fee_id bigint not null + constraint ticket_tax_and_fees_tax_and_fees_id_fk + references taxes_and_fees on delete cascade ); -create index if not exists ticket_tax_and_fees_tax_and_fee_id_index +alter table ticket_taxes_and_fees + owner to username; + +create index ticket_tax_and_fees_tax_and_fee_id_index on ticket_taxes_and_fees (tax_and_fee_id); -create index if not exists ticket_tax_and_fees_ticket_id_index +create index ticket_tax_and_fees_ticket_id_index on ticket_taxes_and_fees (ticket_id); -create table if not exists ticket_prices +create table ticket_prices ( - id bigint generated always as identity, - ticket_id bigint not null, - price numeric(14, 2) not null, + id bigint generated always as identity + constraint pk_ticket_prices + primary key, + ticket_id bigint not null + constraint fk_ticket_prices_ticket_id + references tickets + on delete cascade, + price numeric(14, 2) not null + constraint valid_price_range + check (price >= (0)::numeric), label varchar(255), sale_start_date timestamp, sale_end_date timestamp, @@ -533,29 +612,31 @@ create table if not exists ticket_prices initial_quantity_available integer, quantity_sold integer default 0 not null, is_hidden boolean default false, - "order" integer default 1 not null, - constraint pk_ticket_prices - primary key (id), - constraint fk_ticket_prices_ticket_id - foreign key (ticket_id) references tickets - on delete cascade, - constraint valid_price_range - check (price >= (0)::numeric) + "order" integer default 1 not null ); -create index if not exists idx_ticket_prices_ticket_id +alter table ticket_prices + owner to username; + +create index idx_ticket_prices_ticket_id on ticket_prices (ticket_id); -create index if not exists idx_ticket_prices_dates +create index idx_ticket_prices_dates on ticket_prices (sale_start_date, sale_end_date); -create table if not exists order_items +create table order_items ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, total_before_additions numeric(14, 2) not null, quantity integer not null, - order_id integer not null, - ticket_id integer not null, + order_id integer not null + constraint fk_order_items_order_id + references orders + on delete cascade, + ticket_id integer not null + constraint fk_order_items_ticket_id + references tickets, item_name varchar, price numeric(14, 2) not null, price_before_discount numeric(14, 2), @@ -564,139 +645,150 @@ create table if not exists order_items total_gross numeric(14, 2), total_service_fee numeric(14, 2) default 0.00, taxes_and_fees_rollup jsonb, - ticket_price_id integer not null, - primary key (id), - constraint fk_order_items_ticket_id - foreign key (ticket_id) references tickets, - constraint fk_order_items_order_id - foreign key (order_id) references orders - on delete cascade, - constraint order_items_ticket_prices_id_fk - foreign key (ticket_price_id) references ticket_prices + ticket_price_id integer not null + constraint order_items_ticket_prices_id_fk + references ticket_prices ); -create index if not exists order_items_order_id_index +alter table order_items + owner to username; + +create index order_items_order_id_index on order_items (order_id); -create index if not exists order_items_ticket_id_index +create index order_items_ticket_id_index on order_items (ticket_id); -create index if not exists order_items_ticket_price_id_index +create index order_items_ticket_price_id_index on order_items (ticket_price_id); -create table if not exists attendees +create table attendees ( - id bigint generated always as identity, + id bigint generated always as identity + primary key, short_id varchar not null, first_name varchar(255) default ''::character varying not null, last_name varchar(255) default ''::character varying not null, email varchar(255) not null, - order_id integer not null, - ticket_id integer not null, - event_id integer not null, + order_id integer not null + constraint fk_attendees_order_id + references orders + on delete cascade, + ticket_id integer not null + constraint fk_attendees_ticket_id + references tickets + on delete cascade, + event_id integer not null + constraint attendees_events_id_fk + references events + on delete cascade, public_id varchar not null, status varchar(20) not null, - checked_in_by bigint, + checked_in_by bigint + constraint fk_attendees_checked_in_by_id + references users + on delete cascade, checked_in_at timestamp, created_at timestamp not null, updated_at timestamp not null, deleted_at timestamp, - checked_out_by bigint, - ticket_price_id bigint not null, - primary key (id), - constraint fk_attendees_order_id - foreign key (order_id) references orders - on delete cascade, - constraint fk_attendees_checked_in_by_id - foreign key (checked_in_by) references users - on delete cascade, - constraint fk_attendees_ticket_id - foreign key (ticket_id) references tickets - on delete cascade, - constraint attendees_events_id_fk - foreign key (event_id) references events - on delete cascade, - constraint attendees_users_id_fk - foreign key (checked_out_by) references users, - constraint attendees_ticket_prices_id_fk - foreign key (ticket_price_id) references ticket_prices + checked_out_by bigint + constraint attendees_users_id_fk + references users, + ticket_price_id bigint not null + constraint attendees_ticket_prices_id_fk + references ticket_prices ); -create index if not exists idx_attendees_first_name_trgm +alter table attendees + owner to username; + +create index idx_attendees_first_name_trgm on attendees using gin (first_name gin_trgm_ops); -create index if not exists idx_attendees_last_name_trgm +create index idx_attendees_last_name_trgm on attendees using gin (last_name gin_trgm_ops); -create index if not exists idx_attendees_email_trgm +create index idx_attendees_email_trgm on attendees using gin (email gin_trgm_ops); -create index if not exists idx_attendees_public_id_trgm +create index idx_attendees_public_id_trgm on attendees using gin (public_id gin_trgm_ops); -create index if not exists idx_attendees_public_id_lower +create index idx_attendees_public_id_lower on attendees (lower(public_id::text)); -create table if not exists question_answers +create table question_answers ( - id bigint generated always as identity, - question_id integer not null, - order_id integer not null, - attendee_id integer, - ticket_id integer, + id bigint generated always as identity + primary key, + question_id integer not null + constraint fk_question_answers_question_id + references questions, + order_id integer not null + constraint fk_orders_order_id + references orders, + attendee_id integer + constraint fk_attendeed_attendee_id + references attendees, + ticket_id integer + constraint fk_tickets_ticket_id + references tickets, created_at timestamp not null, updated_at timestamp not null, deleted_at timestamp, - answer jsonb, - primary key (id), - constraint fk_question_answers_question_id - foreign key (question_id) references questions, - constraint fk_orders_order_id - foreign key (order_id) references orders, - constraint fk_attendeed_attendee_id - foreign key (attendee_id) references attendees, - constraint fk_tickets_ticket_id - foreign key (ticket_id) references tickets + answer jsonb ); -create index if not exists question_answers_attendee_id_index +alter table question_answers + owner to username; + +create index question_answers_attendee_id_index on question_answers (attendee_id); -create index if not exists question_answers_order_id_index +create index question_answers_order_id_index on question_answers (order_id); -create index if not exists question_answers_question_id_index +create index question_answers_question_id_index on question_answers (question_id); -create table if not exists ticket_questions +create table ticket_questions ( - id serial, - ticket_id integer not null, - question_id integer not null, - deleted_at timestamp, - primary key (id), - constraint fk_ticket_questions_ticket_id - foreign key (ticket_id) references tickets + id serial + primary key, + ticket_id integer not null + constraint fk_ticket_questions_ticket_id + references tickets on delete cascade, - constraint fk_ticket_questions_question_id - foreign key (question_id) references questions - on delete cascade + question_id integer not null + constraint fk_ticket_questions_question_id + references questions + on delete cascade, + deleted_at timestamp ); -create unique index if not exists idx_ticket_questions_active +alter table ticket_questions + owner to username; + +create unique index idx_ticket_questions_active on ticket_questions (ticket_id, question_id) where (deleted_at IS NULL); -create table if not exists event_settings +create table event_settings ( - id bigint generated always as identity, + id bigint generated always as identity + constraint event_settings_pk + primary key, pre_checkout_message text, post_checkout_message text, ticket_page_message text, continue_button_text varchar(100), email_footer_message text, reply_to_email varchar(255), - event_id bigint not null, + event_id bigint not null + constraint event_settings_events_id_fk + references events + on delete cascade, created_at timestamp not null, updated_at timestamp not null, deleted_at timestamp, @@ -719,51 +811,75 @@ create table if not exists event_settings show_social_media_handles boolean, seo_keywords varchar(255), notify_organizer_of_new_orders boolean default true not null, - price_display_mode varchar(255) default 'INCLUSIVE'::character varying not null, - constraint event_settings_pk - primary key (id), - constraint event_settings_events_id_fk - foreign key (event_id) references events - on delete cascade, - constraint event_settings_price_display_mode_check - check ((price_display_mode)::text = ANY - ((ARRAY ['INCLUSIVE'::character varying, 'EXCLUSIVE'::character varying])::text[])) + price_display_mode varchar(255) default 'INCLUSIVE'::character varying not null + constraint event_settings_price_display_mode_check + check ((price_display_mode)::text = ANY + ((ARRAY ['INCLUSIVE'::character varying, 'EXCLUSIVE'::character varying])::text[])), + hide_getting_started_page boolean default false not null, + show_share_buttons boolean default true not null ); -create index if not exists event_settings_event_id_index +alter table event_settings + owner to username; + +create index event_settings_event_id_index on event_settings (event_id); -create table if not exists account_users +create table account_users ( - id bigint generated always as identity, - account_id bigint not null, - user_id bigint not null, + id bigint generated always as identity + primary key, + account_id bigint not null + constraint fk_account_users_accounts + references accounts + on delete cascade, + user_id bigint not null + constraint fk_account_users_users + references users + on delete cascade, role varchar(100), created_at timestamp default now(), deleted_at timestamp, updated_at timestamp, is_account_owner boolean default false not null, - invited_by_user_id bigint, + invited_by_user_id bigint + constraint account_users_users_id_fk + references users, last_login_at timestamp, status varchar(40) default 'INVITED'::character varying not null, - primary key (id), - unique (account_id, user_id, role), - constraint fk_account_users_accounts - foreign key (account_id) references accounts - on delete cascade, - constraint fk_account_users_users - foreign key (user_id) references users - on delete cascade, - constraint account_users_users_id_fk - foreign key (invited_by_user_id) references users + unique (account_id, user_id, role) ); -create index if not exists idx_account_users_account_id +alter table account_users + owner to username; + +create index idx_account_users_account_id on account_users (account_id); -create index if not exists idx_account_users_user_id +create index idx_account_users_user_id on account_users (user_id); -create index if not exists idx_account_users_role +create index idx_account_users_role on account_users (role); +create view question_and_answer_views + (question_id, event_id, belongs_to, question_type, first_name, last_name, attendee_id, order_id, title, + answer) as +SELECT q.id AS question_id, + q.event_id, + q.belongs_to, + q.type AS question_type, + a.first_name, + a.last_name, + a.id AS attendee_id, + qa.order_id, + q.title, + qa.answer +FROM question_answers qa + LEFT JOIN attendees a ON a.id = qa.attendee_id + JOIN orders o ON qa.order_id = o.id + JOIN questions q ON q.id = qa.question_id; + +alter table question_and_answer_views + owner to username; +