3 # $Id: fs-setup,v 1.40 2001-08-11 05:53:42 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.40 2001-08-11 05:53:42 ivan
38 # Revision 1.39 2001/07/30 07:42:39 ivan
39 # need an DBIx::DBSchema with delcolumn
41 # Revision 1.38 2001/07/30 07:36:04 ivan
44 # Revision 1.37 2001/06/03 14:16:11 ivan
45 # allow empty refund reasons
47 # Revision 1.36 2001/04/15 12:56:31 ivan
48 # s/dbdef/DBIx::DBSchema/
50 # Revision 1.35 2001/04/15 09:36:43 ivan
51 # http://www.sisd.com/freeside/list-archive/msg01450.html
53 # Revision 1.34 2001/04/09 23:05:16 ivan
54 # Transactions Part I!!!
56 # Revision 1.33 2001/02/03 14:03:50 ivan
57 # time-based prepaid cards, session monitor. woop!
59 # Revision 1.32 2000/12/04 00:13:02 ivan
62 # Revision 1.31 2000/12/01 18:34:53 ivan
65 # Revision 1.30 2000/12/01 18:33:32 ivan
68 # Revision 1.29 2000/11/07 15:00:37 ivan
71 # Revision 1.28 2000/10/30 10:47:26 ivan
72 # nas.last can't be defined NULL if indexed
74 # Revision 1.26 2000/07/06 08:57:27 ivan
75 # support for radius check attributes (except importing). poorly documented.
77 # Revision 1.25 2000/06/29 12:00:49 ivan
78 # support for pre-encrypted md5 passwords.
80 # Revision 1.24 2000/03/02 07:44:07 ivan
81 # typo forgot closing '
83 # Revision 1.23 2000/02/03 05:16:52 ivan
84 # beginning of DNS and Apache support
86 # Revision 1.22 2000/01/31 05:22:23 ivan
87 # prepaid "internet cards"
89 # Revision 1.21 2000/01/30 06:03:26 ivan
90 # postgres 6.5 finally supports decimal(10,2)
92 # Revision 1.20 2000/01/28 22:53:33 ivan
93 # track full phone number
95 # Revision 1.19 1999/07/29 08:50:35 ivan
96 # wrong type for cust_pay_batch.exp
98 # Revision 1.18 1999/04/15 22:46:30 ivan
101 # Revision 1.17 1999/04/14 07:58:39 ivan
102 # export getsecrets from FS::UID instead of calling it explicitly
104 # Revision 1.16 1999/02/28 19:44:16 ivan
105 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
107 # Revision 1.15 1999/02/27 21:06:21 ivan
108 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
109 # by Ben Leibig <leibig@colorado.edu>
111 # Revision 1.14 1999/02/07 09:59:14 ivan
112 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
114 # Revision 1.13 1999/02/04 06:09:23 ivan
117 # Revision 1.12 1999/02/03 10:42:27 ivan
118 # *** empty log message ***
120 # Revision 1.11 1999/01/17 03:11:52 ivan
121 # remove preliminary completehost changes
123 # Revision 1.10 1998/12/16 06:05:38 ivan
124 # add table cust_main_invoice
126 # Revision 1.9 1998/12/15 04:36:29 ivan
127 # s/croak/die/; #oops
129 # Revision 1.8 1998/12/15 04:33:27 ivan
130 # dies if it isn't running as the freeside user
132 # Revision 1.7 1998/11/18 09:01:31 ivan
135 # Revision 1.6 1998/11/15 13:18:02 ivan
138 # Revision 1.5 1998/11/15 09:43:03 ivan
139 # update for new config file syntax, new adminsuidsetup
141 # Revision 1.4 1998/10/22 15:51:23 ivan
142 # also varchar with no length specified - postgresql fix broke mysql.
144 # Revision 1.3 1998/10/22 15:46:28 ivan
145 # now smallint is illegal, so remove that too.
148 #to delay loading dbdef until we're ready
149 BEGIN { $FS::Record::setup_hack = 1; }
153 use DBIx::DBSchema 0.18;
154 use DBIx::DBSchema::Table;
155 use DBIx::DBSchema::Column;
156 use DBIx::DBSchema::ColGroup::Unique;
157 use DBIx::DBSchema::ColGroup::Index;
158 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
160 use FS::cust_main_county;
162 die "Not running uid freeside!" unless checkeuid();
164 my $user = shift or die &usage;
167 #needs to match FS::Record
168 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
172 print "\nEnter the maximum username length: ";
173 my($username_len)=&getvalue;
175 print "\n\n", <<END, ":";
176 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
177 reply attribute Framed-IP-Address for each user. You can specify additional
178 check and reply attributes. First enter any additional RADIUS check attributes
179 you need to track for each user, separated by whitespace.
181 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
183 print "\n\n", <<END, ":";
184 Now enter any additional reply attributes you need to track for each user,
185 separated by whitespace.
187 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
189 print "\n\n", <<END, ":";
190 Do you wish to enable the tracking of a second, separate shipping/service
196 my($x)=scalar(<STDIN>);
203 my $x = scalar(<STDIN>);
209 my($char_d) = 80; #default maxlength for text fields
211 #my(@date_type) = ( 'timestamp', '', '' );
212 my(@date_type) = ( 'int', 'NULL', '' );
213 my(@perl_type) = ( 'varchar', 'NULL', 255 );
214 my @money_type = ( 'decimal', '', '10,2' );
217 # create a dbdef object from the old data structure
220 my(%tables)=&tables_hash_hack;
222 #turn it into objects
223 my($dbdef) = new DBIx::DBSchema ( map {
225 while (@{$tables{$_}{'columns'}}) {
226 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
227 push @columns, new DBIx::DBSchema::Column ( $name,$type,$null,$length );
229 DBIx::DBSchema::Table->new(
231 $tables{$_}{'primary_key'},
232 DBIx::DBSchema::ColGroup::Unique->new($tables{$_}{'unique'}),
233 DBIx::DBSchema::ColGroup::Index->new($tables{$_}{'index'}),
238 #remove ship_ from cust_main
240 my $cust_main = $dbdef->table('cust_main');
241 $cust_main->delcolumn($_) foreach ( grep /^ship_/, $cust_main->columns );
244 #add radius attributes to svc_acct
246 my($svc_acct)=$dbdef->table('svc_acct');
249 foreach $attribute (@attributes) {
250 $svc_acct->addcolumn ( new DBIx::DBSchema::Column (
251 'radius_'. $attribute,
258 foreach $attribute (@check_attributes) {
259 $svc_acct->addcolumn( new DBIx::DBSchema::Column (
267 #make part_svc table (but now as object)
269 my($part_svc)=$dbdef->table('part_svc');
271 #because of svc_acct_pop
272 #foreach (grep /^svc_/, $dbdef->tables) {
273 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
274 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
275 my($table)=$dbdef->table($_);
277 foreach $col ( $table->columns ) {
278 next if $col =~ /^svcnum$/;
279 $part_svc->addcolumn( new DBIx::DBSchema::Column (
280 $table->name. '__' . $table->column($col)->name,
281 'varchar', #$table->column($col)->type,
283 $char_d, #$table->column($col)->length,
285 $part_svc->addcolumn ( new DBIx::DBSchema::Column (
286 $table->name. '__'. $table->column($col)->name . "_flag",
295 $dbdef->save($dbdef_file);
296 &FS::Record::reload_dbdef($dbdef_file);
302 my($dbh)=adminsuidsetup $user;
307 my @sql = $dbdef->sql($dbh);
308 foreach my $statement ( $dbdef->sql($dbh) ) {
309 $dbh->do( $statement )
310 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
313 #not really sample data (and shouldn't default to US)
319 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
320 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
321 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
323 my($cust_main_county)=new FS::cust_main_county({
329 $error=$cust_main_county->insert;
330 die $error if $error;
333 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
335 VIC NSW NT QLD TAS ACT WA SA
337 my($cust_main_county)=new FS::cust_main_county({
343 $error=$cust_main_county->insert;
344 die $error if $error;
347 #ISO 2-letter country codes (same as country TLDs) except US and AU
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 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
360 my($cust_main_county)=new FS::cust_main_county({
365 $error=$cust_main_county->insert;
366 die $error if $error;
369 $dbh->disconnect or die $dbh->errstr;
371 print "Freeside database initialized sucessfully\n";
374 die "Usage:\n fs-setup user\n";
378 # Now it becomes an object. much better.
380 sub tables_hash_hack {
382 #note that s/(date|change)/_$1/; to avoid keyword conflict.
383 #put a kludge in FS::Record to catch this or? (pry need some date-handling
386 my(%tables)=( #yech.}
390 'agentnum', 'int', '', '',
391 'agent', 'varchar', '', $char_d,
392 'typenum', 'int', '', '',
393 'freq', 'int', 'NULL', '',
396 'primary_key' => 'agentnum',
398 'index' => [ ['typenum'] ],
403 'typenum', 'int', '', '',
404 'atype', 'varchar', '', $char_d,
406 'primary_key' => 'typenum',
413 'typenum', 'int', '', '',
414 'pkgpart', 'int', '', '',
417 'unique' => [ ['typenum', 'pkgpart'] ],
418 'index' => [ ['typenum'] ],
423 'invnum', 'int', '', '',
424 'custnum', 'int', '', '',
426 'charged', @money_type,
427 'printed', 'int', '', '',
429 'primary_key' => 'invnum',
431 'index' => [ ['custnum'] ],
436 'pkgnum', 'int', '', '',
437 'invnum', 'int', '', '',
438 'setup', @money_type,
439 'recur', @money_type,
444 'unique' => [ ['pkgnum', 'invnum'] ],
445 'index' => [ ['invnum'] ],
450 'crednum', 'int', '', '',
451 'custnum', 'int', '', '',
453 'amount', @money_type,
454 'otaker', 'varchar', '', 8,
455 'reason', 'varchar', 'NULL', 255,
457 'primary_key' => 'crednum',
459 'index' => [ ['custnum'] ],
464 'custnum', 'int', '', '',
465 'agentnum', 'int', '', '',
466 # 'titlenum', 'int', 'NULL', '',
467 'last', 'varchar', '', $char_d,
468 # 'middle', 'varchar', 'NULL', $char_d,
469 'first', 'varchar', '', $char_d,
470 'ss', 'char', 'NULL', 11,
471 'company', 'varchar', 'NULL', $char_d,
472 'address1', 'varchar', '', $char_d,
473 'address2', 'varchar', 'NULL', $char_d,
474 'city', 'varchar', '', $char_d,
475 'county', 'varchar', 'NULL', $char_d,
476 'state', 'varchar', 'NULL', $char_d,
477 'zip', 'varchar', '', 10,
478 'country', 'char', '', 2,
479 'daytime', 'varchar', 'NULL', 20,
480 'night', 'varchar', 'NULL', 20,
481 'fax', 'varchar', 'NULL', 12,
482 'ship_last', 'varchar', 'NULL', $char_d,
483 # 'ship_middle', 'varchar', 'NULL', $char_d,
484 'ship_first', 'varchar', 'NULL', $char_d,
485 'ship_company', 'varchar', 'NULL', $char_d,
486 'ship_address1', 'varchar', 'NULL', $char_d,
487 'ship_address2', 'varchar', 'NULL', $char_d,
488 'ship_city', 'varchar', 'NULL', $char_d,
489 'ship_county', 'varchar', 'NULL', $char_d,
490 'ship_state', 'varchar', 'NULL', $char_d,
491 'ship_zip', 'varchar', 'NULL', 10,
492 'ship_country', 'char', 'NULL', 2,
493 'ship_daytime', 'varchar', 'NULL', 20,
494 'ship_night', 'varchar', 'NULL', 20,
495 'ship_fax', 'varchar', 'NULL', 12,
496 'payby', 'char', '', 4,
497 'payinfo', 'varchar', 'NULL', 16,
498 #'paydate', @date_type,
499 'paydate', 'varchar', 'NULL', 10,
500 'payname', 'varchar', 'NULL', $char_d,
501 'tax', 'char', 'NULL', 1,
502 'otaker', 'varchar', '', 8,
503 'refnum', 'int', '', '',
504 'comments', 'varchar', 'NULL', '',
506 'primary_key' => 'custnum',
508 #'index' => [ ['last'], ['company'] ],
509 'index' => [ ['last'], ],
512 'cust_main_invoice' => {
514 'destnum', 'int', '', '',
515 'custnum', 'int', '', '',
516 'dest', 'varchar', '', $char_d,
518 'primary_key' => 'destnum',
520 'index' => [ ['custnum'], ],
523 'cust_main_county' => { #county+state+country are checked off the
524 #cust_main_county for validation and to provide
527 'taxnum', 'int', '', '',
528 'state', 'varchar', 'NULL', $char_d,
529 'county', 'varchar', 'NULL', $char_d,
530 'country', 'char', '', 2,
531 'tax', 'real', '', '', #tax %
533 'primary_key' => 'taxnum',
535 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
541 'paynum', 'int', '', '',
542 'invnum', 'int', '', '',
545 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
546 # payment type table.
547 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
548 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
550 'primary_key' => 'paynum',
552 'index' => [ ['invnum'] ],
555 'cust_pay_batch' => { #what's this used for again? list of customers
556 #in current CARD batch? (necessarily CARD?)
558 'invnum', 'int', '', '',
559 'custnum', 'int', '', '',
560 'last', 'varchar', '', $char_d,
561 'first', 'varchar', '', $char_d,
562 'address1', 'varchar', '', $char_d,
563 'address2', 'varchar', 'NULL', $char_d,
564 'city', 'varchar', '', $char_d,
565 'state', 'varchar', '', $char_d,
566 'zip', 'varchar', '', 10,
567 'country', 'char', '', 2,
568 'trancode', 'int', '', '',
569 'cardnum', 'varchar', '', 16,
571 'exp', 'varchar', '', 11,
572 'payname', 'varchar', 'NULL', $char_d,
573 'amount', @money_type,
577 'index' => [ ['invnum'], ['custnum'] ],
582 'pkgnum', 'int', '', '',
583 'custnum', 'int', '', '',
584 'pkgpart', 'int', '', '',
585 'otaker', 'varchar', '', 8,
589 'cancel', @date_type,
590 'expire', @date_type,
592 'primary_key' => 'pkgnum',
594 'index' => [ ['custnum'] ],
599 'refundnum', 'int', '', '',
600 'crednum', 'int', '', '',
602 'refund', @money_type,
603 'otaker', 'varchar', '', 8,
604 'reason', 'varchar', '', $char_d,
605 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
606 # into payment type table.
607 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
609 'primary_key' => 'refundnum',
611 'index' => [ ['crednum'] ],
616 'svcnum', 'int', '', '',
617 'pkgnum', 'int', 'NULL', '',
618 'svcpart', 'int', '', '',
620 'primary_key' => 'svcnum',
622 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
627 'pkgpart', 'int', '', '',
628 'pkg', 'varchar', '', $char_d,
629 'comment', 'varchar', '', $char_d,
631 'freq', 'int', '', '', #billing frequency (months)
634 'primary_key' => 'pkgpart',
641 # 'titlenum', 'int', '', '',
642 # 'title', 'varchar', '', $char_d,
644 # 'primary_key' => 'titlenum',
645 # 'unique' => [ [] ],
651 'pkgpart', 'int', '', '',
652 'svcpart', 'int', '', '',
653 'quantity', 'int', '', '',
656 'unique' => [ ['pkgpart', 'svcpart'] ],
657 'index' => [ ['pkgpart'] ],
662 'refnum', 'int', '', '',
663 'referral', 'varchar', '', $char_d,
665 'primary_key' => 'refnum',
672 'svcpart', 'int', '', '',
673 'svc', 'varchar', '', $char_d,
674 'svcdb', 'varchar', '', $char_d,
676 'primary_key' => 'svcpart',
681 #(this should be renamed to part_pop)
684 'popnum', 'int', '', '',
685 'city', 'varchar', '', $char_d,
686 'state', 'varchar', '', $char_d,
688 'exch', 'char', '', 3,
689 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
691 'primary_key' => 'popnum',
698 'svcnum', 'int', '', '',
699 'username', 'varchar', '', $username_len, #unique (& remove dup code)
700 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
701 'popnum', 'int', 'NULL', '',
702 'uid', 'int', 'NULL', '',
703 'gid', 'int', 'NULL', '',
704 'finger', 'varchar', 'NULL', $char_d,
705 'dir', 'varchar', 'NULL', $char_d,
706 'shell', 'varchar', 'NULL', $char_d,
707 'quota', 'varchar', 'NULL', $char_d,
708 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
709 'seconds', 'int', 'NULL', '', #uhhhh
711 'primary_key' => 'svcnum',
713 'index' => [ ['username'] ],
718 'svcnum', 'int', '', '',
719 'domsvc', 'int', '', '',
720 'domuid', 'int', '', '',
721 'domuser', 'varchar', '', $char_d,
723 'primary_key' => 'svcnum',
725 'index' => [ ['domsvc'], ['domuid'] ],
730 # 'svcnum', 'int', '', '',
731 # 'amount', @money_type,
733 # 'primary_key' => 'svcnum',
734 # 'unique' => [ [] ],
740 'svcnum', 'int', '', '',
741 'domain', 'varchar', '', $char_d,
743 'primary_key' => 'svcnum',
744 'unique' => [ ['domain'] ],
750 'recnum', 'int', '', '',
751 'svcnum', 'int', '', '',
752 'reczone', 'varchar', '', $char_d,
753 'recaf', 'char', '', 2,
754 'rectype', 'char', '', 5,
755 'recdata', 'varchar', '', $char_d,
757 'primary_key' => 'recnum',
759 'index' => [ ['svcnum'] ],
764 'svcnum', 'int', '', '',
765 'recnum', 'int', '', '',
766 'usersvc', 'int', '', '',
768 'primary_key' => 'svcnum',
775 # 'svcnum', 'int', '', '',
776 # 'svcnum', 'int', '', '',
777 # 'svcnum', 'int', '', '',
778 # 'worker', 'varchar', '', $char_d,
779 # '_date', @date_type,
781 # 'primary_key' => 'svcnum',
782 # 'unique' => [ [] ],
788 'prepaynum', 'int', '', '',
789 'identifier', 'varchar', '', $char_d,
790 'amount', @money_type,
791 'seconds', 'int', 'NULL', '',
793 'primary_key' => 'prepaynum',
794 'unique' => [ ['identifier'] ],
800 'portnum', 'int', '', '',
801 'ip', 'varchar', 'NULL', 15,
802 'nasport', 'int', 'NULL', '',
803 'nasnum', 'int', '', '',
805 'primary_key' => 'portnum',
812 'nasnum', 'int', '', '',
813 'nas', 'varchar', '', $char_d,
814 'nasip', 'varchar', '', 15,
815 'nasfqdn', 'varchar', '', $char_d,
816 'last', 'int', '', '',
818 'primary_key' => 'nasnum',
819 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
820 'index' => [ [ 'last' ] ],
825 'sessionnum', 'int', '', '',
826 'portnum', 'int', '', '',
827 'svcnum', 'int', '', '',
829 'logout', @date_type,
831 'primary_key' => 'sessionnum',
833 'index' => [ [ 'portnum' ] ],