--
--

\restrict 8T6sdteTl0WHa03ILYznk9YPl8OK3wAshfV5S6thWkYeDdcCbYQNByaTOgByu1J


SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: auth; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA auth;


--
-- Name: update_updated_at_column(); Type: FUNCTION; Schema: auth; Owner: -
--

CREATE FUNCTION auth.update_updated_at_column() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$;




--
-- Name: mfa_settings; Type: TABLE; Schema: auth; Owner: -
--

CREATE TABLE auth.mfa_settings (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid,
    secret_encrypted text NOT NULL,
    backup_codes_encrypted text[],
    verified_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);


--
-- Name: refresh_tokens; Type: TABLE; Schema: auth; Owner: -
--

CREATE TABLE auth.refresh_tokens (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid,
    token_hash character varying(255) NOT NULL,
    expires_at timestamp with time zone NOT NULL,
    revoked_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now()
);


--
-- Name: sessions; Type: TABLE; Schema: auth; Owner: -
--

CREATE TABLE auth.sessions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid,
    token_hash character varying(255) NOT NULL,
    ip_address inet,
    user_agent text,
    expires_at timestamp with time zone NOT NULL,
    revoked_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now()
);


--
-- Name: users; Type: TABLE; Schema: auth; Owner: -
--

CREATE TABLE auth.users (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    workos_user_id character varying(255) NOT NULL,
    email character varying(255) NOT NULL,
    first_name character varying(255),
    last_name character varying(255),
    role character varying(50) DEFAULT 'user'::character varying NOT NULL,
    mfa_enabled boolean DEFAULT false,
    last_login_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);


--
-- Data for Name: mfa_settings; Type: TABLE DATA; Schema: auth; Owner: -
--

COPY auth.mfa_settings (id, user_id, secret_encrypted, backup_codes_encrypted, verified_at, created_at, updated_at) FROM stdin;
\.


--
-- Data for Name: refresh_tokens; Type: TABLE DATA; Schema: auth; Owner: -
--

COPY auth.refresh_tokens (id, user_id, token_hash, expires_at, revoked_at, created_at) FROM stdin;
\.


--
-- Data for Name: sessions; Type: TABLE DATA; Schema: auth; Owner: -
--

COPY auth.sessions (id, user_id, token_hash, ip_address, user_agent, expires_at, revoked_at, created_at) FROM stdin;
\.


--
-- Data for Name: users; Type: TABLE DATA; Schema: auth; Owner: -
--

COPY auth.users (id, workos_user_id, email, first_name, last_name, role, mfa_enabled, last_login_at, created_at, updated_at) FROM stdin;
\.


--
-- Name: mfa_settings mfa_settings_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.mfa_settings
    ADD CONSTRAINT mfa_settings_pkey PRIMARY KEY (id);


--
-- Name: mfa_settings mfa_settings_user_id_key; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.mfa_settings
    ADD CONSTRAINT mfa_settings_user_id_key UNIQUE (user_id);


--
-- Name: refresh_tokens refresh_tokens_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.refresh_tokens
    ADD CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id);


--
-- Name: refresh_tokens refresh_tokens_token_hash_key; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.refresh_tokens
    ADD CONSTRAINT refresh_tokens_token_hash_key UNIQUE (token_hash);


--
-- Name: sessions sessions_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.sessions
    ADD CONSTRAINT sessions_pkey PRIMARY KEY (id);


--
-- Name: sessions sessions_token_hash_key; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.sessions
    ADD CONSTRAINT sessions_token_hash_key UNIQUE (token_hash);


--
-- Name: users users_email_key; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.users
    ADD CONSTRAINT users_email_key UNIQUE (email);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);


--
-- Name: users users_workos_user_id_key; Type: CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.users
    ADD CONSTRAINT users_workos_user_id_key UNIQUE (workos_user_id);


--
-- Name: idx_mfa_settings_user_id; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_mfa_settings_user_id ON auth.mfa_settings USING btree (user_id);


--
-- Name: idx_refresh_tokens_token_hash; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_refresh_tokens_token_hash ON auth.refresh_tokens USING btree (token_hash);


--
-- Name: idx_refresh_tokens_user_id; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_refresh_tokens_user_id ON auth.refresh_tokens USING btree (user_id);


--
-- Name: idx_sessions_expires_at; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_sessions_expires_at ON auth.sessions USING btree (expires_at);


--
-- Name: idx_sessions_token_hash; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_sessions_token_hash ON auth.sessions USING btree (token_hash);


--
-- Name: idx_sessions_user_id; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_sessions_user_id ON auth.sessions USING btree (user_id);


--
-- Name: idx_users_email; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_users_email ON auth.users USING btree (email);


--
-- Name: idx_users_workos_user_id; Type: INDEX; Schema: auth; Owner: -
--

CREATE INDEX idx_users_workos_user_id ON auth.users USING btree (workos_user_id);


--
-- Name: mfa_settings update_mfa_settings_updated_at; Type: TRIGGER; Schema: auth; Owner: -
--

CREATE TRIGGER update_mfa_settings_updated_at BEFORE UPDATE ON auth.mfa_settings FOR EACH ROW EXECUTE FUNCTION auth.update_updated_at_column();


--
-- Name: users update_users_updated_at; Type: TRIGGER; Schema: auth; Owner: -
--

CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON auth.users FOR EACH ROW EXECUTE FUNCTION auth.update_updated_at_column();


--
-- Name: mfa_settings mfa_settings_user_id_fkey; Type: FK CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.mfa_settings
    ADD CONSTRAINT mfa_settings_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: refresh_tokens refresh_tokens_user_id_fkey; Type: FK CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.refresh_tokens
    ADD CONSTRAINT refresh_tokens_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: sessions sessions_user_id_fkey; Type: FK CONSTRAINT; Schema: auth; Owner: -
--

ALTER TABLE ONLY auth.sessions
    ADD CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: SCHEMA auth; Type: ACL; Schema: -; Owner: -
--

GRANT USAGE ON SCHEMA auth TO auth_service_user;


--
-- Name: SCHEMA public; Type: ACL; Schema: -; Owner: -
--

GRANT ALL ON SCHEMA public TO cloudsqlsuperuser;


--
-- Name: TABLE mfa_settings; Type: ACL; Schema: auth; Owner: -
--

GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE auth.mfa_settings TO auth_service_user;


--
-- Name: TABLE refresh_tokens; Type: ACL; Schema: auth; Owner: -
--

GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE auth.refresh_tokens TO auth_service_user;


--
-- Name: TABLE sessions; Type: ACL; Schema: auth; Owner: -
--

GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE auth.sessions TO auth_service_user;


--
-- Name: TABLE users; Type: ACL; Schema: auth; Owner: -
--

GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE auth.users TO auth_service_user;


--
-- Name: DEFAULT PRIVILEGES FOR SEQUENCES; Type: DEFAULT ACL; Schema: auth; Owner: -
--



--
-- Name: DEFAULT PRIVILEGES FOR TABLES; Type: DEFAULT ACL; Schema: auth; Owner: -
--



--
--

\unrestrict 8T6sdteTl0WHa03ILYznk9YPl8OK3wAshfV5S6thWkYeDdcCbYQNByaTOgByu1J

