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|pg)_/ } $schema->tables;
22 foreach my $table ( @tables ) {
23 next if grep { /^h_$table/ } $schema->tables;
24 warn "creating history table for $table\n";
25 my $tableobj = $schema->table($table)
26 or die "unknown table $table (did you run dbdef-create?)\n";
27 my $h_tableobj = DBIx::DBSchema::Table->new( {
29 primary_key => 'historynum',
30 unique => DBIx::DBSchema::ColGroup::Unique->new( [] ),
31 'index' => DBIx::DBSchema::ColGroup::Index->new( [
32 @{$tableobj->unique->lol_ref},
33 @{$tableobj->index->lol_ref}
36 DBIx::DBSchema::Column->new( {
37 'name' => 'historynum',
44 DBIx::DBSchema::Column->new( {
45 'name' => 'history_date',
52 DBIx::DBSchema::Column->new( {
53 'name' => 'history_user',
60 DBIx::DBSchema::Column->new( {
61 'name' => 'history_action',
69 my $column = $tableobj->column($_);
71 if $column->type eq 'serial';
73 if $column->default =~ /^nextval\(/i;
74 ( my $local = $column->local ) =~ s/AUTO_INCREMENT//i;
75 $column->local($local);
80 foreach my $statement ( $h_tableobj->sql_create_table($dbh) ) {
81 $dbh->do( $statement )
82 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
87 $dbh->commit or die $dbh->errstr;
88 $dbh->disconnect or die $dbh->errstr;
91 die "Usage:\n create-history-tables user [ table table ... ] \n";