3 # $Id: dbdef-create,v 1.2 1998-11-19 11:17:44 ivan Exp $
5 # create dbdef file for existing mySQL database (needs SHOW|DESCRIBE command
6 # not in Pg) based on fs-setup
8 # ivan@sisd.com 98-jun-2
10 # $Log: dbdef-create,v $
11 # Revision 1.2 1998-11-19 11:17:44 ivan
12 # adminsuidsetup requires argument
18 use FS::UID qw(adminsuidsetup datasrc);
20 my $user = shift or die &usage;
22 my($dbh)=adminsuidsetup $user;
24 #needs to match FS::Record
25 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
27 my($tables_sth)=$dbh->prepare("SHOW TABLES");
28 my($tables_rv)=$tables_sth->execute;
31 foreach ( @{$tables_sth->fetchall_arrayref} ) {
33 #print "TABLE\t$table\n";
35 my($index_sth)=$dbh->prepare("SHOW INDEX FROM $table");
38 for ( 1 .. $index_sth->execute ) {
39 my($row)=$index_sth->fetchrow_hashref;
40 if ( ${$row}{'Key_name'} eq "PRIMARY" ) {
41 $primary_key=${$row}{'Column_name'};
44 if ( ${$row}{'Non_unique'} ) { #index
45 push @{$index{${$row}{'Key_name'}}}, ${$row}{'Column_name'};
47 push @{$unique{${$row}{'Key_name'}}}, ${$row}{'Column_name'};
51 my(@index)=values %index;
52 my(@unique)=values %unique;
53 #print "\tPRIMARY KEY $primary_key\n";
55 #print "\tINDEX\t", join(', ', @{$_}), "\n";
58 #print "\tUNIQUE\t", join(', ', @{$_}), "\n";
61 my($columns_sth)=$dbh->prepare("SHOW COLUMNS FROM $table");
63 for ( 1 .. $columns_sth->execute ) {
64 my($row)=$columns_sth->fetchrow_hashref;
65 #print "\t", ${$row}{'Field'}, "\n";
66 ${$row}{'Type'} =~ /^(\w+)\(?([\d\,]+)?\)?( unsigned)?$/
67 or die "Illegal type ${$row}{'Type'}\n";
68 my($type,$length)=($1,$2);
69 my($null)=${$row}{'Null'};
71 push @columns, new FS::dbdef_column (
80 push @tables, new FS::dbdef_table (
83 new FS::dbdef_unique (\@unique),
84 new FS::dbdef_index (\@index),
90 my($dbdef) = new FS::dbdef ( @tables );
93 $dbdef->save($dbdef_file);
96 die "Usage:\n dbdef-create user\n";