Document producer_args in SQL::Translator::Diff
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Diff.pm
index 423c256..1c9dea9 100644 (file)
@@ -8,15 +8,59 @@ use warnings;
 use Data::Dumper;
 use Carp::Clan qw/^SQL::Translator/;
 use SQL::Translator::Schema::Constants;
-
-use base 'Class::Accessor::Fast';
-
-# Input/option accessors
-__PACKAGE__->mk_accessors(qw/
-  ignore_index_names ignore_constraint_names ignore_view_sql
-  ignore_proc_sql output_db source_schema target_schema
-  case_insensitive no_batch_alters ignore_missing_methods producer_args
-/);
+use Sub::Quote qw(quote_sub);
+use Moo;
+
+has ignore_index_names => (
+  is => 'rw',
+);
+has ignore_constraint_names => (
+  is => 'rw',
+);
+has ignore_view_sql => (
+  is => 'rw',
+);
+has ignore_proc_sql => (
+  is => 'rw',
+);
+has output_db => (
+  is => 'rw',
+);
+has source_schema => (
+  is => 'rw',
+);
+has target_schema => (
+  is => 'rw',
+);
+has case_insensitive => (
+  is => 'rw',
+);
+has no_batch_alters => (
+  is => 'rw',
+);
+has ignore_missing_methods => (
+  is => 'rw',
+);
+has producer_args => (
+  is => 'rw',
+  lazy => 1,
+  default => quote_sub '{}',
+);
+has tables_to_drop => (
+  is => 'rw',
+  lazy => 1,
+  default => quote_sub '[]',
+);
+has tables_to_create => (
+  is => 'rw',
+  lazy => 1,
+  default => quote_sub '[]',
+);
+has table_diff_hash => (
+  is => 'rw',
+  lazy => 1,
+  default => quote_sub '{}',
+);
 
 my @diff_arrays = qw/
   tables_to_drop
@@ -36,8 +80,6 @@ my @diff_hash_keys = qw/
   table_renamed_from
 /;
 
-__PACKAGE__->mk_accessors(@diff_arrays, 'table_diff_hash');
-
 sub schema_diff {
     #  use Data::Dumper;
     ## we are getting instructions on how to turn the source into the target
@@ -59,18 +101,19 @@ sub schema_diff {
     $obj->compute_differences->produce_diff_sql;
 }
 
-sub new {
-  my ($class, $values) = @_;
-  $values->{$_} ||= [] foreach @diff_arrays;
-  $values->{table_diff_hash} = {};
-
-  $values->{producer_args} ||= {};
-  if ($values->{producer_options}) {
+sub BUILD {
+  my ($self, $args) = @_;
+  if ($args->{producer_options}) {
     carp 'producer_options is deprecated. Please use producer_args';
-    $values->{producer_args} = { %{$values->{producer_options}}, %{$values->{producer_args}} };
+    $self->producer_args({
+      %{$args->{producer_options}},
+      %{$self->producer_args}
+    });
+  }
+
+  if (! $self->output_db) {
+    $self->output_db($args->{source_db})
   }
-  $values->{output_db} ||= $values->{source_db};
-  return $class->SUPER::new($values);
 }
 
 sub compute_differences {
@@ -205,8 +248,7 @@ sub produce_diff_sql {
             my $meth = $producer_class->can($_);
 
             $meth ? map {
-                    my $sql = $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $self->producer_args );
-                    $sql ?  ("$sql") : ();
+                    map { $_ ? "$_" : () } $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $self->producer_args );
                   } @{ $flattened_diffs{$_} }
                   : $self->ignore_missing_methods
                   ? "-- $producer_class cant $_"
@@ -226,7 +268,7 @@ sub produce_diff_sql {
     }
 
     if (my @tables = @{ $self->tables_to_create } ) {
-      my $translator = new SQL::Translator(
+      my $translator = SQL::Translator->new(
         producer_type => $self->output_db,
         add_drop_table => 0,
         no_comments => 1,
@@ -265,7 +307,7 @@ sub produce_diff_sql {
       }
 
       my @return =
-        map { $_ ? ( $_ =~ /;$/xms ? $_ : "$_;\n\n" ) : "\n" }
+        map { $_ ? ( $_ =~ /;\s*\z/xms ? $_ : "$_;\n\n" ) : "\n" }
         ("-- Convert schema '$src_name' to '$tar_name':", @diffs);
 
       return wantarray ? @return : join('', @return);
@@ -339,7 +381,7 @@ sub diff_table_constraints {
 sub diff_table_fields {
   my ($self, $src_table, $tar_table) = @_;
 
-  # List of ones ew've renamed from so we dont drop them
+  # List of ones we've renamed from so we don't drop them
   my %renamed_source_fields;
 
   for my $tar_table_field ( $tar_table->get_fields ) {
@@ -400,7 +442,7 @@ sub diff_table_options {
     my ($a_name, undef, $b_name, undef) = ( %$a, %$b );
     $a_name cmp $b_name;
   };
-  # Need to sort the options so we dont get supruious diffs.
+  # Need to sort the options so we don't get spurious diffs.
   my (@src_opts, @tar_opts);
   @src_opts = sort $cmp $src_table->options;
   @tar_opts = sort $cmp $tar_table->options;
@@ -429,7 +471,7 @@ SQL::Translator::Diff - determine differences between two schemas
 =head1 DESCRIPTION
 
 Takes two input SQL::Translator::Schemas (or SQL files) and produces ALTER
-statments to make them the same
+statements to make them the same
 
 =head1 SNYOPSIS
 
@@ -478,6 +520,11 @@ supports the ability to do all alters for a table as one statement.
 If the diff would need a method that is missing from the producer, just emit a
 comment showing the method is missing, rather than dieing with an error
 
+=item B<producer_args>
+
+Hash of extra arguments passed to L<SQL::Translator/new> and the below
+L</PRODUCER FUNCTIONS>.
+
 =back
 
 =head1 PRODUCER FUNCTIONS
@@ -488,34 +535,35 @@ thrown.
 
 =over
 
-=item * C<alter_create_constraint($con)>
+=item * C<alter_create_constraint($con, $args)>
 
-=item * C<alter_drop_constraint($con)>
+=item * C<alter_drop_constraint($con, $args)>
 
-=item * C<alter_create_index($idx)>
+=item * C<alter_create_index($idx, $args)>
 
-=item * C<alter_drop_index($idx)>
+=item * C<alter_drop_index($idx, $args)>
 
-=item * C<add_field($fld)>
+=item * C<add_field($fld, $args)>
 
-=item * C<alter_field($old_fld, $new_fld)>
+=item * C<alter_field($old_fld, $new_fld, $args)>
 
-=item * C<rename_field($old_fld, $new_fld)>
+=item * C<rename_field($old_fld, $new_fld, $args)>
 
-=item * C<drop_field($fld)>
+=item * C<drop_field($fld, $args)>
 
-=item * C<alter_table($table)>
+=item * C<alter_table($table, $args)>
 
-=item * C<drop_table($table)>
+=item * C<drop_table($table, $args)>
 
-=item * C<rename_table($old_table, $new_table)> (optional)
+=item * C<rename_table($old_table, $new_table, $args)> (optional)
 
-=item * C<batch_alter_table($table, $hash)> (optional)
+=item * C<batch_alter_table($table, $hash, $args)> (optional)
 
 If the producer supports C<batch_alter_table>, it will be called with the
 table to alter and a hash, the keys of which will be the method names listed
 above; values will be arrays of fields or constraints to operate on. In the
-case of the field functions that take two arguments this will appear as a hash.
+case of the field functions that take two arguments this will appear as an
+array reference.
 
 I.e. the hash might look something like the following:
 
@@ -530,7 +578,7 @@ I.e. the hash might look something like the following:
 
 C<preprocess_schema> is called by the Diff code to allow the producer to
 normalize any data it needs to first. For example, the MySQL producer uses
-this method to ensure that FK contraint names are unique.
+this method to ensure that FK constraint names are unique.
 
 Basicaly any changes that need to be made to produce the SQL file for the
 schema should be done here, so that a diff between a parsed SQL file and (say)