__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/
$values->{$_} ||= [] foreach @diff_arrays;
$values->{table_diff_hash} = {};
+ $values->{producer_options} ||= {};
$values->{output_db} ||= $values->{source_db};
return $class->SUPER::new($values);
}
{ map {
$func_map{$_} => $self->table_diff_hash->{$table}{$_}
} keys %func_map
- }
+ },
+ $self->producer_options
);
}
} else {
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 $_";
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;
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";
=over 4
-=item field.list
+=item B<field.list>
Set the list of allowed values for Enum fields.
-=item field.binary, field.unsigned, field.zerofill
+=item B<field.binary>, B<field.unsigned>, B<field.zerofill>
Set the MySQL field options of the same name.
-=item field.renamed_from
+=item B<field.renamed_from>, B<table.renamed_from>
-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<table.mysql_table_type>
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<ENGINE> option is the prefered method of specifying
+the MySQL storage engine to use, but this method still works for backwards
+compatability.
+
+=item B<table.mysql_charset>, B<table.mysql_collate>
Set the tables default charater set and collation order.
-=item field.mysql_charset, field.mysql_collate
+=item B<field.mysql_charset>, B<field.mysql_collate>
Set the fields charater set and collation order.
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 );
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;
my $def = join(' ',
map { $_ || () }
'CONSTRAINT',
- $qt . $c_name . $qt,
+ $qc . $c_name . $qc,
'FOREIGN KEY'
);
{
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',
{
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,
{
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,
{
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,
# 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;
use SQL::Translator::Schema::Constants;
use Storable 'dclone';
-plan tests => 7;
+plan tests => 8;
use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
} (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':
$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");
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':
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
}