3 # $Id: fs-setup,v 1.9 1998-12-15 04:36:29 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.9 1998-12-15 04:36:29 ivan
38 # Revision 1.8 1998/12/15 04:33:27 ivan
39 # dies if it isn't running as the freeside user
41 # Revision 1.7 1998/11/18 09:01:31 ivan
44 # Revision 1.6 1998/11/15 13:18:02 ivan
47 # Revision 1.5 1998/11/15 09:43:03 ivan
48 # update for new config file syntax, new adminsuidsetup
50 # Revision 1.4 1998/10/22 15:51:23 ivan
51 # also varchar with no length specified - postgresql fix broke mysql.
53 # Revision 1.3 1998/10/22 15:46:28 ivan
54 # now smallint is illegal, so remove that too.
57 #to delay loading dbdef until we're ready
58 BEGIN { $FS::Record::setup_hack = 1; }
63 use FS::UID qw(adminsuidsetup datasrc checkeuid);
65 use FS::cust_main_county;
67 die "Not running uid freeside!" unless checkeuid();
69 my $user = shift or die &usage;
70 FS::UID::getsecrets $user;
72 #needs to match FS::Record
73 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
77 print "\nEnter the maximum username length: ";
78 my($username_len)=&getvalue;
80 print "\n\n", <<END, ":";
81 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
82 for each user. Enter any additional RADIUS attributes you need to track for
83 each user, separated by whitespace.
85 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
88 my($x)=scalar(<STDIN>);
95 my($char_d) = 80; #default maxlength for text fields
97 #my(@date_type) = ( 'timestamp', '', '' );
98 my(@date_type) = ( 'int', 'NULL', '' );
99 my(@perl_type) = ( 'varchar', 'NULL', 255 );
101 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
102 @money_type = ( 'money', '', '' );
104 @money_type = ( 'decimal', '', '10,2' );
108 # create a dbdef object from the old data structure
111 my(%tables)=&tables_hash_hack;
113 #turn it into objects
114 my($dbdef) = new FS::dbdef ( map {
116 while (@{$tables{$_}{'columns'}}) {
117 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
118 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
120 FS::dbdef_table->new(
122 $tables{$_}{'primary_key'},
123 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
124 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
125 FS::dbdef_unique->new($tables{$_}{'unique'}),
126 FS::dbdef_index->new($tables{$_}{'index'}),
131 #add radius attributes to svc_acct
133 my($svc_acct)=$dbdef->table('svc_acct');
136 foreach $attribute (@attributes) {
137 $svc_acct->addcolumn ( new FS::dbdef_column (
138 'radius_'. $attribute,
145 #make part_svc table (but now as object)
147 my($part_svc)=$dbdef->table('part_svc');
149 #because of svc_acct_pop
150 #foreach (grep /^svc_/, $dbdef->tables) {
151 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
152 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
153 my($table)=$dbdef->table($_);
155 foreach $col ( $table->columns ) {
156 next if $col =~ /^svcnum$/;
157 $part_svc->addcolumn( new FS::dbdef_column (
158 $table->name. '__' . $table->column($col)->name,
159 'varchar', #$table->column($col)->type,
161 $char_d, #$table->column($col)->length,
163 $part_svc->addcolumn ( new FS::dbdef_column (
164 $table->name. '__'. $table->column($col)->name . "_flag",
173 $dbdef->save($dbdef_file);
174 FS::Record::reload_dbdef($dbdef_file);
180 my($dbh)=adminsuidsetup $user;
186 foreach ($dbdef->tables) {
187 my($table)=$dbdef->table($_);
188 print "Creating $_...";
193 foreach $statement ($table->sql_create_table(datasrc)) {
194 #print $statement, "\n";
195 $dbh->do( $statement )
196 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
202 #not really sample data (and shouldn't default to US)
208 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
209 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
210 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
212 my($cust_main_county)=create FS::cust_main_county({
218 $error=$cust_main_county->insert;
219 die $error if $error;
222 #ISO 2-letter country codes (same as country TLDs) except US
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
235 my($cust_main_county)=create FS::cust_main_county({
240 $error=$cust_main_county->insert;
241 die $error if $error;
244 $dbh->disconnect or die $dbh->errstr;
247 die "Usage:\n fs-setup user\n";
251 # Now it becomes an object. much better.
253 sub tables_hash_hack {
255 #note that s/(date|change)/_$1/; to avoid keyword conflict.
256 #put a kludge in FS::Record to catch this or? (pry need some date-handling
259 my(%tables)=( #yech.}
263 'agentnum', 'int', '', '',
264 'agent', 'varchar', '', $char_d,
265 'typenum', 'int', '', '',
266 'freq', 'int', 'NULL', '',
269 'primary_key' => 'agentnum',
271 'index' => [ ['typenum'] ],
276 'typenum', 'int', '', '',
277 'atype', 'varchar', '', $char_d,
279 'primary_key' => 'typenum',
286 'typenum', 'int', '', '',
287 'pkgpart', 'int', '', '',
290 'unique' => [ ['typenum', 'pkgpart'] ],
291 'index' => [ ['typenum'] ],
296 'invnum', 'int', '', '',
297 'custnum', 'int', '', '',
299 'charged', @money_type,
301 'printed', 'int', '', '',
303 'primary_key' => 'invnum',
305 'index' => [ ['custnum'] ],
310 'pkgnum', 'int', '', '',
311 'invnum', 'int', '', '',
312 'setup', @money_type,
313 'recur', @money_type,
318 'unique' => [ ['pkgnum', 'invnum'] ],
319 'index' => [ ['invnum'] ],
324 'crednum', 'int', '', '',
325 'custnum', 'int', '', '',
327 'amount', @money_type,
328 'credited', @money_type,
329 'otaker', 'varchar', '', 8,
330 'reason', 'varchar', '', 255,
332 'primary_key' => 'crednum',
334 'index' => [ ['custnum'] ],
339 'custnum', 'int', '', '',
340 'agentnum', 'int', '', '',
341 'titlenum', 'int', 'NULL', '',
342 'last', 'varchar', '', $char_d,
343 'middle', 'varchar', 'NULL', $char_d,
344 'first', 'varchar', '', $char_d,
345 'ss', 'char', 'NULL', 11,
346 'company', 'varchar', 'NULL', $char_d,
347 'address1', 'varchar', '', $char_d,
348 'address2', 'varchar', 'NULL', $char_d,
349 'city', 'varchar', '', $char_d,
350 'county', 'varchar', 'NULL', $char_d,
351 'state', 'varchar', '', $char_d,
352 'zip', 'varchar', '', 10,
353 'country', 'char', '', 2,
354 'daytime', 'varchar', 'NULL', 20,
355 'night', 'varchar', 'NULL', 20,
356 'fax', 'varchar', 'NULL', 12,
357 'payby', 'char', '', 4,
358 'payinfo', 'varchar', 'NULL', 16,
359 'paydate', @date_type,
360 'payname', 'varchar', 'NULL', $char_d,
361 'tax', 'char', 'NULL', 1,
362 'otaker', 'varchar', '', 8,
363 'refnum', 'int', '', '',
365 'primary_key' => 'custnum',
367 #'index' => [ ['last'], ['company'] ],
368 'index' => [ ['last'], ],
371 'cust_main_county' => { #county+state+country are checked off the
372 #cust_main_county for validation and to provide
375 'taxnum', 'int', '', '',
376 'state', 'char', 'NULL', $char_d,
377 'county', 'varchar', 'NULL', $char_d,
378 'country', 'char', '', 2,
379 'tax', 'real', '', '', #tax %
381 'primary_key' => 'taxnum',
383 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
389 'paynum', 'int', '', '',
390 'invnum', 'int', '', '',
393 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
394 # payment type table.
395 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
396 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
398 'primary_key' => 'paynum',
400 'index' => [ ['invnum'] ],
403 'cust_pay_batch' => { #what's this used for again? list of customers
404 #in current CARD batch? (necessarily CARD?)
406 'invnum', 'int', '', '',
407 'custnum', 'int', '', '',
408 'last', 'varchar', '', $char_d,
409 'first', 'varchar', '', $char_d,
410 'address1', 'varchar', '', $char_d,
411 'address2', 'varchar', 'NULL', $char_d,
412 'city', 'varchar', '', $char_d,
413 'state', 'char', '', 2,
414 'zip', 'varchar', '', 10,
415 'country', 'char', '', 2,
416 'trancode', 'int', '', '',
417 'cardnum', 'varchar', '', 16,
419 'payname', 'varchar', 'NULL', $char_d,
420 'amount', @money_type,
424 'index' => [ ['invnum'], ['custnum'] ],
429 'pkgnum', 'int', '', '',
430 'custnum', 'int', '', '',
431 'pkgpart', 'int', '', '',
432 'otaker', 'varchar', '', 8,
436 'cancel', @date_type,
437 'expire', @date_type,
439 'primary_key' => 'pkgnum',
441 'index' => [ ['custnum'] ],
446 'refundnum', 'int', '', '',
447 'crednum', 'int', '', '',
449 'refund', @money_type,
450 'otaker', 'varchar', '', 8,
451 'reason', 'varchar', '', $char_d,
452 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
453 # into payment type table.
454 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
456 'primary_key' => 'refundnum',
458 'index' => [ ['crednum'] ],
463 'svcnum', 'int', '', '',
464 'pkgnum', 'int', '', '',
465 'svcpart', 'int', '', '',
467 'primary_key' => 'svcnum',
469 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
474 'pkgpart', 'int', '', '',
475 'pkg', 'varchar', '', $char_d,
476 'comment', 'varchar', '', $char_d,
478 'freq', 'int', '', '', #billing frequency (months)
481 'primary_key' => 'pkgpart',
488 'titlenum', 'int', '', '',
489 'title', 'varchar', '', $char_d,
491 'primary_key' => 'titlenum',
498 'pkgpart', 'int', '', '',
499 'svcpart', 'int', '', '',
500 'quantity', 'int', '', '',
503 'unique' => [ ['pkgpart', 'svcpart'] ],
504 'index' => [ ['pkgpart'] ],
509 'refnum', 'int', '', '',
510 'referral', 'varchar', '', $char_d,
512 'primary_key' => 'refnum',
519 'svcpart', 'int', '', '',
520 'svc', 'varchar', '', $char_d,
521 'svcdb', 'varchar', '', $char_d,
523 'primary_key' => 'svcpart',
528 #(this should be renamed to part_pop)
531 'popnum', 'int', '', '',
532 'city', 'varchar', '', $char_d,
533 'state', 'char', '', 2,
535 'exch', 'char', '', 3,
538 'primary_key' => 'popnum',
545 'svcnum', 'int', '', '',
546 'username', 'varchar', '', $username_len, #unique (& remove dup code)
547 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
548 'popnum', 'int', 'NULL', '',
549 'uid', 'int', 'NULL', '',
550 'gid', 'int', 'NULL', '',
551 'finger', 'varchar', 'NULL', $char_d,
552 'dir', 'varchar', 'NULL', $char_d,
553 'shell', 'varchar', 'NULL', $char_d,
554 'quota', 'varchar', 'NULL', $char_d,
555 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
557 'primary_key' => 'svcnum',
559 'index' => [ ['username'] ],
564 'svcnum', 'int', '', '',
565 'domsvc', 'int', '', '',
566 'domuid', 'int', '', '',
567 'domuser', 'varchar', '', $char_d,
569 'primary_key' => 'svcnum',
571 'index' => [ ['domsvc'], ['domuid'] ],
576 # 'svcnum', 'int', '', '',
577 # 'amount', @money_type,
579 # 'primary_key' => 'svcnum',
580 # 'unique' => [ [] ],
586 'svcnum', 'int', '', '',
587 'domain', 'varchar', '', $char_d,
589 'primary_key' => 'svcnum',
590 'unique' => [ ['domain'] ],
596 # 'svcnum', 'int', '', '',
597 # 'svcnum', 'int', '', '',
598 # 'svcnum', 'int', '', '',
599 # 'worker', 'varchar', '', $char_d,
600 # '_date', @date_type,
602 # 'primary_key' => 'svcnum',
603 # 'unique' => [ [] ],