3 # $Id: fs-setup,v 1.39 2001-07-30 07:42:39 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.39 2001-07-30 07:42:39 ivan
36 # need an DBIx::DBSchema with delcolumn
38 # Revision 1.38 2001/07/30 07:36:04 ivan
41 # Revision 1.37 2001/06/03 14:16:11 ivan
42 # allow empty refund reasons
44 # Revision 1.36 2001/04/15 12:56:31 ivan
45 # s/dbdef/DBIx::DBSchema/
47 # Revision 1.35 2001/04/15 09:36:43 ivan
48 # http://www.sisd.com/freeside/list-archive/msg01450.html
50 # Revision 1.34 2001/04/09 23:05:16 ivan
51 # Transactions Part I!!!
53 # Revision 1.33 2001/02/03 14:03:50 ivan
54 # time-based prepaid cards, session monitor. woop!
56 # Revision 1.32 2000/12/04 00:13:02 ivan
59 # Revision 1.31 2000/12/01 18:34:53 ivan
62 # Revision 1.30 2000/12/01 18:33:32 ivan
65 # Revision 1.29 2000/11/07 15:00:37 ivan
68 # Revision 1.28 2000/10/30 10:47:26 ivan
69 # nas.last can't be defined NULL if indexed
71 # Revision 1.26 2000/07/06 08:57:27 ivan
72 # support for radius check attributes (except importing). poorly documented.
74 # Revision 1.25 2000/06/29 12:00:49 ivan
75 # support for pre-encrypted md5 passwords.
77 # Revision 1.24 2000/03/02 07:44:07 ivan
78 # typo forgot closing '
80 # Revision 1.23 2000/02/03 05:16:52 ivan
81 # beginning of DNS and Apache support
83 # Revision 1.22 2000/01/31 05:22:23 ivan
84 # prepaid "internet cards"
86 # Revision 1.21 2000/01/30 06:03:26 ivan
87 # postgres 6.5 finally supports decimal(10,2)
89 # Revision 1.20 2000/01/28 22:53:33 ivan
90 # track full phone number
92 # Revision 1.19 1999/07/29 08:50:35 ivan
93 # wrong type for cust_pay_batch.exp
95 # Revision 1.18 1999/04/15 22:46:30 ivan
98 # Revision 1.17 1999/04/14 07:58:39 ivan
99 # export getsecrets from FS::UID instead of calling it explicitly
101 # Revision 1.16 1999/02/28 19:44:16 ivan
102 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
104 # Revision 1.15 1999/02/27 21:06:21 ivan
105 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
106 # by Ben Leibig <leibig@colorado.edu>
108 # Revision 1.14 1999/02/07 09:59:14 ivan
109 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
111 # Revision 1.13 1999/02/04 06:09:23 ivan
114 # Revision 1.12 1999/02/03 10:42:27 ivan
115 # *** empty log message ***
117 # Revision 1.11 1999/01/17 03:11:52 ivan
118 # remove preliminary completehost changes
120 # Revision 1.10 1998/12/16 06:05:38 ivan
121 # add table cust_main_invoice
123 # Revision 1.9 1998/12/15 04:36:29 ivan
124 # s/croak/die/; #oops
126 # Revision 1.8 1998/12/15 04:33:27 ivan
127 # dies if it isn't running as the freeside user
129 # Revision 1.7 1998/11/18 09:01:31 ivan
132 # Revision 1.6 1998/11/15 13:18:02 ivan
135 # Revision 1.5 1998/11/15 09:43:03 ivan
136 # update for new config file syntax, new adminsuidsetup
138 # Revision 1.4 1998/10/22 15:51:23 ivan
139 # also varchar with no length specified - postgresql fix broke mysql.
141 # Revision 1.3 1998/10/22 15:46:28 ivan
142 # now smallint is illegal, so remove that too.
145 #to delay loading dbdef until we're ready
146 BEGIN { $FS::Record::setup_hack = 1; }
150 use DBIx::DBSchema 0.18;
151 use DBIx::DBSchema::Table;
152 use DBIx::DBSchema::Column;
153 use DBIx::DBSchema::ColGroup::Unique;
154 use DBIx::DBSchema::ColGroup::Index;
155 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
157 use FS::cust_main_county;
159 die "Not running uid freeside!" unless checkeuid();
161 my $user = shift or die &usage;
164 #needs to match FS::Record
165 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
169 print "\nEnter the maximum username length: ";
170 my($username_len)=&getvalue;
172 print "\n\n", <<END, ":";
173 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
174 reply attribute Framed-IP-Address for each user. You can specify additional
175 check and reply attributes. First enter any additional RADIUS check attributes
176 you need to track for each user, separated by whitespace.
178 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
180 print "\n\n", <<END, ":";
181 Now enter any additional reply attributes you need to track for each user,
182 separated by whitespace.
184 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
186 print "\n\n", <<END, ":";
187 Do you wish to enable the tracking of a second, separate shipping/service
193 my($x)=scalar(<STDIN>);
200 my $x = scalar(<STDIN>);
206 my($char_d) = 80; #default maxlength for text fields
208 #my(@date_type) = ( 'timestamp', '', '' );
209 my(@date_type) = ( 'int', 'NULL', '' );
210 my(@perl_type) = ( 'varchar', 'NULL', 255 );
211 my @money_type = ( 'decimal', '', '10,2' );
214 # create a dbdef object from the old data structure
217 my(%tables)=&tables_hash_hack;
219 #turn it into objects
220 my($dbdef) = new DBIx::DBSchema ( map {
222 while (@{$tables{$_}{'columns'}}) {
223 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
224 push @columns, new DBIx::DBSchema::Column ( $name,$type,$null,$length );
226 DBIx::DBSchema::Table->new(
228 $tables{$_}{'primary_key'},
229 DBIx::DBSchema::ColGroup::Unique->new($tables{$_}{'unique'}),
230 DBIx::DBSchema::ColGroup::Index->new($tables{$_}{'index'}),
235 #remove ship_ from cust_main
237 my $cust_main = $dbdef->table('cust_main');
238 $cust_main->delcolumn($_) foreach ( grep /^ship_/, $cust_main->columns );
241 #add radius attributes to svc_acct
243 my($svc_acct)=$dbdef->table('svc_acct');
246 foreach $attribute (@attributes) {
247 $svc_acct->addcolumn ( new DBIx::DBSchema::Column (
248 'radius_'. $attribute,
255 foreach $attribute (@check_attributes) {
256 $svc_acct->addcolumn( new DBIx::DBSchema::Column (
264 #make part_svc table (but now as object)
266 my($part_svc)=$dbdef->table('part_svc');
268 #because of svc_acct_pop
269 #foreach (grep /^svc_/, $dbdef->tables) {
270 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
271 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
272 my($table)=$dbdef->table($_);
274 foreach $col ( $table->columns ) {
275 next if $col =~ /^svcnum$/;
276 $part_svc->addcolumn( new DBIx::DBSchema::Column (
277 $table->name. '__' . $table->column($col)->name,
278 'varchar', #$table->column($col)->type,
280 $char_d, #$table->column($col)->length,
282 $part_svc->addcolumn ( new DBIx::DBSchema::Column (
283 $table->name. '__'. $table->column($col)->name . "_flag",
292 $dbdef->save($dbdef_file);
293 &FS::Record::reload_dbdef($dbdef_file);
299 my($dbh)=adminsuidsetup $user;
304 my @sql = $dbdef->sql($dbh);
305 foreach my $statement ( $dbdef->sql($dbh) ) {
306 $dbh->do( $statement )
307 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
310 #not really sample data (and shouldn't default to US)
316 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
317 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
318 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
320 my($cust_main_county)=new FS::cust_main_county({
326 $error=$cust_main_county->insert;
327 die $error if $error;
330 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
332 VIC NSW NT QLD TAS ACT WA SA
334 my($cust_main_county)=new FS::cust_main_county({
340 $error=$cust_main_county->insert;
341 die $error if $error;
344 #ISO 2-letter country codes (same as country TLDs) except US and AU
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
357 my($cust_main_county)=new FS::cust_main_county({
362 $error=$cust_main_county->insert;
363 die $error if $error;
366 $dbh->disconnect or die $dbh->errstr;
368 print "Freeside database initialized sucessfully\n";
371 die "Usage:\n fs-setup user\n";
375 # Now it becomes an object. much better.
377 sub tables_hash_hack {
379 #note that s/(date|change)/_$1/; to avoid keyword conflict.
380 #put a kludge in FS::Record to catch this or? (pry need some date-handling
383 my(%tables)=( #yech.}
387 'agentnum', 'int', '', '',
388 'agent', 'varchar', '', $char_d,
389 'typenum', 'int', '', '',
390 'freq', 'int', 'NULL', '',
393 'primary_key' => 'agentnum',
395 'index' => [ ['typenum'] ],
400 'typenum', 'int', '', '',
401 'atype', 'varchar', '', $char_d,
403 'primary_key' => 'typenum',
410 'typenum', 'int', '', '',
411 'pkgpart', 'int', '', '',
414 'unique' => [ ['typenum', 'pkgpart'] ],
415 'index' => [ ['typenum'] ],
420 'invnum', 'int', '', '',
421 'custnum', 'int', '', '',
423 'charged', @money_type,
424 'printed', 'int', '', '',
426 'primary_key' => 'invnum',
428 'index' => [ ['custnum'] ],
433 'pkgnum', 'int', '', '',
434 'invnum', 'int', '', '',
435 'setup', @money_type,
436 'recur', @money_type,
441 'unique' => [ ['pkgnum', 'invnum'] ],
442 'index' => [ ['invnum'] ],
447 'crednum', 'int', '', '',
448 'custnum', 'int', '', '',
450 'amount', @money_type,
451 'otaker', 'varchar', '', 8,
452 'reason', 'varchar', 'NULL', 255,
454 'primary_key' => 'crednum',
456 'index' => [ ['custnum'] ],
461 'custnum', 'int', '', '',
462 'agentnum', 'int', '', '',
463 # 'titlenum', 'int', 'NULL', '',
464 'last', 'varchar', '', $char_d,
465 # 'middle', 'varchar', 'NULL', $char_d,
466 'first', 'varchar', '', $char_d,
467 'ss', 'char', 'NULL', 11,
468 'company', 'varchar', 'NULL', $char_d,
469 'address1', 'varchar', '', $char_d,
470 'address2', 'varchar', 'NULL', $char_d,
471 'city', 'varchar', '', $char_d,
472 'county', 'varchar', 'NULL', $char_d,
473 'state', 'varchar', 'NULL', $char_d,
474 'zip', 'varchar', '', 10,
475 'country', 'char', '', 2,
476 'daytime', 'varchar', 'NULL', 20,
477 'night', 'varchar', 'NULL', 20,
478 'fax', 'varchar', 'NULL', 12,
479 'ship_last', 'varchar', 'NULL', $char_d,
480 # 'ship_middle', 'varchar', 'NULL', $char_d,
481 'ship_first', 'varchar', 'NULL', $char_d,
482 'ship_company', 'varchar', 'NULL', $char_d,
483 'ship_address1', 'varchar', 'NULL', $char_d,
484 'ship_address2', 'varchar', 'NULL', $char_d,
485 'ship_city', 'varchar', 'NULL', $char_d,
486 'ship_county', 'varchar', 'NULL', $char_d,
487 'ship_state', 'varchar', 'NULL', $char_d,
488 'ship_zip', 'varchar', 'NULL', 10,
489 'ship_country', 'char', 'NULL', 2,
490 'ship_daytime', 'varchar', 'NULL', 20,
491 'ship_night', 'varchar', 'NULL', 20,
492 'ship_fax', 'varchar', 'NULL', 12,
493 'payby', 'char', '', 4,
494 'payinfo', 'varchar', 'NULL', 16,
495 #'paydate', @date_type,
496 'paydate', 'varchar', 'NULL', 10,
497 'payname', 'varchar', 'NULL', $char_d,
498 'tax', 'char', 'NULL', 1,
499 'otaker', 'varchar', '', 8,
500 'refnum', 'int', '', '',
502 'primary_key' => 'custnum',
504 #'index' => [ ['last'], ['company'] ],
505 'index' => [ ['last'], ],
508 'cust_main_invoice' => {
510 'destnum', 'int', '', '',
511 'custnum', 'int', '', '',
512 'dest', 'varchar', '', $char_d,
514 'primary_key' => 'destnum',
516 'index' => [ ['custnum'], ],
519 'cust_main_county' => { #county+state+country are checked off the
520 #cust_main_county for validation and to provide
523 'taxnum', 'int', '', '',
524 'state', 'varchar', 'NULL', $char_d,
525 'county', 'varchar', 'NULL', $char_d,
526 'country', 'char', '', 2,
527 'tax', 'real', '', '', #tax %
529 'primary_key' => 'taxnum',
531 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
537 'paynum', 'int', '', '',
538 'invnum', 'int', '', '',
541 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
542 # payment type table.
543 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
544 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
546 'primary_key' => 'paynum',
548 'index' => [ ['invnum'] ],
551 'cust_pay_batch' => { #what's this used for again? list of customers
552 #in current CARD batch? (necessarily CARD?)
554 'invnum', 'int', '', '',
555 'custnum', 'int', '', '',
556 'last', 'varchar', '', $char_d,
557 'first', 'varchar', '', $char_d,
558 'address1', 'varchar', '', $char_d,
559 'address2', 'varchar', 'NULL', $char_d,
560 'city', 'varchar', '', $char_d,
561 'state', 'varchar', '', $char_d,
562 'zip', 'varchar', '', 10,
563 'country', 'char', '', 2,
564 'trancode', 'int', '', '',
565 'cardnum', 'varchar', '', 16,
567 'exp', 'varchar', '', 11,
568 'payname', 'varchar', 'NULL', $char_d,
569 'amount', @money_type,
573 'index' => [ ['invnum'], ['custnum'] ],
578 'pkgnum', 'int', '', '',
579 'custnum', 'int', '', '',
580 'pkgpart', 'int', '', '',
581 'otaker', 'varchar', '', 8,
585 'cancel', @date_type,
586 'expire', @date_type,
588 'primary_key' => 'pkgnum',
590 'index' => [ ['custnum'] ],
595 'refundnum', 'int', '', '',
596 'crednum', 'int', '', '',
598 'refund', @money_type,
599 'otaker', 'varchar', '', 8,
600 'reason', 'varchar', '', $char_d,
601 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
602 # into payment type table.
603 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
605 'primary_key' => 'refundnum',
607 'index' => [ ['crednum'] ],
612 'svcnum', 'int', '', '',
613 'pkgnum', 'int', 'NULL', '',
614 'svcpart', 'int', '', '',
616 'primary_key' => 'svcnum',
618 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
623 'pkgpart', 'int', '', '',
624 'pkg', 'varchar', '', $char_d,
625 'comment', 'varchar', '', $char_d,
627 'freq', 'int', '', '', #billing frequency (months)
630 'primary_key' => 'pkgpart',
637 # 'titlenum', 'int', '', '',
638 # 'title', 'varchar', '', $char_d,
640 # 'primary_key' => 'titlenum',
641 # 'unique' => [ [] ],
647 'pkgpart', 'int', '', '',
648 'svcpart', 'int', '', '',
649 'quantity', 'int', '', '',
652 'unique' => [ ['pkgpart', 'svcpart'] ],
653 'index' => [ ['pkgpart'] ],
658 'refnum', 'int', '', '',
659 'referral', 'varchar', '', $char_d,
661 'primary_key' => 'refnum',
668 'svcpart', 'int', '', '',
669 'svc', 'varchar', '', $char_d,
670 'svcdb', 'varchar', '', $char_d,
672 'primary_key' => 'svcpart',
677 #(this should be renamed to part_pop)
680 'popnum', 'int', '', '',
681 'city', 'varchar', '', $char_d,
682 'state', 'varchar', '', $char_d,
684 'exch', 'char', '', 3,
685 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
687 'primary_key' => 'popnum',
694 'svcnum', 'int', '', '',
695 'username', 'varchar', '', $username_len, #unique (& remove dup code)
696 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
697 'popnum', 'int', 'NULL', '',
698 'uid', 'int', 'NULL', '',
699 'gid', 'int', 'NULL', '',
700 'finger', 'varchar', 'NULL', $char_d,
701 'dir', 'varchar', 'NULL', $char_d,
702 'shell', 'varchar', 'NULL', $char_d,
703 'quota', 'varchar', 'NULL', $char_d,
704 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
705 'seconds', 'int', 'NULL', '', #uhhhh
707 'primary_key' => 'svcnum',
709 'index' => [ ['username'] ],
714 'svcnum', 'int', '', '',
715 'domsvc', 'int', '', '',
716 'domuid', 'int', '', '',
717 'domuser', 'varchar', '', $char_d,
719 'primary_key' => 'svcnum',
721 'index' => [ ['domsvc'], ['domuid'] ],
726 # 'svcnum', 'int', '', '',
727 # 'amount', @money_type,
729 # 'primary_key' => 'svcnum',
730 # 'unique' => [ [] ],
736 'svcnum', 'int', '', '',
737 'domain', 'varchar', '', $char_d,
739 'primary_key' => 'svcnum',
740 'unique' => [ ['domain'] ],
746 'recnum', 'int', '', '',
747 'svcnum', 'int', '', '',
748 'reczone', 'varchar', '', $char_d,
749 'recaf', 'char', '', 2,
750 'rectype', 'char', '', 5,
751 'recdata', 'varchar', '', $char_d,
753 'primary_key' => 'recnum',
755 'index' => [ ['svcnum'] ],
760 'svcnum', 'int', '', '',
761 'recnum', 'int', '', '',
762 'usersvc', 'int', '', '',
764 'primary_key' => 'svcnum',
771 # 'svcnum', 'int', '', '',
772 # 'svcnum', 'int', '', '',
773 # 'svcnum', 'int', '', '',
774 # 'worker', 'varchar', '', $char_d,
775 # '_date', @date_type,
777 # 'primary_key' => 'svcnum',
778 # 'unique' => [ [] ],
784 'prepaynum', 'int', '', '',
785 'identifier', 'varchar', '', $char_d,
786 'amount', @money_type,
787 'seconds', 'int', 'NULL', '',
789 'primary_key' => 'prepaynum',
790 'unique' => [ ['identifier'] ],
796 'portnum', 'int', '', '',
797 'ip', 'varchar', 'NULL', 15,
798 'nasport', 'int', 'NULL', '',
799 'nasnum', 'int', '', '',
801 'primary_key' => 'portnum',
808 'nasnum', 'int', '', '',
809 'nas', 'varchar', '', $char_d,
810 'nasip', 'varchar', '', 15,
811 'nasfqdn', 'varchar', '', $char_d,
812 'last', 'int', '', '',
814 'primary_key' => 'nasnum',
815 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
816 'index' => [ [ 'last' ] ],
821 'sessionnum', 'int', '', '',
822 'portnum', 'int', '', '',
823 'svcnum', 'int', '', '',
825 'logout', @date_type,
827 'primary_key' => 'sessionnum',
829 'index' => [ [ 'portnum' ] ],