3 # $Id: fs-setup,v 1.28 2000-10-30 10:47:26 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.28 2000-10-30 10:47:26 ivan
36 # nas.last can't be defined NULL if indexed
38 # Revision 1.26 2000/07/06 08:57:27 ivan
39 # support for radius check attributes (except importing). poorly documented.
41 # Revision 1.25 2000/06/29 12:00:49 ivan
42 # support for pre-encrypted md5 passwords.
44 # Revision 1.24 2000/03/02 07:44:07 ivan
45 # typo forgot closing '
47 # Revision 1.23 2000/02/03 05:16:52 ivan
48 # beginning of DNS and Apache support
50 # Revision 1.22 2000/01/31 05:22:23 ivan
51 # prepaid "internet cards"
53 # Revision 1.21 2000/01/30 06:03:26 ivan
54 # postgres 6.5 finally supports decimal(10,2)
56 # Revision 1.20 2000/01/28 22:53:33 ivan
57 # track full phone number
59 # Revision 1.19 1999/07/29 08:50:35 ivan
60 # wrong type for cust_pay_batch.exp
62 # Revision 1.18 1999/04/15 22:46:30 ivan
65 # Revision 1.17 1999/04/14 07:58:39 ivan
66 # export getsecrets from FS::UID instead of calling it explicitly
68 # Revision 1.16 1999/02/28 19:44:16 ivan
69 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
71 # Revision 1.15 1999/02/27 21:06:21 ivan
72 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
73 # by Ben Leibig <leibig@colorado.edu>
75 # Revision 1.14 1999/02/07 09:59:14 ivan
76 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
78 # Revision 1.13 1999/02/04 06:09:23 ivan
81 # Revision 1.12 1999/02/03 10:42:27 ivan
82 # *** empty log message ***
84 # Revision 1.11 1999/01/17 03:11:52 ivan
85 # remove preliminary completehost changes
87 # Revision 1.10 1998/12/16 06:05:38 ivan
88 # add table cust_main_invoice
90 # Revision 1.9 1998/12/15 04:36:29 ivan
93 # Revision 1.8 1998/12/15 04:33:27 ivan
94 # dies if it isn't running as the freeside user
96 # Revision 1.7 1998/11/18 09:01:31 ivan
99 # Revision 1.6 1998/11/15 13:18:02 ivan
102 # Revision 1.5 1998/11/15 09:43:03 ivan
103 # update for new config file syntax, new adminsuidsetup
105 # Revision 1.4 1998/10/22 15:51:23 ivan
106 # also varchar with no length specified - postgresql fix broke mysql.
108 # Revision 1.3 1998/10/22 15:46:28 ivan
109 # now smallint is illegal, so remove that too.
112 #to delay loading dbdef until we're ready
113 BEGIN { $FS::Record::setup_hack = 1; }
118 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
120 use FS::cust_main_county;
122 die "Not running uid freeside!" unless checkeuid();
124 my $user = shift or die &usage;
127 #needs to match FS::Record
128 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
132 print "\nEnter the maximum username length: ";
133 my($username_len)=&getvalue;
135 print "\n\n", <<END, ":";
136 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
137 reply attribute Framed-IP-Address for each user. You can specify additional
138 check and reply attributes. First enter any additional RADIUS check attributes
139 you need to track for each user, separated by whitespace.
141 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
143 print "\n\n", <<END, ":";
144 Now enter any additional reply attributes you need to track for each user,
145 separated by whitespace.
147 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
150 my($x)=scalar(<STDIN>);
157 my($char_d) = 80; #default maxlength for text fields
159 #my(@date_type) = ( 'timestamp', '', '' );
160 my(@date_type) = ( 'int', 'NULL', '' );
161 my(@perl_type) = ( 'varchar', 'NULL', 255 );
162 my @money_type = ( 'decimal', '', '10,2' );
165 # create a dbdef object from the old data structure
168 my(%tables)=&tables_hash_hack;
170 #turn it into objects
171 my($dbdef) = new FS::dbdef ( map {
173 while (@{$tables{$_}{'columns'}}) {
174 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
175 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
177 FS::dbdef_table->new(
179 $tables{$_}{'primary_key'},
180 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
181 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
182 FS::dbdef_unique->new($tables{$_}{'unique'}),
183 FS::dbdef_index->new($tables{$_}{'index'}),
188 #add radius attributes to svc_acct
190 my($svc_acct)=$dbdef->table('svc_acct');
193 foreach $attribute (@attributes) {
194 $svc_acct->addcolumn ( new FS::dbdef_column (
195 'radius_'. $attribute,
202 foreach $attribute (@check_attributes) {
203 $svc_acct->addcolumn( new FS::dbdef_column (
211 #make part_svc table (but now as object)
213 my($part_svc)=$dbdef->table('part_svc');
215 #because of svc_acct_pop
216 #foreach (grep /^svc_/, $dbdef->tables) {
217 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
218 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
219 my($table)=$dbdef->table($_);
221 foreach $col ( $table->columns ) {
222 next if $col =~ /^svcnum$/;
223 $part_svc->addcolumn( new FS::dbdef_column (
224 $table->name. '__' . $table->column($col)->name,
225 'varchar', #$table->column($col)->type,
227 $char_d, #$table->column($col)->length,
229 $part_svc->addcolumn ( new FS::dbdef_column (
230 $table->name. '__'. $table->column($col)->name . "_flag",
239 $dbdef->save($dbdef_file);
240 &FS::Record::reload_dbdef($dbdef_file);
246 my($dbh)=adminsuidsetup $user;
252 foreach ($dbdef->tables) {
253 my($table)=$dbdef->table($_);
254 print "Creating $_...";
259 foreach $statement ($table->sql_create_table(datasrc)) {
260 #print $statement, "\n";
261 $dbh->do( $statement )
262 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
268 #not really sample data (and shouldn't default to US)
274 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
275 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
276 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
278 my($cust_main_county)=new FS::cust_main_county({
284 $error=$cust_main_county->insert;
285 die $error if $error;
288 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
290 VIC NSW NT QLD TAS ACT WA SA
292 my($cust_main_county)=new FS::cust_main_county({
298 $error=$cust_main_county->insert;
299 die $error if $error;
302 #ISO 2-letter country codes (same as country TLDs) except US and AU
304 AF AL DZ AS AD AO AI AQ AG AR AM AW AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
315 my($cust_main_county)=new FS::cust_main_county({
320 $error=$cust_main_county->insert;
321 die $error if $error;
324 $dbh->disconnect or die $dbh->errstr;
327 die "Usage:\n fs-setup user\n";
331 # Now it becomes an object. much better.
333 sub tables_hash_hack {
335 #note that s/(date|change)/_$1/; to avoid keyword conflict.
336 #put a kludge in FS::Record to catch this or? (pry need some date-handling
339 my(%tables)=( #yech.}
343 'agentnum', 'int', '', '',
344 'agent', 'varchar', '', $char_d,
345 'typenum', 'int', '', '',
346 'freq', 'int', 'NULL', '',
349 'primary_key' => 'agentnum',
351 'index' => [ ['typenum'] ],
356 'typenum', 'int', '', '',
357 'atype', 'varchar', '', $char_d,
359 'primary_key' => 'typenum',
366 'typenum', 'int', '', '',
367 'pkgpart', 'int', '', '',
370 'unique' => [ ['typenum', 'pkgpart'] ],
371 'index' => [ ['typenum'] ],
376 'invnum', 'int', '', '',
377 'custnum', 'int', '', '',
379 'charged', @money_type,
381 'printed', 'int', '', '',
383 'primary_key' => 'invnum',
385 'index' => [ ['custnum'] ],
390 'pkgnum', 'int', '', '',
391 'invnum', 'int', '', '',
392 'setup', @money_type,
393 'recur', @money_type,
398 'unique' => [ ['pkgnum', 'invnum'] ],
399 'index' => [ ['invnum'] ],
404 'crednum', 'int', '', '',
405 'custnum', 'int', '', '',
407 'amount', @money_type,
408 'credited', @money_type,
409 'otaker', 'varchar', '', 8,
410 'reason', 'varchar', '', 255,
412 'primary_key' => 'crednum',
414 'index' => [ ['custnum'] ],
419 'custnum', 'int', '', '',
420 'agentnum', 'int', '', '',
421 # 'titlenum', 'int', 'NULL', '',
422 'last', 'varchar', '', $char_d,
423 # 'middle', 'varchar', 'NULL', $char_d,
424 'first', 'varchar', '', $char_d,
425 'ss', 'char', 'NULL', 11,
426 'company', 'varchar', 'NULL', $char_d,
427 'address1', 'varchar', '', $char_d,
428 'address2', 'varchar', 'NULL', $char_d,
429 'city', 'varchar', '', $char_d,
430 'county', 'varchar', 'NULL', $char_d,
431 'state', 'varchar', 'NULL', $char_d,
432 'zip', 'varchar', '', 10,
433 'country', 'char', '', 2,
434 'daytime', 'varchar', 'NULL', 20,
435 'night', 'varchar', 'NULL', 20,
436 'fax', 'varchar', 'NULL', 12,
437 'payby', 'char', '', 4,
438 'payinfo', 'varchar', 'NULL', 16,
439 #'paydate', @date_type,
440 'paydate', 'varchar', 'NULL', 10,
441 'payname', 'varchar', 'NULL', $char_d,
442 'tax', 'char', 'NULL', 1,
443 'otaker', 'varchar', '', 8,
444 'refnum', 'int', '', '',
446 'primary_key' => 'custnum',
448 #'index' => [ ['last'], ['company'] ],
449 'index' => [ ['last'], ],
452 'cust_main_invoice' => {
454 'destnum', 'int', '', '',
455 'custnum', 'int', '', '',
456 'dest', 'varchar', '', $char_d,
458 'primary_key' => 'destnum',
460 'index' => [ ['custnum'], ],
463 'cust_main_county' => { #county+state+country are checked off the
464 #cust_main_county for validation and to provide
467 'taxnum', 'int', '', '',
468 'state', 'varchar', 'NULL', $char_d,
469 'county', 'varchar', 'NULL', $char_d,
470 'country', 'char', '', 2,
471 'tax', 'real', '', '', #tax %
473 'primary_key' => 'taxnum',
475 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
481 'paynum', 'int', '', '',
482 'invnum', 'int', '', '',
485 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
486 # payment type table.
487 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
488 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
490 'primary_key' => 'paynum',
492 'index' => [ ['invnum'] ],
495 'cust_pay_batch' => { #what's this used for again? list of customers
496 #in current CARD batch? (necessarily CARD?)
498 'invnum', 'int', '', '',
499 'custnum', 'int', '', '',
500 'last', 'varchar', '', $char_d,
501 'first', 'varchar', '', $char_d,
502 'address1', 'varchar', '', $char_d,
503 'address2', 'varchar', 'NULL', $char_d,
504 'city', 'varchar', '', $char_d,
505 'state', 'varchar', '', $char_d,
506 'zip', 'varchar', '', 10,
507 'country', 'char', '', 2,
508 'trancode', 'int', '', '',
509 'cardnum', 'varchar', '', 16,
511 'exp', 'varchar', '', 11,
512 'payname', 'varchar', 'NULL', $char_d,
513 'amount', @money_type,
517 'index' => [ ['invnum'], ['custnum'] ],
522 'pkgnum', 'int', '', '',
523 'custnum', 'int', '', '',
524 'pkgpart', 'int', '', '',
525 'otaker', 'varchar', '', 8,
529 'cancel', @date_type,
530 'expire', @date_type,
532 'primary_key' => 'pkgnum',
534 'index' => [ ['custnum'] ],
539 'refundnum', 'int', '', '',
540 'crednum', 'int', '', '',
542 'refund', @money_type,
543 'otaker', 'varchar', '', 8,
544 'reason', 'varchar', '', $char_d,
545 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
546 # into payment type table.
547 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
549 'primary_key' => 'refundnum',
551 'index' => [ ['crednum'] ],
556 'svcnum', 'int', '', '',
557 'pkgnum', 'int', '', '',
558 'svcpart', 'int', '', '',
560 'primary_key' => 'svcnum',
562 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
567 'pkgpart', 'int', '', '',
568 'pkg', 'varchar', '', $char_d,
569 'comment', 'varchar', '', $char_d,
571 'freq', 'int', '', '', #billing frequency (months)
574 'primary_key' => 'pkgpart',
581 # 'titlenum', 'int', '', '',
582 # 'title', 'varchar', '', $char_d,
584 # 'primary_key' => 'titlenum',
585 # 'unique' => [ [] ],
591 'pkgpart', 'int', '', '',
592 'svcpart', 'int', '', '',
593 'quantity', 'int', '', '',
596 'unique' => [ ['pkgpart', 'svcpart'] ],
597 'index' => [ ['pkgpart'] ],
602 'refnum', 'int', '', '',
603 'referral', 'varchar', '', $char_d,
605 'primary_key' => 'refnum',
612 'svcpart', 'int', '', '',
613 'svc', 'varchar', '', $char_d,
614 'svcdb', 'varchar', '', $char_d,
616 'primary_key' => 'svcpart',
621 #(this should be renamed to part_pop)
624 'popnum', 'int', '', '',
625 'city', 'varchar', '', $char_d,
626 'state', 'varchar', '', $char_d,
628 'exch', 'char', '', 3,
629 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
631 'primary_key' => 'popnum',
638 'svcnum', 'int', '', '',
639 'username', 'varchar', '', $username_len, #unique (& remove dup code)
640 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
641 'popnum', 'int', 'NULL', '',
642 'uid', 'int', 'NULL', '',
643 'gid', 'int', 'NULL', '',
644 'finger', 'varchar', 'NULL', $char_d,
645 'dir', 'varchar', 'NULL', $char_d,
646 'shell', 'varchar', 'NULL', $char_d,
647 'quota', 'varchar', 'NULL', $char_d,
648 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
650 'primary_key' => 'svcnum',
652 'index' => [ ['username'] ],
657 'svcnum', 'int', '', '',
658 'domsvc', 'int', '', '',
659 'domuid', 'int', '', '',
660 'domuser', 'varchar', '', $char_d,
662 'primary_key' => 'svcnum',
664 'index' => [ ['domsvc'], ['domuid'] ],
669 # 'svcnum', 'int', '', '',
670 # 'amount', @money_type,
672 # 'primary_key' => 'svcnum',
673 # 'unique' => [ [] ],
679 'svcnum', 'int', '', '',
680 'domain', 'varchar', '', $char_d,
682 'primary_key' => 'svcnum',
683 'unique' => [ ['domain'] ],
689 'recnum', 'int', '', '',
690 'svcnum', 'int', '', '',
691 'reczone', 'varchar', '', $char_d,
692 'recaf', 'char', '', 2,
693 'rectype', 'char', '', 5,
694 'recdata', 'varchar', '', $char_d,
696 'primary_key' => 'recnum',
698 'index' => [ ['svcnum'] ],
703 'svcnum', 'int', '', '',
704 'recnum', 'int', '', '',
705 'usersvc', 'int', '', '',
707 'primary_key' => 'svcnum',
714 # 'svcnum', 'int', '', '',
715 # 'svcnum', 'int', '', '',
716 # 'svcnum', 'int', '', '',
717 # 'worker', 'varchar', '', $char_d,
718 # '_date', @date_type,
720 # 'primary_key' => 'svcnum',
721 # 'unique' => [ [] ],
727 'prepaynum', 'int', '', '',
728 'identifier', 'varchar', '', $char_d,
729 'amount', @money_type,
731 'primary_key' => 'prepaynum',
732 'unique' => [ ['identifier'] ],
738 'portnum', 'int', '', '',
739 'ip', 'varchar', NULL, 15,
740 'nasport' 'int', NULL, '',
741 'nasnum', 'int', '', '',
743 'primary_key' => 'portnum',
750 'nasnum', 'int', '', '',
751 'nas', 'varchar', '', $char_d,
752 'nasip', 'varchar', '', 15,
753 'nasfqdn', 'varchar', '', $char_d,
754 # 'last', 'timestamp', '', '',
755 #change to above when move to DBIx::DBSchema!!!
756 'last', 'datetime', '', '',
758 'primary_key' => 'nasnum',
759 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
760 'index' => [ [ 'last' ] ],
765 'sessionnum', 'int', '', '',
766 'portnum', 'int', '', '',
767 'svcnum', 'int', '', '',
768 # 'login', 'timestamp', '', '',
769 # 'logout', 'timestamp', '', '',
770 #change to above when move to DBIx::DBSchema!!!
771 'login', 'datetime', '', '',
772 'logout', 'datetime', NULL, '',
774 'primary_key' => 'sessionnum',
776 'index' => [ [ 'portnum' ] ],