Skip to content

Email for professionals and teams

55.0 KB
Raw Blame
{
  "migrations": [
    {
      "name": "0000_baseline.sql",
      "statements": [
        "CREATE TABLE \"agent_chat_messages\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"conversation_id\" text NOT NULL,\n\t\"role\" text NOT NULL,\n\t\"content\" text NOT NULL,\n\t\"tool_name\" text,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"agent_conversations\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"title\" text DEFAULT 'New conversation' NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"agent_draft_metadata\" (\n\t\"draft_id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"origin\" text NOT NULL,\n\t\"source_message_id\" text,\n\t\"revision\" bigint DEFAULT 1 NOT NULL,\n\t\"human_edited_at\" bigint,\n\t\"scheduled_at\" bigint,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"agent_jobs\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"source_message_id\" text NOT NULL,\n\t\"reviewer_user_id\" text NOT NULL,\n\t\"status\" text DEFAULT 'pending' NOT NULL,\n\t\"attempts\" bigint DEFAULT 0 NOT NULL,\n\t\"next_attempt_at\" bigint NOT NULL,\n\t\"lease_until\" bigint,\n\t\"draft_id\" text,\n\t\"reason\" text,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"agent_send_approvals\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"draft_id\" text NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"request_key_id\" text,\n\t\"revision\" bigint NOT NULL,\n\t\"payload_hash\" text NOT NULL,\n\t\"status\" text DEFAULT 'pending' NOT NULL,\n\t\"claimed_at\" bigint,\n\t\"message_id\" text,\n\t\"expires_at\" bigint NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"ai_usage\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"provider\" text NOT NULL,\n\t\"model\" text NOT NULL,\n\t\"source\" text NOT NULL,\n\t\"input_tokens\" bigint,\n\t\"output_tokens\" bigint,\n\t\"cost_usd_micros\" bigint\n);",
        "CREATE TABLE \"api_keys\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"kind\" text DEFAULT 'legacy' NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"prefix\" text NOT NULL,\n\t\"key_hash\" text NOT NULL,\n\t\"scopes\" text NOT NULL,\n\t\"mailbox_scope_enabled\" boolean DEFAULT false NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"last_used_at\" bigint\n);",
        "CREATE TABLE \"app_settings\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"app_name\" text DEFAULT 'Mailflare' NOT NULL,\n\t\"outbound_attachment_max_mb\" bigint DEFAULT 25 NOT NULL,\n\t\"drive_storage_limit_bytes\" bigint,\n\t\"resend_api_key\" text,\n\t\"aws_config\" text,\n\t\"ses_receiving\" text,\n\t\"resend_webhook_id\" text,\n\t\"resend_webhook_secret\" text,\n\t\"icon_key\" text,\n\t\"agent_enabled\" boolean DEFAULT true NOT NULL,\n\t\"agent_provider\" text,\n\t\"agent_preset\" text,\n\t\"agent_base_url\" text,\n\t\"agent_api_key\" text,\n\t\"agent_model\" text,\n\t\"agent_model_rates\" text,\n\t\"spamhaus_dqs_key\" text,\n\t\"abusech_auth_key\" text,\n\t\"spam_domain_age_check\" boolean DEFAULT true NOT NULL,\n\t\"spam_brands\" text,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"audit_logs\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"actor_user_id\" text,\n\t\"target_user_id\" text,\n\t\"mailbox_id\" text,\n\t\"message_id\" text,\n\t\"action\" text NOT NULL,\n\t\"metadata\" text,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"auto_reply_deliveries\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"recipient\" text NOT NULL,\n\t\"sent_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"backup_settings\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"enabled\" boolean DEFAULT true NOT NULL,\n\t\"schedule_type\" text DEFAULT 'daily' NOT NULL,\n\t\"schedule_value\" bigint,\n\t\"retention_enabled\" boolean DEFAULT false NOT NULL,\n\t\"retention_days\" bigint DEFAULT 30 NOT NULL,\n\t\"excluded_table_groups\" text DEFAULT '[]' NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"backups\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"status\" text DEFAULT 'queued' NOT NULL,\n\t\"trigger\" text NOT NULL,\n\t\"r2_key\" text,\n\t\"filename\" text,\n\t\"size\" bigint,\n\t\"error\" text,\n\t\"created_by_user_id\" text,\n\t\"created_at\" bigint NOT NULL,\n\t\"started_at\" bigint,\n\t\"completed_at\" bigint\n);",
        "CREATE TABLE \"booking_events\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"slug\" text DEFAULT '' NOT NULL,\n\t\"description\" text DEFAULT '' NOT NULL,\n\t\"color\" text DEFAULT '#2563eb' NOT NULL,\n\t\"host_ids\" text DEFAULT '[]' NOT NULL,\n\t\"duration_minutes\" bigint NOT NULL,\n\t\"location\" text DEFAULT '' NOT NULL,\n\t\"weekdays\" text DEFAULT '[1,2,3,4,5]' NOT NULL,\n\t\"start_time\" text DEFAULT '09:00' NOT NULL,\n\t\"end_time\" text DEFAULT '17:00' NOT NULL,\n\t\"time_ranges\" text DEFAULT '[{\"startTime\":\"09:00\",\"endTime\":\"17:00\"}]' NOT NULL,\n\t\"time_zone\" text DEFAULT 'UTC' NOT NULL,\n\t\"enabled\" boolean DEFAULT true NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"calendar_events\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"mailbox_id\" text,\n\t\"title\" text NOT NULL,\n\t\"description\" text DEFAULT '' NOT NULL,\n\t\"location\" text DEFAULT '' NOT NULL,\n\t\"attendees\" text DEFAULT '[]' NOT NULL,\n\t\"color\" text DEFAULT 'blue' NOT NULL,\n\t\"repeat\" text DEFAULT 'none' NOT NULL,\n\t\"repeat_days\" text DEFAULT '[]' NOT NULL,\n\t\"repeat_anchor_day\" bigint,\n\t\"repeat_until\" bigint,\n\t\"excluded_occurrences\" text DEFAULT '[]' NOT NULL,\n\t\"time_zone\" text,\n\t\"starts_at\" bigint NOT NULL,\n\t\"ends_at\" bigint NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"contacts\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"email\" text NOT NULL,\n\t\"display_name\" text,\n\t\"avatar_key\" text,\n\t\"source\" text DEFAULT 'inbound' NOT NULL,\n\t\"blocked\" boolean DEFAULT false NOT NULL,\n\t\"remote_content_allowed\" boolean DEFAULT false NOT NULL,\n\t\"last_seen_at\" bigint,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"domain_tracking_hosts\" (\n\t\"domain_id\" text PRIMARY KEY NOT NULL,\n\t\"hostname\" text NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"domains\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"hostname\" text NOT NULL,\n\t\"zone_id\" text NOT NULL,\n\t\"status\" text DEFAULT 'pending' NOT NULL,\n\t\"routing_status\" text,\n\t\"sending_subdomain_tag\" text,\n\t\"sending_provider\" text DEFAULT 'none' NOT NULL,\n\t\"receiving_provider\" text DEFAULT 'cloudflare' NOT NULL,\n\t\"sending_requested\" boolean DEFAULT false NOT NULL,\n\t\"sending_enabled\" boolean DEFAULT false NOT NULL,\n\t\"routing_enabled\" boolean DEFAULT false NOT NULL,\n\t\"dns_skipped\" boolean DEFAULT false NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"drive_items\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"owner_id\" text NOT NULL,\n\t\"parent_id\" text,\n\t\"kind\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"r2_key\" text,\n\t\"size\" bigint DEFAULT 0 NOT NULL,\n\t\"content_type\" text DEFAULT 'application/octet-stream' NOT NULL,\n\t\"upload_id\" text,\n\t\"upload_part_size\" bigint,\n\t\"upload_fingerprint\" text,\n\t\"link_token\" text,\n\t\"trashed_at\" bigint,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"drive_shares\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"item_id\" text NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"role\" text DEFAULT 'view' NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"drive_upload_parts\" (\n\t\"item_id\" text NOT NULL,\n\t\"part_number\" bigint NOT NULL,\n\t\"etag\" text NOT NULL\n);",
        "CREATE TABLE \"email_templates\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"subject\" text DEFAULT '' NOT NULL,\n\t\"text_body\" text DEFAULT '' NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"folders\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"color\" text DEFAULT '#2563eb' NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"follow_ups\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"message_id\" text NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"due_at\" bigint NOT NULL,\n\t\"status\" text DEFAULT 'pending' NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"import_jobs\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"kind\" text NOT NULL,\n\t\"label\" text NOT NULL,\n\t\"status\" text DEFAULT 'queued' NOT NULL,\n\t\"total\" bigint DEFAULT 0 NOT NULL,\n\t\"processed\" bigint DEFAULT 0 NOT NULL,\n\t\"imported\" bigint DEFAULT 0 NOT NULL,\n\t\"skipped\" bigint DEFAULT 0 NOT NULL,\n\t\"spec\" text NOT NULL,\n\t\"cursor\" text DEFAULT '{}' NOT NULL,\n\t\"errors\" text DEFAULT '[]' NOT NULL,\n\t\"error_count\" bigint DEFAULT 0 NOT NULL,\n\t\"error\" text,\n\t\"created_at\" bigint NOT NULL,\n\t\"started_at\" bigint,\n\t\"updated_at\" bigint NOT NULL,\n\t\"finished_at\" bigint\n);",
        "CREATE TABLE \"jmap_change_floor\" (\n\t\"id\" bigint PRIMARY KEY NOT NULL,\n\t\"seq\" bigint DEFAULT 0 NOT NULL\n);",
        "CREATE TABLE \"jmap_changes\" (\n\t\"seq\" bigserial PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"object_type\" text NOT NULL,\n\t\"object_id\" text NOT NULL,\n\t\"thread_id\" text,\n\t\"change\" text NOT NULL,\n\t\"created_at\" bigint DEFAULT (extract(epoch from now())::bigint) NOT NULL\n);",
        "CREATE TABLE \"jmap_mailbox_revisions\" (\n\t\"mailbox_id\" text PRIMARY KEY NOT NULL,\n\t\"revision\" bigint DEFAULT 0 NOT NULL\n);",
        "CREATE TABLE \"license_settings\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"instance_id\" text NOT NULL,\n\t\"instance_url\" text,\n\t\"license_key_hash\" text,\n\t\"plan\" text DEFAULT 'community' NOT NULL,\n\t\"state\" text DEFAULT 'inactive' NOT NULL,\n\t\"features\" text DEFAULT '[]' NOT NULL,\n\t\"seat_limit\" bigint DEFAULT 1,\n\t\"activated_at\" bigint,\n\t\"validated_at\" bigint,\n\t\"updated_at\" bigint NOT NULL,\n\tCONSTRAINT \"license_settings_instance_id_unique\" UNIQUE(\"instance_id\")\n);",
        "CREATE TABLE \"login_challenges\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"token_hash\" text NOT NULL,\n\t\"expires_at\" bigint NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"login_challenges_token_hash_unique\" UNIQUE(\"token_hash\")\n);",
        "CREATE TABLE \"mailbox_access\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"permission\" text DEFAULT 'read_only' NOT NULL,\n\t\"created_by_user_id\" text,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"mailbox_agent_settings\" (\n\t\"mailbox_id\" text PRIMARY KEY NOT NULL,\n\t\"enabled\" boolean DEFAULT false NOT NULL,\n\t\"model_id\" text,\n\t\"auto_draft_enabled\" boolean DEFAULT false NOT NULL,\n\t\"reviewer_user_id\" text,\n\t\"instructions\" text DEFAULT '' NOT NULL,\n\t\"daily_limit\" bigint DEFAULT 25 NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"mailbox_aliases\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"domain_id\" text NOT NULL,\n\t\"local_part\" text NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"mailboxes\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"domain_id\" text NOT NULL,\n\t\"local_part\" text NOT NULL,\n\t\"display_name\" text,\n\t\"signature\" text,\n\t\"auto_reply_enabled\" boolean DEFAULT false NOT NULL,\n\t\"auto_reply_subject\" text DEFAULT 'Out of office' NOT NULL,\n\t\"auto_reply_body\" text DEFAULT '' NOT NULL,\n\t\"avatar_key\" text,\n\t\"type\" text DEFAULT 'personal' NOT NULL,\n\t\"use_all_domains\" boolean DEFAULT true NOT NULL,\n\t\"disabled\" boolean DEFAULT false NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"mcp_key_mailboxes\" (\n\t\"key_id\" text NOT NULL,\n\t\"mailbox_id\" text NOT NULL\n);",
        "CREATE TABLE \"message_attachments\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"message_id\" text NOT NULL,\n\t\"filename\" text NOT NULL,\n\t\"content_type\" text NOT NULL,\n\t\"size\" bigint NOT NULL,\n\t\"disposition\" text DEFAULT 'attachment' NOT NULL,\n\t\"content_id\" text,\n\t\"r2_key\" text NOT NULL,\n\t\"trashed_at\" bigint,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"message_attachments_r2_key_unique\" UNIQUE(\"r2_key\")\n);",
        "CREATE TABLE \"message_opens\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"token\" text NOT NULL,\n\t\"opened_at\" bigint NOT NULL,\n\t\"client\" text,\n\t\"device\" text,\n\t\"country\" text,\n\t\"city\" text,\n\t\"automated\" boolean DEFAULT false NOT NULL\n);",
        "CREATE TABLE \"message_tracking\" (\n\t\"token\" text PRIMARY KEY NOT NULL,\n\t\"message_id\" text NOT NULL,\n\t\"open_count\" bigint DEFAULT 0 NOT NULL,\n\t\"first_opened_at\" bigint,\n\t\"last_opened_at\" bigint,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"messages\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"mailbox_id\" text,\n\t\"direction\" text NOT NULL,\n\t\"provider_message_id\" text,\n\t\"folder_id\" text,\n\t\"from_addr\" text NOT NULL,\n\t\"to_addr\" text NOT NULL,\n\t\"cc_addr\" text,\n\t\"bcc_addr\" text,\n\t\"reply_to\" text,\n\t\"subject\" text,\n\t\"snippet\" text,\n\t\"text_body\" text,\n\t\"html_body\" text,\n\t\"raw_r2_key\" text,\n\t\"status\" text DEFAULT 'received' NOT NULL,\n\t\"read\" boolean DEFAULT false NOT NULL,\n\t\"starred\" boolean DEFAULT false NOT NULL,\n\t\"snoozed_until\" bigint,\n\t\"thread_id\" text,\n\t\"in_reply_to\" text,\n\t\"references_header\" text,\n\t\"spam_score\" bigint,\n\t\"spam_verdict\" text,\n\t\"spam_signals\" text,\n\t\"spam_analyzed_at\" bigint,\n\t\"spam_analysis_error\" text,\n\t\"trashed_at\" bigint,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"mfa_recovery_codes\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"code_hash\" text NOT NULL,\n\t\"used_at\" bigint,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"mfa_recovery_codes_code_hash_unique\" UNIQUE(\"code_hash\")\n);",
        "CREATE TABLE \"outbound_jobs\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"message_id\" text,\n\t\"status\" text DEFAULT 'queued' NOT NULL,\n\t\"payload\" text NOT NULL,\n\t\"error\" text,\n\t\"scheduled_at\" bigint,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"password_reset_tokens\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"token_hash\" text NOT NULL,\n\t\"expires_at\" bigint NOT NULL,\n\t\"used_at\" bigint,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"password_reset_tokens_token_hash_unique\" UNIQUE(\"token_hash\")\n);",
        "CREATE TABLE \"push_devices\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"api_key_id\" text NOT NULL,\n\t\"platform\" text NOT NULL,\n\t\"token\" text NOT NULL,\n\t\"environment\" text DEFAULT 'production' NOT NULL,\n\t\"topic\" text NOT NULL,\n\t\"public_key\" text,\n\t\"previews\" boolean DEFAULT true NOT NULL,\n\t\"client_ref\" text,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"push_subscriptions\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"session_id\" text NOT NULL,\n\t\"endpoint\" text NOT NULL,\n\t\"p256dh\" text NOT NULL,\n\t\"auth\" text NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"routing_rules\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"domain_id\" text NOT NULL,\n\t\"scope\" text DEFAULT 'mailbox' NOT NULL,\n\t\"name\" text,\n\t\"enabled\" boolean DEFAULT true NOT NULL,\n\t\"pattern\" text NOT NULL,\n\t\"match_field\" text DEFAULT 'email' NOT NULL,\n\t\"match_operator\" text DEFAULT 'contains' NOT NULL,\n\t\"match_value\" text DEFAULT '' NOT NULL,\n\t\"mailbox_id\" text,\n\t\"folder_id\" text,\n\t\"action\" text DEFAULT 'store' NOT NULL,\n\t\"forward_to\" text,\n\t\"keep_copy\" boolean DEFAULT false NOT NULL,\n\t\"reject_reason\" text,\n\t\"priority\" bigint DEFAULT 0 NOT NULL,\n\t\"last_matched_at\" bigint,\n\t\"match_count\" bigint DEFAULT 0 NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"sessions\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"token_hash\" text NOT NULL,\n\t\"expires_at\" bigint NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"sessions_token_hash_unique\" UNIQUE(\"token_hash\")\n);",
        "CREATE TABLE \"shared_attachment_links\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"attachment_id\" text NOT NULL,\n\t\"expires_at\" bigint NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"shared_attachment_links_attachment_id_unique\" UNIQUE(\"attachment_id\")\n);",
        "CREATE TABLE \"spam_feedback\" (\n\t\"message_id\" text PRIMARY KEY NOT NULL,\n\t\"mailbox_id\" text NOT NULL,\n\t\"actor_user_id\" text,\n\t\"classification\" text NOT NULL,\n\t\"training_tokens\" text NOT NULL,\n\t\"reputation_keys\" text NOT NULL,\n\t\"tokenizer_version\" bigint DEFAULT 1 NOT NULL,\n\t\"created_at\" bigint NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"spam_reputation\" (\n\t\"mailbox_id\" text NOT NULL,\n\t\"type\" text NOT NULL,\n\t\"key\" text NOT NULL,\n\t\"messages_seen\" bigint DEFAULT 0 NOT NULL,\n\t\"spam_count\" bigint DEFAULT 0 NOT NULL,\n\t\"ham_count\" bigint DEFAULT 0 NOT NULL,\n\t\"first_seen_at\" bigint NOT NULL,\n\t\"last_seen_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"spam_token_stats\" (\n\t\"mailbox_id\" text NOT NULL,\n\t\"token\" text NOT NULL,\n\t\"spam_count\" bigint DEFAULT 0 NOT NULL,\n\t\"ham_count\" bigint DEFAULT 0 NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"user_compose_preferences\" (\n\t\"user_id\" text PRIMARY KEY NOT NULL,\n\t\"track_opens_default\" boolean DEFAULT false NOT NULL,\n\t\"undo_send_seconds\" bigint DEFAULT 15 NOT NULL,\n\t\"updated_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"users\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"email\" text NOT NULL,\n\t\"reset_email\" text,\n\t\"forwarding_email\" text,\n\t\"password_hash\" text NOT NULL,\n\t\"name\" text NOT NULL,\n\t\"booking_username\" text,\n\t\"time_zone\" text,\n\t\"avatar_key\" text,\n\t\"role\" text DEFAULT 'user' NOT NULL,\n\t\"is_primary_admin\" boolean DEFAULT false NOT NULL,\n\t\"disabled\" boolean DEFAULT false NOT NULL,\n\t\"can_manage_mailboxes\" boolean DEFAULT false NOT NULL,\n\t\"can_manage_domains\" boolean DEFAULT false NOT NULL,\n\t\"can_manage_users\" boolean DEFAULT false NOT NULL,\n\t\"keyboard_shortcuts_enabled\" boolean DEFAULT true NOT NULL,\n\t\"spam_protection_enabled\" boolean DEFAULT true NOT NULL,\n\t\"trash_retention_days\" bigint,\n\t\"show_full_recipient_addresses\" boolean DEFAULT false NOT NULL,\n\t\"remote_content_mode\" text DEFAULT 'allow' NOT NULL,\n\t\"totp_secret\" text,\n\t\"totp_enabled\" boolean DEFAULT false NOT NULL,\n\t\"totp_confirmed_at\" bigint,\n\t\"last_sign_in_at\" bigint,\n\t\"deleted_at\" bigint,\n\t\"created_by_user_id\" text,\n\t\"created_at\" bigint NOT NULL,\n\tCONSTRAINT \"users_email_unique\" UNIQUE(\"email\")\n);",
        "CREATE TABLE \"webhook_deliveries\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"webhook_id\" text NOT NULL,\n\t\"event_type\" text NOT NULL,\n\t\"payload\" text NOT NULL,\n\t\"status\" text DEFAULT 'pending' NOT NULL,\n\t\"attempts\" bigint DEFAULT 0 NOT NULL,\n\t\"response_status\" bigint,\n\t\"error\" text,\n\t\"duration_ms\" bigint,\n\t\"last_attempt_at\" bigint,\n\t\"next_retry_at\" bigint,\n\t\"created_at\" bigint NOT NULL\n);",
        "CREATE TABLE \"webhooks\" (\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"user_id\" text NOT NULL,\n\t\"description\" text,\n\t\"url\" text NOT NULL,\n\t\"secret\" text NOT NULL,\n\t\"events\" text NOT NULL,\n\t\"enabled\" boolean DEFAULT true NOT NULL,\n\t\"max_attempts\" bigint DEFAULT 5 NOT NULL,\n\t\"created_at\" bigint NOT NULL\n);",
        "ALTER TABLE \"agent_chat_messages\" ADD CONSTRAINT \"agent_chat_messages_conversation_id_agent_conversations_id_fk\" FOREIGN KEY (\"conversation_id\") REFERENCES \"public\".\"agent_conversations\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_conversations\" ADD CONSTRAINT \"agent_conversations_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_conversations\" ADD CONSTRAINT \"agent_conversations_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_draft_metadata\" ADD CONSTRAINT \"agent_draft_metadata_draft_id_messages_id_fk\" FOREIGN KEY (\"draft_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_draft_metadata\" ADD CONSTRAINT \"agent_draft_metadata_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_draft_metadata\" ADD CONSTRAINT \"agent_draft_metadata_source_message_id_messages_id_fk\" FOREIGN KEY (\"source_message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"agent_jobs\" ADD CONSTRAINT \"agent_jobs_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_jobs\" ADD CONSTRAINT \"agent_jobs_source_message_id_messages_id_fk\" FOREIGN KEY (\"source_message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_jobs\" ADD CONSTRAINT \"agent_jobs_reviewer_user_id_users_id_fk\" FOREIGN KEY (\"reviewer_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_jobs\" ADD CONSTRAINT \"agent_jobs_draft_id_messages_id_fk\" FOREIGN KEY (\"draft_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"agent_send_approvals\" ADD CONSTRAINT \"agent_send_approvals_draft_id_messages_id_fk\" FOREIGN KEY (\"draft_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_send_approvals\" ADD CONSTRAINT \"agent_send_approvals_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_send_approvals\" ADD CONSTRAINT \"agent_send_approvals_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"agent_send_approvals\" ADD CONSTRAINT \"agent_send_approvals_request_key_id_api_keys_id_fk\" FOREIGN KEY (\"request_key_id\") REFERENCES \"public\".\"api_keys\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"api_keys\" ADD CONSTRAINT \"api_keys_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"audit_logs\" ADD CONSTRAINT \"audit_logs_actor_user_id_users_id_fk\" FOREIGN KEY (\"actor_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"audit_logs\" ADD CONSTRAINT \"audit_logs_target_user_id_users_id_fk\" FOREIGN KEY (\"target_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"audit_logs\" ADD CONSTRAINT \"audit_logs_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"audit_logs\" ADD CONSTRAINT \"audit_logs_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"auto_reply_deliveries\" ADD CONSTRAINT \"auto_reply_deliveries_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"backups\" ADD CONSTRAINT \"backups_created_by_user_id_users_id_fk\" FOREIGN KEY (\"created_by_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"booking_events\" ADD CONSTRAINT \"booking_events_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"calendar_events\" ADD CONSTRAINT \"calendar_events_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"calendar_events\" ADD CONSTRAINT \"calendar_events_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"contacts\" ADD CONSTRAINT \"contacts_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"domain_tracking_hosts\" ADD CONSTRAINT \"domain_tracking_hosts_domain_id_domains_id_fk\" FOREIGN KEY (\"domain_id\") REFERENCES \"public\".\"domains\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"domains\" ADD CONSTRAINT \"domains_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"drive_items\" ADD CONSTRAINT \"drive_items_owner_id_users_id_fk\" FOREIGN KEY (\"owner_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"drive_shares\" ADD CONSTRAINT \"drive_shares_item_id_drive_items_id_fk\" FOREIGN KEY (\"item_id\") REFERENCES \"public\".\"drive_items\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"drive_shares\" ADD CONSTRAINT \"drive_shares_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"drive_upload_parts\" ADD CONSTRAINT \"drive_upload_parts_item_id_drive_items_id_fk\" FOREIGN KEY (\"item_id\") REFERENCES \"public\".\"drive_items\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"email_templates\" ADD CONSTRAINT \"email_templates_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"folders\" ADD CONSTRAINT \"folders_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"folders\" ADD CONSTRAINT \"folders_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"follow_ups\" ADD CONSTRAINT \"follow_ups_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"follow_ups\" ADD CONSTRAINT \"follow_ups_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"import_jobs\" ADD CONSTRAINT \"import_jobs_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"import_jobs\" ADD CONSTRAINT \"import_jobs_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"login_challenges\" ADD CONSTRAINT \"login_challenges_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailbox_access\" ADD CONSTRAINT \"mailbox_access_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailbox_access\" ADD CONSTRAINT \"mailbox_access_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailbox_access\" ADD CONSTRAINT \"mailbox_access_created_by_user_id_users_id_fk\" FOREIGN KEY (\"created_by_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"mailbox_agent_settings\" ADD CONSTRAINT \"mailbox_agent_settings_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailbox_agent_settings\" ADD CONSTRAINT \"mailbox_agent_settings_reviewer_user_id_users_id_fk\" FOREIGN KEY (\"reviewer_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"mailbox_aliases\" ADD CONSTRAINT \"mailbox_aliases_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailbox_aliases\" ADD CONSTRAINT \"mailbox_aliases_domain_id_domains_id_fk\" FOREIGN KEY (\"domain_id\") REFERENCES \"public\".\"domains\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailboxes\" ADD CONSTRAINT \"mailboxes_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mailboxes\" ADD CONSTRAINT \"mailboxes_domain_id_domains_id_fk\" FOREIGN KEY (\"domain_id\") REFERENCES \"public\".\"domains\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mcp_key_mailboxes\" ADD CONSTRAINT \"mcp_key_mailboxes_key_id_api_keys_id_fk\" FOREIGN KEY (\"key_id\") REFERENCES \"public\".\"api_keys\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"mcp_key_mailboxes\" ADD CONSTRAINT \"mcp_key_mailboxes_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"message_attachments\" ADD CONSTRAINT \"message_attachments_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"message_opens\" ADD CONSTRAINT \"message_opens_token_message_tracking_token_fk\" FOREIGN KEY (\"token\") REFERENCES \"public\".\"message_tracking\"(\"token\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"message_tracking\" ADD CONSTRAINT \"message_tracking_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"messages\" ADD CONSTRAINT \"messages_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"messages\" ADD CONSTRAINT \"messages_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"messages\" ADD CONSTRAINT \"messages_folder_id_folders_id_fk\" FOREIGN KEY (\"folder_id\") REFERENCES \"public\".\"folders\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"mfa_recovery_codes\" ADD CONSTRAINT \"mfa_recovery_codes_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"outbound_jobs\" ADD CONSTRAINT \"outbound_jobs_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"outbound_jobs\" ADD CONSTRAINT \"outbound_jobs_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"password_reset_tokens\" ADD CONSTRAINT \"password_reset_tokens_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"push_devices\" ADD CONSTRAINT \"push_devices_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"push_devices\" ADD CONSTRAINT \"push_devices_api_key_id_api_keys_id_fk\" FOREIGN KEY (\"api_key_id\") REFERENCES \"public\".\"api_keys\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"push_subscriptions\" ADD CONSTRAINT \"push_subscriptions_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"push_subscriptions\" ADD CONSTRAINT \"push_subscriptions_session_id_sessions_id_fk\" FOREIGN KEY (\"session_id\") REFERENCES \"public\".\"sessions\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"routing_rules\" ADD CONSTRAINT \"routing_rules_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"routing_rules\" ADD CONSTRAINT \"routing_rules_domain_id_domains_id_fk\" FOREIGN KEY (\"domain_id\") REFERENCES \"public\".\"domains\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"routing_rules\" ADD CONSTRAINT \"routing_rules_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"routing_rules\" ADD CONSTRAINT \"routing_rules_folder_id_folders_id_fk\" FOREIGN KEY (\"folder_id\") REFERENCES \"public\".\"folders\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"sessions\" ADD CONSTRAINT \"sessions_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"shared_attachment_links\" ADD CONSTRAINT \"shared_attachment_links_attachment_id_message_attachments_id_fk\" FOREIGN KEY (\"attachment_id\") REFERENCES \"public\".\"message_attachments\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"spam_feedback\" ADD CONSTRAINT \"spam_feedback_message_id_messages_id_fk\" FOREIGN KEY (\"message_id\") REFERENCES \"public\".\"messages\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"spam_feedback\" ADD CONSTRAINT \"spam_feedback_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"spam_feedback\" ADD CONSTRAINT \"spam_feedback_actor_user_id_users_id_fk\" FOREIGN KEY (\"actor_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"spam_reputation\" ADD CONSTRAINT \"spam_reputation_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"spam_token_stats\" ADD CONSTRAINT \"spam_token_stats_mailbox_id_mailboxes_id_fk\" FOREIGN KEY (\"mailbox_id\") REFERENCES \"public\".\"mailboxes\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"user_compose_preferences\" ADD CONSTRAINT \"user_compose_preferences_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"users\" ADD CONSTRAINT \"users_created_by_user_id_users_id_fk\" FOREIGN KEY (\"created_by_user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE set null ON UPDATE no action;",
        "ALTER TABLE \"webhook_deliveries\" ADD CONSTRAINT \"webhook_deliveries_webhook_id_webhooks_id_fk\" FOREIGN KEY (\"webhook_id\") REFERENCES \"public\".\"webhooks\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "ALTER TABLE \"webhooks\" ADD CONSTRAINT \"webhooks_user_id_users_id_fk\" FOREIGN KEY (\"user_id\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
        "CREATE INDEX \"agent_chat_messages_conversation_idx\" ON \"agent_chat_messages\" USING btree (\"conversation_id\",\"created_at\");",
        "CREATE INDEX \"agent_conversations_owner_idx\" ON \"agent_conversations\" USING btree (\"user_id\",\"mailbox_id\",\"updated_at\");",
        "CREATE INDEX \"agent_conversations_mailbox_idx\" ON \"agent_conversations\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"agent_draft_meta_source_msg_idx\" ON \"agent_draft_metadata\" USING btree (\"source_message_id\");",
        "CREATE INDEX \"agent_draft_metadata_mailbox_idx\" ON \"agent_draft_metadata\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"agent_jobs_source_idx\" ON \"agent_jobs\" USING btree (\"mailbox_id\",\"source_message_id\");",
        "CREATE INDEX \"agent_jobs_due_idx\" ON \"agent_jobs\" USING btree (\"status\",\"next_attempt_at\");",
        "CREATE INDEX \"agent_jobs_source_message_idx\" ON \"agent_jobs\" USING btree (\"source_message_id\");",
        "CREATE INDEX \"agent_jobs_draft_idx\" ON \"agent_jobs\" USING btree (\"draft_id\");",
        "CREATE INDEX \"agent_jobs_reviewer_idx\" ON \"agent_jobs\" USING btree (\"reviewer_user_id\");",
        "CREATE INDEX \"agent_send_approvals_user_idx\" ON \"agent_send_approvals\" USING btree (\"user_id\",\"status\");",
        "CREATE UNIQUE INDEX \"agent_send_approvals_delivery_idx\" ON \"agent_send_approvals\" USING btree (\"draft_id\") WHERE status IN ('claimed', 'sent', 'unknown');",
        "CREATE INDEX \"agent_send_approvals_request_key_idx\" ON \"agent_send_approvals\" USING btree (\"request_key_id\");",
        "CREATE INDEX \"agent_send_approvals_mailbox_idx\" ON \"agent_send_approvals\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"agent_send_approvals_draft_idx\" ON \"agent_send_approvals\" USING btree (\"draft_id\");",
        "CREATE INDEX \"ai_usage_created_idx\" ON \"ai_usage\" USING btree (\"created_at\");",
        "CREATE INDEX \"api_keys_user_idx\" ON \"api_keys\" USING btree (\"user_id\");",
        "CREATE INDEX \"audit_logs_actor_idx\" ON \"audit_logs\" USING btree (\"actor_user_id\");",
        "CREATE INDEX \"audit_logs_mailbox_idx\" ON \"audit_logs\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"audit_logs_created_idx\" ON \"audit_logs\" USING btree (\"created_at\");",
        "CREATE INDEX \"audit_logs_message_id_idx\" ON \"audit_logs\" USING btree (\"message_id\");",
        "CREATE INDEX \"audit_logs_target_user_idx\" ON \"audit_logs\" USING btree (\"target_user_id\");",
        "CREATE UNIQUE INDEX \"auto_reply_deliveries_mailbox_recipient_idx\" ON \"auto_reply_deliveries\" USING btree (\"mailbox_id\",\"recipient\");",
        "CREATE INDEX \"auto_reply_deliveries_sent_idx\" ON \"auto_reply_deliveries\" USING btree (\"sent_at\");",
        "CREATE INDEX \"backups_created_idx\" ON \"backups\" USING btree (\"created_at\");",
        "CREATE INDEX \"backups_status_idx\" ON \"backups\" USING btree (\"status\");",
        "CREATE INDEX \"backups_created_by_idx\" ON \"backups\" USING btree (\"created_by_user_id\");",
        "CREATE INDEX \"booking_events_user_idx\" ON \"booking_events\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"booking_events_user_slug_idx\" ON \"booking_events\" USING btree (\"user_id\",\"slug\");",
        "CREATE INDEX \"calendar_events_user_starts_idx\" ON \"calendar_events\" USING btree (\"user_id\",\"starts_at\");",
        "CREATE INDEX \"calendar_events_mailbox_idx\" ON \"calendar_events\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"contacts_user_email_idx\" ON \"contacts\" USING btree (\"user_id\",\"email\");",
        "CREATE INDEX \"contacts_user_idx\" ON \"contacts\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"domain_tracking_hosts_hostname_idx\" ON \"domain_tracking_hosts\" USING btree (\"hostname\");",
        "CREATE UNIQUE INDEX \"domains_hostname_idx\" ON \"domains\" USING btree (\"hostname\");",
        "CREATE INDEX \"domains_user_idx\" ON \"domains\" USING btree (\"user_id\");",
        "CREATE INDEX \"drive_items_owner_parent_idx\" ON \"drive_items\" USING btree (\"owner_id\",\"parent_id\");",
        "CREATE UNIQUE INDEX \"drive_items_link_token_idx\" ON \"drive_items\" USING btree (\"link_token\");",
        "CREATE UNIQUE INDEX \"drive_shares_item_user_idx\" ON \"drive_shares\" USING btree (\"item_id\",\"user_id\");",
        "CREATE INDEX \"drive_shares_user_idx\" ON \"drive_shares\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"drive_upload_parts_idx\" ON \"drive_upload_parts\" USING btree (\"item_id\",\"part_number\");",
        "CREATE INDEX \"email_templates_user_idx\" ON \"email_templates\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"folders_mailbox_name_idx\" ON \"folders\" USING btree (\"mailbox_id\",\"name\");",
        "CREATE INDEX \"folders_user_idx\" ON \"folders\" USING btree (\"user_id\");",
        "CREATE INDEX \"folders_mailbox_idx\" ON \"folders\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"follow_ups_message_idx\" ON \"follow_ups\" USING btree (\"message_id\");",
        "CREATE INDEX \"follow_ups_status_due_idx\" ON \"follow_ups\" USING btree (\"status\",\"due_at\");",
        "CREATE INDEX \"follow_ups_user_status_idx\" ON \"follow_ups\" USING btree (\"user_id\",\"status\");",
        "CREATE INDEX \"import_jobs_user_idx\" ON \"import_jobs\" USING btree (\"user_id\",\"created_at\");",
        "CREATE INDEX \"import_jobs_status_idx\" ON \"import_jobs\" USING btree (\"status\",\"updated_at\");",
        "CREATE INDEX \"jmap_changes_mailbox_seq_idx\" ON \"jmap_changes\" USING btree (\"mailbox_id\",\"seq\");",
        "CREATE INDEX \"login_challenges_user_idx\" ON \"login_challenges\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"mailbox_access_mailbox_user_idx\" ON \"mailbox_access\" USING btree (\"mailbox_id\",\"user_id\");",
        "CREATE INDEX \"mailbox_access_user_idx\" ON \"mailbox_access\" USING btree (\"user_id\");",
        "CREATE INDEX \"mailbox_access_mailbox_idx\" ON \"mailbox_access\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"mailbox_access_created_by_idx\" ON \"mailbox_access\" USING btree (\"created_by_user_id\");",
        "CREATE INDEX \"mailbox_agent_settings_reviewer_idx\" ON \"mailbox_agent_settings\" USING btree (\"reviewer_user_id\");",
        "CREATE UNIQUE INDEX \"mailbox_aliases_address_idx\" ON \"mailbox_aliases\" USING btree (\"domain_id\",\"local_part\");",
        "CREATE INDEX \"mailbox_aliases_mailbox_idx\" ON \"mailbox_aliases\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"mailboxes_address_idx\" ON \"mailboxes\" USING btree (\"domain_id\",\"local_part\");",
        "CREATE INDEX \"mailboxes_user_idx\" ON \"mailboxes\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"mcp_key_mailbox_idx\" ON \"mcp_key_mailboxes\" USING btree (\"key_id\",\"mailbox_id\");",
        "CREATE INDEX \"mcp_key_mailboxes_mailbox_idx\" ON \"mcp_key_mailboxes\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"message_attachments_message_idx\" ON \"message_attachments\" USING btree (\"message_id\");",
        "CREATE INDEX \"message_opens_token_idx\" ON \"message_opens\" USING btree (\"token\",\"opened_at\");",
        "CREATE UNIQUE INDEX \"message_tracking_message_idx\" ON \"message_tracking\" USING btree (\"message_id\");",
        "CREATE INDEX \"messages_user_created_idx\" ON \"messages\" USING btree (\"user_id\",\"created_at\");",
        "CREATE INDEX \"messages_mailbox_idx\" ON \"messages\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"messages_folder_idx\" ON \"messages\" USING btree (\"folder_id\");",
        "CREATE INDEX \"messages_thread_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"thread_id\");",
        "CREATE INDEX \"messages_provider_message_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"provider_message_id\");",
        "CREATE INDEX \"messages_raw_r2_key_idx\" ON \"messages\" USING btree (\"raw_r2_key\");",
        "CREATE INDEX \"messages_trashed_at_idx\" ON \"messages\" USING btree (\"trashed_at\");",
        "CREATE INDEX \"messages_inbox_page_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"status\",\"folder_id\",\"created_at\" desc,\"id\" desc);",
        "CREATE INDEX \"messages_thread_key_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"status\",\"folder_id\",coalesce(\"thread_id\", \"id\"),\"created_at\");",
        "CREATE INDEX \"messages_mailbox_thread_key_idx\" ON \"messages\" USING btree (\"mailbox_id\",coalesce(\"thread_id\", \"id\"));",
        "CREATE INDEX \"messages_nav_cover_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"status\",\"folder_id\",\"created_at\" desc,\"id\" desc,\"direction\",\"read\",\"thread_id\",\"snoozed_until\");",
        "CREATE INDEX \"messages_counts_cover_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"folder_id\",\"direction\",\"status\",\"read\",\"starred\",\"snoozed_until\");",
        "CREATE INDEX \"messages_conv_cover_idx\" ON \"messages\" USING btree (\"mailbox_id\",\"status\",coalesce(\"thread_id\", \"id\"),\"created_at\",\"id\",\"snoozed_until\");",
        "CREATE INDEX \"mfa_recovery_codes_user_idx\" ON \"mfa_recovery_codes\" USING btree (\"user_id\");",
        "CREATE INDEX \"outbound_jobs_user_idx\" ON \"outbound_jobs\" USING btree (\"user_id\");",
        "CREATE INDEX \"outbound_jobs_message_idx\" ON \"outbound_jobs\" USING btree (\"message_id\");",
        "CREATE INDEX \"password_reset_tokens_user_idx\" ON \"password_reset_tokens\" USING btree (\"user_id\");",
        "CREATE UNIQUE INDEX \"push_devices_platform_token_user_idx\" ON \"push_devices\" USING btree (\"platform\",\"token\",\"user_id\");",
        "CREATE INDEX \"push_devices_user_idx\" ON \"push_devices\" USING btree (\"user_id\");",
        "CREATE INDEX \"push_devices_api_key_idx\" ON \"push_devices\" USING btree (\"api_key_id\");",
        "CREATE UNIQUE INDEX \"push_subscriptions_endpoint_idx\" ON \"push_subscriptions\" USING btree (\"endpoint\");",
        "CREATE INDEX \"push_subscriptions_user_idx\" ON \"push_subscriptions\" USING btree (\"user_id\");",
        "CREATE INDEX \"push_subscriptions_session_idx\" ON \"push_subscriptions\" USING btree (\"session_id\");",
        "CREATE INDEX \"routing_rules_domain_scope_idx\" ON \"routing_rules\" USING btree (\"domain_id\",\"scope\",\"enabled\");",
        "CREATE INDEX \"routing_rules_mailbox_idx\" ON \"routing_rules\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"routing_rules_priority_idx\" ON \"routing_rules\" USING btree (\"priority\");",
        "CREATE INDEX \"routing_rules_user_idx\" ON \"routing_rules\" USING btree (\"user_id\");",
        "CREATE INDEX \"routing_rules_folder_idx\" ON \"routing_rules\" USING btree (\"folder_id\");",
        "CREATE INDEX \"sessions_user_idx\" ON \"sessions\" USING btree (\"user_id\");",
        "CREATE INDEX \"spam_feedback_mailbox_idx\" ON \"spam_feedback\" USING btree (\"mailbox_id\");",
        "CREATE INDEX \"spam_feedback_actor_user_idx\" ON \"spam_feedback\" USING btree (\"actor_user_id\");",
        "CREATE UNIQUE INDEX \"spam_reputation_mailbox_type_key_idx\" ON \"spam_reputation\" USING btree (\"mailbox_id\",\"type\",\"key\");",
        "CREATE INDEX \"spam_reputation_mailbox_idx\" ON \"spam_reputation\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"spam_token_stats_mailbox_token_idx\" ON \"spam_token_stats\" USING btree (\"mailbox_id\",\"token\");",
        "CREATE INDEX \"spam_token_stats_mailbox_idx\" ON \"spam_token_stats\" USING btree (\"mailbox_id\");",
        "CREATE UNIQUE INDEX \"users_booking_username_idx\" ON \"users\" USING btree (\"booking_username\");",
        "CREATE INDEX \"users_created_by_idx\" ON \"users\" USING btree (\"created_by_user_id\");",
        "CREATE INDEX \"webhook_deliveries_webhook_idx\" ON \"webhook_deliveries\" USING btree (\"webhook_id\",\"created_at\");",
        "CREATE INDEX \"webhook_deliveries_status_idx\" ON \"webhook_deliveries\" USING btree (\"status\");",
        "CREATE INDEX \"webhooks_user_idx\" ON \"webhooks\" USING btree (\"user_id\");"
      ]
    },
    {
      "name": "0001_triggers_and_seeds.sql",
      "statements": [
        "INSERT INTO backup_settings (id, enabled, schedule_type, retention_enabled, retention_days, updated_at)\nVALUES ('default', false, 'daily', false, 30, extract(epoch from now())::bigint)\nON CONFLICT DO NOTHING;",
        "INSERT INTO app_settings (id, app_name, updated_at)\nVALUES ('default', 'Mailflare', extract(epoch from now())::bigint)\nON CONFLICT DO NOTHING;",
        "INSERT INTO jmap_change_floor (id, seq) VALUES (1, 0) ON CONFLICT DO NOTHING;",
        "CREATE FUNCTION mailflare_bump_revision(target text) RETURNS void AS $$\nBEGIN\n\tINSERT INTO jmap_mailbox_revisions (mailbox_id, revision) VALUES (target, 1)\n\tON CONFLICT (mailbox_id) DO UPDATE SET revision = jmap_mailbox_revisions.revision + 1;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE FUNCTION mailflare_messages_trashed_at() RETURNS trigger AS $$\nBEGIN\n\tIF TG_OP = 'INSERT' THEN\n\t\tIF NEW.status IN ('trash', 'spam') THEN\n\t\t\tNEW.trashed_at := extract(epoch from now())::bigint;\n\t\tEND IF;\n\tELSIF (NEW.status IN ('trash', 'spam')) <> (OLD.status IN ('trash', 'spam')) THEN\n\t\tNEW.trashed_at := CASE WHEN NEW.status IN ('trash', 'spam') THEN extract(epoch from now())::bigint ELSE NULL END;\n\tEND IF;\n\tRETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE TRIGGER messages_trashed_at_bi BEFORE INSERT OR UPDATE OF status ON messages\nFOR EACH ROW EXECUTE FUNCTION mailflare_messages_trashed_at();",
        "CREATE FUNCTION mailflare_messages_changed() RETURNS trigger AS $$\nBEGIN\n\tIF TG_OP = 'INSERT' THEN\n\t\tIF NEW.mailbox_id IS NOT NULL THEN\n\t\t\tPERFORM mailflare_bump_revision(NEW.mailbox_id);\n\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, thread_id, change)\n\t\t\tVALUES (NEW.mailbox_id, 'email', NEW.id, coalesce(NEW.thread_id, NEW.id), 'created');\n\t\tEND IF;\n\tELSIF TG_OP = 'UPDATE' THEN\n\t\tIF NEW.mailbox_id IS NOT NULL THEN\n\t\t\tPERFORM mailflare_bump_revision(NEW.mailbox_id);\n\t\tEND IF;\n\t\tIF OLD.mailbox_id IS NOT NULL AND OLD.mailbox_id IS DISTINCT FROM NEW.mailbox_id THEN\n\t\t\tPERFORM mailflare_bump_revision(OLD.mailbox_id);\n\t\tEND IF;\n\t\tIF OLD.mailbox_id IS NOT DISTINCT FROM NEW.mailbox_id THEN\n\t\t\tIF NEW.mailbox_id IS NOT NULL THEN\n\t\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, thread_id, change)\n\t\t\t\tVALUES (NEW.mailbox_id, 'email', NEW.id, coalesce(NEW.thread_id, NEW.id), 'updated');\n\t\t\tEND IF;\n\t\tELSE\n\t\t\tIF OLD.mailbox_id IS NOT NULL THEN\n\t\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, thread_id, change)\n\t\t\t\tVALUES (OLD.mailbox_id, 'email', OLD.id, coalesce(OLD.thread_id, OLD.id), 'destroyed');\n\t\t\tEND IF;\n\t\t\tIF NEW.mailbox_id IS NOT NULL THEN\n\t\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, thread_id, change)\n\t\t\t\tVALUES (NEW.mailbox_id, 'email', NEW.id, coalesce(NEW.thread_id, NEW.id), 'created');\n\t\t\tEND IF;\n\t\tEND IF;\n\tELSE\n\t\tIF OLD.mailbox_id IS NOT NULL THEN\n\t\t\tPERFORM mailflare_bump_revision(OLD.mailbox_id);\n\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, thread_id, change)\n\t\t\tVALUES (OLD.mailbox_id, 'email', OLD.id, coalesce(OLD.thread_id, OLD.id), 'destroyed');\n\t\tEND IF;\n\tEND IF;\n\tRETURN NULL;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE TRIGGER messages_jmap_changed AFTER INSERT OR UPDATE OR DELETE ON messages\nFOR EACH ROW EXECUTE FUNCTION mailflare_messages_changed();",
        "CREATE FUNCTION mailflare_folders_changed() RETURNS trigger AS $$\nBEGIN\n\tIF TG_OP = 'INSERT' THEN\n\t\tPERFORM mailflare_bump_revision(NEW.mailbox_id);\n\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, change) VALUES (NEW.mailbox_id, 'mailbox', NEW.id, 'created');\n\tELSIF TG_OP = 'UPDATE' THEN\n\t\tPERFORM mailflare_bump_revision(NEW.mailbox_id);\n\t\tIF OLD.mailbox_id IS DISTINCT FROM NEW.mailbox_id THEN\n\t\t\tPERFORM mailflare_bump_revision(OLD.mailbox_id);\n\t\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, change) VALUES (OLD.mailbox_id, 'mailbox', OLD.id, 'destroyed');\n\t\tEND IF;\n\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, change)\n\t\tVALUES (NEW.mailbox_id, 'mailbox', NEW.id, CASE WHEN OLD.mailbox_id IS NOT DISTINCT FROM NEW.mailbox_id THEN 'updated' ELSE 'created' END);\n\tELSE\n\t\tPERFORM mailflare_bump_revision(OLD.mailbox_id);\n\t\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, change) VALUES (OLD.mailbox_id, 'mailbox', OLD.id, 'destroyed');\n\tEND IF;\n\tRETURN NULL;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE TRIGGER folders_jmap_changed AFTER INSERT OR UPDATE OR DELETE ON folders\nFOR EACH ROW EXECUTE FUNCTION mailflare_folders_changed();",
        "CREATE FUNCTION mailflare_mailboxes_changed() RETURNS trigger AS $$\nBEGIN\n\tPERFORM mailflare_bump_revision(NEW.id);\n\tINSERT INTO jmap_changes (mailbox_id, object_type, object_id, change) VALUES (NEW.id, 'mailbox', NEW.id, 'updated');\n\tRETURN NULL;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE TRIGGER mailboxes_jmap_changed AFTER UPDATE ON mailboxes\nFOR EACH ROW EXECUTE FUNCTION mailflare_mailboxes_changed();",
        "CREATE FUNCTION mailflare_aliases_changed() RETURNS trigger AS $$\nBEGIN\n\tIF TG_OP IN ('INSERT', 'UPDATE') THEN\n\t\tPERFORM mailflare_bump_revision(NEW.mailbox_id);\n\tEND IF;\n\tIF TG_OP = 'DELETE' OR (TG_OP = 'UPDATE' AND OLD.mailbox_id IS DISTINCT FROM NEW.mailbox_id) THEN\n\t\tPERFORM mailflare_bump_revision(OLD.mailbox_id);\n\tEND IF;\n\tRETURN NULL;\nEND;\n$$ LANGUAGE plpgsql;",
        "CREATE TRIGGER mailbox_aliases_jmap_changed AFTER INSERT OR UPDATE OR DELETE ON mailbox_aliases\nFOR EACH ROW EXECUTE FUNCTION mailflare_aliases_changed();",
        "DO $$\nBEGIN\n\tCREATE EXTENSION IF NOT EXISTS pg_trgm;\n\tCREATE INDEX messages_subject_trgm_idx ON messages USING gin (lower(subject) gin_trgm_ops);\n\tCREATE INDEX messages_text_body_trgm_idx ON messages USING gin (lower(text_body) gin_trgm_ops);\nEXCEPTION WHEN OTHERS THEN\n\tRAISE NOTICE 'pg_trgm is unavailable; message search will scan with ILIKE instead (%).', SQLERRM;\nEND;\n$$;"
      ]
    },
    {
      "name": "0002_calendar_reminders.sql",
      "statements": [
        "ALTER TABLE \"calendar_events\" ADD COLUMN \"reminder_minutes\" bigint;",
        "ALTER TABLE \"calendar_events\" ADD COLUMN \"last_reminded_start\" bigint;"
      ]
    },
    {
      "name": "0003_remote_content_ask.sql",
      "statements": [
        "UPDATE \"users\" SET \"remote_content_mode\" = 'ask' WHERE \"remote_content_mode\" = 'allow';"
      ]
    }
  ]
}