1 package SQL::Translator::Filter::Globals;
3 # -------------------------------------------------------------------
4 # $Id: Globals.pm,v 1.1 2006-03-06 17:46:57 grommit Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
29 # e.g. Add timestamp field to all tables.
32 my $sqlt = SQL::Translator->new(
40 data_type => 'TIMESTAMP'
50 ) || die "SQLFairy error : ".SQL::Translator->error;
51 my $sql = $sqlt->translate || die "SQLFairy error : ".$sqlt->error;
56 use vars qw/$VERSION/;
62 my $global_table = $args{global_table} ||= '_GLOBAL_';
64 my (@global_fields, @global_indices);
65 push @global_fields, @{ $args{fields} } if $args{fields};
66 push @global_indices, @{ $args{indices} } if $args{indices};
68 # Pull fields and indices off global table and then remove it.
69 if ( my $gtbl = $schema->get_table( $global_table ) ) {
71 foreach ( $gtbl->get_fields ) {
72 # We don't copy the order attrib so the added fields should get
73 # pushed on the end of each table.
74 push @global_fields, {
76 comments => "".$_->comments,
77 data_type => $_->data_type,
78 default_value => $_->default_value,
80 extra => scalar($_->extra),
81 foreign_key_reference => $_->foreign_key_reference,
82 is_auto_increment => $_->is_auto_increment,
83 is_foreign_key => $_->is_foreign_key,
84 is_nullable => $_->is_nullable,
85 is_primary_key => $_->is_primary_key,
86 is_unique => $_->is_unique,
87 is_valid => $_->is_valid,
91 foreach ( $gtbl->get_indices ) {
92 push @global_indices, {
95 fields => [$_->fields],
96 options => [$_->options],
100 $schema->drop_table($gtbl);
103 # Add globalis to tables
104 foreach my $tbl ( $schema->get_tables ) {
106 foreach my $new_fld ( @global_fields ) {
107 # Don't add if field already there
108 next if $tbl->get_field( $new_fld->{name} );
109 $tbl->add_field( %$new_fld );
112 foreach my $new_index ( @global_indices ) {
113 # Don't add if already there
114 #next if $tbl->get_index( $new_index->{name} );
115 $tbl->add_index( %$new_index );
126 Adds global fields and indices to all tables in the schema.
127 The globals to add can either be defined in the filter args or using a _GLOBAL_
130 If a table already contains a field with the same name as a global then it is
131 skipped for that table.
133 =head2 The _GLOBAL_ Table
135 An alternative to using the args is to add a table called C<_GLOBAL_> to the
136 schema and then just use the filter. Any fields and indices defined on this table
137 will be added to all the tables in the schema and the _GLOBAL_ table removed.
139 The name of the global can be changed using a C<global_table> arg to the
144 L<perl(1)>, L<SQL::Translator>
148 Will generate duplicate indices if an index already exists on a table the same
149 as one added globally.
153 Global addition of constraints.
155 Some extra data values that can be used to control the global addition. e.g.
160 Mark Addison <grommit@users.sourceforge.net>