Skip to content

Commit c1bbeb1

Browse files
committed
Apply sqruff and some sqlfluff to schemas
1 parent e99674e commit c1bbeb1

13 files changed

Lines changed: 557 additions & 391 deletions

File tree

packages/database/.sqruff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[sqruff]
2+
dialect = postgres
3+
exclude_rules = CP05,LT05
4+
5+
[sqruff:indentation]
6+
indent_unit = space
7+
tab_space_size = 4
8+
indented_joins = True

packages/database/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All CLI commands below should be run in this directory (`packages/database`.)
66
2. Install the [supabase CLI](https://supabase.com/docs/guides/local-development). (There is a brew version)
77
3. `supabase login` with your (account-specific) supabase access token. (TODO: Create a group access token.)
88
4. `supabase link`. It will ask you for a project name, use `discourse-graphs`. (Production for now.) It will also ask you for the database password (See 1password.)
9+
5. Install [sqruff](https://github.com/quarylabs/sqruff)
910
2. Usage:
1011
1. Use `supabase start` before you use your local database. URLs will be given for your local supabase database, api endpoint, etc.
1112
2. You may need to `supabase db pull` if changes are deployed while you work.
@@ -15,9 +16,10 @@ All CLI commands below should be run in this directory (`packages/database`.)
1516
2. `supabase stop` if it's running.
1617
3. Make changes to the schema, by editing files in `project/database/supabase/schemas`
1718
4. If you created a new schema file, make sure to add it to `[db.migrations] schema_paths` in `packages/database/supabase/config.toml`. Schema files are applied in that order, you may need to be strategic in placing your file.
18-
4. `supabase db diff -f some_meaningful_migration_name`
19-
5. If applying the new schema fails, repeat steps 2 and 3
20-
6. If all goes well, there should be a new file named `supbase/migration/2..._some_meaningful_migration_name.sql` which you should `git add`.
21-
7. You can start using your changes `supabase start`
22-
8. Regenerate the types file with `supabase gen types typescript --local > types.gen.ts`
23-
9. When your PR gets merged to main, deploy your changes to production with `supabase db push`. (URGENT TODO: make that a CI/CD step.)
19+
5. Check your logic with `sqruff lint supabase/schemas`, and eventually `sqruff fix supabase/schemas`
20+
6. Regenerate the types file with `supabase gen types typescript --local > types.gen.ts`
21+
7. `supabase db diff -f some_meaningful_migration_name`
22+
8. If applying the new schema fails, repeat steps 2 to 7
23+
9. If all goes well, there should be a new file named `supbase/migration/2..._some_meaningful_migration_name.sql` which you should `git add`.
24+
10. You can start using your changes `supabase start`
25+
11. When your PR gets merged to main, deploy your changes to production with `supabase db push`. (URGENT TODO: make that a CI/CD step.)

packages/database/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"init": "supabase login",
1212
"dev": "supabase start",
1313
"stop": "supabase stop",
14+
"lint": "sqruff lint supabase/schemas",
15+
"lint:fix": "sqruff fix supabase/schemas",
1416
"local-types": "supabase start && supabase gen types typescript --local --schema public > types.gen.ts",
1517
"active-types": "supabase start && supabase gen types typescript --project-id \"$PRODUCTION_PROJECT_ID\" --schema public > types.gen.ts",
1618
"new-migration": "supabase stop && supabase db diff -f ",
Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,76 @@
1-
2-
3-
4-
CREATE TABLE IF NOT EXISTS "public"."Account" (
5-
"id" bigint DEFAULT "nextval"('"public"."entity_id_seq"'::"regclass") NOT NULL,
6-
"platform_id" bigint NOT NULL,
7-
"person_id" bigint NOT NULL,
8-
"write_permission" boolean NOT NULL,
9-
"active" boolean DEFAULT true NOT NULL
1+
CREATE TABLE IF NOT EXISTS public."Account" (
2+
id bigint DEFAULT nextval(
3+
'public.entity_id_seq'::regclass
4+
) NOT NULL,
5+
platform_id bigint NOT NULL,
6+
person_id bigint NOT NULL,
7+
write_permission boolean NOT NULL,
8+
active boolean DEFAULT true NOT NULL
109
);
1110

12-
ALTER TABLE "public"."Account" OWNER TO "postgres";
11+
ALTER TABLE public."Account" OWNER TO "postgres";
1312

14-
COMMENT ON TABLE "public"."Account" IS 'A user account on a discourse platform';
13+
COMMENT ON TABLE public."Account" IS 'A user account on a discourse platform';
1514

1615

17-
ALTER TABLE ONLY "public"."Account"
18-
ADD CONSTRAINT "Account_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "public"."Agent"("id") ON UPDATE CASCADE ON DELETE CASCADE;
16+
ALTER TABLE ONLY public."Account"
17+
ADD CONSTRAINT "Account_person_id_fkey" FOREIGN KEY (
18+
person_id
19+
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
1920

20-
ALTER TABLE ONLY "public"."Account"
21-
ADD CONSTRAINT "Account_platform_id_fkey" FOREIGN KEY ("platform_id") REFERENCES "public"."DiscoursePlatform"("id") ON UPDATE CASCADE ON DELETE CASCADE;
21+
ALTER TABLE ONLY public."Account"
22+
ADD CONSTRAINT "Account_platform_id_fkey" FOREIGN KEY (
23+
platform_id
24+
) REFERENCES public."DiscoursePlatform" (
25+
id
26+
) ON UPDATE CASCADE ON DELETE CASCADE;
2227

23-
ALTER TABLE ONLY "public"."Account"
24-
ADD CONSTRAINT "Account_pkey" PRIMARY KEY ("id");
28+
ALTER TABLE ONLY public."Account"
29+
ADD CONSTRAINT "Account_pkey" PRIMARY KEY (id);
2530

2631

27-
CREATE TABLE IF NOT EXISTS "public"."SpaceAccess" (
28-
"id" bigint DEFAULT "nextval"('"public"."entity_id_seq"'::"regclass") NOT NULL,
29-
"space_id" bigint,
30-
"account_id" bigint NOT NULL,
31-
"editor" boolean NOT NULL
32+
CREATE TABLE IF NOT EXISTS public."SpaceAccess" (
33+
id bigint DEFAULT nextval(
34+
'public.entity_id_seq'::regclass
35+
) NOT NULL,
36+
space_id bigint,
37+
account_id bigint NOT NULL,
38+
editor boolean NOT NULL
3239
);
3340

34-
ALTER TABLE ONLY "public"."SpaceAccess"
35-
ADD CONSTRAINT "SpaceAccess_account_id_space_id_key" UNIQUE ("account_id", "space_id");
36-
37-
ALTER TABLE ONLY "public"."SpaceAccess"
38-
ADD CONSTRAINT "SpaceAccess_pkey" PRIMARY KEY ("id");
41+
ALTER TABLE ONLY public."SpaceAccess"
42+
ADD CONSTRAINT "SpaceAccess_account_id_space_id_key" UNIQUE (
43+
account_id, space_id
44+
);
3945

46+
ALTER TABLE ONLY public."SpaceAccess"
47+
ADD CONSTRAINT "SpaceAccess_pkey" PRIMARY KEY (id);
4048

41-
ALTER TABLE "public"."SpaceAccess" OWNER TO "postgres";
4249

43-
COMMENT ON TABLE "public"."SpaceAccess" IS 'An access control entry for a space';
50+
ALTER TABLE public."SpaceAccess" OWNER TO "postgres";
4451

45-
COMMENT ON COLUMN "public"."SpaceAccess"."space_id" IS 'The space in which the content is located';
52+
COMMENT ON TABLE public."SpaceAccess" IS 'An access control entry for a space';
4653

54+
COMMENT ON COLUMN public."SpaceAccess".space_id IS 'The space in which the content is located';
4755

4856

49-
ALTER TABLE ONLY "public"."SpaceAccess"
50-
ADD CONSTRAINT "SpaceAccess_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "public"."Account"("id") ON UPDATE CASCADE ON DELETE CASCADE;
57+
ALTER TABLE ONLY public."SpaceAccess"
58+
ADD CONSTRAINT "SpaceAccess_account_id_fkey" FOREIGN KEY (
59+
account_id
60+
) REFERENCES public."Account" (id) ON UPDATE CASCADE ON DELETE CASCADE;
5161

52-
ALTER TABLE ONLY "public"."SpaceAccess"
53-
ADD CONSTRAINT "SpaceAccess_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "public"."DiscourseSpace"("id") ON UPDATE CASCADE ON DELETE CASCADE;
62+
ALTER TABLE ONLY public."SpaceAccess"
63+
ADD CONSTRAINT "SpaceAccess_space_id_fkey" FOREIGN KEY (
64+
space_id
65+
) REFERENCES public."DiscourseSpace" (
66+
id
67+
) ON UPDATE CASCADE ON DELETE CASCADE;
5468

55-
GRANT ALL ON TABLE "public"."SpaceAccess" TO "anon";
56-
GRANT ALL ON TABLE "public"."SpaceAccess" TO "authenticated";
57-
GRANT ALL ON TABLE "public"."SpaceAccess" TO "service_role";
69+
GRANT ALL ON TABLE public."SpaceAccess" TO anon;
70+
GRANT ALL ON TABLE public."SpaceAccess" TO authenticated;
71+
GRANT ALL ON TABLE public."SpaceAccess" TO service_role;
5872

5973

60-
GRANT ALL ON TABLE "public"."Account" TO "anon";
61-
GRANT ALL ON TABLE "public"."Account" TO "authenticated";
62-
GRANT ALL ON TABLE "public"."Account" TO "service_role";
74+
GRANT ALL ON TABLE public."Account" TO anon;
75+
GRANT ALL ON TABLE public."Account" TO authenticated;
76+
GRANT ALL ON TABLE public."Account" TO service_role;
Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
1-
2-
CREATE TABLE IF NOT EXISTS "public"."Agent" (
3-
"id" bigint DEFAULT "nextval"('"public"."entity_id_seq"'::"regclass") NOT NULL,
4-
"type" "public"."EntityType" NOT NULL
1+
CREATE TABLE IF NOT EXISTS public."Agent" (
2+
id bigint DEFAULT nextval(
3+
'public.entity_id_seq'::regclass
4+
) NOT NULL,
5+
type public."EntityType" NOT NULL
56
);
67

78

8-
ALTER TABLE ONLY "public"."Agent"
9-
ADD CONSTRAINT "Agent_pkey" PRIMARY KEY ("id");
9+
ALTER TABLE ONLY public."Agent"
10+
ADD CONSTRAINT "Agent_pkey" PRIMARY KEY (id);
1011

11-
ALTER TABLE "public"."Agent" OWNER TO "postgres";
12+
ALTER TABLE public."Agent" OWNER TO "postgres";
1213

13-
COMMENT ON TABLE "public"."Agent" IS 'An agent that acts in the system';
14+
COMMENT ON TABLE public."Agent" IS 'An agent that acts in the system';
1415

15-
CREATE TABLE IF NOT EXISTS "public"."AutomatedAgent" (
16-
"id" bigint NOT NULL,
17-
"name" character varying NOT NULL,
18-
"metadata" "jsonb" DEFAULT '{}'::"jsonb" NOT NULL,
19-
"deterministic" boolean DEFAULT false,
20-
"version" character varying
16+
CREATE TABLE IF NOT EXISTS public."AutomatedAgent" (
17+
id bigint NOT NULL,
18+
name character varying NOT NULL,
19+
metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
20+
deterministic boolean DEFAULT false,
21+
version character varying
2122
);
2223

23-
ALTER TABLE ONLY "public"."AutomatedAgent"
24-
ADD CONSTRAINT "AutomatedAgent_pkey" PRIMARY KEY ("id");
24+
ALTER TABLE ONLY public."AutomatedAgent"
25+
ADD CONSTRAINT "AutomatedAgent_pkey" PRIMARY KEY (id);
2526

26-
ALTER TABLE ONLY "public"."AutomatedAgent"
27-
ADD CONSTRAINT "automated_agent_id_fkey" FOREIGN KEY ("id") REFERENCES "public"."Agent"("id") ON UPDATE CASCADE ON DELETE CASCADE;
27+
ALTER TABLE ONLY public."AutomatedAgent"
28+
ADD CONSTRAINT automated_agent_id_fkey FOREIGN KEY (
29+
id
30+
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
2831

2932

30-
ALTER TABLE "public"."AutomatedAgent" OWNER TO "postgres";
33+
ALTER TABLE public."AutomatedAgent" OWNER TO "postgres";
3134

32-
COMMENT ON TABLE "public"."AutomatedAgent" IS 'An automated agent';
35+
COMMENT ON TABLE public."AutomatedAgent" IS 'An automated agent';
3336

34-
CREATE TABLE IF NOT EXISTS "public"."Person" (
35-
"id" bigint NOT NULL,
36-
"name" character varying NOT NULL,
37-
"orcid" character varying(20),
38-
"email" character varying NOT NULL
37+
CREATE TABLE IF NOT EXISTS public."Person" (
38+
id bigint NOT NULL,
39+
name character varying NOT NULL,
40+
orcid character varying(20),
41+
email character varying NOT NULL
3942
);
4043

41-
ALTER TABLE ONLY "public"."Person"
42-
ADD CONSTRAINT "person_id_fkey" FOREIGN KEY ("id") REFERENCES "public"."Agent"("id") ON UPDATE CASCADE ON DELETE CASCADE;
44+
ALTER TABLE ONLY public."Person"
45+
ADD CONSTRAINT person_id_fkey FOREIGN KEY (
46+
id
47+
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
4348

4449

45-
ALTER TABLE "public"."Person" OWNER TO "postgres";
50+
ALTER TABLE public."Person" OWNER TO "postgres";
4651

47-
COMMENT ON TABLE "public"."Person" IS 'A person using the system';
52+
COMMENT ON TABLE public."Person" IS 'A person using the system';
4853

4954

50-
GRANT ALL ON TABLE "public"."Agent" TO "anon";
51-
GRANT ALL ON TABLE "public"."Agent" TO "authenticated";
52-
GRANT ALL ON TABLE "public"."Agent" TO "service_role";
55+
GRANT ALL ON TABLE public."Agent" TO anon;
56+
GRANT ALL ON TABLE public."Agent" TO authenticated;
57+
GRANT ALL ON TABLE public."Agent" TO service_role;
5358

54-
GRANT ALL ON TABLE "public"."AutomatedAgent" TO "anon";
55-
GRANT ALL ON TABLE "public"."AutomatedAgent" TO "authenticated";
56-
GRANT ALL ON TABLE "public"."AutomatedAgent" TO "service_role";
59+
GRANT ALL ON TABLE public."AutomatedAgent" TO anon;
60+
GRANT ALL ON TABLE public."AutomatedAgent" TO authenticated;
61+
GRANT ALL ON TABLE public."AutomatedAgent" TO service_role;
5762

58-
GRANT ALL ON TABLE "public"."Person" TO "anon";
59-
GRANT ALL ON TABLE "public"."Person" TO "authenticated";
60-
GRANT ALL ON TABLE "public"."Person" TO "service_role";
63+
GRANT ALL ON TABLE public."Person" TO anon;
64+
GRANT ALL ON TABLE public."Person" TO authenticated;
65+
GRANT ALL ON TABLE public."Person" TO service_role;
Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
SET statement_timeout = 0;
42
SET lock_timeout = 0;
53
SET idle_in_transaction_session_timeout = 0;
@@ -11,19 +9,19 @@ SET xmloption = content;
119
SET client_min_messages = warning;
1210
SET row_security = on;
1311
SET default_tablespace = '';
14-
SET default_table_access_method = "heap";
12+
SET default_table_access_method = heap;
1513

16-
COMMENT ON SCHEMA "public" IS 'standard public schema';
14+
COMMENT ON SCHEMA public IS 'standard public schema';
1715

1816

19-
ALTER PUBLICATION "supabase_realtime" OWNER TO "postgres";
17+
ALTER PUBLICATION supabase_realtime OWNER TO postgres;
2018

21-
GRANT USAGE ON SCHEMA "public" TO "postgres";
22-
GRANT USAGE ON SCHEMA "public" TO "anon";
23-
GRANT USAGE ON SCHEMA "public" TO "authenticated";
24-
GRANT USAGE ON SCHEMA "public" TO "service_role";
19+
GRANT USAGE ON SCHEMA public TO postgres;
20+
GRANT USAGE ON SCHEMA public TO anon;
21+
GRANT USAGE ON SCHEMA public TO authenticated;
22+
GRANT USAGE ON SCHEMA public TO service_role;
2523

26-
CREATE TYPE "public"."EntityType" AS ENUM (
24+
CREATE TYPE public."EntityType" AS ENUM (
2725
'Platform',
2826
'Space',
2927
'Account',
@@ -37,31 +35,31 @@ CREATE TYPE "public"."EntityType" AS ENUM (
3735
'Occurrence'
3836
);
3937

40-
ALTER TYPE "public"."EntityType" OWNER TO "postgres";
38+
ALTER TYPE public."EntityType" OWNER TO postgres;
4139

42-
CREATE SEQUENCE IF NOT EXISTS "public"."entity_id_seq"
43-
START WITH 1
44-
INCREMENT BY 1
45-
NO MINVALUE
46-
NO MAXVALUE
47-
CACHE 1;
40+
CREATE SEQUENCE IF NOT EXISTS public.entity_id_seq
41+
START WITH 1
42+
INCREMENT BY 1
43+
NO MINVALUE
44+
NO MAXVALUE
45+
CACHE 1;
4846

49-
ALTER TABLE "public"."entity_id_seq" OWNER TO "postgres";
47+
ALTER TABLE public.entity_id_seq OWNER TO "postgres";
5048

5149

52-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "postgres";
53-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "anon";
54-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "authenticated";
55-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "service_role";
56-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "postgres";
57-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "anon";
58-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "authenticated";
59-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "service_role";
60-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "postgres";
61-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "anon";
62-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "authenticated";
63-
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "service_role";
50+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres;
51+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon;
52+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated;
53+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role;
54+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres;
55+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO anon;
56+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated;
57+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role;
58+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO postgres;
59+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO anon;
60+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO authenticated;
61+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO service_role;
6462

65-
GRANT ALL ON SEQUENCE "public"."entity_id_seq" TO "anon";
66-
GRANT ALL ON SEQUENCE "public"."entity_id_seq" TO "authenticated";
67-
GRANT ALL ON SEQUENCE "public"."entity_id_seq" TO "service_role";
63+
GRANT ALL ON SEQUENCE public.entity_id_seq TO anon;
64+
GRANT ALL ON SEQUENCE public.entity_id_seq TO authenticated;
65+
GRANT ALL ON SEQUENCE public.entity_id_seq TO service_role;

0 commit comments

Comments
 (0)