-- Couchers.org database schema
-- Generated from migrations at commit a58c182a9d75f36ec95da20ef7372715561f4b9e on branch dependabot/npm_and_yarn/app/web/dayjs-1.11.21 at 2026-06-21T00:24:52+00:00


SET transaction_timeout = 0;
SELECT pg_catalog.set_config('search_path', '', false);

CREATE SCHEMA logging;

COMMENT ON SCHEMA public IS '';

CREATE EXTENSION IF NOT EXISTS btree_gist WITH SCHEMA public;

COMMENT ON EXTENSION btree_gist IS 'support for indexing common datatypes in GiST';

CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;

COMMENT ON EXTENSION pg_stat_statements IS 'track planning and execution statistics of all SQL statements executed';

CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;

COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';

CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;

COMMENT ON EXTENSION postgis IS 'PostGIS geometry and geography spatial types and functions';

CREATE EXTENSION IF NOT EXISTS unaccent WITH SCHEMA public;

COMMENT ON EXTENSION unaccent IS 'text search dictionary that removes accents';

CREATE TYPE public.activenessprobestatus AS ENUM (
    'pending',
    'expired',
    'still_active',
    'no_longer_active'
);

CREATE TYPE public.adminactionlevel AS ENUM (
    'debug',
    'normal',
    'high'
);

CREATE TYPE public.attendeestatus AS ENUM (
    'going'
);

CREATE TYPE public.backgroundjobstate AS ENUM (
    'pending',
    'completed',
    'error',
    'failed'
);

CREATE TYPE public.clientplatform AS ENUM (
    'web_desktop',
    'web_mobile',
    'app_ios',
    'app_android'
);

CREATE TYPE public.clusterrole AS ENUM (
    'member',
    'admin'
);

CREATE TYPE public.contentchangetype AS ENUM (
    'edit',
    'delete'
);

CREATE TYPE public.contributeoption AS ENUM (
    'yes',
    'maybe',
    'no'
);

CREATE TYPE public.devicetype AS ENUM (
    'ios',
    'android'
);

CREATE TYPE public.donationtype AS ENUM (
    'one_time',
    'recurring'
);

CREATE TYPE public.eventsource AS ENUM (
    'backend',
    'frontend'
);

CREATE TYPE public.exposuresource AS ENUM (
    'backend',
    'client'
);

CREATE TYPE public.friendstatus AS ENUM (
    'pending',
    'accepted',
    'rejected',
    'cancelled'
);

CREATE TYPE public.groupchatrole AS ENUM (
    'admin',
    'participant'
);

CREATE TYPE public.hostingstatus AS ENUM (
    'can_host',
    'maybe',
    'cant_host'
);

CREATE TYPE public.hostrequestquality AS ENUM (
    'high_quality',
    'okay_quality',
    'low_quality'
);

CREATE TYPE public.hostrequeststatus AS ENUM (
    'pending',
    'accepted',
    'rejected',
    'confirmed',
    'cancelled'
);

CREATE TYPE public.invoicetype AS ENUM (
    'on_platform',
    'external_shop'
);

CREATE TYPE public.languagefluency AS ENUM (
    'beginner',
    'conversational',
    'fluent'
);

CREATE TYPE public.meetupstatus AS ENUM (
    'wants_to_meetup',
    'open_to_meetup',
    'does_not_want_to_meetup'
);

CREATE TYPE public.messagetype AS ENUM (
    'text',
    'chat_created',
    'chat_edited',
    'user_invited',
    'user_left',
    'user_made_admin',
    'user_removed_admin',
    'host_request_status_changed',
    'user_removed'
);

CREATE TYPE public.moderationaction AS ENUM (
    'create',
    'approve',
    'hide',
    'flag',
    'unflag',
    'bulk_set_visibility',
    'set_priority'
);

CREATE TYPE public.moderationobjecttype AS ENUM (
    'host_request',
    'group_chat',
    'friend_request',
    'event_occurrence',
    'comment',
    'reply',
    'discussion',
    'reference'
);

CREATE TYPE public.moderationtrigger AS ENUM (
    'initial_review',
    'user_flag',
    'machine_flag',
    'moderator_review'
);

CREATE TYPE public.moderationvisibility AS ENUM (
    'hidden',
    'shadowed',
    'unlisted',
    'visible'
);

CREATE TYPE public.nodetype AS ENUM (
    'world',
    'macroregion',
    'region',
    'subregion',
    'locality',
    'sublocality'
);

CREATE TYPE public.nonvisibleuseraccesstype AS ENUM (
    'login_attempt',
    'profile_view',
    'ghost_served'
);

CREATE TYPE public.nonvisibleuserstate AS ENUM (
    'banned',
    'shadowed',
    'deleted'
);

CREATE TYPE public.notificationdeliverytype AS ENUM (
    'push',
    'email',
    'digest'
);

CREATE TYPE public.notificationtopicaction AS ENUM (
    'friend_request__create',
    'friend_request__accept',
    'host_request__create',
    'host_request__accept',
    'host_request__reject',
    'host_request__confirm',
    'host_request__cancel',
    'host_request__message',
    'password__change',
    'email_address__change',
    'phone_number__change',
    'phone_number__verify',
    'password_reset__start',
    'password_reset__complete',
    'gender__change',
    'birthdate__change',
    'api_key__create',
    'chat__message',
    'badge__add',
    'badge__remove',
    'event__create_any',
    'event__update',
    'event__invite_organizer',
    'event__create_approved',
    'email_address__verify',
    'donation__received',
    'account_deletion__start',
    'account_deletion__complete',
    'account_deletion__recovered',
    'reference__receive_friend',
    'reference__receive_hosted',
    'reference__receive_surfed',
    'reference__reminder_hosted',
    'reference__reminder_surfed',
    'event__cancel',
    'event__delete',
    'chat__missed_messages',
    'onboarding__reminder',
    'host_request__missed_messages',
    'modnote__create',
    'verification__sv_fail',
    'verification__sv_success',
    'event__comment',
    'discussion__create',
    'discussion__comment',
    'thread__reply',
    'activeness__probe',
    'general__new_blog_post',
    'host_request__reminder',
    'event__reminder',
    'postal_verification__postcard_sent',
    'postal_verification__success',
    'postal_verification__failed'
);

CREATE TYPE public.otaplatform AS ENUM (
    'ios',
    'android'
);

CREATE TYPE public.pagetype AS ENUM (
    'main_page',
    'place',
    'guide'
);

CREATE TYPE public.parkingdetails AS ENUM (
    'free_onsite',
    'free_offsite',
    'paid_onsite',
    'paid_offsite'
);

CREATE TYPE public.passportsex AS ENUM (
    'male',
    'female',
    'unspecified'
);

CREATE TYPE public.postalverificationstatus AS ENUM (
    'pending_address_confirmation',
    'in_progress',
    'awaiting_verification',
    'succeeded',
    'failed',
    'cancelled'
);

CREATE TYPE public.profilepublicvisibility AS ENUM (
    'nothing',
    'map_only',
    'limited',
    'most',
    'full'
);

CREATE TYPE public.publictripstatus AS ENUM (
    'searching_for_host',
    'closed'
);

CREATE TYPE public.pushnotificationdeliveryoutcome AS ENUM (
    'success',
    'transient_failure',
    'permanent_message_failure',
    'permanent_subscription_failure'
);

CREATE TYPE public.pushnotificationplatform AS ENUM (
    'web_push',
    'expo'
);

CREATE TYPE public.ratelimitaction AS ENUM (
    'host_request',
    'friend_request',
    'chat_initiation'
);

CREATE TYPE public.referencetype AS ENUM (
    'friend',
    'surfed',
    'hosted'
);

CREATE TYPE public.sleepingarrangement AS ENUM (
    'private',
    'common',
    'shared_room'
);

CREATE TYPE public.smokinglocation AS ENUM (
    'yes',
    'window',
    'outside',
    'no'
);

CREATE TYPE public.strongverificationattemptstatus AS ENUM (
    'succeeded',
    'in_progress_waiting_on_user_to_open_app',
    'in_progress_waiting_on_backend',
    'failed',
    'deleted',
    'in_progress_waiting_on_user_in_app',
    'duplicate'
);

CREATE FUNCTION public.immutable_unaccent(input_text text) RETURNS text
    LANGUAGE sql IMMUTABLE STRICT
    AS $$
        SELECT public.unaccent(input_text);
        $$;

CREATE FUNCTION public.slugify(text text) RETURNS text
    LANGUAGE sql IMMUTABLE STRICT
    AS $_$
    SELECT regexp_replace(
      regexp_replace(
        regexp_replace(
          substring(
            regexp_replace(
              lower(unaccent("text")),
              '[^a-z0-9]+', '-', 'gi'
            ) from 0 for 64
          ), '-$', ''
        ), '^-', ''
      ), '^$', 'slug'
    );
    $_$;



CREATE TABLE logging.api_calls (
    id bigint NOT NULL,
    version character varying NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    method character varying NOT NULL,
    status_code character varying,
    duration double precision NOT NULL,
    user_id bigint,
    request bytea,
    response bytea,
    traceback character varying,
    is_api_key boolean DEFAULT false NOT NULL,
    response_truncated boolean DEFAULT false NOT NULL,
    perf_report character varying,
    ip_address character varying,
    user_agent character varying,
    sofa character varying,
    db_query_count bigint,
    db_write_query_count bigint,
    db_time_ms double precision,
    cpu_ms double precision,
    client_platform public.clientplatform
);

CREATE SEQUENCE logging.api_calls_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE logging.api_calls_id_seq OWNED BY logging.api_calls.id;

CREATE TABLE logging.event_log (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    occurred timestamp with time zone DEFAULT now() NOT NULL,
    version character varying NOT NULL,
    sofa character varying,
    event_type character varying NOT NULL,
    user_id bigint,
    properties jsonb NOT NULL,
    value double precision DEFAULT '1'::double precision NOT NULL,
    source public.eventsource NOT NULL
);

CREATE SEQUENCE logging.event_log_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE logging.event_log_id_seq OWNED BY logging.event_log.id;

CREATE TABLE logging.experiment_exposures (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    version character varying NOT NULL,
    user_id bigint NOT NULL,
    experiment_key character varying NOT NULL,
    variation_id bigint NOT NULL,
    data jsonb NOT NULL,
    source public.exposuresource NOT NULL
);

CREATE SEQUENCE logging.experiment_exposures_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE logging.experiment_exposures_id_seq OWNED BY logging.experiment_exposures.id;

CREATE TABLE logging.feature_usage (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    feature_key character varying NOT NULL,
    value jsonb NOT NULL
);

CREATE SEQUENCE logging.feature_usage_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE logging.feature_usage_id_seq OWNED BY logging.feature_usage.id;

CREATE TABLE logging.nonvisible_user_access (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    version character varying NOT NULL,
    access_type public.nonvisibleuseraccesstype NOT NULL,
    target_user_id bigint NOT NULL,
    target_state public.nonvisibleuserstate NOT NULL,
    actor_user_id bigint,
    ip_address character varying,
    user_agent character varying,
    sofa character varying
);

CREATE SEQUENCE logging.nonvisible_user_access_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE logging.nonvisible_user_access_id_seq OWNED BY logging.nonvisible_user_access.id;

CREATE TABLE public.account_deletion_reason (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    reason character varying
);

CREATE SEQUENCE public.account_deletion_reason_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.account_deletion_reason_id_seq OWNED BY public.account_deletion_reason.id;

CREATE TABLE public.account_deletion_tokens (
    token character varying NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    expiry timestamp with time zone NOT NULL
);

CREATE TABLE public.activeness_probes (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    probe_initiated timestamp with time zone DEFAULT now() NOT NULL,
    notifications_sent integer DEFAULT 0 NOT NULL,
    responded timestamp with time zone,
    response public.activenessprobestatus NOT NULL,
    CONSTRAINT ck_activeness_probes_pending_has_no_responded CHECK ((((responded IS NULL) AND (response = 'pending'::public.activenessprobestatus)) OR ((responded IS NOT NULL) AND (response <> 'pending'::public.activenessprobestatus))))
);

CREATE SEQUENCE public.activeness_probes_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.activeness_probes_id_seq OWNED BY public.activeness_probes.id;

CREATE TABLE public.admin_actions (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    admin_user_id bigint NOT NULL,
    target_user_id bigint NOT NULL,
    action_type character varying NOT NULL,
    level public.adminactionlevel DEFAULT 'normal'::public.adminactionlevel NOT NULL,
    note character varying,
    tag character varying,
    data jsonb,
    CONSTRAINT ck_admin_actions_note_xor_data CHECK (((note IS NULL) OR (data IS NULL)))
);

CREATE SEQUENCE public.admin_actions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.admin_actions_id_seq OWNED BY public.admin_actions.id;

CREATE TABLE public.admin_tags (
    id bigint NOT NULL,
    tag character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.admin_tags_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.admin_tags_id_seq OWNED BY public.admin_tags.id;

CREATE TABLE public.alembic_version (
    version_num character varying(32) NOT NULL
);

CREATE TABLE public.antibot_logs (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint,
    ip_address character varying,
    user_agent character varying,
    action character varying NOT NULL,
    token character varying NOT NULL,
    score double precision NOT NULL,
    provider_data json NOT NULL
);

CREATE SEQUENCE public.antibot_logs_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.antibot_logs_id_seq OWNED BY public.antibot_logs.id;

CREATE TABLE public.background_jobs (
    id bigint NOT NULL,
    job_type character varying NOT NULL,
    state public.backgroundjobstate NOT NULL,
    queued timestamp with time zone DEFAULT now() NOT NULL,
    next_attempt_after timestamp with time zone DEFAULT now() NOT NULL,
    try_count integer NOT NULL,
    max_tries integer NOT NULL,
    payload bytea NOT NULL,
    failure_info character varying,
    priority integer DEFAULT 10 NOT NULL
);

CREATE SEQUENCE public.background_jobs_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.background_jobs_id_seq OWNED BY public.background_jobs.id;

CREATE TABLE public.cluster_subscriptions (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    cluster_id bigint NOT NULL,
    role public.clusterrole NOT NULL
);

CREATE TABLE public.users (
    id bigint NOT NULL,
    username character varying NOT NULL,
    email character varying NOT NULL,
    hashed_password bytea NOT NULL,
    phone character varying,
    geom public.geometry(Point,4326) NOT NULL,
    geom_radius double precision NOT NULL,
    city character varying NOT NULL,
    hometown character varying,
    joined timestamp with time zone DEFAULT now() NOT NULL,
    last_active timestamp with time zone DEFAULT now() NOT NULL,
    last_notified_message_id bigint NOT NULL,
    name character varying NOT NULL,
    gender character varying NOT NULL,
    pronouns character varying,
    birthdate date NOT NULL,
    hosting_status public.hostingstatus NOT NULL,
    meetup_status public.meetupstatus DEFAULT 'open_to_meetup'::public.meetupstatus NOT NULL,
    community_standing double precision,
    occupation character varying,
    education character varying,
    about_me character varying,
    things_i_like character varying,
    about_place character varying,
    additional_information character varying,
    max_guests integer,
    last_minute boolean,
    has_pets boolean,
    accepts_pets boolean,
    pet_details character varying,
    has_kids boolean,
    accepts_kids boolean,
    kid_details character varying,
    has_housemates boolean,
    housemate_details character varying,
    wheelchair_accessible boolean,
    smoking_allowed public.smokinglocation,
    smokes_at_home boolean,
    drinking_allowed boolean,
    drinks_at_home boolean,
    other_host_info character varying,
    sleeping_arrangement public.sleepingarrangement,
    sleeping_details character varying,
    area character varying,
    house_rules character varying,
    parking boolean,
    parking_details public.parkingdetails,
    camping_ok boolean,
    accepted_tos integer NOT NULL,
    new_email character varying,
    new_email_token character varying,
    new_email_token_created timestamp with time zone,
    new_email_token_expiry timestamp with time zone,
    filled_contributor_form boolean DEFAULT false NOT NULL,
    last_onboarding_email_sent timestamp with time zone,
    onboarding_emails_sent integer DEFAULT 0 NOT NULL,
    in_sync_with_newsletter boolean DEFAULT false NOT NULL,
    last_notified_request_message_id bigint DEFAULT 0 NOT NULL,
    phone_verification_attempts integer DEFAULT 0 NOT NULL,
    phone_verification_sent timestamp with time zone DEFAULT to_timestamp((0)::double precision) NOT NULL,
    phone_verification_token character varying(6) DEFAULT NULL::character varying,
    phone_verification_verified timestamp with time zone,
    is_superuser boolean DEFAULT false NOT NULL,
    stripe_customer_id_old character varying,
    accepted_community_guidelines integer DEFAULT 0 NOT NULL,
    stripe_customer_id character varying,
    last_digest_sent timestamp with time zone DEFAULT to_timestamp((0)::double precision) NOT NULL,
    undelete_token character varying,
    undelete_until timestamp with time zone,
    recommendation_score double precision DEFAULT '0'::double precision NOT NULL,
    has_passport_sex_gender_exception boolean DEFAULT false NOT NULL,
    admin_note character varying DEFAULT ''::character varying NOT NULL,
    opt_out_of_newsletter boolean DEFAULT false NOT NULL,
    digest_frequency interval,
    do_not_email boolean DEFAULT false NOT NULL,
    ui_language_preference character varying DEFAULT ''::character varying,
    public_visibility public.profilepublicvisibility DEFAULT 'map_only'::public.profilepublicvisibility NOT NULL,
    has_modified_public_visibility boolean DEFAULT false NOT NULL,
    randomized_geom public.geometry(Point,4326),
    needs_to_update_location boolean DEFAULT false NOT NULL,
    last_antibot timestamp with time zone DEFAULT to_timestamp((0)::double precision) NOT NULL,
    invite_code_id character varying,
    last_donated timestamp with time zone,
    is_editor boolean DEFAULT false NOT NULL,
    profile_gallery_id bigint,
    profile_last_updated timestamp with time zone DEFAULT now() NOT NULL,
    banned_at timestamp with time zone,
    deleted_at timestamp with time zone,
    heard_about_couchers character varying,
    signup_motivations character varying[],
    mod_score double precision DEFAULT '1'::double precision NOT NULL,
    shadowed_at timestamp with time zone,
    CONSTRAINT ck_users_check_new_email_token_state CHECK ((((new_email_token IS NOT NULL) AND (new_email_token_created IS NOT NULL) AND (new_email_token_expiry IS NOT NULL)) OR ((new_email_token IS NULL) AND (new_email_token_created IS NULL) AND (new_email_token_expiry IS NULL)))),
    CONSTRAINT ck_users_do_not_email_inactive CHECK (((do_not_email IS FALSE) OR ((hosting_status = 'cant_host'::public.hostingstatus) AND (meetup_status = 'does_not_want_to_meetup'::public.meetupstatus)))),
    CONSTRAINT ck_users_phone_verified_conditions CHECK ((((((phone IS NULL))::integer + ((phone_verification_verified IS NOT NULL))::integer) + ((phone_verification_token IS NOT NULL))::integer) = 1)),
    CONSTRAINT ck_users_superuser_is_editor CHECK (((is_superuser IS FALSE) OR (is_editor IS TRUE))),
    CONSTRAINT ck_users_undelete_nullity CHECK ((((undelete_token IS NULL) = (undelete_until IS NULL)) AND ((undelete_token IS NULL) OR (deleted_at IS NOT NULL)))),
    CONSTRAINT ck_users_valid_email CHECK (((email)::text ~ '^[0-9a-z]([0-9a-z\-\_\+]|(\.[0-9a-z\-\_\+]))*@([0-9a-z\-]+\.)*[0-9a-z\-]+\.[a-z]{2,}$'::text))
);

CREATE MATERIALIZED VIEW public.cluster_admin_counts AS
 SELECT cluster_subscriptions.cluster_id,
    count(*) AS count
   FROM (public.cluster_subscriptions
     LEFT JOIN public.users ON ((users.id = cluster_subscriptions.user_id)))
  WHERE ((cluster_subscriptions.role = 'admin'::public.clusterrole) AND (users.banned_at IS NULL) AND (users.deleted_at IS NULL))
  GROUP BY cluster_subscriptions.cluster_id
  WITH NO DATA;

CREATE TABLE public.cluster_discussion_associations (
    id bigint NOT NULL,
    discussion_id bigint NOT NULL,
    cluster_id bigint NOT NULL
);

CREATE SEQUENCE public.cluster_discussion_associations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.cluster_discussion_associations_id_seq OWNED BY public.cluster_discussion_associations.id;

CREATE TABLE public.cluster_event_associations (
    id bigint NOT NULL,
    event_id bigint NOT NULL,
    cluster_id bigint NOT NULL
);

CREATE SEQUENCE public.cluster_event_associations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.cluster_event_associations_id_seq OWNED BY public.cluster_event_associations.id;

CREATE TABLE public.cluster_page_associations (
    id bigint NOT NULL,
    page_id bigint NOT NULL,
    cluster_id bigint NOT NULL
);

CREATE SEQUENCE public.cluster_page_associations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.cluster_page_associations_id_seq OWNED BY public.cluster_page_associations.id;

CREATE MATERIALIZED VIEW public.cluster_subscription_counts AS
 SELECT cluster_subscriptions.cluster_id,
    count(*) AS count
   FROM (public.cluster_subscriptions
     LEFT JOIN public.users ON ((users.id = cluster_subscriptions.user_id)))
  WHERE ((users.banned_at IS NULL) AND (users.deleted_at IS NULL))
  GROUP BY cluster_subscriptions.cluster_id
  WITH NO DATA;

CREATE SEQUENCE public.cluster_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.cluster_subscriptions_id_seq OWNED BY public.cluster_subscriptions.id;

CREATE MATERIALIZED VIEW public.clustered_users AS
 WITH clustered AS (
         SELECT users.id,
            users.geom,
            public.st_clusterdbscan(users.geom, (0.15)::double precision, 5) OVER (ORDER BY users.id) AS cluster_id
           FROM public.users
          WHERE ((users.banned_at IS NULL) AND (users.deleted_at IS NULL))
        )
 SELECT public.st_centroid(public.st_collect(clustered.geom)) AS geom,
    count(*) AS count
   FROM clustered
  WHERE (clustered.cluster_id IS NOT NULL)
  GROUP BY clustered.cluster_id
UNION ALL
 SELECT clustered.geom,
    1 AS count
   FROM clustered
  WHERE (clustered.cluster_id IS NULL)
  WITH NO DATA;

CREATE SEQUENCE public.communities_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE TABLE public.clusters (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    parent_node_id bigint NOT NULL,
    name character varying NOT NULL,
    description character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    is_official_cluster boolean NOT NULL,
    small_community_features_enabled boolean DEFAULT true NOT NULL
);

CREATE TABLE public.comment_versions (
    id bigint NOT NULL,
    comment_id bigint NOT NULL,
    editor_user_id bigint NOT NULL,
    change_type public.contentchangetype NOT NULL,
    old_content character varying,
    new_content character varying,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.comment_versions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.comment_versions_id_seq OWNED BY public.comment_versions.id;

CREATE TABLE public.comments (
    id bigint NOT NULL,
    thread_id bigint NOT NULL,
    author_user_id bigint NOT NULL,
    content character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    deleted timestamp with time zone,
    moderation_state_id bigint NOT NULL,
    last_edited timestamp with time zone
);

CREATE SEQUENCE public.comments_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.comments_id_seq OWNED BY public.comments.id;

CREATE TABLE public.content_reports (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    reporting_user_id bigint NOT NULL,
    reason character varying NOT NULL,
    description character varying NOT NULL,
    content_ref character varying NOT NULL,
    author_user_id bigint NOT NULL,
    user_agent character varying NOT NULL,
    page character varying NOT NULL
);

CREATE SEQUENCE public.content_reports_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.content_reports_id_seq OWNED BY public.content_reports.id;

CREATE TABLE public.contributor_forms (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    ideas character varying,
    features character varying,
    experience character varying,
    contribute public.contributeoption,
    contribute_ways character varying[] NOT NULL,
    expertise character varying,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.contributor_forms_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.contributor_forms_id_seq OWNED BY public.contributor_forms.id;

CREATE TABLE public.conversations (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.conversations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.conversations_id_seq OWNED BY public.conversations.id;

CREATE TABLE public.discussion_subscriptions (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    discussion_id bigint NOT NULL,
    joined timestamp with time zone DEFAULT now() NOT NULL,
    "left" timestamp with time zone
);

CREATE SEQUENCE public.discussion_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.discussion_subscriptions_id_seq OWNED BY public.discussion_subscriptions.id;

CREATE TABLE public.discussion_versions (
    id bigint NOT NULL,
    discussion_id bigint NOT NULL,
    editor_user_id bigint NOT NULL,
    change_type public.contentchangetype NOT NULL,
    old_title character varying,
    new_title character varying,
    old_content character varying,
    new_content character varying,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.discussion_versions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.discussion_versions_id_seq OWNED BY public.discussion_versions.id;

CREATE TABLE public.discussions (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    title character varying NOT NULL,
    content character varying NOT NULL,
    thread_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    creator_user_id bigint NOT NULL,
    owner_cluster_id bigint NOT NULL,
    moderation_state_id bigint NOT NULL,
    deleted timestamp with time zone,
    last_edited timestamp with time zone
);

CREATE TABLE public.donation_initiations (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    amount integer NOT NULL,
    stripe_checkout_session_id character varying NOT NULL,
    donation_type public.donationtype NOT NULL,
    source character varying
);

CREATE SEQUENCE public.donation_initiations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.donation_initiations_id_seq OWNED BY public.donation_initiations.id;

CREATE TABLE public.emails (
    id character varying NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    sender_name character varying NOT NULL,
    sender_email character varying NOT NULL,
    recipient character varying NOT NULL,
    subject character varying NOT NULL,
    plain character varying NOT NULL,
    html character varying NOT NULL,
    list_unsubscribe_header character varying,
    source_data character varying
);

CREATE TABLE public.event_community_invite_requests (
    id bigint NOT NULL,
    occurrence_id bigint NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    decided timestamp with time zone,
    decided_by_user_id bigint,
    approved boolean,
    CONSTRAINT ck_event_community_invite_requests_decided_approved CHECK ((((decided IS NULL) AND (decided_by_user_id IS NULL) AND (approved IS NULL)) OR ((decided IS NOT NULL) AND (decided_by_user_id IS NOT NULL) AND (approved IS NOT NULL))))
);

CREATE SEQUENCE public.event_community_invite_requests_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.event_community_invite_requests_id_seq OWNED BY public.event_community_invite_requests.id;

CREATE TABLE public.event_occurrence_attendees (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    occurrence_id bigint NOT NULL,
    responded timestamp with time zone DEFAULT now() NOT NULL,
    attendee_status public.attendeestatus NOT NULL,
    reminder_sent boolean DEFAULT false NOT NULL
);

CREATE SEQUENCE public.event_occurrence_attendees_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.event_occurrence_attendees_id_seq OWNED BY public.event_occurrence_attendees.id;

CREATE TABLE public.event_occurrences (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    event_id bigint NOT NULL,
    creator_user_id bigint NOT NULL,
    content character varying NOT NULL,
    photo_key character varying,
    geom public.geometry(Point,4326),
    address character varying,
    link character varying,
    during tstzrange NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    last_edited timestamp with time zone DEFAULT now() NOT NULL,
    is_cancelled boolean DEFAULT false NOT NULL,
    is_deleted boolean DEFAULT false NOT NULL,
    moderation_state_id bigint NOT NULL,
    CONSTRAINT ck_event_occurrences_geom_iff_address CHECK (((geom IS NULL) = (address IS NULL))),
    CONSTRAINT ck_event_occurrences_link_or_geom CHECK (((geom IS NULL) <> (link IS NULL)))
);

CREATE TABLE public.event_organizers (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    event_id bigint NOT NULL,
    joined timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.event_organizers_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.event_organizers_id_seq OWNED BY public.event_organizers.id;

CREATE TABLE public.event_subscriptions (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    event_id bigint NOT NULL,
    joined timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.event_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.event_subscriptions_id_seq OWNED BY public.event_subscriptions.id;

CREATE TABLE public.events (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    parent_node_id bigint NOT NULL,
    title character varying NOT NULL,
    creator_user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    owner_user_id bigint,
    owner_cluster_id bigint,
    thread_id bigint NOT NULL,
    CONSTRAINT ck_events_one_owner CHECK (((owner_user_id IS NULL) <> (owner_cluster_id IS NULL)))
);

CREATE TABLE public.friend_relationships (
    id bigint NOT NULL,
    from_user_id bigint NOT NULL,
    to_user_id bigint NOT NULL,
    status public.friendstatus NOT NULL,
    time_sent timestamp with time zone DEFAULT now() NOT NULL,
    time_responded timestamp with time zone,
    moderation_state_id bigint NOT NULL
);

CREATE SEQUENCE public.friend_relationships_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.friend_relationships_id_seq OWNED BY public.friend_relationships.id;

CREATE TABLE public.group_chat_subscriptions (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    group_chat_id bigint NOT NULL,
    joined timestamp with time zone DEFAULT now() NOT NULL,
    "left" timestamp with time zone,
    role public.groupchatrole NOT NULL,
    last_seen_message_id bigint NOT NULL,
    muted_until timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
    is_archived boolean DEFAULT false NOT NULL
);

CREATE SEQUENCE public.group_chat_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.group_chat_subscriptions_id_seq OWNED BY public.group_chat_subscriptions.id;

CREATE TABLE public.group_chats (
    id bigint NOT NULL,
    title character varying,
    only_admins_invite boolean NOT NULL,
    creator_id bigint NOT NULL,
    is_dm boolean NOT NULL,
    moderation_state_id bigint NOT NULL
);

CREATE TABLE public.host_request_feedbacks (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    host_request_id bigint NOT NULL,
    from_user_id bigint NOT NULL,
    to_user_id bigint NOT NULL,
    request_quality public.hostrequestquality,
    decline_reason character varying
);

CREATE SEQUENCE public.host_request_feedbacks_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.host_request_feedbacks_id_seq OWNED BY public.host_request_feedbacks.id;

CREATE TABLE public.host_requests (
    id bigint NOT NULL,
    initiator_user_id bigint NOT NULL,
    recipient_user_id bigint NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    status public.hostrequeststatus NOT NULL,
    recipient_last_seen_message_id bigint NOT NULL,
    initiator_last_seen_message_id bigint NOT NULL,
    recipient_sent_reference_reminders bigint DEFAULT 0 NOT NULL,
    initiator_sent_reference_reminders bigint DEFAULT 0 NOT NULL,
    recipient_reason_didnt_meetup character varying,
    initiator_reason_didnt_meetup character varying,
    recipient_sent_request_reminders bigint DEFAULT 0 NOT NULL,
    last_sent_request_reminder_time timestamp with time zone DEFAULT now() NOT NULL,
    is_recipient_archived boolean DEFAULT false NOT NULL,
    is_initiator_archived boolean DEFAULT false NOT NULL,
    hosting_city character varying NOT NULL,
    hosting_location public.geometry(Point,4326) NOT NULL,
    hosting_radius double precision NOT NULL,
    moderation_state_id bigint NOT NULL,
    public_trip_id bigint
);

CREATE TABLE public.initiated_uploads (
    key character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    expiry timestamp with time zone NOT NULL,
    initiator_user_id bigint NOT NULL
);

CREATE TABLE public.invite_codes (
    id character varying NOT NULL,
    created timestamp with time zone NOT NULL,
    disabled timestamp with time zone,
    creator_user_id integer NOT NULL
);

CREATE TABLE public.invoices (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    amount double precision NOT NULL,
    stripe_payment_intent_id character varying NOT NULL,
    stripe_receipt_url character varying NOT NULL,
    invoice_type public.invoicetype NOT NULL
);

CREATE SEQUENCE public.invoices_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.invoices_id_seq OWNED BY public.invoices.id;

CREATE TABLE public.language_abilities (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    language_code character varying(3) NOT NULL,
    fluency public.languagefluency NOT NULL
);

CREATE SEQUENCE public.language_abilities_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.language_abilities_id_seq OWNED BY public.language_abilities.id;

CREATE TABLE public.languages (
    code character varying(3) NOT NULL,
    name character varying NOT NULL
);

CREATE TABLE public.photo_gallery_items (
    id bigint NOT NULL,
    gallery_id bigint NOT NULL,
    upload_key character varying NOT NULL,
    "position" double precision NOT NULL,
    caption character varying,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE public.strong_verification_attempts (
    id bigint NOT NULL,
    verification_attempt_token character varying CONSTRAINT strong_verification_attempt_verification_attempt_token_not_null NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    status public.strongverificationattemptstatus NOT NULL,
    has_full_data boolean NOT NULL,
    passport_encrypted_data bytea,
    passport_date_of_birth date,
    passport_sex public.passportsex,
    has_minimal_data boolean NOT NULL,
    passport_expiry_date date,
    passport_nationality character varying,
    passport_last_three_document_chars character varying,
    iris_token character varying NOT NULL,
    iris_session_id bigint NOT NULL,
    CONSTRAINT ck_strong_verification_attempts_deleted_duplicate_impli_bf37 CHECK (((NOT ((status = 'deleted'::public.strongverificationattemptstatus) OR (status = 'duplicate'::public.strongverificationattemptstatus))) OR (has_minimal_data IS TRUE))),
    CONSTRAINT ck_strong_verification_attempts_full_data_implies_minimal_data CHECK (((has_full_data IS FALSE) OR (has_minimal_data IS TRUE))),
    CONSTRAINT ck_strong_verification_attempts_full_data_status CHECK ((((has_full_data IS TRUE) AND (passport_encrypted_data IS NOT NULL) AND (passport_date_of_birth IS NOT NULL)) OR ((has_full_data IS FALSE) AND (passport_encrypted_data IS NULL) AND (passport_date_of_birth IS NULL)))),
    CONSTRAINT ck_strong_verification_attempts_in_progress_failed_iris_8180 CHECK (((NOT ((status = 'in_progress_waiting_on_user_to_open_app'::public.strongverificationattemptstatus) OR (status = 'in_progress_waiting_on_user_in_app'::public.strongverificationattemptstatus) OR (status = 'in_progress_waiting_on_backend'::public.strongverificationattemptstatus) OR (status = 'failed'::public.strongverificationattemptstatus))) OR (has_minimal_data IS FALSE))),
    CONSTRAINT ck_strong_verification_attempts_minimal_data_status CHECK ((((has_minimal_data IS TRUE) AND (passport_expiry_date IS NOT NULL) AND (passport_nationality IS NOT NULL) AND (passport_last_three_document_chars IS NOT NULL)) OR ((has_minimal_data IS FALSE) AND (passport_expiry_date IS NULL) AND (passport_nationality IS NULL) AND (passport_last_three_document_chars IS NULL)))),
    CONSTRAINT ck_strong_verification_attempts_succeeded_implies_full_data CHECK (((NOT (status = 'succeeded'::public.strongverificationattemptstatus)) OR (has_full_data IS TRUE)))
);

CREATE TABLE public.uploads (
    key character varying NOT NULL,
    filename character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    creator_user_id bigint NOT NULL,
    credit character varying
);

CREATE MATERIALIZED VIEW public.lite_users AS
 SELECT users.id,
    users.username,
    users.name,
    users.city,
    date_part('year'::text, age((users.birthdate)::timestamp with time zone)) AS age,
    users.geom,
    users.geom_radius AS radius,
    ((users.banned_at IS NULL) AND (users.deleted_at IS NULL)) AS is_visible,
    users.shadowed_at,
    uploads.filename AS avatar_filename,
    ((users.profile_gallery_id IS NOT NULL) AND (EXISTS ( SELECT 1 AS anon_1
           FROM public.photo_gallery_items
          WHERE (photo_gallery_items.gallery_id = users.profile_gallery_id))) AND (COALESCE(character_length((users.about_me)::text), 0) >= 150)) AS has_completed_profile,
    ((users.max_guests IS NOT NULL) AND (users.sleeping_arrangement IS NOT NULL) AND ((users.about_place IS NOT NULL) OR (users.other_host_info IS NOT NULL) OR (users.sleeping_details IS NOT NULL) OR (users.area IS NOT NULL) OR (users.house_rules IS NOT NULL))) AS has_completed_my_home,
    COALESCE(sv_subquery."true", false) AS has_strong_verification,
    (json_build_object('type', 'Feature', 'geometry', (public.st_asgeojson(users.geom, 5))::json, 'properties', json_build_object('id', users.id, 'has_completed_profile', ((users.profile_gallery_id IS NOT NULL) AND (EXISTS ( SELECT 1 AS anon_2
           FROM public.photo_gallery_items
          WHERE (photo_gallery_items.gallery_id = users.profile_gallery_id))) AND (COALESCE(character_length((users.about_me)::text), 0) >= 150)))))::text AS geojson
   FROM (((public.users
     LEFT JOIN ( SELECT DISTINCT ON (photo_gallery_items.gallery_id) photo_gallery_items.gallery_id,
            photo_gallery_items.upload_key
           FROM public.photo_gallery_items
          ORDER BY photo_gallery_items.gallery_id, photo_gallery_items."position") avatar_photo ON ((avatar_photo.gallery_id = users.profile_gallery_id)))
     LEFT JOIN public.uploads ON (((uploads.key)::text = (avatar_photo.upload_key)::text)))
     LEFT JOIN ( SELECT DISTINCT users_1.id,
            true AS "true"
           FROM public.strong_verification_attempts,
            public.users users_1
          WHERE ((strong_verification_attempts.status = 'succeeded'::public.strongverificationattemptstatus) AND COALESCE((timezone('Etc/UTC'::text, (strong_verification_attempts.passport_expiry_date)::timestamp without time zone) >= now()), false) AND (strong_verification_attempts.passport_date_of_birth = users_1.birthdate) AND ((((users_1.gender)::text = 'Woman'::text) AND (strong_verification_attempts.passport_sex = 'female'::public.passportsex)) OR (((users_1.gender)::text = 'Man'::text) AND (strong_verification_attempts.passport_sex = 'male'::public.passportsex)) OR (strong_verification_attempts.passport_sex = 'unspecified'::public.passportsex) OR (users_1.has_passport_sex_gender_exception = true)))) sv_subquery ON ((sv_subquery.id = users.id)))
  WITH NO DATA;

CREATE TABLE public.login_tokens (
    token character varying NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    expiry timestamp with time zone NOT NULL
);

CREATE TABLE public.messages (
    id bigint NOT NULL,
    conversation_id bigint NOT NULL,
    author_id bigint NOT NULL,
    message_type public.messagetype NOT NULL,
    target_id bigint,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    text character varying,
    host_request_status_target public.hostrequeststatus
);

CREATE SEQUENCE public.messages_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.messages_id_seq OWNED BY public.messages.id;

CREATE TABLE public.mod_notes (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    acknowledged timestamp with time zone,
    internal_id character varying NOT NULL,
    creator_user_id bigint NOT NULL,
    note_content character varying NOT NULL
);

CREATE SEQUENCE public.mod_notes_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.mod_notes_id_seq OWNED BY public.mod_notes.id;

CREATE SEQUENCE public.moderation_seq
    START WITH 2000000
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE TABLE public.moderation_log (
    id bigint DEFAULT nextval('public.moderation_seq'::regclass) NOT NULL,
    moderation_state_id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    action public.moderationaction NOT NULL,
    moderator_user_id bigint NOT NULL,
    new_visibility public.moderationvisibility,
    reason character varying NOT NULL,
    new_priority integer,
    queue_item_id bigint
);

CREATE TABLE public.moderation_queue (
    id bigint DEFAULT nextval('public.moderation_seq'::regclass) NOT NULL,
    moderation_state_id bigint NOT NULL,
    time_created timestamp with time zone DEFAULT now() NOT NULL,
    trigger public.moderationtrigger NOT NULL,
    reason character varying NOT NULL,
    resolved_by_log_id bigint,
    priority integer DEFAULT 0 NOT NULL
);

CREATE TABLE public.moderation_states (
    id bigint DEFAULT nextval('public.moderation_seq'::regclass) NOT NULL,
    object_type public.moderationobjecttype NOT NULL,
    object_id bigint NOT NULL,
    visibility public.moderationvisibility NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    updated timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE public.moderation_user_list_members (
    user_id bigint NOT NULL,
    moderation_list_id bigint NOT NULL
);

CREATE TABLE public.moderation_user_lists (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.moderation_user_lists_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.moderation_user_lists_id_seq OWNED BY public.moderation_user_lists.id;

CREATE TABLE public.native_client_users (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    eas_client_id uuid NOT NULL,
    user_id bigint NOT NULL
);

CREATE SEQUENCE public.native_client_users_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.native_client_users_id_seq OWNED BY public.native_client_users.id;

CREATE TABLE public.node_cluster_associations (
    id bigint NOT NULL,
    node_id bigint NOT NULL,
    cluster_id bigint NOT NULL
);

CREATE SEQUENCE public.node_cluster_associations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.node_cluster_associations_id_seq OWNED BY public.node_cluster_associations.id;

CREATE TABLE public.nodes (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    parent_node_id bigint,
    geom public.geometry(MultiPolygon,4326) NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    node_type public.nodetype NOT NULL
);

CREATE TABLE public.notification_deliveries (
    id bigint NOT NULL,
    notification_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    delivered timestamp with time zone,
    read timestamp with time zone,
    delivery_type public.notificationdeliverytype NOT NULL
);

CREATE SEQUENCE public.notification_deliveries_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.notification_deliveries_id_seq OWNED BY public.notification_deliveries.id;

CREATE TABLE public.notification_preferences (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    topic_action public.notificationtopicaction NOT NULL,
    delivery_type public.notificationdeliverytype NOT NULL,
    deliver boolean NOT NULL
);

CREATE SEQUENCE public.notification_preferences_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.notification_preferences_id_seq OWNED BY public.notification_preferences.id;

CREATE TABLE public.notifications (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    topic_action public.notificationtopicaction NOT NULL,
    key character varying NOT NULL,
    data bytea NOT NULL,
    is_seen boolean DEFAULT false NOT NULL,
    moderation_state_id bigint
);

CREATE SEQUENCE public.notifications_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.notifications_id_seq OWNED BY public.notifications.id;

CREATE TABLE public.ota_packages (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    creator_user_id bigint NOT NULL,
    platform public.otaplatform NOT NULL,
    fingerprint character varying NOT NULL,
    version character varying NOT NULL,
    manifest_created_at timestamp with time zone NOT NULL,
    manifest_id character varying NOT NULL,
    banned_at timestamp with time zone,
    banned_by_user_id bigint,
    banned_reason character varying,
    CONSTRAINT ck_ota_packages_ck_ota_packages_ban_columns_consistent CHECK ((((banned_at IS NULL) AND (banned_by_user_id IS NULL) AND (banned_reason IS NULL)) OR ((banned_at IS NOT NULL) AND (banned_by_user_id IS NOT NULL) AND (banned_reason IS NOT NULL))))
);

CREATE SEQUENCE public.ota_packages_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.ota_packages_id_seq OWNED BY public.ota_packages.id;

CREATE TABLE public.page_versions (
    id bigint NOT NULL,
    page_id bigint NOT NULL,
    editor_user_id bigint NOT NULL,
    title character varying NOT NULL,
    content character varying NOT NULL,
    photo_key character varying,
    address character varying,
    geom public.geometry(Point,4326),
    created timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT ck_page_versions_geom_iff_address CHECK (((geom IS NULL) = (address IS NULL)))
);

CREATE SEQUENCE public.page_versions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.page_versions_id_seq OWNED BY public.page_versions.id;

CREATE TABLE public.pages (
    id bigint DEFAULT nextval('public.communities_seq'::regclass) NOT NULL,
    parent_node_id bigint NOT NULL,
    type public.pagetype NOT NULL,
    creator_user_id bigint NOT NULL,
    owner_user_id bigint,
    owner_cluster_id bigint,
    thread_id bigint NOT NULL,
    CONSTRAINT ck_pages_main_page_owned_by_cluster CHECK ((NOT ((owner_cluster_id IS NULL) AND (type = 'main_page'::public.pagetype)))),
    CONSTRAINT ck_pages_one_owner CHECK (((owner_user_id IS NULL) <> (owner_cluster_id IS NULL)))
);

CREATE TABLE public.password_reset_tokens (
    token character varying NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    expiry timestamp with time zone NOT NULL
);

CREATE TABLE public.photo_galleries (
    id bigint NOT NULL,
    owner_user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    last_updated timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.photo_galleries_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.photo_galleries_id_seq OWNED BY public.photo_galleries.id;

CREATE SEQUENCE public.photo_gallery_items_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.photo_gallery_items_id_seq OWNED BY public.photo_gallery_items.id;

CREATE TABLE public.postal_verification_attempts (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    status public.postalverificationstatus NOT NULL,
    address_line_1 character varying NOT NULL,
    address_line_2 character varying,
    city character varying NOT NULL,
    state character varying,
    postal_code character varying,
    country_code character varying NOT NULL,
    original_address_json character varying,
    verification_code character varying,
    address_confirmed_at timestamp with time zone,
    postcard_sent_at timestamp with time zone,
    verified_at timestamp with time zone,
    code_attempts integer DEFAULT 0 NOT NULL,
    mypostcard_job_id integer,
    CONSTRAINT ck_postal_verification_attempts_postal_verification_code_status CHECK ((((status = ANY (ARRAY['pending_address_confirmation'::public.postalverificationstatus, 'cancelled'::public.postalverificationstatus])) AND (verification_code IS NULL)) OR ((status = ANY (ARRAY['in_progress'::public.postalverificationstatus, 'awaiting_verification'::public.postalverificationstatus, 'succeeded'::public.postalverificationstatus, 'failed'::public.postalverificationstatus])) AND (verification_code IS NOT NULL)))),
    CONSTRAINT ck_postal_verification_attempts_postal_verification_pos_aeb2 CHECK (((status <> ALL (ARRAY['awaiting_verification'::public.postalverificationstatus, 'succeeded'::public.postalverificationstatus])) OR (postcard_sent_at IS NOT NULL))),
    CONSTRAINT ck_postal_verification_attempts_postal_verification_ver_323a CHECK (((status <> 'succeeded'::public.postalverificationstatus) OR (verified_at IS NOT NULL)))
);

CREATE SEQUENCE public.postal_verification_attempts_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.postal_verification_attempts_id_seq OWNED BY public.postal_verification_attempts.id;

CREATE TABLE public.public_trips (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    node_id bigint NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    description character varying NOT NULL,
    status public.publictripstatus NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    same_gender_only boolean DEFAULT false NOT NULL,
    CONSTRAINT ck_public_trips_valid_date_range CHECK ((from_date <= to_date))
);

CREATE SEQUENCE public.public_trips_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.public_trips_id_seq OWNED BY public.public_trips.id;

CREATE TABLE public.push_notification_delivery_attempt (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    push_notification_subscription_id bigint CONSTRAINT push_notification_delivery__push_notification_subscrip_not_null NOT NULL,
    status_code integer,
    response character varying,
    outcome public.pushnotificationdeliveryoutcome NOT NULL,
    expo_ticket_id character varying,
    receipt_checked_at timestamp with time zone,
    receipt_status character varying,
    receipt_error_code character varying
);

CREATE SEQUENCE public.push_notification_delivery_attempt_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.push_notification_delivery_attempt_id_seq OWNED BY public.push_notification_delivery_attempt.id;

CREATE TABLE public.push_notification_subscriptions (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    endpoint character varying,
    auth_key bytea,
    p256dh_key bytea,
    full_subscription_info character varying,
    disabled_at timestamp with time zone DEFAULT '9876-12-31 23:59:59+00'::timestamp with time zone NOT NULL,
    user_agent character varying,
    platform public.pushnotificationplatform NOT NULL,
    token character varying,
    device_name character varying,
    device_type public.devicetype,
    CONSTRAINT ck_push_notification_subscriptions_platform_columns CHECK ((((platform = 'web_push'::public.pushnotificationplatform) AND (endpoint IS NOT NULL) AND (auth_key IS NOT NULL) AND (p256dh_key IS NOT NULL) AND (full_subscription_info IS NOT NULL) AND (token IS NULL)) OR ((platform = 'expo'::public.pushnotificationplatform) AND (token IS NOT NULL) AND (endpoint IS NULL) AND (auth_key IS NULL) AND (p256dh_key IS NULL) AND (full_subscription_info IS NULL))))
);

CREATE SEQUENCE public.push_notification_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.push_notification_subscriptions_id_seq OWNED BY public.push_notification_subscriptions.id;

CREATE TABLE public.rate_limit_violations (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    user_id bigint NOT NULL,
    action public.ratelimitaction NOT NULL,
    is_hard_limit boolean NOT NULL
);

CREATE SEQUENCE public.rate_limit_violations_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.rate_limit_violations_id_seq OWNED BY public.rate_limit_violations.id;

CREATE TABLE public."references" (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    from_user_id bigint NOT NULL,
    to_user_id bigint NOT NULL,
    reference_type public.referencetype NOT NULL,
    host_request_id bigint,
    text character varying NOT NULL,
    rating double precision NOT NULL,
    was_appropriate boolean NOT NULL,
    private_text character varying,
    moderation_state_id bigint NOT NULL,
    CONSTRAINT ck_references_host_request_id_xor_friend_reference CHECK (((host_request_id IS NOT NULL) <> (reference_type = 'friend'::public.referencetype))),
    CONSTRAINT ck_references_rating_between_0_and_1 CHECK (((rating >= (0)::double precision) AND (rating <= (1)::double precision)))
);

CREATE SEQUENCE public.references_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.references_id_seq OWNED BY public."references".id;

CREATE TABLE public.regions (
    code character varying(3) NOT NULL,
    name character varying NOT NULL
);

CREATE TABLE public.regions_lived (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    region_code character varying(3) NOT NULL
);

CREATE SEQUENCE public.regions_lived_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.regions_lived_id_seq OWNED BY public.regions_lived.id;

CREATE TABLE public.regions_visited (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    region_code character varying(3) NOT NULL
);

CREATE SEQUENCE public.regions_visited_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.regions_visited_id_seq OWNED BY public.regions_visited.id;

CREATE TABLE public.replies (
    id bigint NOT NULL,
    comment_id bigint NOT NULL,
    author_user_id bigint NOT NULL,
    content character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    deleted timestamp with time zone,
    moderation_state_id bigint NOT NULL,
    last_edited timestamp with time zone
);

CREATE SEQUENCE public.replies_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.replies_id_seq OWNED BY public.replies.id;

CREATE TABLE public.reply_versions (
    id bigint NOT NULL,
    reply_id bigint NOT NULL,
    editor_user_id bigint NOT NULL,
    change_type public.contentchangetype NOT NULL,
    old_content character varying,
    new_content character varying,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.reply_versions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.reply_versions_id_seq OWNED BY public.reply_versions.id;

CREATE TABLE public.sessions (
    token character varying NOT NULL,
    user_id bigint NOT NULL,
    long_lived boolean NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    expiry timestamp with time zone DEFAULT (now() + '730 days'::interval) NOT NULL,
    deleted timestamp with time zone,
    last_seen timestamp with time zone DEFAULT now() NOT NULL,
    api_calls integer NOT NULL,
    ip_address character varying,
    user_agent character varying,
    is_api_key boolean DEFAULT false NOT NULL
);

CREATE TABLE public.signup_flows (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    flow_token character varying NOT NULL,
    email_verified boolean NOT NULL,
    email_sent boolean NOT NULL,
    email_token character varying,
    email_token_expiry timestamp with time zone,
    name character varying NOT NULL,
    email character varying NOT NULL,
    username character varying,
    hashed_password bytea,
    birthdate date,
    gender character varying,
    hosting_status public.hostingstatus,
    city character varying,
    geom public.geometry(Point,4326),
    geom_radius double precision,
    accepted_tos integer,
    filled_feedback boolean NOT NULL,
    ideas character varying,
    features character varying,
    experience character varying,
    contribute public.contributeoption,
    contribute_ways character varying[],
    expertise character varying,
    accepted_community_guidelines integer DEFAULT 0 NOT NULL,
    opt_out_of_newsletter boolean,
    invite_code_id character varying,
    filled_motivations boolean DEFAULT false NOT NULL,
    heard_about_couchers character varying,
    signup_motivations character varying[] DEFAULT '{}'::character varying[] NOT NULL
);

CREATE SEQUENCE public.signup_flows_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.signup_flows_id_seq OWNED BY public.signup_flows.id;

CREATE TABLE public.smss (
    id bigint NOT NULL,
    "time" timestamp with time zone DEFAULT now() NOT NULL,
    message_id character varying NOT NULL,
    sms_sender_id character varying NOT NULL,
    number character varying NOT NULL,
    message character varying NOT NULL
);

CREATE SEQUENCE public.smss_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.smss_id_seq OWNED BY public.smss.id;

CREATE SEQUENCE public.strong_verification_attempts_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.strong_verification_attempts_id_seq OWNED BY public.strong_verification_attempts.id;

CREATE TABLE public.strong_verification_callback_events (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    verification_attempt_id bigint CONSTRAINT strong_verification_callback_e_verification_attempt_id_not_null NOT NULL,
    iris_status character varying NOT NULL
);

CREATE SEQUENCE public.strong_verification_callback_events_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.strong_verification_callback_events_id_seq OWNED BY public.strong_verification_callback_events.id;

CREATE TABLE public.threads (
    id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    deleted timestamp with time zone
);

CREATE SEQUENCE public.threads_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.threads_id_seq OWNED BY public.threads.id;

CREATE TABLE public.timezone_areas (
    id bigint NOT NULL,
    tzid character varying,
    geom public.geometry(MultiPolygon,4326) NOT NULL
);

CREATE SEQUENCE public.timezone_areas_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.timezone_areas_id_seq OWNED BY public.timezone_areas.id;

CREATE TABLE public.user_activity (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    period timestamp with time zone NOT NULL,
    ip_address inet,
    user_agent character varying,
    api_calls integer NOT NULL,
    sofa character varying,
    client_platform public.clientplatform
);

CREATE SEQUENCE public.user_activity_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.user_activity_id_seq OWNED BY public.user_activity.id;

CREATE TABLE public.user_admin_tags (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    admin_tag_id bigint NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.user_admin_tags_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.user_admin_tags_id_seq OWNED BY public.user_admin_tags.id;

CREATE TABLE public.user_badges (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    badge_id character varying NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.user_badges_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.user_badges_id_seq OWNED BY public.user_badges.id;

CREATE TABLE public.user_blocks (
    id bigint NOT NULL,
    blocking_user_id bigint NOT NULL,
    blocked_user_id bigint NOT NULL,
    time_blocked timestamp with time zone DEFAULT now() NOT NULL
);

CREATE SEQUENCE public.user_blocks_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.user_blocks_id_seq OWNED BY public.user_blocks.id;

CREATE MATERIALIZED VIEW public.user_response_rates AS
 SELECT user_id,
    count(*) AS requests,
    ((count(response_time))::numeric / (count(*))::numeric) AS response_rate,
    avg(response_time) AS avg_response_time,
    percentile_disc((0.33)::double precision) WITHIN GROUP (ORDER BY COALESCE(response_time, make_interval(secs => (86400000.0)::double precision))) AS response_time_33p,
    percentile_disc((0.66)::double precision) WITHIN GROUP (ORDER BY COALESCE(response_time, make_interval(secs => (86400000.0)::double precision))) AS response_time_66p
   FROM ( SELECT host_requests.recipient_user_id AS user_id,
            (anon_2."time" - anon_3."time") AS response_time
           FROM ((public.host_requests
             JOIN ( SELECT messages.conversation_id,
                    messages."time"
                   FROM public.messages
                  WHERE (messages.message_type = 'chat_created'::public.messagetype)) anon_3 ON ((anon_3.conversation_id = host_requests.id)))
             LEFT JOIN ( SELECT messages.conversation_id,
                    messages.author_id,
                    min(messages."time") AS "time"
                   FROM public.messages
                  GROUP BY messages.conversation_id, messages.author_id) anon_2 ON (((anon_2.conversation_id = host_requests.id) AND (anon_2.author_id = host_requests.recipient_user_id))))
        UNION ALL
         SELECT activeness_probes.user_id,
                CASE
                    WHEN (activeness_probes.response <> 'expired'::public.activenessprobestatus) THEN (activeness_probes.responded - activeness_probes.probe_initiated)
                    ELSE NULL::interval
                END AS response_time
           FROM public.activeness_probes) anon_1
  GROUP BY user_id
  WITH NO DATA;

CREATE SEQUENCE public.users_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;

CREATE TABLE public.volunteers (
    id bigint NOT NULL,
    user_id bigint NOT NULL,
    role character varying NOT NULL,
    sort_key double precision,
    started_volunteering date DEFAULT CURRENT_DATE NOT NULL,
    stopped_volunteering date,
    link_type character varying,
    link_text character varying,
    link_url character varying,
    show_on_team_page boolean DEFAULT true NOT NULL,
    display_name character varying,
    display_location character varying,
    CONSTRAINT ck_volunteers_link_type_text CHECK ((((link_type IS NULL) = (link_text IS NULL)) AND ((link_type IS NULL) = (link_url IS NULL))))
);

CREATE SEQUENCE public.volunteers_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE public.volunteers_id_seq OWNED BY public.volunteers.id;

ALTER TABLE ONLY logging.api_calls ALTER COLUMN id SET DEFAULT nextval('logging.api_calls_id_seq'::regclass);

ALTER TABLE ONLY logging.event_log ALTER COLUMN id SET DEFAULT nextval('logging.event_log_id_seq'::regclass);

ALTER TABLE ONLY logging.experiment_exposures ALTER COLUMN id SET DEFAULT nextval('logging.experiment_exposures_id_seq'::regclass);

ALTER TABLE ONLY logging.feature_usage ALTER COLUMN id SET DEFAULT nextval('logging.feature_usage_id_seq'::regclass);

ALTER TABLE ONLY logging.nonvisible_user_access ALTER COLUMN id SET DEFAULT nextval('logging.nonvisible_user_access_id_seq'::regclass);

ALTER TABLE ONLY public.account_deletion_reason ALTER COLUMN id SET DEFAULT nextval('public.account_deletion_reason_id_seq'::regclass);

ALTER TABLE ONLY public.activeness_probes ALTER COLUMN id SET DEFAULT nextval('public.activeness_probes_id_seq'::regclass);

ALTER TABLE ONLY public.admin_actions ALTER COLUMN id SET DEFAULT nextval('public.admin_actions_id_seq'::regclass);

ALTER TABLE ONLY public.admin_tags ALTER COLUMN id SET DEFAULT nextval('public.admin_tags_id_seq'::regclass);

ALTER TABLE ONLY public.antibot_logs ALTER COLUMN id SET DEFAULT nextval('public.antibot_logs_id_seq'::regclass);

ALTER TABLE ONLY public.background_jobs ALTER COLUMN id SET DEFAULT nextval('public.background_jobs_id_seq'::regclass);

ALTER TABLE ONLY public.cluster_discussion_associations ALTER COLUMN id SET DEFAULT nextval('public.cluster_discussion_associations_id_seq'::regclass);

ALTER TABLE ONLY public.cluster_event_associations ALTER COLUMN id SET DEFAULT nextval('public.cluster_event_associations_id_seq'::regclass);

ALTER TABLE ONLY public.cluster_page_associations ALTER COLUMN id SET DEFAULT nextval('public.cluster_page_associations_id_seq'::regclass);

ALTER TABLE ONLY public.cluster_subscriptions ALTER COLUMN id SET DEFAULT nextval('public.cluster_subscriptions_id_seq'::regclass);

ALTER TABLE ONLY public.comment_versions ALTER COLUMN id SET DEFAULT nextval('public.comment_versions_id_seq'::regclass);

ALTER TABLE ONLY public.comments ALTER COLUMN id SET DEFAULT nextval('public.comments_id_seq'::regclass);

ALTER TABLE ONLY public.content_reports ALTER COLUMN id SET DEFAULT nextval('public.content_reports_id_seq'::regclass);

ALTER TABLE ONLY public.contributor_forms ALTER COLUMN id SET DEFAULT nextval('public.contributor_forms_id_seq'::regclass);

ALTER TABLE ONLY public.conversations ALTER COLUMN id SET DEFAULT nextval('public.conversations_id_seq'::regclass);

ALTER TABLE ONLY public.discussion_subscriptions ALTER COLUMN id SET DEFAULT nextval('public.discussion_subscriptions_id_seq'::regclass);

ALTER TABLE ONLY public.discussion_versions ALTER COLUMN id SET DEFAULT nextval('public.discussion_versions_id_seq'::regclass);

ALTER TABLE ONLY public.donation_initiations ALTER COLUMN id SET DEFAULT nextval('public.donation_initiations_id_seq'::regclass);

ALTER TABLE ONLY public.event_community_invite_requests ALTER COLUMN id SET DEFAULT nextval('public.event_community_invite_requests_id_seq'::regclass);

ALTER TABLE ONLY public.event_occurrence_attendees ALTER COLUMN id SET DEFAULT nextval('public.event_occurrence_attendees_id_seq'::regclass);

ALTER TABLE ONLY public.event_organizers ALTER COLUMN id SET DEFAULT nextval('public.event_organizers_id_seq'::regclass);

ALTER TABLE ONLY public.event_subscriptions ALTER COLUMN id SET DEFAULT nextval('public.event_subscriptions_id_seq'::regclass);

ALTER TABLE ONLY public.friend_relationships ALTER COLUMN id SET DEFAULT nextval('public.friend_relationships_id_seq'::regclass);

ALTER TABLE ONLY public.group_chat_subscriptions ALTER COLUMN id SET DEFAULT nextval('public.group_chat_subscriptions_id_seq'::regclass);

ALTER TABLE ONLY public.host_request_feedbacks ALTER COLUMN id SET DEFAULT nextval('public.host_request_feedbacks_id_seq'::regclass);

ALTER TABLE ONLY public.invoices ALTER COLUMN id SET DEFAULT nextval('public.invoices_id_seq'::regclass);

ALTER TABLE ONLY public.language_abilities ALTER COLUMN id SET DEFAULT nextval('public.language_abilities_id_seq'::regclass);

ALTER TABLE ONLY public.messages ALTER COLUMN id SET DEFAULT nextval('public.messages_id_seq'::regclass);

ALTER TABLE ONLY public.mod_notes ALTER COLUMN id SET DEFAULT nextval('public.mod_notes_id_seq'::regclass);

ALTER TABLE ONLY public.moderation_user_lists ALTER COLUMN id SET DEFAULT nextval('public.moderation_user_lists_id_seq'::regclass);

ALTER TABLE ONLY public.native_client_users ALTER COLUMN id SET DEFAULT nextval('public.native_client_users_id_seq'::regclass);

ALTER TABLE ONLY public.node_cluster_associations ALTER COLUMN id SET DEFAULT nextval('public.node_cluster_associations_id_seq'::regclass);

ALTER TABLE ONLY public.notification_deliveries ALTER COLUMN id SET DEFAULT nextval('public.notification_deliveries_id_seq'::regclass);

ALTER TABLE ONLY public.notification_preferences ALTER COLUMN id SET DEFAULT nextval('public.notification_preferences_id_seq'::regclass);

ALTER TABLE ONLY public.notifications ALTER COLUMN id SET DEFAULT nextval('public.notifications_id_seq'::regclass);

ALTER TABLE ONLY public.ota_packages ALTER COLUMN id SET DEFAULT nextval('public.ota_packages_id_seq'::regclass);

ALTER TABLE ONLY public.page_versions ALTER COLUMN id SET DEFAULT nextval('public.page_versions_id_seq'::regclass);

ALTER TABLE ONLY public.photo_galleries ALTER COLUMN id SET DEFAULT nextval('public.photo_galleries_id_seq'::regclass);

ALTER TABLE ONLY public.photo_gallery_items ALTER COLUMN id SET DEFAULT nextval('public.photo_gallery_items_id_seq'::regclass);

ALTER TABLE ONLY public.postal_verification_attempts ALTER COLUMN id SET DEFAULT nextval('public.postal_verification_attempts_id_seq'::regclass);

ALTER TABLE ONLY public.public_trips ALTER COLUMN id SET DEFAULT nextval('public.public_trips_id_seq'::regclass);

ALTER TABLE ONLY public.push_notification_delivery_attempt ALTER COLUMN id SET DEFAULT nextval('public.push_notification_delivery_attempt_id_seq'::regclass);

ALTER TABLE ONLY public.push_notification_subscriptions ALTER COLUMN id SET DEFAULT nextval('public.push_notification_subscriptions_id_seq'::regclass);

ALTER TABLE ONLY public.rate_limit_violations ALTER COLUMN id SET DEFAULT nextval('public.rate_limit_violations_id_seq'::regclass);

ALTER TABLE ONLY public."references" ALTER COLUMN id SET DEFAULT nextval('public.references_id_seq'::regclass);

ALTER TABLE ONLY public.regions_lived ALTER COLUMN id SET DEFAULT nextval('public.regions_lived_id_seq'::regclass);

ALTER TABLE ONLY public.regions_visited ALTER COLUMN id SET DEFAULT nextval('public.regions_visited_id_seq'::regclass);

ALTER TABLE ONLY public.replies ALTER COLUMN id SET DEFAULT nextval('public.replies_id_seq'::regclass);

ALTER TABLE ONLY public.reply_versions ALTER COLUMN id SET DEFAULT nextval('public.reply_versions_id_seq'::regclass);

ALTER TABLE ONLY public.signup_flows ALTER COLUMN id SET DEFAULT nextval('public.signup_flows_id_seq'::regclass);

ALTER TABLE ONLY public.smss ALTER COLUMN id SET DEFAULT nextval('public.smss_id_seq'::regclass);

ALTER TABLE ONLY public.strong_verification_attempts ALTER COLUMN id SET DEFAULT nextval('public.strong_verification_attempts_id_seq'::regclass);

ALTER TABLE ONLY public.strong_verification_callback_events ALTER COLUMN id SET DEFAULT nextval('public.strong_verification_callback_events_id_seq'::regclass);

ALTER TABLE ONLY public.threads ALTER COLUMN id SET DEFAULT nextval('public.threads_id_seq'::regclass);

ALTER TABLE ONLY public.timezone_areas ALTER COLUMN id SET DEFAULT nextval('public.timezone_areas_id_seq'::regclass);

ALTER TABLE ONLY public.user_activity ALTER COLUMN id SET DEFAULT nextval('public.user_activity_id_seq'::regclass);

ALTER TABLE ONLY public.user_admin_tags ALTER COLUMN id SET DEFAULT nextval('public.user_admin_tags_id_seq'::regclass);

ALTER TABLE ONLY public.user_badges ALTER COLUMN id SET DEFAULT nextval('public.user_badges_id_seq'::regclass);

ALTER TABLE ONLY public.user_blocks ALTER COLUMN id SET DEFAULT nextval('public.user_blocks_id_seq'::regclass);

ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);

ALTER TABLE ONLY public.volunteers ALTER COLUMN id SET DEFAULT nextval('public.volunteers_id_seq'::regclass);

ALTER TABLE ONLY logging.api_calls
    ADD CONSTRAINT pk_api_calls PRIMARY KEY (id);

ALTER TABLE ONLY logging.event_log
    ADD CONSTRAINT pk_event_log PRIMARY KEY (id);

ALTER TABLE ONLY logging.experiment_exposures
    ADD CONSTRAINT pk_experiment_exposures PRIMARY KEY (id);

ALTER TABLE ONLY logging.feature_usage
    ADD CONSTRAINT pk_feature_usage PRIMARY KEY (id);

ALTER TABLE ONLY logging.nonvisible_user_access
    ADD CONSTRAINT pk_nonvisible_user_access PRIMARY KEY (id);

ALTER TABLE ONLY logging.experiment_exposures
    ADD CONSTRAINT uq_experiment_exposures_user_exp_var UNIQUE (user_id, experiment_key, variation_id);

ALTER TABLE ONLY public.alembic_version
    ADD CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT event_occurrences_event_id_during_excl EXCLUDE USING gist (event_id WITH =, during WITH &&);

ALTER TABLE ONLY public.account_deletion_reason
    ADD CONSTRAINT pk_account_deletion_reason PRIMARY KEY (id);

ALTER TABLE ONLY public.account_deletion_tokens
    ADD CONSTRAINT pk_account_deletion_tokens PRIMARY KEY (token);

ALTER TABLE ONLY public.activeness_probes
    ADD CONSTRAINT pk_activeness_probes PRIMARY KEY (id);

ALTER TABLE ONLY public.admin_actions
    ADD CONSTRAINT pk_admin_actions PRIMARY KEY (id);

ALTER TABLE ONLY public.admin_tags
    ADD CONSTRAINT pk_admin_tags PRIMARY KEY (id);

ALTER TABLE ONLY public.antibot_logs
    ADD CONSTRAINT pk_antibot_logs PRIMARY KEY (id);

ALTER TABLE ONLY public.background_jobs
    ADD CONSTRAINT pk_background_jobs PRIMARY KEY (id);

ALTER TABLE ONLY public.cluster_discussion_associations
    ADD CONSTRAINT pk_cluster_discussion_associations PRIMARY KEY (id);

ALTER TABLE ONLY public.cluster_event_associations
    ADD CONSTRAINT pk_cluster_event_associations PRIMARY KEY (id);

ALTER TABLE ONLY public.cluster_page_associations
    ADD CONSTRAINT pk_cluster_page_associations PRIMARY KEY (id);

ALTER TABLE ONLY public.cluster_subscriptions
    ADD CONSTRAINT pk_cluster_subscriptions PRIMARY KEY (id);

ALTER TABLE ONLY public.clusters
    ADD CONSTRAINT pk_clusters PRIMARY KEY (id);

ALTER TABLE ONLY public.comment_versions
    ADD CONSTRAINT pk_comment_versions PRIMARY KEY (id);

ALTER TABLE ONLY public.comments
    ADD CONSTRAINT pk_comments PRIMARY KEY (id);

ALTER TABLE ONLY public.content_reports
    ADD CONSTRAINT pk_content_reports PRIMARY KEY (id);

ALTER TABLE ONLY public.contributor_forms
    ADD CONSTRAINT pk_contributor_forms PRIMARY KEY (id);

ALTER TABLE ONLY public.conversations
    ADD CONSTRAINT pk_conversations PRIMARY KEY (id);

ALTER TABLE ONLY public.discussion_subscriptions
    ADD CONSTRAINT pk_discussion_subscriptions PRIMARY KEY (id);

ALTER TABLE ONLY public.discussion_versions
    ADD CONSTRAINT pk_discussion_versions PRIMARY KEY (id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT pk_discussions PRIMARY KEY (id);

ALTER TABLE ONLY public.donation_initiations
    ADD CONSTRAINT pk_donation_initiations PRIMARY KEY (id);

ALTER TABLE ONLY public.emails
    ADD CONSTRAINT pk_emails PRIMARY KEY (id);

ALTER TABLE ONLY public.event_community_invite_requests
    ADD CONSTRAINT pk_event_community_invite_requests PRIMARY KEY (id);

ALTER TABLE ONLY public.event_occurrence_attendees
    ADD CONSTRAINT pk_event_occurrence_attendees PRIMARY KEY (id);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT pk_event_occurrences PRIMARY KEY (id);

ALTER TABLE ONLY public.event_organizers
    ADD CONSTRAINT pk_event_organizers PRIMARY KEY (id);

ALTER TABLE ONLY public.event_subscriptions
    ADD CONSTRAINT pk_event_subscriptions PRIMARY KEY (id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT pk_events PRIMARY KEY (id);

ALTER TABLE ONLY public.friend_relationships
    ADD CONSTRAINT pk_friend_relationships PRIMARY KEY (id);

ALTER TABLE ONLY public.group_chat_subscriptions
    ADD CONSTRAINT pk_group_chat_subscriptions PRIMARY KEY (id);

ALTER TABLE ONLY public.group_chats
    ADD CONSTRAINT pk_group_chats PRIMARY KEY (id);

ALTER TABLE ONLY public.host_request_feedbacks
    ADD CONSTRAINT pk_host_request_feedbacks PRIMARY KEY (id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT pk_host_requests PRIMARY KEY (id);

ALTER TABLE ONLY public.initiated_uploads
    ADD CONSTRAINT pk_initiated_uploads PRIMARY KEY (key);

ALTER TABLE ONLY public.invite_codes
    ADD CONSTRAINT pk_invite_codes PRIMARY KEY (id);

ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT pk_invoices PRIMARY KEY (id);

ALTER TABLE ONLY public.language_abilities
    ADD CONSTRAINT pk_language_abilities PRIMARY KEY (id);

ALTER TABLE ONLY public.languages
    ADD CONSTRAINT pk_languages PRIMARY KEY (code);

ALTER TABLE ONLY public.login_tokens
    ADD CONSTRAINT pk_login_tokens PRIMARY KEY (token);

ALTER TABLE ONLY public.messages
    ADD CONSTRAINT pk_messages PRIMARY KEY (id);

ALTER TABLE ONLY public.mod_notes
    ADD CONSTRAINT pk_mod_notes PRIMARY KEY (id);

ALTER TABLE ONLY public.moderation_log
    ADD CONSTRAINT pk_moderation_log PRIMARY KEY (id);

ALTER TABLE ONLY public.moderation_queue
    ADD CONSTRAINT pk_moderation_queue PRIMARY KEY (id);

ALTER TABLE ONLY public.moderation_states
    ADD CONSTRAINT pk_moderation_states PRIMARY KEY (id);

ALTER TABLE ONLY public.moderation_user_list_members
    ADD CONSTRAINT pk_moderation_user_list_members PRIMARY KEY (user_id, moderation_list_id);

ALTER TABLE ONLY public.moderation_user_lists
    ADD CONSTRAINT pk_moderation_user_lists PRIMARY KEY (id);

ALTER TABLE ONLY public.native_client_users
    ADD CONSTRAINT pk_native_client_users PRIMARY KEY (id);

ALTER TABLE ONLY public.node_cluster_associations
    ADD CONSTRAINT pk_node_cluster_associations PRIMARY KEY (id);

ALTER TABLE ONLY public.nodes
    ADD CONSTRAINT pk_nodes PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_deliveries
    ADD CONSTRAINT pk_notification_deliveries PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_preferences
    ADD CONSTRAINT pk_notification_preferences PRIMARY KEY (id);

ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT pk_notifications PRIMARY KEY (id);

ALTER TABLE ONLY public.ota_packages
    ADD CONSTRAINT pk_ota_packages PRIMARY KEY (id);

ALTER TABLE ONLY public.page_versions
    ADD CONSTRAINT pk_page_versions PRIMARY KEY (id);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT pk_pages PRIMARY KEY (id);

ALTER TABLE ONLY public.password_reset_tokens
    ADD CONSTRAINT pk_password_reset_tokens PRIMARY KEY (token);

ALTER TABLE ONLY public.photo_galleries
    ADD CONSTRAINT pk_photo_galleries PRIMARY KEY (id);

ALTER TABLE ONLY public.photo_gallery_items
    ADD CONSTRAINT pk_photo_gallery_items PRIMARY KEY (id);

ALTER TABLE ONLY public.postal_verification_attempts
    ADD CONSTRAINT pk_postal_verification_attempts PRIMARY KEY (id);

ALTER TABLE ONLY public.public_trips
    ADD CONSTRAINT pk_public_trips PRIMARY KEY (id);

ALTER TABLE ONLY public.push_notification_delivery_attempt
    ADD CONSTRAINT pk_push_notification_delivery_attempt PRIMARY KEY (id);

ALTER TABLE ONLY public.push_notification_subscriptions
    ADD CONSTRAINT pk_push_notification_subscriptions PRIMARY KEY (id);

ALTER TABLE ONLY public.rate_limit_violations
    ADD CONSTRAINT pk_rate_limit_violations PRIMARY KEY (id);

ALTER TABLE ONLY public."references"
    ADD CONSTRAINT pk_references PRIMARY KEY (id);

ALTER TABLE ONLY public.regions
    ADD CONSTRAINT pk_regions PRIMARY KEY (code);

ALTER TABLE ONLY public.regions_lived
    ADD CONSTRAINT pk_regions_lived PRIMARY KEY (id);

ALTER TABLE ONLY public.regions_visited
    ADD CONSTRAINT pk_regions_visited PRIMARY KEY (id);

ALTER TABLE ONLY public.replies
    ADD CONSTRAINT pk_replies PRIMARY KEY (id);

ALTER TABLE ONLY public.reply_versions
    ADD CONSTRAINT pk_reply_versions PRIMARY KEY (id);

ALTER TABLE ONLY public.sessions
    ADD CONSTRAINT pk_sessions PRIMARY KEY (token);

ALTER TABLE ONLY public.signup_flows
    ADD CONSTRAINT pk_signup_flows PRIMARY KEY (id);

ALTER TABLE ONLY public.smss
    ADD CONSTRAINT pk_smss PRIMARY KEY (id);

ALTER TABLE ONLY public.strong_verification_attempts
    ADD CONSTRAINT pk_strong_verification_attempts PRIMARY KEY (id);

ALTER TABLE ONLY public.strong_verification_callback_events
    ADD CONSTRAINT pk_strong_verification_callback_events PRIMARY KEY (id);

ALTER TABLE ONLY public.threads
    ADD CONSTRAINT pk_threads PRIMARY KEY (id);

ALTER TABLE ONLY public.timezone_areas
    ADD CONSTRAINT pk_timezone_areas PRIMARY KEY (id);

ALTER TABLE ONLY public.uploads
    ADD CONSTRAINT pk_uploads PRIMARY KEY (key);

ALTER TABLE ONLY public.user_activity
    ADD CONSTRAINT pk_user_activity PRIMARY KEY (id);

ALTER TABLE ONLY public.user_admin_tags
    ADD CONSTRAINT pk_user_admin_tags PRIMARY KEY (id);

ALTER TABLE ONLY public.user_badges
    ADD CONSTRAINT pk_user_badges PRIMARY KEY (id);

ALTER TABLE ONLY public.user_blocks
    ADD CONSTRAINT pk_user_blocks PRIMARY KEY (id);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT pk_users PRIMARY KEY (id);

ALTER TABLE ONLY public.volunteers
    ADD CONSTRAINT pk_volunteers PRIMARY KEY (id);

ALTER TABLE ONLY public.photo_gallery_items
    ADD CONSTRAINT uix_gallery_upload UNIQUE (gallery_id, upload_key);

ALTER TABLE ONLY public.admin_tags
    ADD CONSTRAINT uq_admin_tags_tag UNIQUE (tag);

ALTER TABLE ONLY public.cluster_discussion_associations
    ADD CONSTRAINT uq_cluster_discussion_associations_discussion_id UNIQUE (discussion_id, cluster_id);

ALTER TABLE ONLY public.cluster_event_associations
    ADD CONSTRAINT uq_cluster_event_associations_event_id UNIQUE (event_id, cluster_id);

ALTER TABLE ONLY public.cluster_page_associations
    ADD CONSTRAINT uq_cluster_page_associations_page_id UNIQUE (page_id, cluster_id);

ALTER TABLE ONLY public.cluster_subscriptions
    ADD CONSTRAINT uq_cluster_subscriptions_user_id UNIQUE (user_id, cluster_id);

ALTER TABLE ONLY public.discussion_subscriptions
    ADD CONSTRAINT uq_discussion_subscriptions_discussion_id UNIQUE (discussion_id, user_id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT uq_discussions_thread_id UNIQUE (thread_id);

ALTER TABLE ONLY public.event_community_invite_requests
    ADD CONSTRAINT uq_event_community_invite_requests_occurrence_id UNIQUE (occurrence_id, user_id);

ALTER TABLE ONLY public.event_occurrence_attendees
    ADD CONSTRAINT uq_event_occurrence_attendees_occurrence_id UNIQUE (occurrence_id, user_id);

ALTER TABLE ONLY public.event_organizers
    ADD CONSTRAINT uq_event_organizers_event_id UNIQUE (event_id, user_id);

ALTER TABLE ONLY public.event_subscriptions
    ADD CONSTRAINT uq_event_subscriptions_event_id UNIQUE (event_id, user_id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT uq_events_thread_id UNIQUE (thread_id);

ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT uq_invoices_stripe_payment_intent_id UNIQUE (stripe_payment_intent_id);

ALTER TABLE ONLY public.language_abilities
    ADD CONSTRAINT uq_language_abilities_user_id UNIQUE (user_id, language_code);

ALTER TABLE ONLY public.languages
    ADD CONSTRAINT uq_languages_name UNIQUE (name);

ALTER TABLE ONLY public.node_cluster_associations
    ADD CONSTRAINT uq_node_cluster_associations_node_id UNIQUE (node_id, cluster_id);

ALTER TABLE ONLY public.notification_deliveries
    ADD CONSTRAINT uq_notification_deliveries_notification_id UNIQUE (notification_id, delivery_type);

ALTER TABLE ONLY public.notification_preferences
    ADD CONSTRAINT uq_notification_preferences_user_id UNIQUE (user_id, topic_action, delivery_type);

ALTER TABLE ONLY public.ota_packages
    ADD CONSTRAINT uq_ota_packages_platform_manifest_id UNIQUE (platform, manifest_id);

ALTER TABLE ONLY public.ota_packages
    ADD CONSTRAINT uq_ota_packages_platform_version UNIQUE (platform, version);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT uq_pages_thread_id UNIQUE (thread_id);

ALTER TABLE ONLY public.regions_lived
    ADD CONSTRAINT uq_regions_lived_user_id UNIQUE (user_id, region_code);

ALTER TABLE ONLY public.regions
    ADD CONSTRAINT uq_regions_name UNIQUE (name);

ALTER TABLE ONLY public.regions_visited
    ADD CONSTRAINT uq_regions_visited_user_id UNIQUE (user_id, region_code);

ALTER TABLE ONLY public.signup_flows
    ADD CONSTRAINT uq_signup_flows_email UNIQUE (email);

ALTER TABLE ONLY public.signup_flows
    ADD CONSTRAINT uq_signup_flows_flow_token UNIQUE (flow_token);

ALTER TABLE ONLY public.signup_flows
    ADD CONSTRAINT uq_signup_flows_username UNIQUE (username);

ALTER TABLE ONLY public.strong_verification_attempts
    ADD CONSTRAINT uq_strong_verification_attempts_iris_session_id UNIQUE (iris_session_id);

ALTER TABLE ONLY public.strong_verification_attempts
    ADD CONSTRAINT uq_strong_verification_attempts_iris_token UNIQUE (iris_token);

ALTER TABLE ONLY public.strong_verification_attempts
    ADD CONSTRAINT uq_strong_verification_attempts_verification_attempt_token UNIQUE (verification_attempt_token);

ALTER TABLE ONLY public.user_admin_tags
    ADD CONSTRAINT uq_user_admin_tags_user_id UNIQUE (user_id, admin_tag_id);

ALTER TABLE ONLY public.user_badges
    ADD CONSTRAINT uq_user_badges_user_id UNIQUE (user_id, badge_id);

ALTER TABLE ONLY public.user_blocks
    ADD CONSTRAINT uq_user_blocks_blocking_user_id UNIQUE (blocking_user_id, blocked_user_id);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT uq_users_email UNIQUE (email);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT uq_users_username UNIQUE (username);

ALTER TABLE ONLY public.volunteers
    ADD CONSTRAINT uq_volunteers_user_id UNIQUE (user_id);

CREATE INDEX ix_logging_event_log_created ON logging.event_log USING btree (created);

CREATE INDEX ix_logging_event_log_event_type_created ON logging.event_log USING btree (event_type, created);

CREATE INDEX ix_logging_event_log_user_id_created ON logging.event_log USING btree (user_id, created);

CREATE INDEX ix_logging_experiment_exposures_experiment_key_created ON logging.experiment_exposures USING btree (experiment_key, created);

CREATE INDEX ix_logging_experiment_exposures_user_id_created ON logging.experiment_exposures USING btree (user_id, created);

CREATE INDEX ix_logging_feature_usage_feature_key_time ON logging.feature_usage USING btree (feature_key, "time");

CREATE INDEX ix_logging_feature_usage_user_id_time ON logging.feature_usage USING btree (user_id, "time");

CREATE INDEX ix_logging_nonvisible_user_access_actor_user_id_time ON logging.nonvisible_user_access USING btree (actor_user_id, "time");

CREATE INDEX ix_logging_nonvisible_user_access_ip_address ON logging.nonvisible_user_access USING btree (ip_address);

CREATE INDEX ix_logging_nonvisible_user_access_sofa ON logging.nonvisible_user_access USING btree (sofa);

CREATE INDEX ix_logging_nonvisible_user_access_target_user_id_time ON logging.nonvisible_user_access USING btree (target_user_id, "time");

CREATE INDEX idx_clustered_users_geom ON public.clustered_users USING gist (geom);

CREATE INDEX idx_clusters_name_unaccented_trgm ON public.clusters USING gin (public.immutable_unaccent((name)::text) public.gin_trgm_ops);

CREATE INDEX idx_event_occurrences_geom ON public.event_occurrences USING gist (geom);

CREATE INDEX idx_host_requests_hosting_location ON public.host_requests USING gist (hosting_location);

CREATE INDEX idx_lite_users_geom ON public.lite_users USING gist (geom);

CREATE INDEX idx_nodes_geom ON public.nodes USING gist (geom);

CREATE INDEX idx_page_versions_geom ON public.page_versions USING gist (geom);

CREATE INDEX idx_signup_flows_geom ON public.signup_flows USING gist (geom);

CREATE INDEX idx_timezone_areas_geom ON public.timezone_areas USING gist (geom);

CREATE INDEX idx_users_geom ON public.users USING gist (geom);

CREATE INDEX idx_users_randomized_geom ON public.users USING gist (randomized_geom);

CREATE INDEX ix_account_deletion_tokens_user_id ON public.account_deletion_tokens USING btree (user_id);

CREATE UNIQUE INDEX ix_activeness_probe_unique_pending_response ON public.activeness_probes USING btree (user_id) WHERE (responded IS NULL);

CREATE INDEX ix_activeness_probes_user_id ON public.activeness_probes USING btree (user_id);

CREATE INDEX ix_admin_actions_admin_user_id ON public.admin_actions USING btree (admin_user_id);

CREATE INDEX ix_admin_actions_target_created ON public.admin_actions USING btree (target_user_id, created);

CREATE INDEX ix_admin_actions_target_user_id ON public.admin_actions USING btree (target_user_id);

CREATE INDEX ix_background_jobs_lookup ON public.background_jobs USING btree (priority DESC, next_attempt_after, ((max_tries - try_count))) WHERE ((state = 'pending'::public.backgroundjobstate) OR (state = 'error'::public.backgroundjobstate));

CREATE INDEX ix_cluster_discussion_associations_cluster_id ON public.cluster_discussion_associations USING btree (cluster_id);

CREATE INDEX ix_cluster_discussion_associations_discussion_id ON public.cluster_discussion_associations USING btree (discussion_id);

CREATE INDEX ix_cluster_event_associations_cluster_id ON public.cluster_event_associations USING btree (cluster_id);

CREATE INDEX ix_cluster_event_associations_event_id ON public.cluster_event_associations USING btree (event_id);

CREATE INDEX ix_cluster_page_associations_cluster_id ON public.cluster_page_associations USING btree (cluster_id);

CREATE INDEX ix_cluster_page_associations_page_id ON public.cluster_page_associations USING btree (page_id);

CREATE INDEX ix_cluster_subscriptions_admins ON public.cluster_subscriptions USING btree (user_id, cluster_id) WHERE (role = 'admin'::public.clusterrole);

CREATE INDEX ix_cluster_subscriptions_cluster_id ON public.cluster_subscriptions USING btree (cluster_id);

CREATE INDEX ix_cluster_subscriptions_members ON public.cluster_subscriptions USING btree (cluster_id, user_id);

CREATE INDEX ix_cluster_subscriptions_user_id ON public.cluster_subscriptions USING btree (user_id);

CREATE UNIQUE INDEX ix_clusters_owner_parent_node_id_is_official_cluster ON public.clusters USING btree (parent_node_id, is_official_cluster) WHERE is_official_cluster;

CREATE INDEX ix_clusters_parent_node_id ON public.clusters USING btree (parent_node_id);

CREATE INDEX ix_comment_versions_comment_id ON public.comment_versions USING btree (comment_id);

CREATE INDEX ix_comment_versions_editor_user_id ON public.comment_versions USING btree (editor_user_id);

CREATE INDEX ix_comments_moderation_state_id ON public.comments USING btree (moderation_state_id);

CREATE INDEX ix_comments_thread_id ON public.comments USING btree (thread_id);

CREATE INDEX ix_content_reports_reporting_user_id ON public.content_reports USING btree (reporting_user_id);

CREATE INDEX ix_contributor_forms_user_id ON public.contributor_forms USING btree (user_id);

CREATE INDEX ix_discussion_subscriptions_discussion_id ON public.discussion_subscriptions USING btree (discussion_id);

CREATE INDEX ix_discussion_subscriptions_user_id ON public.discussion_subscriptions USING btree (user_id);

CREATE INDEX ix_discussion_versions_discussion_id ON public.discussion_versions USING btree (discussion_id);

CREATE INDEX ix_discussion_versions_editor_user_id ON public.discussion_versions USING btree (editor_user_id);

CREATE INDEX ix_discussions_creator_user_id ON public.discussions USING btree (creator_user_id);

CREATE INDEX ix_discussions_moderation_state_id ON public.discussions USING btree (moderation_state_id);

CREATE INDEX ix_discussions_owner_cluster_id ON public.discussions USING btree (owner_cluster_id);

CREATE INDEX ix_donation_initiations_user_id ON public.donation_initiations USING btree (user_id);

CREATE INDEX ix_event_community_invite_requests_occurrence_id ON public.event_community_invite_requests USING btree (occurrence_id);

CREATE UNIQUE INDEX ix_event_community_invite_requests_unique ON public.event_community_invite_requests USING btree (occurrence_id) WHERE ((approved IS NOT NULL) AND (approved = true));

CREATE INDEX ix_event_community_invite_requests_user_id ON public.event_community_invite_requests USING btree (user_id);

CREATE INDEX ix_event_occurrence_attendees_occurrence_id ON public.event_occurrence_attendees USING btree (occurrence_id);

CREATE INDEX ix_event_occurrence_attendees_user_id ON public.event_occurrence_attendees USING btree (user_id);

CREATE INDEX ix_event_occurrences_creator_user_id ON public.event_occurrences USING btree (creator_user_id);

CREATE INDEX ix_event_occurrences_event_id ON public.event_occurrences USING btree (event_id);

CREATE INDEX ix_event_occurrences_moderation_state_id ON public.event_occurrences USING btree (moderation_state_id);

CREATE INDEX ix_event_organizers_event_id ON public.event_organizers USING btree (event_id);

CREATE INDEX ix_event_organizers_user_id ON public.event_organizers USING btree (user_id);

CREATE INDEX ix_event_subscriptions_event_id ON public.event_subscriptions USING btree (event_id);

CREATE INDEX ix_event_subscriptions_user_id ON public.event_subscriptions USING btree (user_id);

CREATE INDEX ix_events_creator_user_id ON public.events USING btree (creator_user_id);

CREATE INDEX ix_events_owner_cluster_id ON public.events USING btree (owner_cluster_id);

CREATE INDEX ix_events_owner_user_id ON public.events USING btree (owner_user_id);

CREATE INDEX ix_events_parent_node_id ON public.events USING btree (parent_node_id);

CREATE INDEX ix_friend_relationships_from_user_id ON public.friend_relationships USING btree (from_user_id);

CREATE INDEX ix_friend_relationships_moderation_state_id ON public.friend_relationships USING btree (moderation_state_id);

CREATE INDEX ix_friend_relationships_status_to_from ON public.friend_relationships USING btree (status, to_user_id, from_user_id);

CREATE INDEX ix_friend_relationships_to_user_id ON public.friend_relationships USING btree (to_user_id);

CREATE INDEX ix_group_chat_subscriptions_group_chat_id ON public.group_chat_subscriptions USING btree (group_chat_id);

CREATE INDEX ix_group_chat_subscriptions_user_id ON public.group_chat_subscriptions USING btree (user_id);

CREATE INDEX ix_group_chats_creator_id ON public.group_chats USING btree (creator_id);

CREATE INDEX ix_group_chats_moderation_state_id ON public.group_chats USING btree (moderation_state_id);

CREATE INDEX ix_host_request_feedbacks_from_user_id ON public.host_request_feedbacks USING btree (from_user_id);

CREATE INDEX ix_host_request_feedbacks_to_user_id ON public.host_request_feedbacks USING btree (to_user_id);

CREATE INDEX ix_host_requests_initiator_didnt_meetup ON public.host_requests USING btree (((initiator_reason_didnt_meetup IS NOT NULL)));

CREATE INDEX ix_host_requests_initiator_user_id ON public.host_requests USING btree (initiator_user_id);

CREATE INDEX ix_host_requests_moderation_state_id ON public.host_requests USING btree (moderation_state_id);

CREATE INDEX ix_host_requests_public_trip_id ON public.host_requests USING btree (public_trip_id);

CREATE INDEX ix_host_requests_recipient_didnt_meetup ON public.host_requests USING btree (((recipient_reason_didnt_meetup IS NOT NULL)));

CREATE INDEX ix_host_requests_recipient_user_id ON public.host_requests USING btree (recipient_user_id);

CREATE INDEX ix_host_requests_status_reminder_counts ON public.host_requests USING btree (status, recipient_sent_request_reminders, last_sent_request_reminder_time, from_date);

CREATE INDEX ix_initiated_uploads_initiator_user_id ON public.initiated_uploads USING btree (initiator_user_id);

CREATE INDEX ix_language_abilities_user_id ON public.language_abilities USING btree (user_id);

CREATE INDEX ix_lite_users_id_visible ON public.lite_users USING hash (id) WHERE is_visible;

CREATE INDEX ix_lite_users_username_visible ON public.lite_users USING hash (username) WHERE is_visible;

CREATE INDEX ix_login_tokens_user_id ON public.login_tokens USING btree (user_id);

CREATE INDEX ix_messages_author_id ON public.messages USING btree (author_id);

CREATE INDEX ix_messages_conversation_id ON public.messages USING btree (conversation_id);

CREATE INDEX ix_messages_conversation_id_id_text_only ON public.messages USING btree (conversation_id, id) WHERE (message_type = 'text'::public.messagetype);

CREATE INDEX ix_messages_target_id ON public.messages USING btree (target_id);

CREATE INDEX ix_mod_notes_unacknowledged ON public.mod_notes USING btree (user_id) WHERE (acknowledged IS NULL);

CREATE INDEX ix_mod_notes_user_id ON public.mod_notes USING btree (user_id);

CREATE INDEX ix_moderation_log_moderation_state_id ON public.moderation_log USING btree (moderation_state_id);

CREATE INDEX ix_moderation_log_queue_item_id ON public.moderation_log USING btree (queue_item_id);

CREATE INDEX ix_moderation_log_state_time ON public.moderation_log USING btree (moderation_state_id, "time" DESC);

CREATE INDEX ix_moderation_queue_moderation_state_id ON public.moderation_queue USING btree (moderation_state_id);

CREATE INDEX ix_moderation_queue_resolved_by_log_id ON public.moderation_queue USING btree (resolved_by_log_id);

CREATE INDEX ix_moderation_queue_unresolved ON public.moderation_queue USING btree (moderation_state_id, time_created) WHERE (resolved_by_log_id IS NULL);

CREATE INDEX ix_moderation_states_id_visibility ON public.moderation_states USING btree (id, visibility);

CREATE UNIQUE INDEX ix_moderation_states_object ON public.moderation_states USING btree (object_type, object_id);

CREATE INDEX ix_moderation_states_type_visibility ON public.moderation_states USING btree (object_type, visibility);

CREATE INDEX ix_native_client_users_eas_client_id ON public.native_client_users USING btree (eas_client_id);

CREATE INDEX ix_native_client_users_user_id ON public.native_client_users USING btree (user_id);

CREATE INDEX ix_node_cluster_associations_cluster_id ON public.node_cluster_associations USING btree (cluster_id);

CREATE INDEX ix_node_cluster_associations_node_id ON public.node_cluster_associations USING btree (node_id);

CREATE INDEX ix_nodes_parent_node_id ON public.nodes USING btree (parent_node_id);

CREATE INDEX ix_notification_deliveries_delivery_type ON public.notification_deliveries USING btree (delivery_type) WHERE (delivered IS NOT NULL);

CREATE INDEX ix_notification_deliveries_dt_ni_dnull ON public.notification_deliveries USING btree (delivery_type, notification_id, ((delivered IS NULL)));

CREATE INDEX ix_notification_deliveries_notification_id ON public.notification_deliveries USING btree (notification_id);

CREATE INDEX ix_notification_preferences_user_id ON public.notification_preferences USING btree (user_id);

CREATE INDEX ix_notifications_created ON public.notifications USING btree (created);

CREATE INDEX ix_notifications_latest ON public.notifications USING btree (user_id, id DESC, topic_action);

CREATE INDEX ix_notifications_moderation_state_id ON public.notifications USING btree (moderation_state_id);

CREATE INDEX ix_notifications_unseen ON public.notifications USING btree (user_id, topic_action) WHERE (is_seen = false);

CREATE INDEX ix_ota_packages_creator_user_id ON public.ota_packages USING btree (creator_user_id);

CREATE INDEX ix_ota_packages_resolve ON public.ota_packages USING btree (platform, fingerprint, manifest_created_at);

CREATE INDEX ix_page_versions_editor_user_id ON public.page_versions USING btree (editor_user_id);

CREATE INDEX ix_page_versions_page_id ON public.page_versions USING btree (page_id);

CREATE INDEX ix_pages_creator_user_id ON public.pages USING btree (creator_user_id);

CREATE INDEX ix_pages_owner_cluster_id ON public.pages USING btree (owner_cluster_id);

CREATE UNIQUE INDEX ix_pages_owner_cluster_id_type ON public.pages USING btree (owner_cluster_id, type) WHERE (type = 'main_page'::public.pagetype);

CREATE INDEX ix_pages_owner_user_id ON public.pages USING btree (owner_user_id);

CREATE INDEX ix_pages_parent_node_id ON public.pages USING btree (parent_node_id);

CREATE INDEX ix_password_reset_tokens_user_id ON public.password_reset_tokens USING btree (user_id);

CREATE INDEX ix_photo_galleries_owner_user_id ON public.photo_galleries USING btree (owner_user_id);

CREATE INDEX ix_photo_gallery_items_gallery_id ON public.photo_gallery_items USING btree (gallery_id);

CREATE INDEX ix_postal_verification_attempts_current ON public.postal_verification_attempts USING btree (user_id) WHERE (status = 'succeeded'::public.postalverificationstatus);

CREATE INDEX ix_postal_verification_attempts_user_id ON public.postal_verification_attempts USING btree (user_id);

CREATE UNIQUE INDEX ix_postal_verification_one_active_per_user ON public.postal_verification_attempts USING btree (user_id) WHERE ((status = 'pending_address_confirmation'::public.postalverificationstatus) OR (status = 'in_progress'::public.postalverificationstatus) OR (status = 'awaiting_verification'::public.postalverificationstatus));

CREATE INDEX ix_public_trips_node_from_date_active ON public.public_trips USING btree (node_id, from_date) WHERE (status = 'searching_for_host'::public.publictripstatus);

CREATE INDEX ix_public_trips_node_id ON public.public_trips USING btree (node_id);

CREATE INDEX ix_public_trips_user_id ON public.public_trips USING btree (user_id);

CREATE INDEX ix_push_notification_delivery_attempt_push_notification_9256 ON public.push_notification_delivery_attempt USING btree (push_notification_subscription_id);

CREATE UNIQUE INDEX ix_push_notification_subscriptions_token ON public.push_notification_subscriptions USING btree (token);

CREATE INDEX ix_push_notification_subscriptions_user_id ON public.push_notification_subscriptions USING btree (user_id);

CREATE INDEX ix_rate_limits_by_user ON public.rate_limit_violations USING btree (user_id, action, is_hard_limit, created);

CREATE INDEX ix_references_from_user_id ON public."references" USING btree (from_user_id);

CREATE INDEX ix_references_moderation_state_id ON public."references" USING btree (moderation_state_id);

CREATE INDEX ix_references_to_user_id ON public."references" USING btree (to_user_id);

CREATE UNIQUE INDEX ix_references_unique_friend_reference ON public."references" USING btree (from_user_id, to_user_id, reference_type) WHERE (reference_type = 'friend'::public.referencetype);

CREATE UNIQUE INDEX ix_references_unique_per_host_request ON public."references" USING btree (from_user_id, to_user_id, host_request_id) WHERE (host_request_id IS NOT NULL);

CREATE INDEX ix_regions_lived_user_id ON public.regions_lived USING btree (user_id);

CREATE INDEX ix_regions_visited_user_id ON public.regions_visited USING btree (user_id);

CREATE INDEX ix_replies_comment_id ON public.replies USING btree (comment_id);

CREATE INDEX ix_replies_moderation_state_id ON public.replies USING btree (moderation_state_id);

CREATE INDEX ix_reply_versions_editor_user_id ON public.reply_versions USING btree (editor_user_id);

CREATE INDEX ix_reply_versions_reply_id ON public.reply_versions USING btree (reply_id);

CREATE INDEX ix_sessions_by_token ON public.sessions USING hash (token);

CREATE INDEX ix_sessions_user_id ON public.sessions USING btree (user_id);

CREATE INDEX ix_strong_verification_attempts_current ON public.strong_verification_attempts USING btree (user_id, passport_expiry_date) WHERE (status = 'succeeded'::public.strongverificationattemptstatus);

CREATE UNIQUE INDEX ix_strong_verification_attempts_unique_succeeded ON public.strong_verification_attempts USING btree (passport_expiry_date, passport_nationality, passport_last_three_document_chars) WHERE ((status = 'succeeded'::public.strongverificationattemptstatus) OR (status = 'deleted'::public.strongverificationattemptstatus));

CREATE INDEX ix_strong_verification_attempts_user_id ON public.strong_verification_attempts USING btree (user_id);

CREATE INDEX ix_strong_verification_callback_events_verification_attempt_id ON public.strong_verification_callback_events USING btree (verification_attempt_id);

CREATE INDEX ix_timezone_areas_geom_tzid ON public.timezone_areas USING gist (geom, tzid);

CREATE UNIQUE INDEX ix_unique_host_req_feedback ON public.host_request_feedbacks USING btree (from_user_id, to_user_id, host_request_id);

CREATE INDEX ix_uploads_creator_user_id ON public.uploads USING btree (creator_user_id);

CREATE UNIQUE INDEX ix_user_activity_user_id_period_ip_address_user_agent_sofa ON public.user_activity USING btree (user_id, period, ip_address, user_agent, sofa) NULLS NOT DISTINCT;

CREATE INDEX ix_user_admin_tags_admin_tag_id ON public.user_admin_tags USING btree (admin_tag_id);

CREATE INDEX ix_user_admin_tags_user_id ON public.user_admin_tags USING btree (user_id);

CREATE INDEX ix_user_badges_badge_id ON public.user_badges USING btree (badge_id);

CREATE INDEX ix_user_badges_user_id ON public.user_badges USING btree (user_id);

CREATE INDEX ix_user_blocks_blocked_user_id ON public.user_blocks USING btree (blocked_user_id, blocking_user_id);

CREATE INDEX ix_user_blocks_blocking_user_id ON public.user_blocks USING btree (blocking_user_id, blocked_user_id);

CREATE INDEX ix_users_active ON public.users USING btree (id) WHERE ((banned_at IS NULL) AND (deleted_at IS NULL));

CREATE INDEX ix_users_by_id ON public.users USING hash (id) WHERE ((banned_at IS NULL) AND (deleted_at IS NULL));

CREATE INDEX ix_users_by_username ON public.users USING hash (username) WHERE ((banned_at IS NULL) AND (deleted_at IS NULL));

CREATE INDEX ix_users_geom_active ON public.users USING gist (geom, id, username) WHERE ((banned_at IS NULL) AND (deleted_at IS NULL));

CREATE UNIQUE INDEX ix_users_unique_phone ON public.users USING btree (phone) WHERE (phone_verification_verified IS NOT NULL);

CREATE INDEX ix_users_visible_with_about_me ON public.users USING btree (id) WHERE ((banned_at IS NULL) AND (deleted_at IS NULL) AND (profile_gallery_id IS NOT NULL) AND (COALESCE(character_length((about_me)::text), 0) >= 150));

CREATE UNIQUE INDEX uq_cluster_admin_counts_cluster_id ON public.cluster_admin_counts USING btree (cluster_id);

CREATE UNIQUE INDEX uq_cluster_subscription_counts_cluster_id ON public.cluster_subscription_counts USING btree (cluster_id);

CREATE UNIQUE INDEX uq_friend_relationships_active_pair ON public.friend_relationships USING btree (LEAST(from_user_id, to_user_id), GREATEST(from_user_id, to_user_id)) WHERE (status = ANY (ARRAY['pending'::public.friendstatus, 'accepted'::public.friendstatus]));

CREATE UNIQUE INDEX uq_lite_users_id ON public.lite_users USING btree (id);

CREATE UNIQUE INDEX uq_lite_users_username ON public.lite_users USING btree (username);

CREATE UNIQUE INDEX uq_user_response_rates_id ON public.user_response_rates USING btree (user_id);

ALTER TABLE ONLY public.account_deletion_reason
    ADD CONSTRAINT fk_account_deletion_reason_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.account_deletion_tokens
    ADD CONSTRAINT fk_account_deletion_tokens_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.activeness_probes
    ADD CONSTRAINT fk_activeness_probes_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.admin_actions
    ADD CONSTRAINT fk_admin_actions_admin_user_id_users FOREIGN KEY (admin_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.admin_actions
    ADD CONSTRAINT fk_admin_actions_target_user_id_users FOREIGN KEY (target_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.antibot_logs
    ADD CONSTRAINT fk_antibot_logs_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.cluster_discussion_associations
    ADD CONSTRAINT fk_cluster_discussion_associations_cluster_id_clusters FOREIGN KEY (cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.cluster_discussion_associations
    ADD CONSTRAINT fk_cluster_discussion_associations_discussion_id_discussions FOREIGN KEY (discussion_id) REFERENCES public.discussions(id);

ALTER TABLE ONLY public.cluster_event_associations
    ADD CONSTRAINT fk_cluster_event_associations_cluster_id_clusters FOREIGN KEY (cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.cluster_event_associations
    ADD CONSTRAINT fk_cluster_event_associations_event_id_events FOREIGN KEY (event_id) REFERENCES public.events(id);

ALTER TABLE ONLY public.cluster_page_associations
    ADD CONSTRAINT fk_cluster_page_associations_cluster_id_clusters FOREIGN KEY (cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.cluster_page_associations
    ADD CONSTRAINT fk_cluster_page_associations_page_id_pages FOREIGN KEY (page_id) REFERENCES public.pages(id);

ALTER TABLE ONLY public.cluster_subscriptions
    ADD CONSTRAINT fk_cluster_subscriptions_cluster_id_clusters FOREIGN KEY (cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.cluster_subscriptions
    ADD CONSTRAINT fk_cluster_subscriptions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.clusters
    ADD CONSTRAINT fk_clusters_parent_node_id_nodes FOREIGN KEY (parent_node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.comment_versions
    ADD CONSTRAINT fk_comment_versions_comment_id_comments FOREIGN KEY (comment_id) REFERENCES public.comments(id);

ALTER TABLE ONLY public.comment_versions
    ADD CONSTRAINT fk_comment_versions_editor_user_id_users FOREIGN KEY (editor_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.comments
    ADD CONSTRAINT fk_comments_author_user_id_users FOREIGN KEY (author_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.comments
    ADD CONSTRAINT fk_comments_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.comments
    ADD CONSTRAINT fk_comments_thread_id_threads FOREIGN KEY (thread_id) REFERENCES public.threads(id);

ALTER TABLE ONLY public.content_reports
    ADD CONSTRAINT fk_content_reports_author_user_id_users FOREIGN KEY (author_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.content_reports
    ADD CONSTRAINT fk_content_reports_reporting_user_id_users FOREIGN KEY (reporting_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.contributor_forms
    ADD CONSTRAINT fk_contributor_forms_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.discussion_subscriptions
    ADD CONSTRAINT fk_discussion_subscriptions_discussion_id_discussions FOREIGN KEY (discussion_id) REFERENCES public.discussions(id);

ALTER TABLE ONLY public.discussion_subscriptions
    ADD CONSTRAINT fk_discussion_subscriptions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.discussion_versions
    ADD CONSTRAINT fk_discussion_versions_discussion_id_discussions FOREIGN KEY (discussion_id) REFERENCES public.discussions(id);

ALTER TABLE ONLY public.discussion_versions
    ADD CONSTRAINT fk_discussion_versions_editor_user_id_users FOREIGN KEY (editor_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT fk_discussions_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT fk_discussions_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT fk_discussions_owner_cluster_id_clusters FOREIGN KEY (owner_cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.discussions
    ADD CONSTRAINT fk_discussions_thread_id_threads FOREIGN KEY (thread_id) REFERENCES public.threads(id);

ALTER TABLE ONLY public.donation_initiations
    ADD CONSTRAINT fk_donation_initiations_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_community_invite_requests
    ADD CONSTRAINT fk_event_community_invite_requests_decided_by_user_id_users FOREIGN KEY (decided_by_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_community_invite_requests
    ADD CONSTRAINT fk_event_community_invite_requests_occurrence_id_event__829a FOREIGN KEY (occurrence_id) REFERENCES public.event_occurrences(id);

ALTER TABLE ONLY public.event_community_invite_requests
    ADD CONSTRAINT fk_event_community_invite_requests_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_occurrence_attendees
    ADD CONSTRAINT fk_event_occurrence_attendees_occurrence_id_event_occurrences FOREIGN KEY (occurrence_id) REFERENCES public.event_occurrences(id);

ALTER TABLE ONLY public.event_occurrence_attendees
    ADD CONSTRAINT fk_event_occurrence_attendees_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT fk_event_occurrences_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT fk_event_occurrences_event_id_events FOREIGN KEY (event_id) REFERENCES public.events(id);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT fk_event_occurrences_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.event_occurrences
    ADD CONSTRAINT fk_event_occurrences_photo_key_uploads FOREIGN KEY (photo_key) REFERENCES public.uploads(key);

ALTER TABLE ONLY public.event_organizers
    ADD CONSTRAINT fk_event_organizers_event_id_events FOREIGN KEY (event_id) REFERENCES public.events(id);

ALTER TABLE ONLY public.event_organizers
    ADD CONSTRAINT fk_event_organizers_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.event_subscriptions
    ADD CONSTRAINT fk_event_subscriptions_event_id_events FOREIGN KEY (event_id) REFERENCES public.events(id);

ALTER TABLE ONLY public.event_subscriptions
    ADD CONSTRAINT fk_event_subscriptions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT fk_events_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT fk_events_owner_cluster_id_clusters FOREIGN KEY (owner_cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT fk_events_owner_user_id_users FOREIGN KEY (owner_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT fk_events_parent_node_id_nodes FOREIGN KEY (parent_node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.events
    ADD CONSTRAINT fk_events_thread_id_threads FOREIGN KEY (thread_id) REFERENCES public.threads(id);

ALTER TABLE ONLY public.friend_relationships
    ADD CONSTRAINT fk_friend_relationships_from_user_id_users FOREIGN KEY (from_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.friend_relationships
    ADD CONSTRAINT fk_friend_relationships_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.friend_relationships
    ADD CONSTRAINT fk_friend_relationships_to_user_id_users FOREIGN KEY (to_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.group_chat_subscriptions
    ADD CONSTRAINT fk_group_chat_subscriptions_group_chat_id_group_chats FOREIGN KEY (group_chat_id) REFERENCES public.group_chats(id);

ALTER TABLE ONLY public.group_chat_subscriptions
    ADD CONSTRAINT fk_group_chat_subscriptions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.group_chats
    ADD CONSTRAINT fk_group_chats_creator_id_users FOREIGN KEY (creator_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.group_chats
    ADD CONSTRAINT fk_group_chats_id_conversations FOREIGN KEY (id) REFERENCES public.conversations(id);

ALTER TABLE ONLY public.group_chats
    ADD CONSTRAINT fk_group_chats_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.host_request_feedbacks
    ADD CONSTRAINT fk_host_request_feedbacks_from_user_id_users FOREIGN KEY (from_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.host_request_feedbacks
    ADD CONSTRAINT fk_host_request_feedbacks_host_request_id_host_requests FOREIGN KEY (host_request_id) REFERENCES public.host_requests(id);

ALTER TABLE ONLY public.host_request_feedbacks
    ADD CONSTRAINT fk_host_request_feedbacks_to_user_id_users FOREIGN KEY (to_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT fk_host_requests_id_conversations FOREIGN KEY (id) REFERENCES public.conversations(id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT fk_host_requests_initiator_user_id_users FOREIGN KEY (initiator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT fk_host_requests_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT fk_host_requests_public_trip_id_public_trips FOREIGN KEY (public_trip_id) REFERENCES public.public_trips(id);

ALTER TABLE ONLY public.host_requests
    ADD CONSTRAINT fk_host_requests_recipient_user_id_users FOREIGN KEY (recipient_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.initiated_uploads
    ADD CONSTRAINT fk_initiated_uploads_initiator_user_id_users FOREIGN KEY (initiator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.invite_codes
    ADD CONSTRAINT fk_invite_codes_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT fk_invoices_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.language_abilities
    ADD CONSTRAINT fk_language_abilities_language_code_languages FOREIGN KEY (language_code) REFERENCES public.languages(code) DEFERRABLE;

ALTER TABLE ONLY public.language_abilities
    ADD CONSTRAINT fk_language_abilities_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.login_tokens
    ADD CONSTRAINT fk_login_tokens_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.messages
    ADD CONSTRAINT fk_messages_author_id_users FOREIGN KEY (author_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.messages
    ADD CONSTRAINT fk_messages_conversation_id_conversations FOREIGN KEY (conversation_id) REFERENCES public.conversations(id);

ALTER TABLE ONLY public.messages
    ADD CONSTRAINT fk_messages_target_id_users FOREIGN KEY (target_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.mod_notes
    ADD CONSTRAINT fk_mod_notes_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.mod_notes
    ADD CONSTRAINT fk_mod_notes_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.moderation_log
    ADD CONSTRAINT fk_moderation_log_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.moderation_log
    ADD CONSTRAINT fk_moderation_log_moderator_user_id_users FOREIGN KEY (moderator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.moderation_log
    ADD CONSTRAINT fk_moderation_log_queue_item_id_moderation_queue FOREIGN KEY (queue_item_id) REFERENCES public.moderation_queue(id);

ALTER TABLE ONLY public.moderation_queue
    ADD CONSTRAINT fk_moderation_queue_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.moderation_queue
    ADD CONSTRAINT fk_moderation_queue_resolved_by_log_id_moderation_log FOREIGN KEY (resolved_by_log_id) REFERENCES public.moderation_log(id);

ALTER TABLE ONLY public.moderation_user_list_members
    ADD CONSTRAINT fk_moderation_user_list_members_moderation_list_id_mode_4537 FOREIGN KEY (moderation_list_id) REFERENCES public.moderation_user_lists(id);

ALTER TABLE ONLY public.moderation_user_list_members
    ADD CONSTRAINT fk_moderation_user_list_members_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.native_client_users
    ADD CONSTRAINT fk_native_client_users_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.node_cluster_associations
    ADD CONSTRAINT fk_node_cluster_associations_cluster_id_clusters FOREIGN KEY (cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.node_cluster_associations
    ADD CONSTRAINT fk_node_cluster_associations_node_id_nodes FOREIGN KEY (node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.nodes
    ADD CONSTRAINT fk_nodes_parent_node_id_nodes FOREIGN KEY (parent_node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.notification_deliveries
    ADD CONSTRAINT fk_notification_deliveries_notification_id_notifications FOREIGN KEY (notification_id) REFERENCES public.notifications(id);

ALTER TABLE ONLY public.notification_preferences
    ADD CONSTRAINT fk_notification_preferences_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT fk_notifications_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT fk_notifications_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.ota_packages
    ADD CONSTRAINT fk_ota_packages_banned_by_user_id_users FOREIGN KEY (banned_by_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.ota_packages
    ADD CONSTRAINT fk_ota_packages_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.page_versions
    ADD CONSTRAINT fk_page_versions_editor_user_id_users FOREIGN KEY (editor_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.page_versions
    ADD CONSTRAINT fk_page_versions_page_id_pages FOREIGN KEY (page_id) REFERENCES public.pages(id);

ALTER TABLE ONLY public.page_versions
    ADD CONSTRAINT fk_page_versions_photo_key_uploads FOREIGN KEY (photo_key) REFERENCES public.uploads(key);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT fk_pages_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT fk_pages_owner_cluster_id_clusters FOREIGN KEY (owner_cluster_id) REFERENCES public.clusters(id);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT fk_pages_owner_user_id_users FOREIGN KEY (owner_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT fk_pages_parent_node_id_nodes FOREIGN KEY (parent_node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.pages
    ADD CONSTRAINT fk_pages_thread_id_threads FOREIGN KEY (thread_id) REFERENCES public.threads(id);

ALTER TABLE ONLY public.password_reset_tokens
    ADD CONSTRAINT fk_password_reset_tokens_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.photo_galleries
    ADD CONSTRAINT fk_photo_galleries_owner_user_id_users FOREIGN KEY (owner_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.photo_gallery_items
    ADD CONSTRAINT fk_photo_gallery_items_gallery_id_photo_galleries FOREIGN KEY (gallery_id) REFERENCES public.photo_galleries(id);

ALTER TABLE ONLY public.photo_gallery_items
    ADD CONSTRAINT fk_photo_gallery_items_upload_key_uploads FOREIGN KEY (upload_key) REFERENCES public.uploads(key);

ALTER TABLE ONLY public.postal_verification_attempts
    ADD CONSTRAINT fk_postal_verification_attempts_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.public_trips
    ADD CONSTRAINT fk_public_trips_node_id_nodes FOREIGN KEY (node_id) REFERENCES public.nodes(id);

ALTER TABLE ONLY public.public_trips
    ADD CONSTRAINT fk_public_trips_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.push_notification_delivery_attempt
    ADD CONSTRAINT fk_push_notification_delivery_attempt_push_notification_8329 FOREIGN KEY (push_notification_subscription_id) REFERENCES public.push_notification_subscriptions(id);

ALTER TABLE ONLY public.push_notification_subscriptions
    ADD CONSTRAINT fk_push_notification_subscriptions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.rate_limit_violations
    ADD CONSTRAINT fk_rate_limit_violations_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public."references"
    ADD CONSTRAINT fk_references_from_user_id_users FOREIGN KEY (from_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public."references"
    ADD CONSTRAINT fk_references_host_request_id_host_requests FOREIGN KEY (host_request_id) REFERENCES public.host_requests(id);

ALTER TABLE ONLY public."references"
    ADD CONSTRAINT fk_references_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public."references"
    ADD CONSTRAINT fk_references_to_user_id_users FOREIGN KEY (to_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.regions_lived
    ADD CONSTRAINT fk_regions_lived_region_code_regions FOREIGN KEY (region_code) REFERENCES public.regions(code) DEFERRABLE;

ALTER TABLE ONLY public.regions_lived
    ADD CONSTRAINT fk_regions_lived_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.regions_visited
    ADD CONSTRAINT fk_regions_visited_region_code_regions FOREIGN KEY (region_code) REFERENCES public.regions(code) DEFERRABLE;

ALTER TABLE ONLY public.regions_visited
    ADD CONSTRAINT fk_regions_visited_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.replies
    ADD CONSTRAINT fk_replies_author_user_id_users FOREIGN KEY (author_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.replies
    ADD CONSTRAINT fk_replies_comment_id_comments FOREIGN KEY (comment_id) REFERENCES public.comments(id);

ALTER TABLE ONLY public.replies
    ADD CONSTRAINT fk_replies_moderation_state_id_moderation_states FOREIGN KEY (moderation_state_id) REFERENCES public.moderation_states(id);

ALTER TABLE ONLY public.reply_versions
    ADD CONSTRAINT fk_reply_versions_editor_user_id_users FOREIGN KEY (editor_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.reply_versions
    ADD CONSTRAINT fk_reply_versions_reply_id_replies FOREIGN KEY (reply_id) REFERENCES public.replies(id);

ALTER TABLE ONLY public.sessions
    ADD CONSTRAINT fk_sessions_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.signup_flows
    ADD CONSTRAINT fk_signup_flows_invite_code_id_invite_codes FOREIGN KEY (invite_code_id) REFERENCES public.invite_codes(id);

ALTER TABLE ONLY public.strong_verification_attempts
    ADD CONSTRAINT fk_strong_verification_attempts_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.strong_verification_callback_events
    ADD CONSTRAINT fk_strong_verification_callback_events_verification_att_2705 FOREIGN KEY (verification_attempt_id) REFERENCES public.strong_verification_attempts(id);

ALTER TABLE ONLY public.uploads
    ADD CONSTRAINT fk_uploads_creator_user_id_users FOREIGN KEY (creator_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.user_activity
    ADD CONSTRAINT fk_user_activity_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.user_admin_tags
    ADD CONSTRAINT fk_user_admin_tags_admin_tag_id_admin_tags FOREIGN KEY (admin_tag_id) REFERENCES public.admin_tags(id);

ALTER TABLE ONLY public.user_admin_tags
    ADD CONSTRAINT fk_user_admin_tags_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.user_badges
    ADD CONSTRAINT fk_user_badges_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.user_blocks
    ADD CONSTRAINT fk_user_blocks_blocked_user_id_users FOREIGN KEY (blocked_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.user_blocks
    ADD CONSTRAINT fk_user_blocks_blocking_user_id_users FOREIGN KEY (blocking_user_id) REFERENCES public.users(id);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT fk_users_invite_code_id_invite_codes FOREIGN KEY (invite_code_id) REFERENCES public.invite_codes(id);

ALTER TABLE ONLY public.users
    ADD CONSTRAINT fk_users_profile_gallery_id_photo_galleries FOREIGN KEY (profile_gallery_id) REFERENCES public.photo_galleries(id);

ALTER TABLE ONLY public.volunteers
    ADD CONSTRAINT fk_volunteers_user_id_users FOREIGN KEY (user_id) REFERENCES public.users(id);

REVOKE USAGE ON SCHEMA public FROM PUBLIC;

