Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 76273ada

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

Script foreign_key_constraints_on_delete als Perl-Script neu implementiert

Grund: die Foreign Keys können durchaus auch anders
heißen (z.B. "$1").

Unterschiede anzeigen:

SL/DBUpgrade2/Base.pm
119 119
  return 1;
120 120
}
121 121

  
122
sub drop_constraints {
123
  my ($self, %params) = @_;
124

  
125
  croak "Missing parameter 'table'" unless $params{table};
126
  $params{type}   ||= 'FOREIGN KEY';
127
  $params{schema} ||= 'public';
128

  
129
  my $constraints = $self->dbh->selectall_arrayref(<<SQL, undef, $params{type}, $params{schema}, $params{table});
130
    SELECT constraint_name
131
    FROM information_schema.table_constraints
132
    WHERE (constraint_type = ?)
133
      AND (table_schema    = ?)
134
      AND (table_name      = ?)
135
SQL
136

  
137
  $self->db_query(qq|ALTER TABLE auth."$params{table}" DROP CONSTRAINT "${_}"|) for map { $_->[0] } @{ $constraints };
138
}
139

  
122 140
1;
123 141
__END__
124 142

  
......
259 277

  
260 278
=back
261 279

  
280
=item C<drop_constraints %params>
281

  
282
Drops all constraints of a type (e.g. foreign keys) on a table. One
283
parameter is mandatory: C<table>. Optional parameters include:
284

  
285
=over 2
286

  
287
=item * C<schema> -- if missing defaults to C<public>
288

  
289
=item * C<type> -- if missing defaults to C<FOREIGN KEY>. Must be one of
290
the values contained in the C<information_schema.table_constraints>
291
view in the C<constraint_type> column.
292

  
293
=back
294

  
262 295
=item C<execute_script>
263 296

  
264 297
Executes a named database upgrade script. This function is not
sql/Pg-upgrade2-auth/foreign_key_constraints_on_delete.pl
1
# @tag: foreign_key_constraints_on_delete
2
# @description: Ändert "FOREIGN KEY" constraints auf "ON DELETE CASCADE"
3
# @depends: clients
4
# @ignore: 0
5
package SL::DBUpgrade2::foreign_key_constraints_on_delete;
6

  
7
use Data::Dumper;
8

  
9

  
10
use strict;
11
use utf8;
12

  
13
use parent qw(SL::DBUpgrade2::Base);
14

  
15
sub run {
16
  my ($self) = @_;
17

  
18
  $self->drop_constraints(schema => 'auth', table => $_) for qw(clients_groups clients_users group_rights session_content user_config user_group);
19

  
20
  my @add_constraints = (
21
    qq|ALTER TABLE auth.clients_groups ADD FOREIGN KEY (client_id) REFERENCES auth.clients (id) ON DELETE CASCADE|,
22
    qq|ALTER TABLE auth.clients_groups ADD FOREIGN KEY (group_id)  REFERENCES auth."group" (id) ON DELETE CASCADE|,
23

  
24
    qq|ALTER TABLE auth.clients_users ADD FOREIGN KEY (client_id) REFERENCES auth.clients (id) ON DELETE CASCADE|,
25
    qq|ALTER TABLE auth.clients_users ADD FOREIGN KEY (user_id)   REFERENCES auth."user"  (id) ON DELETE CASCADE|,
26

  
27
    qq|ALTER TABLE auth.group_rights ADD FOREIGN KEY (group_id) REFERENCES auth."group" (id) ON DELETE CASCADE|,
28

  
29

  
30
    qq|ALTER TABLE auth.session_content ADD FOREIGN KEY (session_id) REFERENCES auth.session (id) ON DELETE CASCADE|,
31

  
32
    qq|ALTER TABLE auth.user_config ADD FOREIGN KEY (user_id) REFERENCES auth."user" (id) ON DELETE CASCADE|,
33

  
34
    qq|ALTER TABLE auth.user_group ADD FOREIGN KEY (user_id)  REFERENCES auth."user"  (id) ON DELETE CASCADE|,
35
    qq|ALTER TABLE auth.user_group ADD FOREIGN KEY (group_id) REFERENCES auth."group" (id) ON DELETE CASCADE|,
36
  );
37

  
38
  $self->db_query($_) for @add_constraints;
39

  
40
  return 1;
41
}
42

  
43
1;
sql/Pg-upgrade2-auth/foreign_key_constraints_on_delete.sql
1
-- @tag: foreign_key_constraints_on_delete
2
-- @description: Ändert "FOREIGN KEY" constraints auf "ON DELETE CASCADE"
3
-- @depends: clients
4
-- @charset: utf-8
5

  
6
-- auth.clients_groups
7
ALTER TABLE auth.clients_groups DROP CONSTRAINT clients_groups_client_id_fkey;
8
ALTER TABLE auth.clients_groups DROP CONSTRAINT clients_groups_group_id_fkey;
9

  
10
ALTER TABLE auth.clients_groups ADD FOREIGN KEY (client_id) REFERENCES auth.clients (id) ON DELETE CASCADE;
11
ALTER TABLE auth.clients_groups ADD FOREIGN KEY (group_id)  REFERENCES auth."group" (id) ON DELETE CASCADE;
12

  
13
-- auth.clients_users
14
ALTER TABLE auth.clients_users DROP CONSTRAINT clients_users_client_id_fkey;
15
ALTER TABLE auth.clients_users DROP CONSTRAINT clients_users_user_id_fkey;
16

  
17
ALTER TABLE auth.clients_users ADD FOREIGN KEY (client_id) REFERENCES auth.clients (id) ON DELETE CASCADE;
18
ALTER TABLE auth.clients_users ADD FOREIGN KEY (user_id)   REFERENCES auth."user"  (id) ON DELETE CASCADE;
19

  
20
-- auth.group_rights
21
ALTER TABLE auth.group_rights DROP CONSTRAINT group_rights_group_id_fkey;
22

  
23
ALTER TABLE auth.group_rights ADD FOREIGN KEY (group_id) REFERENCES auth."group" (id) ON DELETE CASCADE;
24

  
25
 -- auth.session_content
26
ALTER TABLE auth.session_content DROP CONSTRAINT session_content_session_id_fkey;
27

  
28
ALTER TABLE auth.session_content ADD FOREIGN KEY (session_id) REFERENCES auth.session (id) ON DELETE CASCADE;
29

  
30
 -- auth.user_config
31
ALTER TABLE auth.user_config DROP CONSTRAINT user_config_user_id_fkey;
32

  
33
ALTER TABLE auth.user_config ADD FOREIGN KEY (user_id) REFERENCES auth."user" (id) ON DELETE CASCADE;
34

  
35
-- auth.user_group
36
ALTER TABLE auth.user_group DROP CONSTRAINT user_group_user_id_fkey;
37
ALTER TABLE auth.user_group DROP CONSTRAINT user_group_group_id_fkey;
38

  
39
ALTER TABLE auth.user_group ADD FOREIGN KEY (user_id)  REFERENCES auth."user"  (id) ON DELETE CASCADE;
40
ALTER TABLE auth.user_group ADD FOREIGN KEY (group_id) REFERENCES auth."group" (id) ON DELETE CASCADE;

Auch abrufbar als: Unified diff