3 # $Id: fs-setup,v 1.8 1998-12-15 04:33:27 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.8 1998-12-15 04:33:27 ivan
36 # dies if it isn't running as the freeside user
38 # Revision 1.7 1998/11/18 09:01:31 ivan
41 # Revision 1.6 1998/11/15 13:18:02 ivan
44 # Revision 1.5 1998/11/15 09:43:03 ivan
45 # update for new config file syntax, new adminsuidsetup
47 # Revision 1.4 1998/10/22 15:51:23 ivan
48 # also varchar with no length specified - postgresql fix broke mysql.
50 # Revision 1.3 1998/10/22 15:46:28 ivan
51 # now smallint is illegal, so remove that too.
54 #to delay loading dbdef until we're ready
55 BEGIN { $FS::Record::setup_hack = 1; }
60 use FS::UID qw(adminsuidsetup datasrc checkeuid);
62 use FS::cust_main_county;
64 croak "Not running uid freeside!" unless checkeuid();
66 my $user = shift or die &usage;
67 FS::UID::getsecrets $user;
69 #needs to match FS::Record
70 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
74 print "\nEnter the maximum username length: ";
75 my($username_len)=&getvalue;
77 print "\n\n", <<END, ":";
78 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
79 for each user. Enter any additional RADIUS attributes you need to track for
80 each user, separated by whitespace.
82 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
85 my($x)=scalar(<STDIN>);
92 my($char_d) = 80; #default maxlength for text fields
94 #my(@date_type) = ( 'timestamp', '', '' );
95 my(@date_type) = ( 'int', 'NULL', '' );
96 my(@perl_type) = ( 'varchar', 'NULL', 255 );
98 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
99 @money_type = ( 'money', '', '' );
101 @money_type = ( 'decimal', '', '10,2' );
105 # create a dbdef object from the old data structure
108 my(%tables)=&tables_hash_hack;
110 #turn it into objects
111 my($dbdef) = new FS::dbdef ( map {
113 while (@{$tables{$_}{'columns'}}) {
114 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
115 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
117 FS::dbdef_table->new(
119 $tables{$_}{'primary_key'},
120 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
121 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
122 FS::dbdef_unique->new($tables{$_}{'unique'}),
123 FS::dbdef_index->new($tables{$_}{'index'}),
128 #add radius attributes to svc_acct
130 my($svc_acct)=$dbdef->table('svc_acct');
133 foreach $attribute (@attributes) {
134 $svc_acct->addcolumn ( new FS::dbdef_column (
135 'radius_'. $attribute,
142 #make part_svc table (but now as object)
144 my($part_svc)=$dbdef->table('part_svc');
146 #because of svc_acct_pop
147 #foreach (grep /^svc_/, $dbdef->tables) {
148 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
149 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
150 my($table)=$dbdef->table($_);
152 foreach $col ( $table->columns ) {
153 next if $col =~ /^svcnum$/;
154 $part_svc->addcolumn( new FS::dbdef_column (
155 $table->name. '__' . $table->column($col)->name,
156 'varchar', #$table->column($col)->type,
158 $char_d, #$table->column($col)->length,
160 $part_svc->addcolumn ( new FS::dbdef_column (
161 $table->name. '__'. $table->column($col)->name . "_flag",
170 $dbdef->save($dbdef_file);
171 FS::Record::reload_dbdef($dbdef_file);
177 my($dbh)=adminsuidsetup $user;
183 foreach ($dbdef->tables) {
184 my($table)=$dbdef->table($_);
185 print "Creating $_...";
190 foreach $statement ($table->sql_create_table(datasrc)) {
191 #print $statement, "\n";
192 $dbh->do( $statement )
193 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
199 #not really sample data (and shouldn't default to US)
205 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
206 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
207 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
209 my($cust_main_county)=create FS::cust_main_county({
215 $error=$cust_main_county->insert;
216 die $error if $error;
219 #ISO 2-letter country codes (same as country TLDs) except US
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
232 my($cust_main_county)=create FS::cust_main_county({
237 $error=$cust_main_county->insert;
238 die $error if $error;
241 $dbh->disconnect or die $dbh->errstr;
244 die "Usage:\n fs-setup user\n";
248 # Now it becomes an object. much better.
250 sub tables_hash_hack {
252 #note that s/(date|change)/_$1/; to avoid keyword conflict.
253 #put a kludge in FS::Record to catch this or? (pry need some date-handling
256 my(%tables)=( #yech.}
260 'agentnum', 'int', '', '',
261 'agent', 'varchar', '', $char_d,
262 'typenum', 'int', '', '',
263 'freq', 'int', 'NULL', '',
266 'primary_key' => 'agentnum',
268 'index' => [ ['typenum'] ],
273 'typenum', 'int', '', '',
274 'atype', 'varchar', '', $char_d,
276 'primary_key' => 'typenum',
283 'typenum', 'int', '', '',
284 'pkgpart', 'int', '', '',
287 'unique' => [ ['typenum', 'pkgpart'] ],
288 'index' => [ ['typenum'] ],
293 'invnum', 'int', '', '',
294 'custnum', 'int', '', '',
296 'charged', @money_type,
298 'printed', 'int', '', '',
300 'primary_key' => 'invnum',
302 'index' => [ ['custnum'] ],
307 'pkgnum', 'int', '', '',
308 'invnum', 'int', '', '',
309 'setup', @money_type,
310 'recur', @money_type,
315 'unique' => [ ['pkgnum', 'invnum'] ],
316 'index' => [ ['invnum'] ],
321 'crednum', 'int', '', '',
322 'custnum', 'int', '', '',
324 'amount', @money_type,
325 'credited', @money_type,
326 'otaker', 'varchar', '', 8,
327 'reason', 'varchar', '', 255,
329 'primary_key' => 'crednum',
331 'index' => [ ['custnum'] ],
336 'custnum', 'int', '', '',
337 'agentnum', 'int', '', '',
338 'titlenum', 'int', 'NULL', '',
339 'last', 'varchar', '', $char_d,
340 'middle', 'varchar', 'NULL', $char_d,
341 'first', 'varchar', '', $char_d,
342 'ss', 'char', 'NULL', 11,
343 'company', 'varchar', 'NULL', $char_d,
344 'address1', 'varchar', '', $char_d,
345 'address2', 'varchar', 'NULL', $char_d,
346 'city', 'varchar', '', $char_d,
347 'county', 'varchar', 'NULL', $char_d,
348 'state', 'varchar', '', $char_d,
349 'zip', 'varchar', '', 10,
350 'country', 'char', '', 2,
351 'daytime', 'varchar', 'NULL', 20,
352 'night', 'varchar', 'NULL', 20,
353 'fax', 'varchar', 'NULL', 12,
354 'payby', 'char', '', 4,
355 'payinfo', 'varchar', 'NULL', 16,
356 'paydate', @date_type,
357 'payname', 'varchar', 'NULL', $char_d,
358 'tax', 'char', 'NULL', 1,
359 'otaker', 'varchar', '', 8,
360 'refnum', 'int', '', '',
362 'primary_key' => 'custnum',
364 #'index' => [ ['last'], ['company'] ],
365 'index' => [ ['last'], ],
368 'cust_main_county' => { #county+state+country are checked off the
369 #cust_main_county for validation and to provide
372 'taxnum', 'int', '', '',
373 'state', 'char', 'NULL', $char_d,
374 'county', 'varchar', 'NULL', $char_d,
375 'country', 'char', '', 2,
376 'tax', 'real', '', '', #tax %
378 'primary_key' => 'taxnum',
380 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
386 'paynum', 'int', '', '',
387 'invnum', 'int', '', '',
390 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
391 # payment type table.
392 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
393 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
395 'primary_key' => 'paynum',
397 'index' => [ ['invnum'] ],
400 'cust_pay_batch' => { #what's this used for again? list of customers
401 #in current CARD batch? (necessarily CARD?)
403 'invnum', 'int', '', '',
404 'custnum', 'int', '', '',
405 'last', 'varchar', '', $char_d,
406 'first', 'varchar', '', $char_d,
407 'address1', 'varchar', '', $char_d,
408 'address2', 'varchar', 'NULL', $char_d,
409 'city', 'varchar', '', $char_d,
410 'state', 'char', '', 2,
411 'zip', 'varchar', '', 10,
412 'country', 'char', '', 2,
413 'trancode', 'int', '', '',
414 'cardnum', 'varchar', '', 16,
416 'payname', 'varchar', 'NULL', $char_d,
417 'amount', @money_type,
421 'index' => [ ['invnum'], ['custnum'] ],
426 'pkgnum', 'int', '', '',
427 'custnum', 'int', '', '',
428 'pkgpart', 'int', '', '',
429 'otaker', 'varchar', '', 8,
433 'cancel', @date_type,
434 'expire', @date_type,
436 'primary_key' => 'pkgnum',
438 'index' => [ ['custnum'] ],
443 'refundnum', 'int', '', '',
444 'crednum', 'int', '', '',
446 'refund', @money_type,
447 'otaker', 'varchar', '', 8,
448 'reason', 'varchar', '', $char_d,
449 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
450 # into payment type table.
451 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
453 'primary_key' => 'refundnum',
455 'index' => [ ['crednum'] ],
460 'svcnum', 'int', '', '',
461 'pkgnum', 'int', '', '',
462 'svcpart', 'int', '', '',
464 'primary_key' => 'svcnum',
466 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
471 'pkgpart', 'int', '', '',
472 'pkg', 'varchar', '', $char_d,
473 'comment', 'varchar', '', $char_d,
475 'freq', 'int', '', '', #billing frequency (months)
478 'primary_key' => 'pkgpart',
485 'titlenum', 'int', '', '',
486 'title', 'varchar', '', $char_d,
488 'primary_key' => 'titlenum',
495 'pkgpart', 'int', '', '',
496 'svcpart', 'int', '', '',
497 'quantity', 'int', '', '',
500 'unique' => [ ['pkgpart', 'svcpart'] ],
501 'index' => [ ['pkgpart'] ],
506 'refnum', 'int', '', '',
507 'referral', 'varchar', '', $char_d,
509 'primary_key' => 'refnum',
516 'svcpart', 'int', '', '',
517 'svc', 'varchar', '', $char_d,
518 'svcdb', 'varchar', '', $char_d,
520 'primary_key' => 'svcpart',
525 #(this should be renamed to part_pop)
528 'popnum', 'int', '', '',
529 'city', 'varchar', '', $char_d,
530 'state', 'char', '', 2,
532 'exch', 'char', '', 3,
535 'primary_key' => 'popnum',
542 'svcnum', 'int', '', '',
543 'username', 'varchar', '', $username_len, #unique (& remove dup code)
544 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
545 'popnum', 'int', 'NULL', '',
546 'uid', 'int', 'NULL', '',
547 'gid', 'int', 'NULL', '',
548 'finger', 'varchar', 'NULL', $char_d,
549 'dir', 'varchar', 'NULL', $char_d,
550 'shell', 'varchar', 'NULL', $char_d,
551 'quota', 'varchar', 'NULL', $char_d,
552 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
554 'primary_key' => 'svcnum',
556 'index' => [ ['username'] ],
561 'svcnum', 'int', '', '',
562 'domsvc', 'int', '', '',
563 'domuid', 'int', '', '',
564 'domuser', 'varchar', '', $char_d,
566 'primary_key' => 'svcnum',
568 'index' => [ ['domsvc'], ['domuid'] ],
573 # 'svcnum', 'int', '', '',
574 # 'amount', @money_type,
576 # 'primary_key' => 'svcnum',
577 # 'unique' => [ [] ],
583 'svcnum', 'int', '', '',
584 'domain', 'varchar', '', $char_d,
586 'primary_key' => 'svcnum',
587 'unique' => [ ['domain'] ],
593 # 'svcnum', 'int', '', '',
594 # 'svcnum', 'int', '', '',
595 # 'svcnum', 'int', '', '',
596 # 'worker', 'varchar', '', $char_d,
597 # '_date', @date_type,
599 # 'primary_key' => 'svcnum',
600 # 'unique' => [ [] ],