3 # $Id: fs-setup,v 1.11 1999-01-17 03:11:52 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.11 1999-01-17 03:11:52 ivan
36 # remove preliminary completehost changes
38 # Revision 1.10 1998/12/16 06:05:38 ivan
39 # add table cust_main_invoice
41 # Revision 1.9 1998/12/15 04:36:29 ivan
44 # Revision 1.8 1998/12/15 04:33:27 ivan
45 # dies if it isn't running as the freeside user
47 # Revision 1.7 1998/11/18 09:01:31 ivan
50 # Revision 1.6 1998/11/15 13:18:02 ivan
53 # Revision 1.5 1998/11/15 09:43:03 ivan
54 # update for new config file syntax, new adminsuidsetup
56 # Revision 1.4 1998/10/22 15:51:23 ivan
57 # also varchar with no length specified - postgresql fix broke mysql.
59 # Revision 1.3 1998/10/22 15:46:28 ivan
60 # now smallint is illegal, so remove that too.
63 #to delay loading dbdef until we're ready
64 BEGIN { $FS::Record::setup_hack = 1; }
69 use FS::UID qw(adminsuidsetup datasrc checkeuid);
71 use FS::cust_main_county;
73 die "Not running uid freeside!" unless checkeuid();
75 my $user = shift or die &usage;
76 FS::UID::getsecrets $user;
78 #needs to match FS::Record
79 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
83 print "\nEnter the maximum username length: ";
84 my($username_len)=&getvalue;
86 print "\n\n", <<END, ":";
87 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
88 for each user. Enter any additional RADIUS attributes you need to track for
89 each user, separated by whitespace.
91 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
94 my($x)=scalar(<STDIN>);
101 my($char_d) = 80; #default maxlength for text fields
103 #my(@date_type) = ( 'timestamp', '', '' );
104 my(@date_type) = ( 'int', 'NULL', '' );
105 my(@perl_type) = ( 'varchar', 'NULL', 255 );
107 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
108 @money_type = ( 'money', '', '' );
110 @money_type = ( 'decimal', '', '10,2' );
114 # create a dbdef object from the old data structure
117 my(%tables)=&tables_hash_hack;
119 #turn it into objects
120 my($dbdef) = new FS::dbdef ( map {
122 while (@{$tables{$_}{'columns'}}) {
123 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
124 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
126 FS::dbdef_table->new(
128 $tables{$_}{'primary_key'},
129 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
130 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
131 FS::dbdef_unique->new($tables{$_}{'unique'}),
132 FS::dbdef_index->new($tables{$_}{'index'}),
137 #add radius attributes to svc_acct
139 my($svc_acct)=$dbdef->table('svc_acct');
142 foreach $attribute (@attributes) {
143 $svc_acct->addcolumn ( new FS::dbdef_column (
144 'radius_'. $attribute,
151 #make part_svc table (but now as object)
153 my($part_svc)=$dbdef->table('part_svc');
155 #because of svc_acct_pop
156 #foreach (grep /^svc_/, $dbdef->tables) {
157 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
158 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
159 my($table)=$dbdef->table($_);
161 foreach $col ( $table->columns ) {
162 next if $col =~ /^svcnum$/;
163 $part_svc->addcolumn( new FS::dbdef_column (
164 $table->name. '__' . $table->column($col)->name,
165 'varchar', #$table->column($col)->type,
167 $char_d, #$table->column($col)->length,
169 $part_svc->addcolumn ( new FS::dbdef_column (
170 $table->name. '__'. $table->column($col)->name . "_flag",
179 $dbdef->save($dbdef_file);
180 FS::Record::reload_dbdef($dbdef_file);
186 my($dbh)=adminsuidsetup $user;
192 foreach ($dbdef->tables) {
193 my($table)=$dbdef->table($_);
194 print "Creating $_...";
199 foreach $statement ($table->sql_create_table(datasrc)) {
200 #print $statement, "\n";
201 $dbh->do( $statement )
202 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
208 #not really sample data (and shouldn't default to US)
214 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
215 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
216 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
218 my($cust_main_county)=create FS::cust_main_county({
224 $error=$cust_main_county->insert;
225 die $error if $error;
228 #ISO 2-letter country codes (same as country TLDs) except US
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
241 my($cust_main_county)=create FS::cust_main_county({
246 $error=$cust_main_county->insert;
247 die $error if $error;
250 $dbh->disconnect or die $dbh->errstr;
253 die "Usage:\n fs-setup user\n";
257 # Now it becomes an object. much better.
259 sub tables_hash_hack {
261 #note that s/(date|change)/_$1/; to avoid keyword conflict.
262 #put a kludge in FS::Record to catch this or? (pry need some date-handling
265 my(%tables)=( #yech.}
269 'agentnum', 'int', '', '',
270 'agent', 'varchar', '', $char_d,
271 'typenum', 'int', '', '',
272 'freq', 'int', 'NULL', '',
275 'primary_key' => 'agentnum',
277 'index' => [ ['typenum'] ],
282 'typenum', 'int', '', '',
283 'atype', 'varchar', '', $char_d,
285 'primary_key' => 'typenum',
292 'typenum', 'int', '', '',
293 'pkgpart', 'int', '', '',
296 'unique' => [ ['typenum', 'pkgpart'] ],
297 'index' => [ ['typenum'] ],
302 'invnum', 'int', '', '',
303 'custnum', 'int', '', '',
305 'charged', @money_type,
307 'printed', 'int', '', '',
309 'primary_key' => 'invnum',
311 'index' => [ ['custnum'] ],
316 'pkgnum', 'int', '', '',
317 'invnum', 'int', '', '',
318 'setup', @money_type,
319 'recur', @money_type,
324 'unique' => [ ['pkgnum', 'invnum'] ],
325 'index' => [ ['invnum'] ],
330 'crednum', 'int', '', '',
331 'custnum', 'int', '', '',
333 'amount', @money_type,
334 'credited', @money_type,
335 'otaker', 'varchar', '', 8,
336 'reason', 'varchar', '', 255,
338 'primary_key' => 'crednum',
340 'index' => [ ['custnum'] ],
345 'custnum', 'int', '', '',
346 'agentnum', 'int', '', '',
347 # 'titlenum', 'int', 'NULL', '',
348 'last', 'varchar', '', $char_d,
349 # 'middle', 'varchar', 'NULL', $char_d,
350 'first', 'varchar', '', $char_d,
351 'ss', 'char', 'NULL', 11,
352 'company', 'varchar', 'NULL', $char_d,
353 'address1', 'varchar', '', $char_d,
354 'address2', 'varchar', 'NULL', $char_d,
355 'city', 'varchar', '', $char_d,
356 'county', 'varchar', 'NULL', $char_d,
357 'state', 'varchar', '', $char_d,
358 'zip', 'varchar', '', 10,
359 'country', 'char', '', 2,
360 'daytime', 'varchar', 'NULL', 20,
361 'night', 'varchar', 'NULL', 20,
362 'fax', 'varchar', 'NULL', 12,
363 'payby', 'char', '', 4,
364 'payinfo', 'varchar', 'NULL', 16,
365 'paydate', @date_type,
366 'payname', 'varchar', 'NULL', $char_d,
367 'tax', 'char', 'NULL', 1,
368 'otaker', 'varchar', '', 8,
369 'refnum', 'int', '', '',
371 'primary_key' => 'custnum',
373 #'index' => [ ['last'], ['company'] ],
374 'index' => [ ['last'], ],
377 'cust_main_invoice' => {
379 'destnum', 'int', '', '',
380 'custnum', 'int', '', '',
381 'dest', 'varchar', '', $char_d,
383 'primary_key' => 'destnum',
385 'index' => [ ['custnum'], ],
388 'cust_main_county' => { #county+state+country are checked off the
389 #cust_main_county for validation and to provide
392 'taxnum', 'int', '', '',
393 'state', 'char', 'NULL', $char_d,
394 'county', 'varchar', 'NULL', $char_d,
395 'country', 'char', '', 2,
396 'tax', 'real', '', '', #tax %
398 'primary_key' => 'taxnum',
400 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
406 'paynum', 'int', '', '',
407 'invnum', 'int', '', '',
410 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
411 # payment type table.
412 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
413 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
415 'primary_key' => 'paynum',
417 'index' => [ ['invnum'] ],
420 'cust_pay_batch' => { #what's this used for again? list of customers
421 #in current CARD batch? (necessarily CARD?)
423 'invnum', 'int', '', '',
424 'custnum', 'int', '', '',
425 'last', 'varchar', '', $char_d,
426 'first', 'varchar', '', $char_d,
427 'address1', 'varchar', '', $char_d,
428 'address2', 'varchar', 'NULL', $char_d,
429 'city', 'varchar', '', $char_d,
430 'state', 'char', '', 2,
431 'zip', 'varchar', '', 10,
432 'country', 'char', '', 2,
433 'trancode', 'int', '', '',
434 'cardnum', 'varchar', '', 16,
436 'payname', 'varchar', 'NULL', $char_d,
437 'amount', @money_type,
441 'index' => [ ['invnum'], ['custnum'] ],
446 'pkgnum', 'int', '', '',
447 'custnum', 'int', '', '',
448 'pkgpart', 'int', '', '',
449 'otaker', 'varchar', '', 8,
453 'cancel', @date_type,
454 'expire', @date_type,
456 'primary_key' => 'pkgnum',
458 'index' => [ ['custnum'] ],
463 'refundnum', 'int', '', '',
464 'crednum', 'int', '', '',
466 'refund', @money_type,
467 'otaker', 'varchar', '', 8,
468 'reason', 'varchar', '', $char_d,
469 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
470 # into payment type table.
471 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
473 'primary_key' => 'refundnum',
475 'index' => [ ['crednum'] ],
480 'svcnum', 'int', '', '',
481 'pkgnum', 'int', '', '',
482 'svcpart', 'int', '', '',
484 'primary_key' => 'svcnum',
486 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
491 'pkgpart', 'int', '', '',
492 'pkg', 'varchar', '', $char_d,
493 'comment', 'varchar', '', $char_d,
495 'freq', 'int', '', '', #billing frequency (months)
498 'primary_key' => 'pkgpart',
505 # 'titlenum', 'int', '', '',
506 # 'title', 'varchar', '', $char_d,
508 # 'primary_key' => 'titlenum',
509 # 'unique' => [ [] ],
515 'pkgpart', 'int', '', '',
516 'svcpart', 'int', '', '',
517 'quantity', 'int', '', '',
520 'unique' => [ ['pkgpart', 'svcpart'] ],
521 'index' => [ ['pkgpart'] ],
526 'refnum', 'int', '', '',
527 'referral', 'varchar', '', $char_d,
529 'primary_key' => 'refnum',
536 'svcpart', 'int', '', '',
537 'svc', 'varchar', '', $char_d,
538 'svcdb', 'varchar', '', $char_d,
540 'primary_key' => 'svcpart',
545 #(this should be renamed to part_pop)
548 'popnum', 'int', '', '',
549 'city', 'varchar', '', $char_d,
550 'state', 'char', '', 2,
552 'exch', 'char', '', 3,
555 'primary_key' => 'popnum',
562 'svcnum', 'int', '', '',
563 'username', 'varchar', '', $username_len, #unique (& remove dup code)
564 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
565 'popnum', 'int', 'NULL', '',
566 'uid', 'int', 'NULL', '',
567 'gid', 'int', 'NULL', '',
568 'finger', 'varchar', 'NULL', $char_d,
569 'dir', 'varchar', 'NULL', $char_d,
570 'shell', 'varchar', 'NULL', $char_d,
571 'quota', 'varchar', 'NULL', $char_d,
572 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
574 'primary_key' => 'svcnum',
576 'index' => [ ['username'] ],
581 'svcnum', 'int', '', '',
582 'domsvc', 'int', '', '',
583 'domuid', 'int', '', '',
584 'domuser', 'varchar', '', $char_d,
586 'primary_key' => 'svcnum',
588 'index' => [ ['domsvc'], ['domuid'] ],
593 # 'svcnum', 'int', '', '',
594 # 'amount', @money_type,
596 # 'primary_key' => 'svcnum',
597 # 'unique' => [ [] ],
603 'svcnum', 'int', '', '',
604 'domain', 'varchar', '', $char_d,
606 'primary_key' => 'svcnum',
607 'unique' => [ ['domain'] ],
613 # 'svcnum', 'int', '', '',
614 # 'svcnum', 'int', '', '',
615 # 'svcnum', 'int', '', '',
616 # 'worker', 'varchar', '', $char_d,
617 # '_date', @date_type,
619 # 'primary_key' => 'svcnum',
620 # 'unique' => [ [] ],