3 # $Id: fs-setup,v 1.12 1999-02-03 10:42:27 ivan Exp $
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.12 1999-02-03 10:42:27 ivan
36 # *** empty log message ***
38 # Revision 1.11 1999/01/17 03:11:52 ivan
39 # remove preliminary completehost changes
41 # Revision 1.10 1998/12/16 06:05:38 ivan
42 # add table cust_main_invoice
44 # Revision 1.9 1998/12/15 04:36:29 ivan
47 # Revision 1.8 1998/12/15 04:33:27 ivan
48 # dies if it isn't running as the freeside user
50 # Revision 1.7 1998/11/18 09:01:31 ivan
53 # Revision 1.6 1998/11/15 13:18:02 ivan
56 # Revision 1.5 1998/11/15 09:43:03 ivan
57 # update for new config file syntax, new adminsuidsetup
59 # Revision 1.4 1998/10/22 15:51:23 ivan
60 # also varchar with no length specified - postgresql fix broke mysql.
62 # Revision 1.3 1998/10/22 15:46:28 ivan
63 # now smallint is illegal, so remove that too.
66 #to delay loading dbdef until we're ready
67 BEGIN { $FS::Record::setup_hack = 1; }
72 use FS::UID qw(adminsuidsetup datasrc checkeuid);
74 use FS::cust_main_county;
76 die "Not running uid freeside!" unless checkeuid();
78 my $user = shift or die &usage;
79 FS::UID::getsecrets $user;
81 #needs to match FS::Record
82 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
86 print "\nEnter the maximum username length: ";
87 my($username_len)=&getvalue;
89 print "\n\n", <<END, ":";
90 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
91 for each user. Enter any additional RADIUS attributes you need to track for
92 each user, separated by whitespace.
94 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
97 my($x)=scalar(<STDIN>);
104 my($char_d) = 80; #default maxlength for text fields
106 #my(@date_type) = ( 'timestamp', '', '' );
107 my(@date_type) = ( 'int', 'NULL', '' );
108 my(@perl_type) = ( 'varchar', 'NULL', 255 );
110 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
111 @money_type = ( 'money', '', '' );
113 @money_type = ( 'decimal', '', '10,2' );
117 # create a dbdef object from the old data structure
120 my(%tables)=&tables_hash_hack;
122 #turn it into objects
123 my($dbdef) = new FS::dbdef ( map {
125 while (@{$tables{$_}{'columns'}}) {
126 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
127 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
129 FS::dbdef_table->new(
131 $tables{$_}{'primary_key'},
132 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
133 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
134 FS::dbdef_unique->new($tables{$_}{'unique'}),
135 FS::dbdef_index->new($tables{$_}{'index'}),
140 #add radius attributes to svc_acct
142 my($svc_acct)=$dbdef->table('svc_acct');
145 foreach $attribute (@attributes) {
146 $svc_acct->addcolumn ( new FS::dbdef_column (
147 'radius_'. $attribute,
154 #make part_svc table (but now as object)
156 my($part_svc)=$dbdef->table('part_svc');
158 #because of svc_acct_pop
159 #foreach (grep /^svc_/, $dbdef->tables) {
160 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
161 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
162 my($table)=$dbdef->table($_);
164 foreach $col ( $table->columns ) {
165 next if $col =~ /^svcnum$/;
166 $part_svc->addcolumn( new FS::dbdef_column (
167 $table->name. '__' . $table->column($col)->name,
168 'varchar', #$table->column($col)->type,
170 $char_d, #$table->column($col)->length,
172 $part_svc->addcolumn ( new FS::dbdef_column (
173 $table->name. '__'. $table->column($col)->name . "_flag",
182 $dbdef->save($dbdef_file);
183 &FS::Record::reload_dbdef($dbdef_file);
189 my($dbh)=adminsuidsetup $user;
195 foreach ($dbdef->tables) {
196 my($table)=$dbdef->table($_);
197 print "Creating $_...";
202 foreach $statement ($table->sql_create_table(datasrc)) {
203 #print $statement, "\n";
204 $dbh->do( $statement )
205 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
211 #not really sample data (and shouldn't default to US)
217 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
218 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
219 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
221 my($cust_main_county)=create FS::cust_main_county({
227 $error=$cust_main_county->insert;
228 die $error if $error;
231 #ISO 2-letter country codes (same as country TLDs) except US
233 AF AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
234 BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC CO KM CG CK CR CI
235 HR CU CY CZ DK DJ DM DO TP EC EG SV GQ ER EE ET FK FO FJ FI FR FX GF PF TF GA
236 GM GE DE GH GI GR GL GD GP GU GT GN GW GY HT HM HN HK HU IS IN ID IR IQ IE IL
237 IT JM JP JO KZ KE KI KP KR KW KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV
238 ML MT MH MQ MR MU YT MX FM MD MC MN MS MA MZ MM NA NR NP NL AN NC NZ NI NE NG
239 NU NF MP NO OM PK PW PA PG PY PE PH PN PL PT PR QA RE RO RU RW KN LC VC WS SM
240 ST SA SN SC SL SG SK SI SB SO ZA GS ES LK SH PM SD SR SJ SZ SE CH SY TW TJ TZ
241 TH TG TK TO TT TN TR TM TC TV UG UA AE GB UM UY UZ VU VA VE VN VG VI WF EH
244 my($cust_main_county)=create FS::cust_main_county({
249 $error=$cust_main_county->insert;
250 die $error if $error;
253 $dbh->disconnect or die $dbh->errstr;
256 die "Usage:\n fs-setup user\n";
260 # Now it becomes an object. much better.
262 sub tables_hash_hack {
264 #note that s/(date|change)/_$1/; to avoid keyword conflict.
265 #put a kludge in FS::Record to catch this or? (pry need some date-handling
268 my(%tables)=( #yech.}
272 'agentnum', 'int', '', '',
273 'agent', 'varchar', '', $char_d,
274 'typenum', 'int', '', '',
275 'freq', 'int', 'NULL', '',
278 'primary_key' => 'agentnum',
280 'index' => [ ['typenum'] ],
285 'typenum', 'int', '', '',
286 'atype', 'varchar', '', $char_d,
288 'primary_key' => 'typenum',
295 'typenum', 'int', '', '',
296 'pkgpart', 'int', '', '',
299 'unique' => [ ['typenum', 'pkgpart'] ],
300 'index' => [ ['typenum'] ],
305 'invnum', 'int', '', '',
306 'custnum', 'int', '', '',
308 'charged', @money_type,
310 'printed', 'int', '', '',
312 'primary_key' => 'invnum',
314 'index' => [ ['custnum'] ],
319 'pkgnum', 'int', '', '',
320 'invnum', 'int', '', '',
321 'setup', @money_type,
322 'recur', @money_type,
327 'unique' => [ ['pkgnum', 'invnum'] ],
328 'index' => [ ['invnum'] ],
333 'crednum', 'int', '', '',
334 'custnum', 'int', '', '',
336 'amount', @money_type,
337 'credited', @money_type,
338 'otaker', 'varchar', '', 8,
339 'reason', 'varchar', '', 255,
341 'primary_key' => 'crednum',
343 'index' => [ ['custnum'] ],
348 'custnum', 'int', '', '',
349 'agentnum', 'int', '', '',
350 # 'titlenum', 'int', 'NULL', '',
351 'last', 'varchar', '', $char_d,
352 # 'middle', 'varchar', 'NULL', $char_d,
353 'first', 'varchar', '', $char_d,
354 'ss', 'char', 'NULL', 11,
355 'company', 'varchar', 'NULL', $char_d,
356 'address1', 'varchar', '', $char_d,
357 'address2', 'varchar', 'NULL', $char_d,
358 'city', 'varchar', '', $char_d,
359 'county', 'varchar', 'NULL', $char_d,
360 'state', 'varchar', '', $char_d,
361 'zip', 'varchar', '', 10,
362 'country', 'char', '', 2,
363 'daytime', 'varchar', 'NULL', 20,
364 'night', 'varchar', 'NULL', 20,
365 'fax', 'varchar', 'NULL', 12,
366 'payby', 'char', '', 4,
367 'payinfo', 'varchar', 'NULL', 16,
368 'paydate', @date_type,
369 'payname', 'varchar', 'NULL', $char_d,
370 'tax', 'char', 'NULL', 1,
371 'otaker', 'varchar', '', 8,
372 'refnum', 'int', '', '',
374 'primary_key' => 'custnum',
376 #'index' => [ ['last'], ['company'] ],
377 'index' => [ ['last'], ],
380 'cust_main_invoice' => {
382 'destnum', 'int', '', '',
383 'custnum', 'int', '', '',
384 'dest', 'varchar', '', $char_d,
386 'primary_key' => 'destnum',
388 'index' => [ ['custnum'], ],
391 'cust_main_county' => { #county+state+country are checked off the
392 #cust_main_county for validation and to provide
395 'taxnum', 'int', '', '',
396 'state', 'char', 'NULL', $char_d,
397 'county', 'varchar', 'NULL', $char_d,
398 'country', 'char', '', 2,
399 'tax', 'real', '', '', #tax %
401 'primary_key' => 'taxnum',
403 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
409 'paynum', 'int', '', '',
410 'invnum', 'int', '', '',
413 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
414 # payment type table.
415 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
416 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
418 'primary_key' => 'paynum',
420 'index' => [ ['invnum'] ],
423 'cust_pay_batch' => { #what's this used for again? list of customers
424 #in current CARD batch? (necessarily CARD?)
426 'invnum', 'int', '', '',
427 'custnum', 'int', '', '',
428 'last', 'varchar', '', $char_d,
429 'first', 'varchar', '', $char_d,
430 'address1', 'varchar', '', $char_d,
431 'address2', 'varchar', 'NULL', $char_d,
432 'city', 'varchar', '', $char_d,
433 'state', 'char', '', 2,
434 'zip', 'varchar', '', 10,
435 'country', 'char', '', 2,
436 'trancode', 'int', '', '',
437 'cardnum', 'varchar', '', 16,
439 'payname', 'varchar', 'NULL', $char_d,
440 'amount', @money_type,
444 'index' => [ ['invnum'], ['custnum'] ],
449 'pkgnum', 'int', '', '',
450 'custnum', 'int', '', '',
451 'pkgpart', 'int', '', '',
452 'otaker', 'varchar', '', 8,
456 'cancel', @date_type,
457 'expire', @date_type,
459 'primary_key' => 'pkgnum',
461 'index' => [ ['custnum'] ],
466 'refundnum', 'int', '', '',
467 'crednum', 'int', '', '',
469 'refund', @money_type,
470 'otaker', 'varchar', '', 8,
471 'reason', 'varchar', '', $char_d,
472 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
473 # into payment type table.
474 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
476 'primary_key' => 'refundnum',
478 'index' => [ ['crednum'] ],
483 'svcnum', 'int', '', '',
484 'pkgnum', 'int', '', '',
485 'svcpart', 'int', '', '',
487 'primary_key' => 'svcnum',
489 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
494 'pkgpart', 'int', '', '',
495 'pkg', 'varchar', '', $char_d,
496 'comment', 'varchar', '', $char_d,
498 'freq', 'int', '', '', #billing frequency (months)
501 'primary_key' => 'pkgpart',
508 # 'titlenum', 'int', '', '',
509 # 'title', 'varchar', '', $char_d,
511 # 'primary_key' => 'titlenum',
512 # 'unique' => [ [] ],
518 'pkgpart', 'int', '', '',
519 'svcpart', 'int', '', '',
520 'quantity', 'int', '', '',
523 'unique' => [ ['pkgpart', 'svcpart'] ],
524 'index' => [ ['pkgpart'] ],
529 'refnum', 'int', '', '',
530 'referral', 'varchar', '', $char_d,
532 'primary_key' => 'refnum',
539 'svcpart', 'int', '', '',
540 'svc', 'varchar', '', $char_d,
541 'svcdb', 'varchar', '', $char_d,
543 'primary_key' => 'svcpart',
548 #(this should be renamed to part_pop)
551 'popnum', 'int', '', '',
552 'city', 'varchar', '', $char_d,
553 'state', 'char', '', 2,
555 'exch', 'char', '', 3,
558 'primary_key' => 'popnum',
565 'svcnum', 'int', '', '',
566 'username', 'varchar', '', $username_len, #unique (& remove dup code)
567 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
568 'popnum', 'int', 'NULL', '',
569 'uid', 'int', 'NULL', '',
570 'gid', 'int', 'NULL', '',
571 'finger', 'varchar', 'NULL', $char_d,
572 'dir', 'varchar', 'NULL', $char_d,
573 'shell', 'varchar', 'NULL', $char_d,
574 'quota', 'varchar', 'NULL', $char_d,
575 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
577 'primary_key' => 'svcnum',
579 'index' => [ ['username'] ],
584 'svcnum', 'int', '', '',
585 'domsvc', 'int', '', '',
586 'domuid', 'int', '', '',
587 'domuser', 'varchar', '', $char_d,
589 'primary_key' => 'svcnum',
591 'index' => [ ['domsvc'], ['domuid'] ],
596 # 'svcnum', 'int', '', '',
597 # 'amount', @money_type,
599 # 'primary_key' => 'svcnum',
600 # 'unique' => [ [] ],
606 'svcnum', 'int', '', '',
607 'domain', 'varchar', '', $char_d,
609 'primary_key' => 'svcnum',
610 'unique' => [ ['domain'] ],
616 # 'svcnum', 'int', '', '',
617 # 'svcnum', 'int', '', '',
618 # 'svcnum', 'int', '', '',
619 # 'worker', 'varchar', '', $char_d,
620 # '_date', @date_type,
622 # 'primary_key' => 'svcnum',
623 # 'unique' => [ [] ],