3 # create database and necessary tables, etc. DBI version.
5 # ivan@sisd.com 97-nov-8,9
7 # agent_type and type_pkgs added.
8 # (index need to be declared, & primary keys shoudln't have mysql syntax)
9 # ivan@sisd.com 97-nov-13
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
18 # finished up ivan@sisd.com 98-jun-1
20 # part_svc fields are all forced NULL, not the opposite
21 # hmm: also are forced varchar($char_d) as fixed '0' for things like
22 # uid is Not Good. will this break anything else?
23 # ivan@sisd.com 98-jun-29
25 # ss is 11 chars ivan@sisd.com 98-jul-20
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
32 # fix radius attributes ivan@sisd.com 98-sep-27
35 # Revision 1.5 1998-11-15 09:43:03 ivan
36 # update for new config file syntax, new adminsuidsetup
38 # Revision 1.4 1998/10/22 15:51:23 ivan
39 # also varchar with no length specified - postgresql fix broke mysql.
41 # Revision 1.3 1998/10/22 15:46:28 ivan
42 # now smallint is illegal, so remove that too.
45 #to delay loading dbdef until we're ready
46 BEGIN { $FS::Record::setup_hack = 1; }
51 use FS::UID qw(adminsuidsetup datasrc);
53 use FS::cust_main_county;
55 my $user = shift or die &usage;
56 FS::UID::getsecrets $user;
58 #needs to match FS::Record
59 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
63 print "\nEnter the maximum username length: ";
64 my($username_len)=&getvalue;
66 print "\n\n", <<END, ":";
67 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
68 for each user. Enter any additional RADIUS attributes you need to track for
69 each user, separated by whitespace.
71 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
74 my($x)=scalar(<STDIN>);
81 my($char_d) = 80; #default maxlength for text fields
83 #my(@date_type) = ( 'timestamp', '', '' );
84 my(@date_type) = ( 'int', 'NULL', '' );
85 my(@perl_type) = ( 'varchar', 'NULL', 255 );
87 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
88 @money_type = ( 'money', '', '' );
90 @money_type = ( 'decimal', '', '10,2' );
94 # create a dbdef object from the old data structure
97 my(%tables)=&tables_hash_hack;
100 my($dbdef) = new FS::dbdef ( map {
102 while (@{$tables{$_}{'columns'}}) {
103 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
104 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
106 FS::dbdef_table->new(
108 $tables{$_}{'primary_key'},
109 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
110 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
111 FS::dbdef_unique->new($tables{$_}{'unique'}),
112 FS::dbdef_index->new($tables{$_}{'index'}),
117 #add radius attributes to svc_acct
119 my($svc_acct)=$dbdef->table('svc_acct');
122 foreach $attribute (@attributes) {
123 $svc_acct->addcolumn ( new FS::dbdef_column (
124 'radius_'. $attribute,
131 #make part_svc table (but now as object)
133 my($part_svc)=$dbdef->table('part_svc');
135 #because of svc_acct_pop
136 #foreach (grep /^svc_/, $dbdef->tables) {
137 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
138 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
139 my($table)=$dbdef->table($_);
141 foreach $col ( $table->columns ) {
142 next if $col =~ /^svcnum$/;
143 $part_svc->addcolumn( new FS::dbdef_column (
144 $table->name. '__' . $table->column($col)->name,
145 'varchar', #$table->column($col)->type,
147 $char_d, #$table->column($col)->length,
149 $part_svc->addcolumn ( new FS::dbdef_column (
150 $table->name. '__'. $table->column($col)->name . "_flag",
159 $dbdef->save($dbdef_file);
160 FS::Record::reload_dbdef($dbdef_file);
166 my($dbh)=adminsuidsetup $user;
173 foreach ($dbdef->tables) {
174 my($table)=$dbdef->table($_);
175 print "Creating $_...";
180 foreach $statement ($table->sql_create_table(datasrc)) {
181 #print $statement, "\n";
182 $dbh->do( $statement )
183 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
189 #not really sample data (and shouldn't default to US)
193 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
194 ME MH MD MA MI MN MS MO MT NC ND NE NH NJ NM NV NY MP OH OK OR PA PW PR RI
195 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
197 my($cust_main_county)=create FS::cust_main_county({
202 $error=$cust_main_county->insert;
203 die $error if $error;
206 $dbh->disconnect or die $dbh->errstr;
209 die "Usage:\n fs-setup user\n";
213 # Now it becomes an object. much better.
215 sub tables_hash_hack {
217 #note that s/(date|change)/_$1/; to avoid keyword conflict.
218 #put a kludge in FS::Record to catch this or? (pry need some date-handling
221 my(%tables)=( #yech.}
225 'agentnum', 'int', '', '',
226 'agent', 'varchar', '', $char_d,
227 'typenum', 'int', '', '',
228 'freq', 'int', 'NULL', '',
231 'primary_key' => 'agentnum',
233 'index' => [ ['typenum'] ],
238 'typenum', 'int', '', '',
239 'atype', 'varchar', '', $char_d,
241 'primary_key' => 'typenum',
248 'typenum', 'int', '', '',
249 'pkgpart', 'int', '', '',
252 'unique' => [ ['typenum', 'pkgpart'] ],
253 'index' => [ ['typenum'] ],
258 'invnum', 'int', '', '',
259 'custnum', 'int', '', '',
261 'charged', @money_type,
263 'printed', 'int', '', '',
265 'primary_key' => 'invnum',
267 'index' => [ ['custnum'] ],
272 'pkgnum', 'int', '', '',
273 'invnum', 'int', '', '',
274 'setup', @money_type,
275 'recur', @money_type,
280 'unique' => [ ['pkgnum', 'invnum'] ],
281 'index' => [ ['invnum'] ],
286 'crednum', 'int', '', '',
287 'custnum', 'int', '', '',
289 'amount', @money_type,
290 'credited', @money_type,
291 'otaker', 'varchar', '', 8,
292 'reason', 'varchar', '', 255,
294 'primary_key' => 'crednum',
296 'index' => [ ['custnum'] ],
301 'custnum', 'int', '', '',
302 'agentnum', 'int', '', '',
303 'last', 'varchar', '', $char_d,
304 'first', 'varchar', '', $char_d,
305 'ss', 'char', 'NULL', 11,
306 'company', 'varchar', 'NULL', $char_d,
307 'address1', 'varchar', '', $char_d,
308 'address2', 'varchar', 'NULL', $char_d,
309 'city', 'varchar', '', $char_d,
310 'county', 'varchar', 'NULL', $char_d,
311 'state', 'char', '', 2,
312 'zip', 'varchar', '', 10,
313 'country', 'char', '', 2,
314 'daytime', 'varchar', 'NULL', 20,
315 'night', 'varchar', 'NULL', 20,
316 'fax', 'varchar', 'NULL', 12,
317 'payby', 'char', '', 4,
318 'payinfo', 'varchar', 'NULL', 16,
319 'paydate', @date_type,
320 'payname', 'varchar', 'NULL', $char_d,
321 'tax', 'char', 'NULL', 1,
322 'otaker', 'varchar', '', 8,
323 'refnum', 'int', '', '',
325 'primary_key' => 'custnum',
327 #'index' => [ ['last'], ['company'] ],
328 'index' => [ ['last'], ],
331 'cust_main_county' => { #county+state are checked off the cust_main_county
332 #table for validation and to provide a tax rate.
335 'taxnum', 'int', '', '',
336 'state', 'char', '', 2, #two letters max in US... elsewhere?
337 'county', 'varchar', 'NULL', $char_d,
338 'tax', 'real', '', '', #tax %
340 'primary_key' => 'taxnum',
342 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
348 'paynum', 'int', '', '',
349 'invnum', 'int', '', '',
352 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
353 # payment type table.
354 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
355 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
357 'primary_key' => 'paynum',
359 'index' => [ ['invnum'] ],
362 'cust_pay_batch' => { #what's this used for again? list of customers
363 #in current CARD batch? (necessarily CARD?)
365 'invnum', 'int', '', '',
366 'custnum', 'int', '', '',
367 'last', 'varchar', '', $char_d,
368 'first', 'varchar', '', $char_d,
369 'address1', 'varchar', '', $char_d,
370 'address2', 'varchar', 'NULL', $char_d,
371 'city', 'varchar', '', $char_d,
372 'state', 'char', '', 2,
373 'zip', 'varchar', '', 10,
374 'country', 'char', '', 2,
375 'trancode', 'int', '', '',
376 'cardnum', 'varchar', '', 16,
378 'payname', 'varchar', 'NULL', $char_d,
379 'amount', @money_type,
383 'index' => [ ['invnum'], ['custnum'] ],
388 'pkgnum', 'int', '', '',
389 'custnum', 'int', '', '',
390 'pkgpart', 'int', '', '',
391 'otaker', 'varchar', '', 8,
395 'cancel', @date_type,
396 'expire', @date_type,
398 'primary_key' => 'pkgnum',
400 'index' => [ ['custnum'] ],
405 'refundnum', 'int', '', '',
406 'crednum', 'int', '', '',
408 'refund', @money_type,
409 'otaker', 'varchar', '', 8,
410 'reason', 'varchar', '', $char_d,
411 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
412 # into payment type table.
413 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
415 'primary_key' => 'refundnum',
417 'index' => [ ['crednum'] ],
422 'svcnum', 'int', '', '',
423 'pkgnum', 'int', '', '',
424 'svcpart', 'int', '', '',
426 'primary_key' => 'svcnum',
428 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
433 'pkgpart', 'int', '', '',
434 'pkg', 'varchar', '', $char_d,
435 'comment', 'varchar', '', $char_d,
437 'freq', 'int', '', '', #billing frequency (months)
440 'primary_key' => 'pkgpart',
447 'pkgpart', 'int', '', '',
448 'svcpart', 'int', '', '',
449 'quantity', 'int', '', '',
452 'unique' => [ ['pkgpart', 'svcpart'] ],
453 'index' => [ ['pkgpart'] ],
458 'refnum', 'int', '', '',
459 'referral', 'varchar', '', $char_d,
461 'primary_key' => 'refnum',
468 'svcpart', 'int', '', '',
469 'svc', 'varchar', '', $char_d,
470 'svcdb', 'varchar', '', $char_d,
472 'primary_key' => 'svcpart',
477 #(this should be renamed to part_pop)
480 'popnum', 'int', '', '',
481 'city', 'varchar', '', $char_d,
482 'state', 'char', '', 2,
484 'exch', 'char', '', 3,
487 'primary_key' => 'popnum',
494 'svcnum', 'int', '', '',
495 'username', 'varchar', '', $username_len, #unique (& remove dup code)
496 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
497 'popnum', 'int', 'NULL', '',
498 'uid', 'int', 'NULL', '',
499 'gid', 'int', 'NULL', '',
500 'finger', 'varchar', 'NULL', $char_d,
501 'dir', 'varchar', 'NULL', $char_d,
502 'shell', 'varchar', 'NULL', $char_d,
503 'quota', 'varchar', 'NULL', $char_d,
504 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
506 'primary_key' => 'svcnum',
508 'index' => [ ['username'] ],
513 'svcnum', 'int', '', '',
514 'domsvc', 'int', '', '',
515 'domuid', 'int', '', '',
516 'domuser', 'varchar', '', $char_d,
518 'primary_key' => 'svcnum',
520 'index' => [ ['domsvc'], ['domuid'] ],
525 # 'svcnum', 'int', '', '',
526 # 'amount', @money_type,
528 # 'primary_key' => 'svcnum',
529 # 'unique' => [ [] ],
535 'svcnum', 'int', '', '',
536 'domain', 'varchar', '', $char_d,
538 'primary_key' => 'svcnum',
539 'unique' => [ ['domain'] ],
545 # 'svcnum', 'int', '', '',
546 # 'svcnum', 'int', '', '',
547 # 'svcnum', 'int', '', '',
548 # 'worker', 'varchar', '', $char_d,
549 # '_date', @date_type,
551 # 'primary_key' => 'svcnum',
552 # 'unique' => [ [] ],