33eb0e5a39fc336475a56a88328cef60d5df2a34
[freeside.git] / bin / create-history-tables
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use DBI;
5 use DBIx::DBSchema 0.21;
6 use DBIx::DBSchema::Table;
7 use DBIx::DBSchema::Column;
8 use DBIx::DBSchema::ColGroup::Unique;
9 use DBIx::DBSchema::ColGroup::Index;
10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(dbdef);
12
13 my $user = shift or die &usage;
14 my $dbh = adminsuidsetup $user;
15
16 my $schema = dbdef();
17
18 #false laziness w/fs-setup
19 my @tables = scalar(@ARGV)
20                ? @ARGV
21                : grep { ! /^h_/ } $schema->tables;
22 foreach my $table ( @tables ) {
23   warn "creating history table for $table\n";
24   my $tableobj = $schema->table($table)
25     or die "unknown table $table (did you run dbdef-create?)\n";
26   my $h_tableobj = DBIx::DBSchema::Table->new( {
27     name        => "h_$table",
28     primary_key => 'historynum',
29     unique      => DBIx::DBSchema::ColGroup::Unique->new( [] ),
30     'index'     => DBIx::DBSchema::ColGroup::Index->new( [
31                      @{$tableobj->unique->lol_ref},
32                      @{$tableobj->index->lol_ref}
33                    ] ),
34     columns     => [
35                      DBIx::DBSchema::Column->new( {
36                        'name'    => 'historynum',
37                        'type'    => 'serial',
38                        'null'    => 'NOT NULL',
39                        'length'  => '',
40                        'default' => '',
41                        'local'   => '',
42                      } ),
43                      DBIx::DBSchema::Column->new( {
44                        'name'    => 'history_date',
45                        'type'    => 'int',
46                        'null'    => 'NULL',
47                        'length'  => '',
48                        'default' => '',
49                        'local'   => '',
50                      } ),
51                      DBIx::DBSchema::Column->new( {
52                        'name'    => 'history_user',
53                        'type'    => 'varchar',
54                        'null'    => 'NOT NULL',
55                        'length'  => '80',
56                        'default' => '',
57                        'local'   => '',
58                      } ),
59                      DBIx::DBSchema::Column->new( {
60                        'name'    => 'history_action',
61                        'type'    => 'varchar',
62                        'null'    => 'NOT NULL',
63                        'length'  => '80',
64                        'default' => '',
65                        'local'   => '',
66                      } ),
67                      map {
68                        my $column = $tableobj->column($_);
69                        $column->type('int')
70                          if $column->type eq 'serial';
71                        $column->default('')
72                          if $column->default =~ /^nextval\(/i;
73                        ( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
74                        $column->local($local);
75                        $column;
76                      } $tableobj->columns
77                    ],
78   } );
79   foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
80     $dbh->do( $statement )
81       or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
82   }
83
84 }
85
86 $dbh->commit or die $dbh->errstr;
87 $dbh->disconnect or die $dbh->errstr;
88
89 sub usage {
90   die "Usage:\n  create-history-tables user [ table table ... ] \n";
91 }
92