Files
minecraft-account-manager/packages/database/drizzle/0000_supreme_human_fly.sql
T

128 lines
7.5 KiB
SQL

CREATE TYPE "public"."ip_classification" AS ENUM('unknown', 'clear', 'vpn', 'proxy', 'hosting', 'tor');--> statement-breakpoint
CREATE TYPE "public"."ip_observation_source" AS ENUM('web', 'game');--> statement-breakpoint
CREATE TYPE "public"."minecraft_validation_status" AS ENUM('verified', 'user_confirmed');--> statement-breakpoint
CREATE TABLE "app_settings" (
"id" text PRIMARY KEY DEFAULT 'default' NOT NULL,
"discord_guild_id" text,
"registration_message" text DEFAULT 'Please register your Minecraft account before joining.' NOT NULL,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "events" (
"id" uuid PRIMARY KEY NOT NULL,
"spec_version" text DEFAULT '1.0' NOT NULL,
"source" text NOT NULL,
"type" text NOT NULL,
"subject" text,
"time" timestamp (3) with time zone NOT NULL,
"data_content_type" text DEFAULT 'application/json' NOT NULL,
"data_schema" text,
"data" jsonb NOT NULL,
"actor_user_id" uuid,
"ip_address" "inet",
"correlation_id" uuid,
"published_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ip_intelligence" (
"ip_address" "inet" PRIMARY KEY NOT NULL,
"classification" "ip_classification" DEFAULT 'unknown' NOT NULL,
"provider" text,
"raw_response" jsonb,
"checked_at" timestamp (3) with time zone,
"expires_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ip_observations" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid,
"minecraft_account_id" uuid,
"source" "ip_observation_source" NOT NULL,
"ip_address" "inet" NOT NULL,
"minecraft_uuid" varchar(32),
"username" varchar(16),
"classification" "ip_classification" DEFAULT 'unknown' NOT NULL,
"observed_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "login_codes" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"token_hash" text NOT NULL,
"discord_user_id" text NOT NULL,
"discord_username" text NOT NULL,
"discord_global_name" text,
"expires_at" timestamp (3) with time zone NOT NULL,
"consumed_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "minecraft_accounts" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"minecraft_uuid" varchar(32),
"username" varchar(16) NOT NULL,
"validation_status" "minecraft_validation_status" NOT NULL,
"is_primary" boolean DEFAULT false NOT NULL,
"last_verified_at" timestamp (3) with time zone,
"deleted_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "plugin_credentials" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"server_id" text NOT NULL,
"secret_hash" text NOT NULL,
"revoked_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sessions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"token_hash" text NOT NULL,
"expires_at" timestamp (3) with time zone NOT NULL,
"last_seen_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"revoked_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"discord_user_id" text NOT NULL,
"discord_username" text NOT NULL,
"discord_global_name" text,
"first_name" text,
"onboarding_completed_at" timestamp (3) with time zone,
"created_at" timestamp (3) with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp (3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "events" ADD CONSTRAINT "events_actor_user_id_users_id_fk" FOREIGN KEY ("actor_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ip_observations" ADD CONSTRAINT "ip_observations_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ip_observations" ADD CONSTRAINT "ip_observations_minecraft_account_id_minecraft_accounts_id_fk" FOREIGN KEY ("minecraft_account_id") REFERENCES "public"."minecraft_accounts"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "minecraft_accounts" ADD CONSTRAINT "minecraft_accounts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
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;--> statement-breakpoint
CREATE INDEX "events_time_idx" ON "events" USING btree ("time");--> statement-breakpoint
CREATE INDEX "events_type_time_idx" ON "events" USING btree ("type","time");--> statement-breakpoint
CREATE INDEX "events_subject_time_idx" ON "events" USING btree ("subject","time");--> statement-breakpoint
CREATE INDEX "events_unpublished_idx" ON "events" USING btree ("created_at") WHERE "events"."published_at" is null;--> statement-breakpoint
CREATE INDEX "ip_observations_user_observed_idx" ON "ip_observations" USING btree ("user_id","observed_at");--> statement-breakpoint
CREATE INDEX "ip_observations_account_observed_idx" ON "ip_observations" USING btree ("minecraft_account_id","observed_at");--> statement-breakpoint
CREATE UNIQUE INDEX "login_codes_token_hash_uidx" ON "login_codes" USING btree ("token_hash");--> statement-breakpoint
CREATE INDEX "login_codes_discord_user_idx" ON "login_codes" USING btree ("discord_user_id");--> statement-breakpoint
CREATE INDEX "login_codes_expires_idx" ON "login_codes" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "minecraft_accounts_user_idx" ON "minecraft_accounts" USING btree ("user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "minecraft_accounts_active_uuid_uidx" ON "minecraft_accounts" USING btree ("minecraft_uuid") WHERE "minecraft_accounts"."deleted_at" is null and "minecraft_accounts"."minecraft_uuid" is not null;--> statement-breakpoint
CREATE UNIQUE INDEX "minecraft_accounts_active_username_uidx" ON "minecraft_accounts" USING btree (lower("username")) WHERE "minecraft_accounts"."deleted_at" is null;--> statement-breakpoint
CREATE UNIQUE INDEX "minecraft_accounts_one_primary_per_user_uidx" ON "minecraft_accounts" USING btree ("user_id") WHERE "minecraft_accounts"."is_primary" = true and "minecraft_accounts"."deleted_at" is null;--> statement-breakpoint
CREATE UNIQUE INDEX "plugin_credentials_server_id_uidx" ON "plugin_credentials" USING btree ("server_id");--> statement-breakpoint
CREATE UNIQUE INDEX "sessions_token_hash_uidx" ON "sessions" USING btree ("token_hash");--> statement-breakpoint
CREATE INDEX "sessions_user_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "sessions_expires_idx" ON "sessions" USING btree ("expires_at");--> statement-breakpoint
CREATE UNIQUE INDEX "users_discord_user_id_uidx" ON "users" USING btree ("discord_user_id");