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 our $VERSION = '1.59';
46 my $global_table = $args{global_table} ||= '_GLOBAL_';
48 my (@global_fields, @global_indices, @global_constraints);
49 push @global_fields, @{ $args{fields} } if $args{fields};
50 push @global_indices, @{ $args{indices} } if $args{indices};
51 push @global_constraints, @{ $args{constraints} } if $args{constraints};
53 # Pull fields and indices off global table and then remove it.
54 if ( my $gtbl = $schema->get_table( $global_table ) ) {
56 foreach ( $gtbl->get_fields ) {
57 # We don't copy the order attrib so the added fields should get
58 # pushed on the end of each table.
59 push @global_fields, {
61 comments => "".$_->comments,
62 data_type => $_->data_type,
63 default_value => $_->default_value,
65 extra => scalar($_->extra),
66 foreign_key_reference => $_->foreign_key_reference,
67 is_auto_increment => $_->is_auto_increment,
68 is_foreign_key => $_->is_foreign_key,
69 is_nullable => $_->is_nullable,
70 is_primary_key => $_->is_primary_key,
71 is_unique => $_->is_unique,
72 is_valid => $_->is_valid,
76 foreach ( $gtbl->get_indices ) {
77 push @global_indices, {
80 fields => [$_->fields],
81 options => [$_->options],
82 extra => scalar($_->extra),
86 foreach ( $gtbl->get_constraints ) {
87 push @global_constraints, {
89 fields => [$_->fields],
90 deferrable => $_->deferrable,
91 expression => $_->expression,
92 match_type => $_->match_type,
93 options => [$_->options],
94 on_delete => $_->on_delete,
95 on_update => $_->on_update,
96 reference_fields => [$_->reference_fields],
97 reference_table => $_->reference_table,
100 extra => scalar($_->extra),
104 $schema->drop_table($gtbl);
107 # Add globalis to tables
108 foreach my $tbl ( $schema->get_tables ) {
110 foreach my $new_fld ( @global_fields ) {
111 # Don't add if field already there
112 next if $tbl->get_field( $new_fld->{name} );
113 $tbl->add_field( %$new_fld );
116 foreach my $new_index ( @global_indices ) {
117 $tbl->add_index( %$new_index );
120 foreach my $new_constraint ( @global_constraints ) {
121 $tbl->add_constraint( %$new_constraint );
132 Adds global fields, indices and constraints to all tables in the schema.
133 The globals to add can either be defined in the filter args or using a _GLOBAL_
136 If a table already contains a field with the same name as a global then it is
137 skipped for that table.
139 =head2 The _GLOBAL_ Table
141 An alternative to using the args is to add a table called C<_GLOBAL_> to the
142 schema and then just use the filter. Any fields and indices defined on this table
143 will be added to all the tables in the schema and the _GLOBAL_ table removed.
145 The name of the global can be changed using a C<global_table> arg to the
150 L<perl(1)>, L<SQL::Translator>
154 Will generate duplicate indices if an index already exists on a table the same
155 as one added globally.
157 Will generate duplicate constraints if a constraint already exists on a table
158 the same as one added globally.
162 Some extra data values that can be used to control the global addition. e.g.
167 Mark Addison <grommit@users.sourceforge.net>