i guess svc_www.usersvc can be null...
[freeside.git] / FS / FS / Schema.pm
index 5ff549d..adc32a2 100644 (file)
@@ -3,12 +3,10 @@ package FS::Schema;
 use vars qw(@ISA @EXPORT_OK $DEBUG $setup_hack %dbdef_cache);
 use subs qw(reload_dbdef);
 use Exporter;
-use DBIx::DBSchema 0.30;
+use DBIx::DBSchema 0.33;
 use DBIx::DBSchema::Table;
 use DBIx::DBSchema::Column 0.06;
-use DBIx::DBSchema::ColGroup::Unique;
-use DBIx::DBSchema::ColGroup::Index;
-use FS::UID qw(datasrc);
+use DBIx::DBSchema::Index;
 
 @ISA = qw(Exporter);
 @EXPORT_OK = qw( dbdef dbdef_dist reload_dbdef );
@@ -81,30 +79,58 @@ sub dbdef_dist {
 
   my $tables_hashref = tables_hashref();
 
+
   #turn it into objects
   my $dbdef = new DBIx::DBSchema map {  
+
+    my $tablename = $_;
+    my $indexnum = 1;
+
     my @columns;
-    while (@{$tables_hashref->{$_}{'columns'}}) {
+    while (@{$tables_hashref->{$tablename}{'columns'}}) {
       #my($name, $type, $null, $length, $default, $local) =
       my @coldef = 
-        splice @{$tables_hashref->{$_}{'columns'}}, 0, 6;
+        splice @{$tables_hashref->{$tablename}{'columns'}}, 0, 6;
       my %hash = map { $_ => shift @coldef }
                      qw( name type null length default local );
 
       unless ( defined $hash{'default'} ) {
-        warn "$_:\n".
+        warn "$tablename:\n".
              join('', map "$_ => $hash{$_}\n", keys %hash) ;# $stop = <STDIN>;
       }
 
       push @columns, new DBIx::DBSchema::Column ( \%hash );
     }
-    DBIx::DBSchema::Table->new(
-      $_,
-      $tables_hashref->{$_}{'primary_key'},
-      DBIx::DBSchema::ColGroup::Unique->new($tables_hashref->{$_}{'unique'}),
-      DBIx::DBSchema::ColGroup::Index->new($tables_hashref->{$_}{'index'}),
-      @columns,
-    );
+
+    #false laziness w/sub indices in DBIx::DBSchema::DBD (well, sorta)
+    #and sub sql_create_table in DBIx::DBSchema::Table (slighty more?)
+    my $unique = $tables_hashref->{$tablename}{'unique'};
+    my $index  = $tables_hashref->{$tablename}{'index'};
+    my @indices = ();
+    push @indices, map {
+                         DBIx::DBSchema::Index->new({
+                           'name'    => $tablename. $indexnum++,
+                           'unique'  => 1,
+                           'columns' => $_,
+                         });
+                       }
+                       @$unique;
+    push @indices, map {
+                         DBIx::DBSchema::Index->new({
+                           'name'    => $tablename. $indexnum++,
+                           'unique'  => 0,
+                           'columns' => $_,
+                         });
+                       }
+                       @$index;
+
+    DBIx::DBSchema::Table->new({
+      'name'        => $tablename,
+      'primary_key' => $tables_hashref->{$tablename}{'primary_key'},
+      'columns'     => \@columns,
+      'indices'     => \@indices,
+    });
+
   } keys %$tables_hashref;
 
   if ( $DEBUG ) {
@@ -112,14 +138,6 @@ sub dbdef_dist {
     warn "[debug]$me   $_\n" foreach $dbdef->tables;
   }
   
-  my $cust_main = $dbdef->table('cust_main');
-  #unless ($ship) { #remove ship_ from cust_main
-  #  $cust_main->delcolumn($_) foreach ( grep /^ship_/, $cust_main->columns );
-  #} else { #add indices
-    push @{$cust_main->index->lol_ref},
-      map { [ "ship_$_" ] } qw( last company daytime night fax );
-  #}
-  
   #add radius attributes to svc_acct
   #
   #my($svc_acct)=$dbdef->table('svc_acct');
@@ -151,72 +169,76 @@ sub dbdef_dist {
   ) {
     my $tableobj = $dbdef->table($table)
       or die "unknown table $table";
-  
-    die "unique->lol_ref undefined for $table"
-      unless defined $tableobj->unique->lol_ref;
-    die "index->lol_ref undefined for $table"
-      unless defined $tableobj->index->lol_ref;
-  
+
+    my %indices = $tableobj->indices;
+    
+    my %h_indices = map { 
+                          ( "h_$_" =>
+                              DBIx::DBSchema::Index->new({
+                                'name'    => 'h_'. $indices{$_}->name,
+                                'unique'  => 0,
+                                'columns' => [ @{$indices{$_}->columns} ],
+                              })
+                          );
+                        }
+                        keys %indices;
+
     my $h_tableobj = DBIx::DBSchema::Table->new( {
-      name        => "h_$table",
-      primary_key => 'historynum',
-      unique      => DBIx::DBSchema::ColGroup::Unique->new( [] ),
-      'index'     => DBIx::DBSchema::ColGroup::Index->new( [
-                       @{$tableobj->unique->lol_ref},
-                       @{$tableobj->index->lol_ref}
-                     ] ),
-      columns     => [
-                       DBIx::DBSchema::Column->new( {
-                         'name'    => 'historynum',
-                         'type'    => 'serial',
-                         'null'    => 'NOT NULL',
-                         'length'  => '',
-                         'default' => '',
-                         'local'   => '',
-                       } ),
-                       DBIx::DBSchema::Column->new( {
-                         'name'    => 'history_date',
-                         'type'    => 'int',
-                         'null'    => 'NULL',
-                         'length'  => '',
-                         'default' => '',
-                         'local'   => '',
-                       } ),
-                       DBIx::DBSchema::Column->new( {
-                         'name'    => 'history_user',
-                         'type'    => 'varchar',
-                         'null'    => 'NOT NULL',
-                         'length'  => '80',
-                         'default' => '',
-                         'local'   => '',
-                       } ),
-                       DBIx::DBSchema::Column->new( {
-                         'name'    => 'history_action',
-                         'type'    => 'varchar',
-                         'null'    => 'NOT NULL',
-                         'length'  => '80',
-                         'default' => '',
-                         'local'   => '',
-                       } ),
-                       map {
-                         my $column = $tableobj->column($_);
-  
-                         #clone so as to not disturb the original
-                         $column = DBIx::DBSchema::Column->new( {
-                           map { $_ => $column->$_() }
-                             qw( name type null length default local )
-                         } );
-  
-                         if ( $column->type eq 'serial' ) {
-                           $column->type('int');
-                           $column->null('NULL');
-                         }
-                         #$column->default('')
-                         #  if $column->default =~ /^nextval\(/i;
-                         #( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
-                         #$column->local($local);
-                         $column;
-                       } $tableobj->columns
+      'name'        => "h_$table",
+      'primary_key' => 'historynum',
+      'indices'     => \%h_indices,
+      'columns'     => [
+                         DBIx::DBSchema::Column->new( {
+                           'name'    => 'historynum',
+                           'type'    => 'serial',
+                           'null'    => 'NOT NULL',
+                           'length'  => '',
+                           'default' => '',
+                           'local'   => '',
+                         } ),
+                         DBIx::DBSchema::Column->new( {
+                           'name'    => 'history_date',
+                           'type'    => 'int',
+                           'null'    => 'NULL',
+                           'length'  => '',
+                           'default' => '',
+                           'local'   => '',
+                         } ),
+                         DBIx::DBSchema::Column->new( {
+                           'name'    => 'history_user',
+                           'type'    => 'varchar',
+                           'null'    => 'NOT NULL',
+                           'length'  => '80',
+                           'default' => '',
+                           'local'   => '',
+                         } ),
+                         DBIx::DBSchema::Column->new( {
+                           'name'    => 'history_action',
+                           'type'    => 'varchar',
+                           'null'    => 'NOT NULL',
+                           'length'  => '80',
+                           'default' => '',
+                           'local'   => '',
+                         } ),
+                         map {
+                           my $column = $tableobj->column($_);
+    
+                           #clone so as to not disturb the original
+                           $column = DBIx::DBSchema::Column->new( {
+                             map { $_ => $column->$_() }
+                               qw( name type null length default local )
+                           } );
+    
+                           if ( $column->type eq 'serial' ) {
+                             $column->type('int');
+                             $column->null('NULL');
+                           }
+                           #$column->default('')
+                           #  if $column->default =~ /^nextval\(/i;
+                           #( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
+                           #$column->local($local);
+                           $column;
+                         } $tableobj->columns
                      ],
     } );
     $dbdef->addtable($h_tableobj);
@@ -243,15 +265,17 @@ sub tables_hashref {
 
     'agent' => {
       'columns' => [
-        'agentnum', 'serial',            '',     '', '', '', 
-        'agent',    'varchar',           '',     $char_d, '', '', 
-        'typenum',  'int',            '',     '', '', '', 
-        'freq',     'int',       'NULL', '', '', '', 
-        'prog',     @perl_type, '', '', 
-        'disabled',     'char', 'NULL', 1, '', '', 
-        'username', 'varchar',       'NULL',     $char_d, '', '', 
-        '_password','varchar',       'NULL',     $char_d, '', '', 
-        'ticketing_queueid', 'int', 'NULL', '', '', '', 
+        'agentnum',          'serial',    '',       '', '', '', 
+        'agent',            'varchar',    '',  $char_d, '', '', 
+        'typenum',              'int',    '',       '', '', '', 
+        'disabled',            'char', 'NULL',       1, '', '', 
+        'ticketing_queueid',    'int', 'NULL',      '', '', '', 
+        'invoice_template', 'varchar', 'NULL', $char_d, '', '',
+        'username',         'varchar', 'NULL', $char_d, '', '', #deprecated
+        '_password',        'varchar', 'NULL', $char_d, '', '', #deprecated
+        'freq',              'int', 'NULL', '', '', '', #deprecated (never used)
+        'prog',                     @perl_type, '', '', #deprecated (never used)
+
       ],
       'primary_key' => 'agentnum',
       'unique' => [],
@@ -327,6 +351,84 @@ sub tables_hashref {
       'index' => [ ['payby'], ['disabled'], ],
     },
 
+    'part_event' => {
+      'columns' => [
+        'eventpart',   'serial',      '',      '', '', '', 
+        'agentnum',    'int',     'NULL',      '', '', '', 
+        'event',       'varchar',     '', $char_d, '', '', 
+        'eventtable',  'varchar',     '', $char_d, '', '',
+        'check_freq',  'varchar', 'NULL', $char_d, '', '', 
+        'weight',      'int',         '',      '', '', '', 
+        'action',      'varchar',     '', $char_d, '', '',
+        'disabled',     'char',   'NULL',       1, '', '', 
+      ],
+      'primary_key' => 'eventpart',
+      'unique' => [],
+      'index' => [ ['agentnum'], ['eventtable'], ['check_freq'], ['disabled'], ],
+    },
+
+    'part_event_option' => {
+      'columns' => [
+        'optionnum', 'serial', '', '', '', '', 
+        'eventpart', 'int', '', '', '', '', 
+        'optionname', 'varchar', '', $char_d, '', '', 
+        'optionvalue', 'text', 'NULL', '', '', '', 
+      ],
+      'primary_key' => 'optionnum',
+      'unique'      => [],
+      'index'       => [ [ 'eventpart' ], [ 'optionname' ] ],
+    },
+
+    'part_event_condition' => {
+      'columns' => [
+        'eventconditionnum', 'serial', '', '', '', '', 
+        'eventpart', 'int', '', '', '', '', 
+        'conditionname', 'varchar', '', $char_d, '', '', 
+      ],
+      'primary_key' => 'eventconditionnum',
+      'unique'      => [],
+      'index'       => [ [ 'eventpart' ], [ 'conditionname' ] ],
+    },
+
+    'part_event_condition_option' => {
+      'columns' => [
+        'optionnum', 'serial', '', '', '', '', 
+        'eventconditionnum', 'int', '', '', '', '', 
+        'optionname', 'varchar', '', $char_d, '', '', 
+        'optionvalue', 'text', 'NULL', '', '', '', 
+      ],
+      'primary_key' => 'optionnum',
+      'unique'      => [],
+      'index'       => [ [ 'eventconditionnum' ], [ 'optionname' ] ],
+    },
+
+    'part_event_condition_option_option' => {
+      'columns' => [
+        'optionoptionnum', 'serial', '', '', '', '', 
+        'optionnum', 'int', '', '', '', '', 
+        'optionname', 'varchar', '', $char_d, '', '', 
+        'optionvalue', 'text', 'NULL', '', '', '', 
+      ],
+      'primary_key' => 'optionoptionnum',
+      'unique'      => [],
+      'index'       => [ [ 'optionnum' ], [ 'optionname' ] ],
+    },
+
+    'cust_event' => {
+      'columns' => [
+        'eventnum',    'serial',  '', '', '', '', 
+        'eventpart',   'int',  '', '', '', '', 
+        'tablenum',   'int',  '', '', '', '', 
+        '_date',     @date_type, '', '', 
+        'status', 'varchar', '', $char_d, '', '', 
+        'statustext', 'text', 'NULL', '', '', '', 
+      ],
+      'primary_key' => 'eventnum',
+      #no... there are retries now #'unique' => [ [ 'eventpart', 'invnum' ] ],
+      'unique' => [],
+      'index' => [ ['eventpart'], ['tablenum'], ['status'] ],
+    },
+
     'cust_bill_pkg' => {
       'columns' => [
         'billpkgnum', 'serial', '', '', '', '', 
@@ -465,8 +567,8 @@ sub tables_hashref {
                    [ 'daytime' ], [ 'night' ], [ 'fax' ], [ 'refnum' ],
                    [ 'county' ], [ 'state' ], [ 'country' ], [ 'zip' ],
                    [ 'ship_last' ], [ 'ship_company' ],
+                   [ 'ship_daytime' ], [ 'ship_night' ], [ 'ship_fax' ],
                    [ 'payby' ], [ 'paydate' ],
-
                  ],
     },
 
@@ -529,10 +631,11 @@ sub tables_hashref {
        'paymask', 'varchar', 'NULL', $char_d, '', '', 
         'paydate',  'varchar', 'NULL', 10, '', '', 
         'paybatch', 'varchar',   'NULL', $char_d, '', '', #for auditing purposes.
+        'payunique', 'varchar', 'NULL', $char_d, '', '', #separate paybatch "unique" functions from current usage
         'closed',    'char', 'NULL', 1, '', '', 
       ],
       'primary_key' => 'paynum',
-      'unique' => [],
+      'unique' => [ [ 'payunique' ] ],
       'index' => [ [ 'custnum' ], [ 'paybatch' ], [ 'payby' ], [ '_date' ] ],
     },
 
@@ -658,7 +761,10 @@ sub tables_hashref {
       ],
       'primary_key' => 'pkgnum',
       'unique' => [],
-      'index' => [ ['custnum'], ['pkgpart'] ],
+      'index' => [ ['custnum'], ['pkgpart'],
+                   ['setup'], ['last_bill'], ['bill'], ['susp'], ['adjourn'],
+                   ['expire'], ['cancel']
+                 ],
     },
 
     'cust_pkg_option' => {
@@ -705,7 +811,7 @@ sub tables_hashref {
       ],
       'primary_key' => 'refundnum',
       'unique' => [],
-      'index' => [],
+      'index' => [ [ 'custnum' ] ],
     },
 
     'cust_credit_refund' => {
@@ -945,10 +1051,10 @@ sub tables_hashref {
 
     'svc_www' => {
       'columns' => [
-        'svcnum',   'int',    '',  '', '', '', 
-        'recnum',   'int',    '',  '', '', '', 
-        'usersvc',  'int',    '',  '', '', '', 
-        'config',   'text',   'NULL',  '', '', '', 
+        'svcnum',   'int',      '',  '', '', '', 
+        'recnum',   'int',      '',  '', '', '', 
+        'usersvc',  'int',  'NULL',  '', '', '', 
+        'config',   'text', 'NULL',  '', '', '', 
       ],
       'primary_key' => 'svcnum',
       'unique'      => [],
@@ -1708,6 +1814,16 @@ sub tables_hashref {
       'index' => [],
     },
 
+    'pkg_referral' => {
+      'columns' => [
+        'pkgrefnum',     'serial', '', '', '', '',
+        'pkgnum',        'int',    '', '', '', '',
+        'refnum',        'int',    '', '', '', '',
+      ],
+      'primary_key' => 'pkgrefnum',
+      'unique'      => [ [ 'pkgnum', 'refnum' ] ],
+      'index'       => [ [ 'pkgnum' ], [ 'refnum' ] ],
+    },
     # name type nullability length default local
 
     #'new_table' => {