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);
13 my $user = shift or die &usage;
14 my $dbh = adminsuidsetup $user;
18 #false laziness w/fs-setup
19 my @tables = scalar(@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( {
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}
35 DBIx::DBSchema::Column->new( {
36 'name' => 'historynum',
43 DBIx::DBSchema::Column->new( {
44 'name' => 'history_date',
51 DBIx::DBSchema::Column->new( {
52 'name' => 'history_user',
59 DBIx::DBSchema::Column->new( {
60 'name' => 'history_action',
68 my $column = $tableobj->column($_);
70 if $column->type eq 'serial';
72 if $column->default =~ /^nextval\(/i;
73 ( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
74 $column->local($local);
79 foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
80 $dbh->do( $statement )
81 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
86 $dbh->commit or die $dbh->errstr;
87 $dbh->disconnect or die $dbh->errstr;
90 die "Usage:\n create-history-tables user [ table table ... ] \n";