3 # $Id: fs-setup,v 1.37.2.1 2001-08-08 17:45:35 jeff 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.37.2.1 2001-08-08 17:45:35 jeff
36 # initial vpopmail support
38 # Revision 1.2 2001/08/07 22:05:42 jeff
41 # Initial vpopmail changes
43 # Revision 1.1.1.1 2001/07/17 18:08:43 jeff
44 # Import of Freeside 1.3.1 pre
46 # Revision 1.37 2001/06/03 14:16:11 ivan
47 # allow empty refund reasons
49 # Revision 1.36 2001/04/15 12:56:31 ivan
50 # s/dbdef/DBIx::DBSchema/
52 # Revision 1.35 2001/04/15 09:36:43 ivan
53 # http://www.sisd.com/freeside/list-archive/msg01450.html
55 # Revision 1.34 2001/04/09 23:05:16 ivan
56 # Transactions Part I!!!
58 # Revision 1.33 2001/02/03 14:03:50 ivan
59 # time-based prepaid cards, session monitor. woop!
61 # Revision 1.32 2000/12/04 00:13:02 ivan
64 # Revision 1.31 2000/12/01 18:34:53 ivan
67 # Revision 1.30 2000/12/01 18:33:32 ivan
70 # Revision 1.29 2000/11/07 15:00:37 ivan
73 # Revision 1.28 2000/10/30 10:47:26 ivan
74 # nas.last can't be defined NULL if indexed
76 # Revision 1.26 2000/07/06 08:57:27 ivan
77 # support for radius check attributes (except importing). poorly documented.
79 # Revision 1.25 2000/06/29 12:00:49 ivan
80 # support for pre-encrypted md5 passwords.
82 # Revision 1.24 2000/03/02 07:44:07 ivan
83 # typo forgot closing '
85 # Revision 1.23 2000/02/03 05:16:52 ivan
86 # beginning of DNS and Apache support
88 # Revision 1.22 2000/01/31 05:22:23 ivan
89 # prepaid "internet cards"
91 # Revision 1.21 2000/01/30 06:03:26 ivan
92 # postgres 6.5 finally supports decimal(10,2)
94 # Revision 1.20 2000/01/28 22:53:33 ivan
95 # track full phone number
97 # Revision 1.19 1999/07/29 08:50:35 ivan
98 # wrong type for cust_pay_batch.exp
100 # Revision 1.18 1999/04/15 22:46:30 ivan
103 # Revision 1.17 1999/04/14 07:58:39 ivan
104 # export getsecrets from FS::UID instead of calling it explicitly
106 # Revision 1.16 1999/02/28 19:44:16 ivan
107 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
109 # Revision 1.15 1999/02/27 21:06:21 ivan
110 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
111 # by Ben Leibig <leibig@colorado.edu>
113 # Revision 1.14 1999/02/07 09:59:14 ivan
114 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
116 # Revision 1.13 1999/02/04 06:09:23 ivan
119 # Revision 1.12 1999/02/03 10:42:27 ivan
120 # *** empty log message ***
122 # Revision 1.11 1999/01/17 03:11:52 ivan
123 # remove preliminary completehost changes
125 # Revision 1.10 1998/12/16 06:05:38 ivan
126 # add table cust_main_invoice
128 # Revision 1.9 1998/12/15 04:36:29 ivan
129 # s/croak/die/; #oops
131 # Revision 1.8 1998/12/15 04:33:27 ivan
132 # dies if it isn't running as the freeside user
134 # Revision 1.7 1998/11/18 09:01:31 ivan
137 # Revision 1.6 1998/11/15 13:18:02 ivan
140 # Revision 1.5 1998/11/15 09:43:03 ivan
141 # update for new config file syntax, new adminsuidsetup
143 # Revision 1.4 1998/10/22 15:51:23 ivan
144 # also varchar with no length specified - postgresql fix broke mysql.
146 # Revision 1.3 1998/10/22 15:46:28 ivan
147 # now smallint is illegal, so remove that too.
150 #to delay loading dbdef until we're ready
151 BEGIN { $FS::Record::setup_hack = 1; }
156 use DBIx::DBSchema::Table;
157 use DBIx::DBSchema::Column;
158 use DBIx::DBSchema::ColGroup::Unique;
159 use DBIx::DBSchema::ColGroup::Index;
160 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
162 use FS::cust_main_county;
164 die "Not running uid freeside!" unless checkeuid();
166 my $user = shift or die &usage;
169 #needs to match FS::Record
170 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
174 print "\nEnter the maximum username length: ";
175 my($username_len)=&getvalue;
177 print "\n\n", <<END, ":";
178 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
179 reply attribute Framed-IP-Address for each user. You can specify additional
180 check and reply attributes. First enter any additional RADIUS check attributes
181 you need to track for each user, separated by whitespace.
183 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
185 print "\n\n", <<END, ":";
186 Now enter any additional reply attributes you need to track for each user,
187 separated by whitespace.
189 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
192 my($x)=scalar(<STDIN>);
199 my($char_d) = 80; #default maxlength for text fields
201 #my(@date_type) = ( 'timestamp', '', '' );
202 my(@date_type) = ( 'int', 'NULL', '' );
203 my(@perl_type) = ( 'varchar', 'NULL', 255 );
204 my @money_type = ( 'decimal', '', '10,2' );
207 # create a dbdef object from the old data structure
210 my(%tables)=&tables_hash_hack;
212 #turn it into objects
213 my($dbdef) = new DBIx::DBSchema ( map {
215 while (@{$tables{$_}{'columns'}}) {
216 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
217 push @columns, new DBIx::DBSchema::Column ( $name,$type,$null,$length );
219 DBIx::DBSchema::Table->new(
221 $tables{$_}{'primary_key'},
222 DBIx::DBSchema::ColGroup::Unique->new($tables{$_}{'unique'}),
223 DBIx::DBSchema::ColGroup::Index->new($tables{$_}{'index'}),
228 #add radius attributes to svc_acct
230 my($svc_acct)=$dbdef->table('svc_acct');
233 foreach $attribute (@attributes) {
234 $svc_acct->addcolumn ( new DBIx::DBSchema::Column (
235 'radius_'. $attribute,
242 foreach $attribute (@check_attributes) {
243 $svc_acct->addcolumn( new DBIx::DBSchema::Column (
251 #make part_svc table (but now as object)
253 my($part_svc)=$dbdef->table('part_svc');
255 #because of svc_acct_pop
256 #foreach (grep /^svc_/, $dbdef->tables) {
257 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
258 foreach (qw(svc_acct svc_acct_sm svc_domain svc_forward svc_www)) {
259 my($table)=$dbdef->table($_);
261 foreach $col ( $table->columns ) {
262 next if $col =~ /^svcnum$/;
263 $part_svc->addcolumn( new DBIx::DBSchema::Column (
264 $table->name. '__' . $table->column($col)->name,
265 'varchar', #$table->column($col)->type,
267 $char_d, #$table->column($col)->length,
269 $part_svc->addcolumn ( new DBIx::DBSchema::Column (
270 $table->name. '__'. $table->column($col)->name . "_flag",
279 $dbdef->save($dbdef_file);
280 &FS::Record::reload_dbdef($dbdef_file);
286 my($dbh)=adminsuidsetup $user;
291 my @sql = $dbdef->sql($dbh);
292 foreach my $statement ( $dbdef->sql($dbh) ) {
293 $dbh->do( $statement )
294 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
297 #not really sample data (and shouldn't default to US)
303 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
304 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
305 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
307 my($cust_main_county)=new FS::cust_main_county({
313 $error=$cust_main_county->insert;
314 die $error if $error;
317 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
319 VIC NSW NT QLD TAS ACT WA SA
321 my($cust_main_county)=new FS::cust_main_county({
327 $error=$cust_main_county->insert;
328 die $error if $error;
331 #ISO 2-letter country codes (same as country TLDs) except US and AU
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
344 my($cust_main_county)=new FS::cust_main_county({
349 $error=$cust_main_county->insert;
350 die $error if $error;
353 $dbh->disconnect or die $dbh->errstr;
355 print "Freeside database initialized sucessfully\n";
358 die "Usage:\n fs-setup user\n";
362 # Now it becomes an object. much better.
364 sub tables_hash_hack {
366 #note that s/(date|change)/_$1/; to avoid keyword conflict.
367 #put a kludge in FS::Record to catch this or? (pry need some date-handling
370 my(%tables)=( #yech.}
374 'agentnum', 'int', '', '',
375 'agent', 'varchar', '', $char_d,
376 'typenum', 'int', '', '',
377 'freq', 'int', 'NULL', '',
380 'primary_key' => 'agentnum',
382 'index' => [ ['typenum'] ],
387 'typenum', 'int', '', '',
388 'atype', 'varchar', '', $char_d,
390 'primary_key' => 'typenum',
397 'typenum', 'int', '', '',
398 'pkgpart', 'int', '', '',
401 'unique' => [ ['typenum', 'pkgpart'] ],
402 'index' => [ ['typenum'] ],
407 'invnum', 'int', '', '',
408 'custnum', 'int', '', '',
410 'charged', @money_type,
411 'printed', 'int', '', '',
413 'primary_key' => 'invnum',
415 'index' => [ ['custnum'] ],
420 'pkgnum', 'int', '', '',
421 'invnum', 'int', '', '',
422 'setup', @money_type,
423 'recur', @money_type,
428 'unique' => [ ['pkgnum', 'invnum'] ],
429 'index' => [ ['invnum'] ],
434 'crednum', 'int', '', '',
435 'custnum', 'int', '', '',
437 'amount', @money_type,
438 'otaker', 'varchar', '', 8,
439 'reason', 'varchar', 'NULL', 255,
441 'primary_key' => 'crednum',
443 'index' => [ ['custnum'] ],
448 'custnum', 'int', '', '',
449 'agentnum', 'int', '', '',
450 # 'titlenum', 'int', 'NULL', '',
451 'last', 'varchar', '', $char_d,
452 # 'middle', 'varchar', 'NULL', $char_d,
453 'first', 'varchar', '', $char_d,
454 'ss', 'char', 'NULL', 11,
455 'company', 'varchar', 'NULL', $char_d,
456 'address1', 'varchar', '', $char_d,
457 'address2', 'varchar', 'NULL', $char_d,
458 'city', 'varchar', '', $char_d,
459 'county', 'varchar', 'NULL', $char_d,
460 'state', 'varchar', 'NULL', $char_d,
461 'zip', 'varchar', '', 10,
462 'country', 'char', '', 2,
463 'daytime', 'varchar', 'NULL', 20,
464 'night', 'varchar', 'NULL', 20,
465 'fax', 'varchar', 'NULL', 12,
466 'payby', 'char', '', 4,
467 'payinfo', 'varchar', 'NULL', 16,
468 #'paydate', @date_type,
469 'paydate', 'varchar', 'NULL', 10,
470 'payname', 'varchar', 'NULL', $char_d,
471 'tax', 'char', 'NULL', 1,
472 'otaker', 'varchar', '', 8,
473 'refnum', 'int', '', '',
475 'primary_key' => 'custnum',
477 #'index' => [ ['last'], ['company'] ],
478 'index' => [ ['last'], ],
481 'cust_main_invoice' => {
483 'destnum', 'int', '', '',
484 'custnum', 'int', '', '',
485 'dest', 'varchar', '', $char_d,
487 'primary_key' => 'destnum',
489 'index' => [ ['custnum'], ],
492 'cust_main_county' => { #county+state+country are checked off the
493 #cust_main_county for validation and to provide
496 'taxnum', 'int', '', '',
497 'state', 'varchar', 'NULL', $char_d,
498 'county', 'varchar', 'NULL', $char_d,
499 'country', 'char', '', 2,
500 'tax', 'real', '', '', #tax %
502 'primary_key' => 'taxnum',
504 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
510 'paynum', 'int', '', '',
511 'invnum', 'int', '', '',
514 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
515 # payment type table.
516 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
517 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
519 'primary_key' => 'paynum',
521 'index' => [ ['invnum'] ],
524 'cust_pay_batch' => { #what's this used for again? list of customers
525 #in current CARD batch? (necessarily CARD?)
527 'invnum', 'int', '', '',
528 'custnum', 'int', '', '',
529 'last', 'varchar', '', $char_d,
530 'first', 'varchar', '', $char_d,
531 'address1', 'varchar', '', $char_d,
532 'address2', 'varchar', 'NULL', $char_d,
533 'city', 'varchar', '', $char_d,
534 'state', 'varchar', '', $char_d,
535 'zip', 'varchar', '', 10,
536 'country', 'char', '', 2,
537 'trancode', 'int', '', '',
538 'cardnum', 'varchar', '', 16,
540 'exp', 'varchar', '', 11,
541 'payname', 'varchar', 'NULL', $char_d,
542 'amount', @money_type,
546 'index' => [ ['invnum'], ['custnum'] ],
551 'pkgnum', 'int', '', '',
552 'custnum', 'int', '', '',
553 'pkgpart', 'int', '', '',
554 'otaker', 'varchar', '', 8,
558 'cancel', @date_type,
559 'expire', @date_type,
561 'primary_key' => 'pkgnum',
563 'index' => [ ['custnum'] ],
568 'refundnum', 'int', '', '',
569 'crednum', 'int', '', '',
571 'refund', @money_type,
572 'otaker', 'varchar', '', 8,
573 'reason', 'varchar', '', $char_d,
574 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
575 # into payment type table.
576 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
578 'primary_key' => 'refundnum',
580 'index' => [ ['crednum'] ],
585 'svcnum', 'int', '', '',
586 'pkgnum', 'int', 'NULL', '',
587 'svcpart', 'int', '', '',
589 'primary_key' => 'svcnum',
591 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
596 'pkgpart', 'int', '', '',
597 'pkg', 'varchar', '', $char_d,
598 'comment', 'varchar', '', $char_d,
600 'freq', 'int', '', '', #billing frequency (months)
603 'primary_key' => 'pkgpart',
610 # 'titlenum', 'int', '', '',
611 # 'title', 'varchar', '', $char_d,
613 # 'primary_key' => 'titlenum',
614 # 'unique' => [ [] ],
620 'pkgpart', 'int', '', '',
621 'svcpart', 'int', '', '',
622 'quantity', 'int', '', '',
625 'unique' => [ ['pkgpart', 'svcpart'] ],
626 'index' => [ ['pkgpart'] ],
631 'refnum', 'int', '', '',
632 'referral', 'varchar', '', $char_d,
634 'primary_key' => 'refnum',
641 'svcpart', 'int', '', '',
642 'svc', 'varchar', '', $char_d,
643 'svcdb', 'varchar', '', $char_d,
645 'primary_key' => 'svcpart',
650 #(this should be renamed to part_pop)
653 'popnum', 'int', '', '',
654 'city', 'varchar', '', $char_d,
655 'state', 'varchar', '', $char_d,
657 'exch', 'char', '', 3,
658 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
660 'primary_key' => 'popnum',
667 'svcnum', 'int', '', '',
668 'username', 'varchar', '', $username_len, #unique (& remove dup code)
669 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
670 'popnum', 'int', 'NULL', '',
671 'uid', 'int', 'NULL', '',
672 'gid', 'int', 'NULL', '',
673 'finger', 'varchar', 'NULL', $char_d,
674 'dir', 'varchar', 'NULL', $char_d,
675 'shell', 'varchar', 'NULL', $char_d,
676 'quota', 'varchar', 'NULL', $char_d,
677 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
678 'seconds', 'int', 'NULL', '', #uhhhh
679 'domsvc', 'int', '', '',
681 'primary_key' => 'svcnum',
683 'index' => [ ['username'], ['domsvc'] ],
688 'svcnum', 'int', '', '',
689 'domsvc', 'int', '', '',
690 'domuid', 'int', '', '',
691 'domuser', 'varchar', '', $char_d,
693 'primary_key' => 'svcnum',
695 'index' => [ ['domsvc'], ['domuid'] ],
700 # 'svcnum', 'int', '', '',
701 # 'amount', @money_type,
703 # 'primary_key' => 'svcnum',
704 # 'unique' => [ [] ],
710 'svcnum', 'int', '', '',
711 'domain', 'varchar', '', $char_d,
712 'catchall', 'int', '', '',
714 'primary_key' => 'svcnum',
715 'unique' => [ ['domain'] ],
721 'recnum', 'int', '', '',
722 'svcnum', 'int', '', '',
723 'reczone', 'varchar', '', $char_d,
724 'recaf', 'char', '', 2,
725 'rectype', 'char', '', 5,
726 'recdata', 'varchar', '', $char_d,
728 'primary_key' => 'recnum',
730 'index' => [ ['svcnum'] ],
735 'svcnum', 'int', '', '',
736 'srcsvc', 'int', '', '',
737 'dstsvc', 'int', '', '',
738 'dst', 'varchar', 'NULL', $char_d,
740 'primary_key' => 'svcnum',
742 'index' => [ ['srcsvc'], ['dstsvc'] ],
747 'svcnum', 'int', '', '',
748 'recnum', 'int', '', '',
749 'usersvc', 'int', '', '',
751 'primary_key' => 'svcnum',
758 # 'svcnum', 'int', '', '',
759 # 'svcnum', 'int', '', '',
760 # 'svcnum', 'int', '', '',
761 # 'worker', 'varchar', '', $char_d,
762 # '_date', @date_type,
764 # 'primary_key' => 'svcnum',
765 # 'unique' => [ [] ],
771 'prepaynum', 'int', '', '',
772 'identifier', 'varchar', '', $char_d,
773 'amount', @money_type,
774 'seconds', 'int', 'NULL', '',
776 'primary_key' => 'prepaynum',
777 'unique' => [ ['identifier'] ],
783 'portnum', 'int', '', '',
784 'ip', 'varchar', 'NULL', 15,
785 'nasport', 'int', 'NULL', '',
786 'nasnum', 'int', '', '',
788 'primary_key' => 'portnum',
795 'nasnum', 'int', '', '',
796 'nas', 'varchar', '', $char_d,
797 'nasip', 'varchar', '', 15,
798 'nasfqdn', 'varchar', '', $char_d,
799 'last', 'int', '', '',
801 'primary_key' => 'nasnum',
802 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
803 'index' => [ [ 'last' ] ],
808 'sessionnum', 'int', '', '',
809 'portnum', 'int', '', '',
810 'svcnum', 'int', '', '',
812 'logout', @date_type,
814 'primary_key' => 'sessionnum',
816 'index' => [ [ 'portnum' ] ],