]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/Result/BinVer.pm
add bin_pkg to src_pkg convenience linking table with triggers
[debbugs.git] / Debbugs / DB / Result / BinVer.pm
1 use utf8;
2 package Debbugs::DB::Result::BinVer;
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::BinVer - Binary versions
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_ver>
33
34 =cut
35
36 __PACKAGE__->table("bin_ver");
37
38 =head1 ACCESSORS
39
40 =head2 id
41
42   data_type: 'integer'
43   is_auto_increment: 1
44   is_nullable: 0
45   sequence: 'bin_ver_id_seq'
46
47 Binary version id
48
49 =head2 bin_pkg
50
51   data_type: 'integer'
52   is_foreign_key: 1
53   is_nullable: 0
54
55 Binary package id (matches bin_pkg)
56
57 =head2 src_ver
58
59   data_type: 'integer'
60   is_foreign_key: 1
61   is_nullable: 0
62
63 Source version (matchines src_ver)
64
65 =head2 arch
66
67   data_type: 'integer'
68   is_foreign_key: 1
69   is_nullable: 0
70
71 Architecture id (matches arch)
72
73 =head2 ver
74
75   data_type: 'debversion'
76   is_nullable: 0
77
78 Binary version
79
80 =cut
81
82 __PACKAGE__->add_columns(
83   "id",
84   {
85     data_type         => "integer",
86     is_auto_increment => 1,
87     is_nullable       => 0,
88     sequence          => "bin_ver_id_seq",
89   },
90   "bin_pkg",
91   { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
92   "src_ver",
93   { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
94   "arch",
95   { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
96   "ver",
97   { data_type => "debversion", is_nullable => 0 },
98 );
99
100 =head1 PRIMARY KEY
101
102 =over 4
103
104 =item * L</id>
105
106 =back
107
108 =cut
109
110 __PACKAGE__->set_primary_key("id");
111
112 =head1 UNIQUE CONSTRAINTS
113
114 =head2 C<bin_ver_bin_pkg_id_arch_idx>
115
116 =over 4
117
118 =item * L</bin_pkg>
119
120 =item * L</arch>
121
122 =item * L</ver>
123
124 =back
125
126 =cut
127
128 __PACKAGE__->add_unique_constraint("bin_ver_bin_pkg_id_arch_idx", ["bin_pkg", "arch", "ver"]);
129
130 =head1 RELATIONS
131
132 =head2 arch
133
134 Type: belongs_to
135
136 Related object: L<Debbugs::DB::Result::Arch>
137
138 =cut
139
140 __PACKAGE__->belongs_to(
141   "arch",
142   "Debbugs::DB::Result::Arch",
143   { id => "arch" },
144   { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
145 );
146
147 =head2 bin_associations
148
149 Type: has_many
150
151 Related object: L<Debbugs::DB::Result::BinAssociation>
152
153 =cut
154
155 __PACKAGE__->has_many(
156   "bin_associations",
157   "Debbugs::DB::Result::BinAssociation",
158   { "foreign.bin" => "self.id" },
159   { cascade_copy => 0, cascade_delete => 0 },
160 );
161
162 =head2 bin_pkg
163
164 Type: belongs_to
165
166 Related object: L<Debbugs::DB::Result::BinPkg>
167
168 =cut
169
170 __PACKAGE__->belongs_to(
171   "bin_pkg",
172   "Debbugs::DB::Result::BinPkg",
173   { id => "bin_pkg" },
174   { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
175 );
176
177 =head2 src_ver
178
179 Type: belongs_to
180
181 Related object: L<Debbugs::DB::Result::SrcVer>
182
183 =cut
184
185 __PACKAGE__->belongs_to(
186   "src_ver",
187   "Debbugs::DB::Result::SrcVer",
188   { id => "src_ver" },
189   { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
190 );
191
192
193 # Created by DBIx::Class::Schema::Loader v0.07045 @ 2016-11-24 09:08:27
194 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:DzTzZbPkilT8WMhXoZv9xw
195
196
197 sub sqlt_deploy_hook {
198     my ($self, $sqlt_table) = @_;
199     for my $idx (qw(ver bin_pkg src_ver)) {
200         $sqlt_table->add_index(name => 'bin_ver_'.$idx.'_id_idx',
201                                fields => [$idx]);
202     }
203     $sqlt_table->add_index(name => 'bin_ver_src_ver_id_arch_idx',
204                            fields => [qw(src_ver arch)]
205                           );
206     $sqlt_table->schema->
207         add_procedure(name => 'bin_ver_to_src_pkg',
208                       sql => <<'EOF',
209 CREATE OR REPLACE FUNCTION bin_ver_to_src_pkg(bin_ver INT) RETURNS INT
210   AS $src_pkg_from_bin_ver$
211   DECLARE
212   src_pkg int;
213   BEGIN
214         SELECT sv.src_pkg INTO STRICT src_pkg
215                FROM bin_ver bv JOIN src_ver sv ON bv.src_ver=sv.id
216                WHERE bv.id=bin_ver;
217         RETURN src_pkg;
218   END
219   $src_pkg_from_bin_ver$ LANGUAGE plpgsql;
220 EOF
221                      );
222     $sqlt_table->schema->
223         add_procedure(name => 'update_bin_pkg_src_pkg_bin_ver',
224                       sql => <<'EOF',
225 CREATE OR REPLACE FUNCTION update_bin_pkg_src_pkg_bin_ver () RETURNS TRIGGER
226   AS $update_bin_pkg_src_pkg_bin_ver$
227   DECLARE
228   src_ver_rows integer;
229   BEGIN
230   IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' )  THEN
231      -- if there is still a bin_ver with this src_pkg, then do nothing
232      PERFORM * FROM bin_ver bv JOIN src_ver sv ON bv.src_ver = sv.id
233             WHERE sv.id = OLD.src_ver LIMIT 2;
234      GET DIAGNOSTICS src_ver_rows = ROW_COUNT;
235      IF (src_ver_rows <= 1) THEN
236         DELETE FROM bin_pkg_src_pkg
237                WHERE bin_pkg=OLD.bin_pkg AND
238                      src_pkg=src_ver_to_src_pkg(OLD.src_ver);
239      END IF;
240   END IF;
241   IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
242      BEGIN
243      INSERT INTO bin_pkg_src_pkg (bin_pkg,src_pkg)
244         VALUES (NEW.bin_pkg,src_ver_to_src_pkg(NEW.src_ver))
245         ON CONFLICT (bin_pkg,src_pkg) DO NOTHING;
246      END;
247   END IF;
248   RETURN NULL;
249   END
250   $update_bin_pkg_src_pkg_bin_ver$ LANGUAGE plpgsql;
251 EOF
252                      );
253 #     $sqlt_table->schema->
254 #       add_trigger(name => 'bin_ver_update_bin_pkg_src_pkg',
255 #                   perform_action_when => 'after',
256 #                   database_events => [qw(INSERT UPDATE DELETE)],
257 #                   on_table => 'bin_ver',
258 #                   action => <<'EOF',
259 # FOR EACH ROW EXECUTE PROCEDURE update_bin_pkg_src_pkg_bin_ver();
260 # EOF
261 #                  );
262 }
263
264 1;