CREATE TABLE IF NOT EXISTS "push_subscriptions" (
  "id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  "user_id" integer NOT NULL,
  "endpoint" text NOT NULL,
  "p256dh" text NOT NULL,
  "auth" text NOT NULL,
  "user_agent" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "last_seen_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "push_subscriptions" ADD CONSTRAINT "push_subscriptions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "push_subscriptions_endpoint_idx" ON "push_subscriptions" USING btree ("endpoint");
--> statement-breakpoint
DO $$ BEGIN
  IF EXISTS (SELECT FROM pg_roles WHERE rolname = 'slackwsh_app') THEN
    GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE push_subscriptions TO slackwsh_app;
    GRANT USAGE, SELECT ON SEQUENCE push_subscriptions_id_seq TO slackwsh_app;
  END IF;
END $$;
