3 # create database and necessary tables, etc. DBI version.
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.7 1998-11-18 09:01:31 ivan
38 # Revision 1.6 1998/11/15 13:18:02 ivan
41 # Revision 1.5 1998/11/15 09:43:03 ivan
42 # update for new config file syntax, new adminsuidsetup
44 # Revision 1.4 1998/10/22 15:51:23 ivan
45 # also varchar with no length specified - postgresql fix broke mysql.
47 # Revision 1.3 1998/10/22 15:46:28 ivan
48 # now smallint is illegal, so remove that too.
51 #to delay loading dbdef until we're ready
52 BEGIN { $FS::Record::setup_hack = 1; }
57 use FS::UID qw(adminsuidsetup datasrc);
59 use FS::cust_main_county;
61 my $user = shift or die &usage;
62 FS::UID::getsecrets $user;
64 #needs to match FS::Record
65 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
69 print "\nEnter the maximum username length: ";
70 my($username_len)=&getvalue;
72 print "\n\n", <<END, ":";
73 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
74 for each user. Enter any additional RADIUS attributes you need to track for
75 each user, separated by whitespace.
77 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
80 my($x)=scalar(<STDIN>);
87 my($char_d) = 80; #default maxlength for text fields
89 #my(@date_type) = ( 'timestamp', '', '' );
90 my(@date_type) = ( 'int', 'NULL', '' );
91 my(@perl_type) = ( 'varchar', 'NULL', 255 );
93 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
94 @money_type = ( 'money', '', '' );
96 @money_type = ( 'decimal', '', '10,2' );
100 # create a dbdef object from the old data structure
103 my(%tables)=&tables_hash_hack;
105 #turn it into objects
106 my($dbdef) = new FS::dbdef ( map {
108 while (@{$tables{$_}{'columns'}}) {
109 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
110 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
112 FS::dbdef_table->new(
114 $tables{$_}{'primary_key'},
115 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
116 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
117 FS::dbdef_unique->new($tables{$_}{'unique'}),
118 FS::dbdef_index->new($tables{$_}{'index'}),
123 #add radius attributes to svc_acct
125 my($svc_acct)=$dbdef->table('svc_acct');
128 foreach $attribute (@attributes) {
129 $svc_acct->addcolumn ( new FS::dbdef_column (
130 'radius_'. $attribute,
137 #make part_svc table (but now as object)
139 my($part_svc)=$dbdef->table('part_svc');
141 #because of svc_acct_pop
142 #foreach (grep /^svc_/, $dbdef->tables) {
143 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
144 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
145 my($table)=$dbdef->table($_);
147 foreach $col ( $table->columns ) {
148 next if $col =~ /^svcnum$/;
149 $part_svc->addcolumn( new FS::dbdef_column (
150 $table->name. '__' . $table->column($col)->name,
151 'varchar', #$table->column($col)->type,
153 $char_d, #$table->column($col)->length,
155 $part_svc->addcolumn ( new FS::dbdef_column (
156 $table->name. '__'. $table->column($col)->name . "_flag",
165 $dbdef->save($dbdef_file);
166 FS::Record::reload_dbdef($dbdef_file);
172 my($dbh)=adminsuidsetup $user;
178 foreach ($dbdef->tables) {
179 my($table)=$dbdef->table($_);
180 print "Creating $_...";
185 foreach $statement ($table->sql_create_table(datasrc)) {
186 #print $statement, "\n";
187 $dbh->do( $statement )
188 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
194 #not really sample data (and shouldn't default to US)
200 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
201 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
202 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
204 my($cust_main_county)=create FS::cust_main_county({
210 $error=$cust_main_county->insert;
211 die $error if $error;
214 #ISO 2-letter country codes (same as country TLDs) except US
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
227 my($cust_main_county)=create FS::cust_main_county({
232 $error=$cust_main_county->insert;
233 die $error if $error;
236 $dbh->disconnect or die $dbh->errstr;
239 die "Usage:\n fs-setup user\n";
243 # Now it becomes an object. much better.
245 sub tables_hash_hack {
247 #note that s/(date|change)/_$1/; to avoid keyword conflict.
248 #put a kludge in FS::Record to catch this or? (pry need some date-handling
251 my(%tables)=( #yech.}
255 'agentnum', 'int', '', '',
256 'agent', 'varchar', '', $char_d,
257 'typenum', 'int', '', '',
258 'freq', 'int', 'NULL', '',
261 'primary_key' => 'agentnum',
263 'index' => [ ['typenum'] ],
268 'typenum', 'int', '', '',
269 'atype', 'varchar', '', $char_d,
271 'primary_key' => 'typenum',
278 'typenum', 'int', '', '',
279 'pkgpart', 'int', '', '',
282 'unique' => [ ['typenum', 'pkgpart'] ],
283 'index' => [ ['typenum'] ],
288 'invnum', 'int', '', '',
289 'custnum', 'int', '', '',
291 'charged', @money_type,
293 'printed', 'int', '', '',
295 'primary_key' => 'invnum',
297 'index' => [ ['custnum'] ],
302 'pkgnum', 'int', '', '',
303 'invnum', 'int', '', '',
304 'setup', @money_type,
305 'recur', @money_type,
310 'unique' => [ ['pkgnum', 'invnum'] ],
311 'index' => [ ['invnum'] ],
316 'crednum', 'int', '', '',
317 'custnum', 'int', '', '',
319 'amount', @money_type,
320 'credited', @money_type,
321 'otaker', 'varchar', '', 8,
322 'reason', 'varchar', '', 255,
324 'primary_key' => 'crednum',
326 'index' => [ ['custnum'] ],
331 'custnum', 'int', '', '',
332 'agentnum', 'int', '', '',
333 'titlenum', 'int', 'NULL', '',
334 'last', 'varchar', '', $char_d,
335 'middle', 'varchar', 'NULL', $char_d,
336 'first', 'varchar', '', $char_d,
337 'ss', 'char', 'NULL', 11,
338 'company', 'varchar', 'NULL', $char_d,
339 'address1', 'varchar', '', $char_d,
340 'address2', 'varchar', 'NULL', $char_d,
341 'city', 'varchar', '', $char_d,
342 'county', 'varchar', 'NULL', $char_d,
343 'state', 'varchar', '', $char_d,
344 'zip', 'varchar', '', 10,
345 'country', 'char', '', 2,
346 'daytime', 'varchar', 'NULL', 20,
347 'night', 'varchar', 'NULL', 20,
348 'fax', 'varchar', 'NULL', 12,
349 'payby', 'char', '', 4,
350 'payinfo', 'varchar', 'NULL', 16,
351 'paydate', @date_type,
352 'payname', 'varchar', 'NULL', $char_d,
353 'tax', 'char', 'NULL', 1,
354 'otaker', 'varchar', '', 8,
355 'refnum', 'int', '', '',
357 'primary_key' => 'custnum',
359 #'index' => [ ['last'], ['company'] ],
360 'index' => [ ['last'], ],
363 'cust_main_county' => { #county+state+country are checked off the
364 #cust_main_county for validation and to provide
367 'taxnum', 'int', '', '',
368 'state', 'char', 'NULL', $char_d,
369 'county', 'varchar', 'NULL', $char_d,
370 'country', 'char', '', 2,
371 'tax', 'real', '', '', #tax %
373 'primary_key' => 'taxnum',
375 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
381 'paynum', 'int', '', '',
382 'invnum', 'int', '', '',
385 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
386 # payment type table.
387 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
388 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
390 'primary_key' => 'paynum',
392 'index' => [ ['invnum'] ],
395 'cust_pay_batch' => { #what's this used for again? list of customers
396 #in current CARD batch? (necessarily CARD?)
398 'invnum', 'int', '', '',
399 'custnum', 'int', '', '',
400 'last', 'varchar', '', $char_d,
401 'first', 'varchar', '', $char_d,
402 'address1', 'varchar', '', $char_d,
403 'address2', 'varchar', 'NULL', $char_d,
404 'city', 'varchar', '', $char_d,
405 'state', 'char', '', 2,
406 'zip', 'varchar', '', 10,
407 'country', 'char', '', 2,
408 'trancode', 'int', '', '',
409 'cardnum', 'varchar', '', 16,
411 'payname', 'varchar', 'NULL', $char_d,
412 'amount', @money_type,
416 'index' => [ ['invnum'], ['custnum'] ],
421 'pkgnum', 'int', '', '',
422 'custnum', 'int', '', '',
423 'pkgpart', 'int', '', '',
424 'otaker', 'varchar', '', 8,
428 'cancel', @date_type,
429 'expire', @date_type,
431 'primary_key' => 'pkgnum',
433 'index' => [ ['custnum'] ],
438 'refundnum', 'int', '', '',
439 'crednum', 'int', '', '',
441 'refund', @money_type,
442 'otaker', 'varchar', '', 8,
443 'reason', 'varchar', '', $char_d,
444 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
445 # into payment type table.
446 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
448 'primary_key' => 'refundnum',
450 'index' => [ ['crednum'] ],
455 'svcnum', 'int', '', '',
456 'pkgnum', 'int', '', '',
457 'svcpart', 'int', '', '',
459 'primary_key' => 'svcnum',
461 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
466 'pkgpart', 'int', '', '',
467 'pkg', 'varchar', '', $char_d,
468 'comment', 'varchar', '', $char_d,
470 'freq', 'int', '', '', #billing frequency (months)
473 'primary_key' => 'pkgpart',
480 'titlenum', 'int', '', '',
481 'title', 'varchar', '', $char_d,
483 'primary_key' => 'titlenum',
490 'pkgpart', 'int', '', '',
491 'svcpart', 'int', '', '',
492 'quantity', 'int', '', '',
495 'unique' => [ ['pkgpart', 'svcpart'] ],
496 'index' => [ ['pkgpart'] ],
501 'refnum', 'int', '', '',
502 'referral', 'varchar', '', $char_d,
504 'primary_key' => 'refnum',
511 'svcpart', 'int', '', '',
512 'svc', 'varchar', '', $char_d,
513 'svcdb', 'varchar', '', $char_d,
515 'primary_key' => 'svcpart',
520 #(this should be renamed to part_pop)
523 'popnum', 'int', '', '',
524 'city', 'varchar', '', $char_d,
525 'state', 'char', '', 2,
527 'exch', 'char', '', 3,
530 'primary_key' => 'popnum',
537 'svcnum', 'int', '', '',
538 'username', 'varchar', '', $username_len, #unique (& remove dup code)
539 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
540 'popnum', 'int', 'NULL', '',
541 'uid', 'int', 'NULL', '',
542 'gid', 'int', 'NULL', '',
543 'finger', 'varchar', 'NULL', $char_d,
544 'dir', 'varchar', 'NULL', $char_d,
545 'shell', 'varchar', 'NULL', $char_d,
546 'quota', 'varchar', 'NULL', $char_d,
547 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
549 'primary_key' => 'svcnum',
551 'index' => [ ['username'] ],
556 'svcnum', 'int', '', '',
557 'domsvc', 'int', '', '',
558 'domuid', 'int', '', '',
559 'domuser', 'varchar', '', $char_d,
561 'primary_key' => 'svcnum',
563 'index' => [ ['domsvc'], ['domuid'] ],
568 # 'svcnum', 'int', '', '',
569 # 'amount', @money_type,
571 # 'primary_key' => 'svcnum',
572 # 'unique' => [ [] ],
578 'svcnum', 'int', '', '',
579 'domain', 'varchar', '', $char_d,
581 'primary_key' => 'svcnum',
582 'unique' => [ ['domain'] ],
588 # 'svcnum', 'int', '', '',
589 # 'svcnum', 'int', '', '',
590 # 'svcnum', 'int', '', '',
591 # 'worker', 'varchar', '', $char_d,
592 # '_date', @date_type,
594 # 'primary_key' => 'svcnum',
595 # 'unique' => [ [] ],