allow payment modification so we can import order_number info
[freeside.git] / bin / create-history-tables
index fb4c866..39248bf 100755 (executable)
@@ -2,7 +2,7 @@
 
 use strict;
 use DBI;
-use DBIx::DBSchema 0.20;
+use DBIx::DBSchema 0.21;
 use DBIx::DBSchema::Table;
 use DBIx::DBSchema::Column;
 use DBIx::DBSchema::ColGroup::Unique;
@@ -16,8 +16,14 @@ my $dbh = adminsuidsetup $user;
 my $schema = dbdef();
 
 #false laziness w/fs-setup
-foreach my $table ( grep { ! /^h_/ } $schema->tables ) {
-  my $tableobj = $schema->table($table);
+my @tables = scalar(@ARGV)
+               ? @ARGV
+               : grep { ! /^(h|pg)_/ } $schema->tables;
+foreach my $table ( @tables ) {
+  next if grep { /^h_$table/ } $schema->tables;
+  warn "creating history table for $table\n";
+  my $tableobj = $schema->table($table)
+    or die "unknown table $table (did you run dbdef-create?)\n";
   my $h_tableobj = DBIx::DBSchema::Table->new( {
     name        => "h_$table",
     primary_key => 'historynum',
@@ -59,7 +65,16 @@ foreach my $table ( grep { ! /^h_/ } $schema->tables ) {
                        'default' => '',
                        'local'   => '',
                      } ),
-                     map { $tableobj->column($_) } $tableobj->columns
+                     map {
+                       my $column = $tableobj->column($_);
+                       $column->type('int')
+                         if $column->type eq 'serial';
+                       $column->default('')
+                         if $column->default =~ /^nextval\(/i;
+                       ( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
+                       $column->local($local);
+                       $column;
+                     } $tableobj->columns
                    ],
   } );
   foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
@@ -73,6 +88,6 @@ $dbh->commit or die $dbh->errstr;
 $dbh->disconnect or die $dbh->errstr;
 
 sub usage {
-  die "Usage:\n  create-history-tables user\n";
+  die "Usage:\n  create-history-tables user [ table table ... ] \n";
 }