SET check_function_bodies = false; CREATE FUNCTION public.set_current_timestamp_updated_at() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE _new record; BEGIN _new := NEW; _new."updated_at" = NOW(); RETURN _new; END; $$; CREATE TABLE public.tenants ( id integer NOT NULL, name text NOT NULL, uuid uuid DEFAULT gen_random_uuid() NOT NULL, email_domain text NOT NULL ); CREATE SEQUENCE public.tenants_id_seq AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER SEQUENCE public.tenants_id_seq OWNED BY public.tenants.id; CREATE TABLE public.users ( id integer NOT NULL, email text NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, first_name text NOT NULL, last_name text NOT NULL, tenant_id integer NOT NULL ); CREATE SEQUENCE public.users_id_seq AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; ALTER TABLE ONLY public.tenants ALTER COLUMN id SET DEFAULT nextval('public.tenants_id_seq'::regclass); ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); ALTER TABLE ONLY public.tenants ADD CONSTRAINT tenants_email_domain_key UNIQUE (email_domain); ALTER TABLE ONLY public.tenants ADD CONSTRAINT tenants_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.tenants ADD CONSTRAINT tenants_uuid_key UNIQUE (uuid); ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id); CREATE TRIGGER set_public_users_updated_at BEFORE UPDATE ON public.users FOR EACH ROW EXECUTE FUNCTION public.set_current_timestamp_updated_at(); COMMENT ON TRIGGER set_public_users_updated_at ON public.users IS 'trigger to set value of column "updated_at" to current timestamp on row update'; ALTER TABLE ONLY public.users ADD CONSTRAINT users_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON UPDATE RESTRICT ON DELETE RESTRICT;