skip comment & blank lines
[freeside.git] / bin / create-history-tables
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use DBI;
5 use DBIx::DBSchema 0.20;
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 { $tableobj->column($_) } $tableobj->columns
68                    ],
69   } );
70   foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
71     $dbh->do( $statement )
72       or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
73   }
74
75 }
76
77 $dbh->commit or die $dbh->errstr;
78 $dbh->disconnect or die $dbh->errstr;
79
80 sub usage {
81   die "Usage:\n  create-history-tables user [ table table ... ] \n";
82 }
83