1
+ -- CreateTable
2
+ CREATE TABLE
3
+ " certification_authority_on_certification_authority_structure" (
4
+ " id" UUID NOT NULL DEFAULT uuid_generate_v4 (),
5
+ " certification_authority_id" UUID NOT NULL ,
6
+ " certification_authority_structure_id" UUID NOT NULL ,
7
+ " created_at" TIMESTAMPTZ (6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP ,
8
+ " updated_at" TIMESTAMPTZ (6 ),
9
+ CONSTRAINT " certification_authority_on_certification_authority_structu_pkey" PRIMARY KEY (" id" )
10
+ );
11
+
12
+ -- CreateIndex
13
+ CREATE UNIQUE INDEX "certification_authority_on_certification_authority_structur_key " ON " certification_authority_on_certification_authority_structure" (
14
+ " certification_authority_id" ,
15
+ " certification_authority_structure_id"
16
+ );
17
+
18
+ -- AddForeignKey
19
+ ALTER TABLE " certification_authority_on_certification_authority_structure" ADD CONSTRAINT " ca_on_cas_ca" FOREIGN KEY (" certification_authority_id" ) REFERENCES " certification_authority" (" id" ) ON DELETE CASCADE ON UPDATE CASCADE;
20
+
21
+ -- AddForeignKey
22
+ ALTER TABLE " certification_authority_on_certification_authority_structure" ADD CONSTRAINT " ca_on_cas_cas" FOREIGN KEY (" certification_authority_structure_id" ) REFERENCES " certification_authority_structure" (" id" ) ON DELETE CASCADE ON UPDATE CASCADE;
23
+
24
+ -- Migrate all existing certification_authority to certification_authority_on_certification_authority_structure relatuionships
25
+ INSERT INTO
26
+ " certification_authority_on_certification_authority_structure" (
27
+ " certification_authority_id" ,
28
+ " certification_authority_structure_id"
29
+ )
30
+ SELECT
31
+ " id" ,
32
+ " certification_authority_structure_id"
33
+ FROM
34
+ " certification_authority" ;
35
+
36
+ -- Add certification_authority certification_authority_structure relationships based on certification_authorities certifications
37
+ INSERT INTO
38
+ " certification_authority_on_certification_authority_structure" (
39
+ " certification_authority_id" ,
40
+ " certification_authority_structure_id"
41
+ )
42
+ SELECT DISTINCT
43
+ " certification_authority" ." id" ,
44
+ " certification" ." certification_authority_structure_id"
45
+ FROM
46
+ " certification_authority"
47
+ JOIN " certification_authority_on_certification" ON " certification_authority_on_certification" ." certification_authority_id" = " certification_authority" ." id"
48
+ JOIN " certification" ON " certification" ." id" = " certification_authority_on_certification" ." certification_id"
49
+ WHERE
50
+ (
51
+ " certification_authority" ." id" ,
52
+ " certification" ." certification_authority_structure_id"
53
+ ) NOT IN (
54
+ SELECT
55
+ " certification_authority_id" ,
56
+ " certification_authority_structure_id"
57
+ FROM
58
+ " certification_authority_on_certification_authority_structure"
59
+ );
0 commit comments