1 package SQL::Translator::Filter::Globals;
5 SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
9 # e.g. Add timestamp field to all tables.
12 my $sqlt = SQL::Translator->new(
20 data_type => 'TIMESTAMP'
34 ) || die "SQLFairy error : ".SQL::Translator->error;
35 my $sql = $sqlt->translate || die "SQLFairy error : ".$sqlt->error;
41 use vars qw/$VERSION/;
47 my $global_table = $args{global_table} ||= '_GLOBAL_';
49 my (@global_fields, @global_indices, @global_constraints);
50 push @global_fields, @{ $args{fields} } if $args{fields};
51 push @global_indices, @{ $args{indices} } if $args{indices};
52 push @global_constraints, @{ $args{constraints} } if $args{constraints};
54 # Pull fields and indices off global table and then remove it.
55 if ( my $gtbl = $schema->get_table( $global_table ) ) {
57 foreach ( $gtbl->get_fields ) {
58 # We don't copy the order attrib so the added fields should get
59 # pushed on the end of each table.
60 push @global_fields, {
62 comments => "".$_->comments,
63 data_type => $_->data_type,
64 default_value => $_->default_value,
66 extra => scalar($_->extra),
67 foreign_key_reference => $_->foreign_key_reference,
68 is_auto_increment => $_->is_auto_increment,
69 is_foreign_key => $_->is_foreign_key,
70 is_nullable => $_->is_nullable,
71 is_primary_key => $_->is_primary_key,
72 is_unique => $_->is_unique,
73 is_valid => $_->is_valid,
77 foreach ( $gtbl->get_indices ) {
78 push @global_indices, {
81 fields => [$_->fields],
82 options => [$_->options],
83 extra => scalar($_->extra),
87 foreach ( $gtbl->get_constraints ) {
88 push @global_constraints, {
90 fields => [$_->fields],
91 deferrable => $_->deferrable,
92 expression => $_->expression,
93 match_type => $_->match_type,
94 options => [$_->options],
95 on_delete => $_->on_delete,
96 on_update => $_->on_update,
97 reference_fields => [$_->reference_fields],
98 reference_table => $_->reference_table,
101 extra => scalar($_->extra),
105 $schema->drop_table($gtbl);
108 # Add globalis to tables
109 foreach my $tbl ( $schema->get_tables ) {
111 foreach my $new_fld ( @global_fields ) {
112 # Don't add if field already there
113 next if $tbl->get_field( $new_fld->{name} );
114 $tbl->add_field( %$new_fld );
117 foreach my $new_index ( @global_indices ) {
118 $tbl->add_index( %$new_index );
121 foreach my $new_constraint ( @global_constraints ) {
122 $tbl->add_constraint( %$new_constraint );
133 Adds global fields, indices and constraints to all tables in the schema.
134 The globals to add can either be defined in the filter args or using a _GLOBAL_
137 If a table already contains a field with the same name as a global then it is
138 skipped for that table.
140 =head2 The _GLOBAL_ Table
142 An alternative to using the args is to add a table called C<_GLOBAL_> to the
143 schema and then just use the filter. Any fields and indices defined on this table
144 will be added to all the tables in the schema and the _GLOBAL_ table removed.
146 The name of the global can be changed using a C<global_table> arg to the
151 L<perl(1)>, L<SQL::Translator>
155 Will generate duplicate indices if an index already exists on a table the same
156 as one added globally.
158 Will generate duplicate constraints if a constraint already exists on a table
159 the same as one added globally.
163 Some extra data values that can be used to control the global addition. e.g.
168 Mark Addison <grommit@users.sourceforge.net>