From: Ash Berlin Date: Mon, 3 Mar 2008 19:00:25 +0000 (+0000) Subject: Allow quote and other option to be passed to the producers. X-Git-Tag: v0.11008~334 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7467c458f256ef9d0d7028a0913412a0c854eab8;p=dbsrgits%2FSQL-Translator.git Allow quote and other option to be passed to the producers. --- diff --git a/lib/SQL/Translator/Diff.pm b/lib/SQL/Translator/Diff.pm index 5cbb0b0..29c00fd 100644 --- a/lib/SQL/Translator/Diff.pm +++ b/lib/SQL/Translator/Diff.pm @@ -14,7 +14,7 @@ use base 'Class::Accessor::Fast'; __PACKAGE__->mk_accessors(qw/ ignore_index_names ignore_constraint_names ignore_view_sql ignore_proc_sql output_db source_schema source_db target_schema target_db - case_insensitive no_batch_alters ignore_missing_methods + case_insensitive no_batch_alters ignore_missing_methods producer_options /); my @diff_arrays = qw/ @@ -64,6 +64,7 @@ sub new { $values->{$_} ||= [] foreach @diff_arrays; $values->{table_diff_hash} = {}; + $values->{producer_options} ||= {}; $values->{output_db} ||= $values->{source_db}; return $class->SUPER::new($values); } @@ -180,7 +181,8 @@ sub produce_diff_sql { { map { $func_map{$_} => $self->table_diff_hash->{$table}{$_} } keys %func_map - } + }, + $self->producer_options ); } } else { @@ -198,7 +200,10 @@ sub produce_diff_sql { if (@{ $flattened_diffs{$_} || [] }) { my $meth = $producer_class->can($_); - $meth ? map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ? ("$sql;") : () } @{ $flattened_diffs{$_} } + $meth ? map { + my $sql = $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $self->producer_options ); + $sql ? ("$sql;") : (); + } @{ $flattened_diffs{$_} } : $self->ignore_missing_methods ? "-- $producer_class cant $_" : die "$producer_class cant $_"; @@ -222,8 +227,7 @@ sub produce_diff_sql { add_drop_table => 0, no_comments => 1, # TODO: sort out options - quote_table_names => 0, - quote_field_names => 0, + %{ $self->producer_options } ); my $schema = $translator->schema; @@ -237,7 +241,7 @@ sub produce_diff_sql { if (my @tables_to_drop = @{ $self->{tables_to_drop} || []} ) { my $meth = $producer_class->can('drop_table'); - push @diffs, $meth ? map( { $meth->($_) } @tables_to_drop ) + push @diffs, $meth ? map( { $meth->($_, $self->producer_options) } @tables_to_drop ) : $self->ignore_missing_methods ? "-- $producer_class cant drop_table" : die "$producer_class cant drop_table"; diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 22d0e37..1274b28 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -55,30 +55,34 @@ The producer recognises the following extra attributes on the Schema objects. =over 4 -=item field.list +=item B Set the list of allowed values for Enum fields. -=item field.binary, field.unsigned, field.zerofill +=item B, B, B Set the MySQL field options of the same name. -=item field.renamed_from +=item B, B -Used when producing diffs to say this column is the new name fo the specified -old column. +Use when producing diffs to indicate that the current table/field has been +renamed from the old name as given in the attribute value. -=item table.mysql_table_type +=item B Set the type of the table e.g. 'InnoDB', 'MyISAM'. This will be automatically set for tables involved in foreign key constraints if it is not already set explicitly. See L<"Table Types">. -=item table.mysql_charset, table.mysql_collate +Please note that the C option is the prefered method of specifying +the MySQL storage engine to use, but this method still works for backwards +compatability. + +=item B, B Set the tables default charater set and collation order. -=item field.mysql_charset, field.mysql_collate +=item B, B Set the fields charater set and collation order. @@ -501,7 +505,7 @@ sub alter_drop_constraint my $qc = $options->{quote_constraint_names} || ''; my $out = sprintf('ALTER TABLE %s DROP %s %s', - $c->table->name, + $qt . $c->table->name . $qt, $c->type eq FOREIGN_KEY ? $c->type : "INDEX", $qc . $c->name . $qc ); @@ -526,6 +530,7 @@ sub create_constraint my $qf = $options->{quote_field_names} || ''; my $qt = $options->{quote_table_names} || ''; + my $qc = $options->{quote_constraint_names} || ''; my $leave_name = $options->{leave_name} || undef; my @fields = $c->fields or next; @@ -550,7 +555,7 @@ sub create_constraint my $def = join(' ', map { $_ || () } 'CONSTRAINT', - $qt . $c_name . $qt, + $qc . $c_name . $qc, 'FOREIGN KEY' ); @@ -602,7 +607,7 @@ sub alter_table { my ($to_table, $options) = @_; - my $qt = $options->{quote_table_name} || ''; + my $qt = $options->{quote_table_names} || ''; my $table_options = generate_table_options($to_table) || ''; my $out = sprintf('ALTER TABLE %s%s', @@ -617,8 +622,8 @@ sub alter_field { my ($from_field, $to_field, $options) = @_; - my $qf = $options->{quote_field_name} || ''; - my $qt = $options->{quote_table_name} || ''; + my $qf = $options->{quote_field_names} || ''; + my $qt = $options->{quote_table_names} || ''; my $out = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s', $qt . $to_field->table->name . $qt, @@ -632,7 +637,7 @@ sub add_field { my ($new_field, $options) = @_; - my $qt = $options->{quote_table_name} || ''; + my $qt = $options->{quote_table_names} || ''; my $out = sprintf('ALTER TABLE %s ADD COLUMN %s', $qt . $new_field->table->name . $qt, @@ -646,8 +651,8 @@ sub drop_field { my ($old_field, $options) = @_; - my $qf = $options->{quote_field_name} || ''; - my $qt = $options->{quote_table_name} || ''; + my $qf = $options->{quote_field_names} || ''; + my $qt = $options->{quote_table_names} || ''; my $out = sprintf('ALTER TABLE %s DROP COLUMN %s', $qt . $old_field->table->name . $qt, @@ -717,7 +722,7 @@ sub batch_alter_table { # Now strip off the 'ALTER TABLE xyz' of all but the first one - my $qt = $options->{quote_table_name} || ''; + my $qt = $options->{quote_table_names} || ''; my $table_name = $qt . $table->name . $qt; diff --git a/t/30sqlt-new-diff-mysql.t b/t/30sqlt-new-diff-mysql.t index 4f3ad16..9e1237a 100644 --- a/t/30sqlt-new-diff-mysql.t +++ b/t/30sqlt-new-diff-mysql.t @@ -13,7 +13,7 @@ use Test::SQL::Translator qw(maybe_plan); use SQL::Translator::Schema::Constants; use Storable 'dclone'; -plan tests => 7; +plan tests => 8; use_ok('SQL::Translator::Diff') or die "Cannot continue\n"; @@ -34,7 +34,7 @@ my ( $source_schema, $target_schema, $parsed_sql_schema ) = map { } (qw( create1.yml create2.yml )); # Test for differences -my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1} ); +my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1, producer_options => { quote_table_names => 0 } } ); eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); -- Convert schema 'create1.yml' to 'create2.yml': @@ -76,7 +76,8 @@ COMMIT; $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL', { ignore_index_names => 1, - ignore_constraint_names => 1 + ignore_constraint_names => 1, + producer_options => { quote_table_names => 0 }, }); eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); @@ -145,7 +146,7 @@ eq_or_diff($out, <<'## END OF DIFF', "No differences found"); my $field = $target_schema->get_table('employee')->get_field('employee_id'); $field->data_type('integer'); $field->size(0); - $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL' ); + $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL', { producer_options => { quote_table_names => 0 } } ); eq_or_diff($out, <<'## END OF DIFF', "No differences found"); -- Convert schema 'create.sql' to 'create2.yml': @@ -274,4 +275,20 @@ ALTER TABLE employee RENAME TO fnord, COMMIT; ## END OF DIFF + + # Test quoting works too. + $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL', + { producer_options => { quote_table_names => '`' } } + ); + eq_or_diff($out, <<'## END OF DIFF', "Quoting can be turned on"); +-- Convert schema 'Schema 3' to 'Schema 4': + +BEGIN; + +ALTER TABLE `employee` RENAME TO `fnord`, + DROP FOREIGN KEY bar_fk, + ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES `foo` (id); + +COMMIT; +## END OF DIFF }