CREATE EXTENSION IF NOT EXISTS citext; CREATE TABLE "credentials" ( "id" text PRIMARY KEY NOT NULL, "user_id" text NOT NULL, "type" text DEFAULT 'password' NOT NULL, "secret_data" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE "roles" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "roles_name_unique" UNIQUE("name") ); CREATE TABLE "user_roles" ( "id" text PRIMARY KEY NOT NULL, "user_id" text NOT NULL, "role_id" text NOT NULL, "primary" boolean DEFAULT false, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE "users" ( "id" text PRIMARY KEY NOT NULL, "username" text, "email" "citext" NOT NULL, "first_name" text, "last_name" text, "email_verified" boolean DEFAULT false, "mfa_enabled" boolean DEFAULT false NOT NULL, "avatar" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "users_username_unique" UNIQUE("username"), CONSTRAINT "users_email_unique" UNIQUE("email") ); CREATE TABLE "two_factor" ( "id" text PRIMARY KEY NOT NULL, "user_id" text NOT NULL, "secret" text NOT NULL, "enabled" boolean DEFAULT false NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "two_factor_user_id_unique" UNIQUE("user_id") ); CREATE TABLE "recovery_codes" ( "id" text PRIMARY KEY NOT NULL, "user_id" text NOT NULL, "code" text NOT NULL, "used" boolean DEFAULT false, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "recovery_codes_user_id_unique" UNIQUE("user_id") ); ALTER TABLE "credentials" ADD CONSTRAINT "credentials_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "user_roles" ADD CONSTRAINT "user_roles_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "user_roles" ADD CONSTRAINT "user_roles_role_id_roles_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."roles"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "two_factor" ADD CONSTRAINT "two_factor_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action; ALTER TABLE "recovery_codes" ADD CONSTRAINT "recovery_codes_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;