*** empty log message ***
[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 foreach my $table ( grep { ! /^h_/ } $schema->tables ) {
20   my $tableobj = $schema->table($table);
21   my $h_tableobj = DBIx::DBSchema::Table->new( {
22     name        => "h_$table",
23     primary_key => 'historynum',
24     unique      => DBIx::DBSchema::ColGroup::Unique->new( [] ),
25     'index'     => DBIx::DBSchema::ColGroup::Index->new( [
26                      @{$tableobj->unique->lol_ref},
27                      @{$tableobj->index->lol_ref}
28                    ] ),
29     columns     => [
30                      DBIx::DBSchema::Column->new( {
31                        'name'    => 'historynum',
32                        'type'    => 'serial',
33                        'null'    => 'NOT NULL',
34                        'length'  => '',
35                        'default' => '',
36                        'local'   => '',
37                      } ),
38                      DBIx::DBSchema::Column->new( {
39                        'name'    => 'history_date',
40                        'type'    => 'int',
41                        'null'    => 'NULL',
42                        'length'  => '',
43                        'default' => '',
44                        'local'   => '',
45                      } ),
46                      DBIx::DBSchema::Column->new( {
47                        'name'    => 'history_user',
48                        'type'    => 'varchar',
49                        'null'    => 'NOT NULL',
50                        'length'  => '80',
51                        'default' => '',
52                        'local'   => '',
53                      } ),
54                      DBIx::DBSchema::Column->new( {
55                        'name'    => 'history_action',
56                        'type'    => 'varchar',
57                        'null'    => 'NOT NULL',
58                        'length'  => '80',
59                        'default' => '',
60                        'local'   => '',
61                      } ),
62                      map { $tableobj->column($_) } $tableobj->columns
63                    ],
64   } );
65   foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
66     $dbh->do( $statement )
67       or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
68   }
69
70 }
71
72 $dbh->commit or die $dbh->errstr;
73 $dbh->disconnect or die $dbh->errstr;
74
75 sub usage {
76   die "Usage:\n  create-history-tables user\n";
77 }
78