]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/Result/BinPkgSrcPkg.pm
add binpkg/srcpkg relation table
[debbugs.git] / Debbugs / DB / Result / BinPkgSrcPkg.pm
1 use utf8;
2 package Debbugs::DB::Result::BinPkgSrcPkg;
3
4 # Created by DBIx::Class::Schema::Loader
5 # DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7 =head1 NAME
8
9 Debbugs::DB::Result::BinPkgSrcPkg - Binary package <-> source package mapping sumpmary table
10
11 =cut
12
13 use strict;
14 use warnings;
15
16 use base 'DBIx::Class::Core';
17
18 =head1 COMPONENTS LOADED
19
20 =over 4
21
22 =item * L<DBIx::Class::InflateColumn::DateTime>
23
24 =item * L<DBIx::Class::TimeStamp>
25
26 =back
27
28 =cut
29
30 __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp");
31
32 =head1 TABLE: C<bin_pkg_src_pkg>
33
34 =cut
35
36 __PACKAGE__->table("bin_pkg_src_pkg");
37
38 =head1 ACCESSORS
39
40 =head2 bin_pkg
41
42   data_type: 'integer'
43   is_foreign_key: 1
44   is_nullable: 0
45
46 Binary package id (matches bin_pkg)
47
48 =head2 src_pkg
49
50   data_type: 'integer'
51   is_foreign_key: 1
52   is_nullable: 0
53
54 Source package id (matches src_pkg)
55
56 =cut
57
58 __PACKAGE__->add_columns(
59   "bin_pkg",
60   { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
61   "src_pkg",
62   { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
63 );
64
65 =head1 UNIQUE CONSTRAINTS
66
67 =head2 C<bin_pkg_src_pkg_bin_pkg_src_pkg>
68
69 =over 4
70
71 =item * L</bin_pkg>
72
73 =item * L</src_pkg>
74
75 =back
76
77 =cut
78
79 __PACKAGE__->add_unique_constraint("bin_pkg_src_pkg_bin_pkg_src_pkg", ["bin_pkg", "src_pkg"]);
80
81 =head2 C<bin_pkg_src_pkg_src_pkg_bin_pkg>
82
83 =over 4
84
85 =item * L</src_pkg>
86
87 =item * L</bin_pkg>
88
89 =back
90
91 =cut
92
93 __PACKAGE__->add_unique_constraint("bin_pkg_src_pkg_src_pkg_bin_pkg", ["src_pkg", "bin_pkg"]);
94
95 =head1 RELATIONS
96
97 =head2 bin_pkg
98
99 Type: belongs_to
100
101 Related object: L<Debbugs::DB::Result::BinPkg>
102
103 =cut
104
105 __PACKAGE__->belongs_to(
106   "bin_pkg",
107   "Debbugs::DB::Result::BinPkg",
108   { id => "bin_pkg" },
109   { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
110 );
111
112 =head2 src_pkg
113
114 Type: belongs_to
115
116 Related object: L<Debbugs::DB::Result::SrcPkg>
117
118 =cut
119
120 __PACKAGE__->belongs_to(
121   "src_pkg",
122   "Debbugs::DB::Result::SrcPkg",
123   { id => "src_pkg" },
124   { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
125 );
126
127
128 # Created by DBIx::Class::Schema::Loader v0.07048 @ 2018-04-18 16:55:56
129 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:O/v5RtjJF9SgxXEy76U/xw
130
131 sub sqlt_deploy_hook {
132     my ($self, $sqlt_table) = @_;
133     $sqlt_table->schema->
134         add_procedure(name => 'bin_ver_to_src_pkg',
135                       sql => <<'EOF',
136 CREATE OR REPLACE FUNCTION bin_ver_to_src_pkg(bin_ver INT) RETURNS INT
137   AS $src_pkg_from_bin_ver$
138   DECLARE
139   src_pkg int;
140   BEGIN
141         SELECT sv.src_pkg INTO STRICT src_pkg
142                FROM bin_ver bv JOIN src_ver sv ON bv.src_ver=sv.id
143                WHERE bv.id=bin_ver;
144         RETURN src_pkg;
145   END
146   $src_pkg_from_bin_ver$ LANGUAGE plpgsql;
147 EOF
148                       );
149     $sqlt_table->schema->
150         add_procedure(name => 'src_ver_to_src_pkg',
151                       sql => <<'EOF',
152 CREATE OR REPLACE FUNCTION src_ver_to_src_pkg(src_ver INT) RETURNS INT
153   AS $src_ver_to_src_pkg$
154   DECLARE
155   src_pkg int;
156   BEGIN
157         SELECT sv.src_pkg INTO STRICT src_pkg
158                FROM src_ver sv WHERE sv.id=src_ver;
159         RETURN src_pkg;
160   END
161   $src_ver_to_src_pkg$ LANGUAGE plpgsql;
162 EOF
163                       );
164     $sqlt_table->schema->
165         add_procedure(name => 'update_bin_pkg_src_pkg_bin_ver',
166                       sql => <<'EOF',
167 CREATE OR REPLACE FUNCTION update_bin_pkg_src_pkg_bin_ver () RETURNS TRIGGER
168   AS $update_bin_pkg_src_pkg_bin_ver$
169   DECLARE
170   src_ver_rows integer;
171   BEGIN
172   IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' )  THEN
173      -- if there is still a bin_ver with this src_pkg, then do nothing
174      PERFORM * FROM bin_ver bv JOIN src_ver sv ON bv.src_ver = sv.id
175             WHERE sv.id = OLD.src_ver LIMIT 2;
176      GET DIAGNOSTICS src_ver_rows = ROW_COUNT;
177      IF (src_ver_rows <= 1) THEN
178         DELETE FROM bin_pkg_src_pkg
179                WHERE bin_pkg=OLD.bin_pkg AND
180                      src_pkg=src_ver_to_src_pkg(OLD.src_ver);
181      END IF;
182   END IF;
183   IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
184      BEGIN
185      INSERT INTO bin_pkg_src_pkg (bin_pkg,src_pkg)
186         VALUES (NEW.bin_pkg,src_ver_to_src_pkg(NEW.src_ver))
187         ON CONFLICT (bin_pkg,src_pkg) DO NOTHING;
188      END;
189   END IF;
190   RETURN NULL;
191   END
192   $update_bin_pkg_src_pkg_bin_ver$ LANGUAGE plpgsql;
193 EOF
194                      );
195
196 }
197
198 1;