CREATE TABLE "cars"."photos" ( "id" serial PRIMARY KEY, "model_id" integer NOT NULL, "position" smallint NOT NULL, "kind" text NOT NULL, "file" text NOT NULL, "caption" text, "thumb" text NOT NULL, "width" integer NOT NULL, "height" integer NOT NULL, "author" text, "license" text, "license_url" text ); --> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "wikidata" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "wikipedia" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "logo" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "logo_file" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "logo_author" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "logo_license" text;--> statement-breakpoint ALTER TABLE "cars"."brands" ADD COLUMN "photo" text;--> statement-breakpoint ALTER TABLE "cars"."models" ADD COLUMN "wikidata" text;--> statement-breakpoint ALTER TABLE "cars"."models" ADD COLUMN "wikipedia" text;--> statement-breakpoint ALTER TABLE "cars"."models" ADD COLUMN "photo" text;--> statement-breakpoint ALTER TABLE "cars"."models" ADD COLUMN "photo_count" smallint DEFAULT 0 NOT NULL;--> statement-breakpoint ALTER TABLE "cars"."models" ADD COLUMN "photos_checked_at" timestamp with time zone;--> statement-breakpoint CREATE UNIQUE INDEX "photos_model_position_key" ON "cars"."photos" ("model_id","position");--> statement-breakpoint ALTER TABLE "cars"."photos" ADD CONSTRAINT "photos_model_id_models_id_fkey" FOREIGN KEY ("model_id") REFERENCES "cars"."models"("id") ON DELETE CASCADE; --> statement-breakpoint -- hand-written: grants + registry for the photos table DO $$ BEGIN IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN GRANT SELECT, INSERT, UPDATE, DELETE ON cars.photos TO personal_app; GRANT USAGE, SELECT ON SEQUENCE cars.photos_id_seq TO personal_app; END IF; IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN GRANT SELECT ON cars.photos TO personal_ro; END IF; END $$; --> statement-breakpoint DO $$ BEGIN IF to_regclass('meta.tables') IS NOT NULL THEN INSERT INTO meta.tables (schema_name, table_name, slug, display_name, kind, editable, has_done, sort_order) VALUES ('cars','photos','cars-photos','Car photos','core',false,false,1000) ON CONFLICT DO NOTHING; END IF; END $$;