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.6 1998-11-15 13:18:02 ivan
38 # Revision 1.5 1998/11/15 09:43:03 ivan
39 # update for new config file syntax, new adminsuidsetup
41 # Revision 1.4 1998/10/22 15:51:23 ivan
42 # also varchar with no length specified - postgresql fix broke mysql.
44 # Revision 1.3 1998/10/22 15:46:28 ivan
45 # now smallint is illegal, so remove that too.
48 #to delay loading dbdef until we're ready
49 BEGIN { $FS::Record::setup_hack = 1; }
54 use FS::UID qw(adminsuidsetup datasrc);
56 use FS::cust_main_county;
58 my $user = shift or die &usage;
59 FS::UID::getsecrets $user;
61 #needs to match FS::Record
62 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
66 print "\nEnter the maximum username length: ";
67 my($username_len)=&getvalue;
69 print "\n\n", <<END, ":";
70 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
71 for each user. Enter any additional RADIUS attributes you need to track for
72 each user, separated by whitespace.
74 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
77 my($x)=scalar(<STDIN>);
84 my($char_d) = 80; #default maxlength for text fields
86 #my(@date_type) = ( 'timestamp', '', '' );
87 my(@date_type) = ( 'int', 'NULL', '' );
88 my(@perl_type) = ( 'varchar', 'NULL', 255 );
90 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
91 @money_type = ( 'money', '', '' );
93 @money_type = ( 'decimal', '', '10,2' );
97 # create a dbdef object from the old data structure
100 my(%tables)=&tables_hash_hack;
102 #turn it into objects
103 my($dbdef) = new FS::dbdef ( map {
105 while (@{$tables{$_}{'columns'}}) {
106 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
107 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
109 FS::dbdef_table->new(
111 $tables{$_}{'primary_key'},
112 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
113 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
114 FS::dbdef_unique->new($tables{$_}{'unique'}),
115 FS::dbdef_index->new($tables{$_}{'index'}),
120 #add radius attributes to svc_acct
122 my($svc_acct)=$dbdef->table('svc_acct');
125 foreach $attribute (@attributes) {
126 $svc_acct->addcolumn ( new FS::dbdef_column (
127 'radius_'. $attribute,
134 #make part_svc table (but now as object)
136 my($part_svc)=$dbdef->table('part_svc');
138 #because of svc_acct_pop
139 #foreach (grep /^svc_/, $dbdef->tables) {
140 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
141 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
142 my($table)=$dbdef->table($_);
144 foreach $col ( $table->columns ) {
145 next if $col =~ /^svcnum$/;
146 $part_svc->addcolumn( new FS::dbdef_column (
147 $table->name. '__' . $table->column($col)->name,
148 'varchar', #$table->column($col)->type,
150 $char_d, #$table->column($col)->length,
152 $part_svc->addcolumn ( new FS::dbdef_column (
153 $table->name. '__'. $table->column($col)->name . "_flag",
162 $dbdef->save($dbdef_file);
163 FS::Record::reload_dbdef($dbdef_file);
169 my($dbh)=adminsuidsetup $user;
175 foreach ($dbdef->tables) {
176 my($table)=$dbdef->table($_);
177 print "Creating $_...";
182 foreach $statement ($table->sql_create_table(datasrc)) {
183 #print $statement, "\n";
184 $dbh->do( $statement )
185 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
191 #not really sample data (and shouldn't default to US)
195 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
196 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
197 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
199 my($cust_main_county)=create FS::cust_main_county({
204 $error=$cust_main_county->insert;
205 die $error if $error;
208 $dbh->disconnect or die $dbh->errstr;
211 die "Usage:\n fs-setup user\n";
215 # Now it becomes an object. much better.
217 sub tables_hash_hack {
219 #note that s/(date|change)/_$1/; to avoid keyword conflict.
220 #put a kludge in FS::Record to catch this or? (pry need some date-handling
223 my(%tables)=( #yech.}
227 'agentnum', 'int', '', '',
228 'agent', 'varchar', '', $char_d,
229 'typenum', 'int', '', '',
230 'freq', 'int', 'NULL', '',
233 'primary_key' => 'agentnum',
235 'index' => [ ['typenum'] ],
240 'typenum', 'int', '', '',
241 'atype', 'varchar', '', $char_d,
243 'primary_key' => 'typenum',
250 'typenum', 'int', '', '',
251 'pkgpart', 'int', '', '',
254 'unique' => [ ['typenum', 'pkgpart'] ],
255 'index' => [ ['typenum'] ],
260 'invnum', 'int', '', '',
261 'custnum', 'int', '', '',
263 'charged', @money_type,
265 'printed', 'int', '', '',
267 'primary_key' => 'invnum',
269 'index' => [ ['custnum'] ],
274 'pkgnum', 'int', '', '',
275 'invnum', 'int', '', '',
276 'setup', @money_type,
277 'recur', @money_type,
282 'unique' => [ ['pkgnum', 'invnum'] ],
283 'index' => [ ['invnum'] ],
288 'crednum', 'int', '', '',
289 'custnum', 'int', '', '',
291 'amount', @money_type,
292 'credited', @money_type,
293 'otaker', 'varchar', '', 8,
294 'reason', 'varchar', '', 255,
296 'primary_key' => 'crednum',
298 'index' => [ ['custnum'] ],
303 'custnum', 'int', '', '',
304 'agentnum', 'int', '', '',
305 'last', 'varchar', '', $char_d,
306 'first', 'varchar', '', $char_d,
307 'ss', 'char', 'NULL', 11,
308 'company', 'varchar', 'NULL', $char_d,
309 'address1', 'varchar', '', $char_d,
310 'address2', 'varchar', 'NULL', $char_d,
311 'city', 'varchar', '', $char_d,
312 'county', 'varchar', 'NULL', $char_d,
313 'state', 'char', '', 2,
314 'zip', 'varchar', '', 10,
315 'country', 'char', '', 2,
316 'daytime', 'varchar', 'NULL', 20,
317 'night', 'varchar', 'NULL', 20,
318 'fax', 'varchar', 'NULL', 12,
319 'payby', 'char', '', 4,
320 'payinfo', 'varchar', 'NULL', 16,
321 'paydate', @date_type,
322 'payname', 'varchar', 'NULL', $char_d,
323 'tax', 'char', 'NULL', 1,
324 'otaker', 'varchar', '', 8,
325 'refnum', 'int', '', '',
327 'primary_key' => 'custnum',
329 #'index' => [ ['last'], ['company'] ],
330 'index' => [ ['last'], ],
333 'cust_main_county' => { #county+state are checked off the cust_main_county
334 #table for validation and to provide a tax rate.
337 'taxnum', 'int', '', '',
338 'state', 'char', '', 2, #two letters max in US... elsewhere?
339 'county', 'varchar', 'NULL', $char_d,
340 'tax', 'real', '', '', #tax %
342 'primary_key' => 'taxnum',
344 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
350 'paynum', 'int', '', '',
351 'invnum', 'int', '', '',
354 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
355 # payment type table.
356 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
357 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
359 'primary_key' => 'paynum',
361 'index' => [ ['invnum'] ],
364 'cust_pay_batch' => { #what's this used for again? list of customers
365 #in current CARD batch? (necessarily CARD?)
367 'invnum', 'int', '', '',
368 'custnum', 'int', '', '',
369 'last', 'varchar', '', $char_d,
370 'first', 'varchar', '', $char_d,
371 'address1', 'varchar', '', $char_d,
372 'address2', 'varchar', 'NULL', $char_d,
373 'city', 'varchar', '', $char_d,
374 'state', 'char', '', 2,
375 'zip', 'varchar', '', 10,
376 'country', 'char', '', 2,
377 'trancode', 'int', '', '',
378 'cardnum', 'varchar', '', 16,
380 'payname', 'varchar', 'NULL', $char_d,
381 'amount', @money_type,
385 'index' => [ ['invnum'], ['custnum'] ],
390 'pkgnum', 'int', '', '',
391 'custnum', 'int', '', '',
392 'pkgpart', 'int', '', '',
393 'otaker', 'varchar', '', 8,
397 'cancel', @date_type,
398 'expire', @date_type,
400 'primary_key' => 'pkgnum',
402 'index' => [ ['custnum'] ],
407 'refundnum', 'int', '', '',
408 'crednum', 'int', '', '',
410 'refund', @money_type,
411 'otaker', 'varchar', '', 8,
412 'reason', 'varchar', '', $char_d,
413 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
414 # into payment type table.
415 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
417 'primary_key' => 'refundnum',
419 'index' => [ ['crednum'] ],
424 'svcnum', 'int', '', '',
425 'pkgnum', 'int', '', '',
426 'svcpart', 'int', '', '',
428 'primary_key' => 'svcnum',
430 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
435 'pkgpart', 'int', '', '',
436 'pkg', 'varchar', '', $char_d,
437 'comment', 'varchar', '', $char_d,
439 'freq', 'int', '', '', #billing frequency (months)
442 'primary_key' => 'pkgpart',
449 'pkgpart', 'int', '', '',
450 'svcpart', 'int', '', '',
451 'quantity', 'int', '', '',
454 'unique' => [ ['pkgpart', 'svcpart'] ],
455 'index' => [ ['pkgpart'] ],
460 'refnum', 'int', '', '',
461 'referral', 'varchar', '', $char_d,
463 'primary_key' => 'refnum',
470 'svcpart', 'int', '', '',
471 'svc', 'varchar', '', $char_d,
472 'svcdb', 'varchar', '', $char_d,
474 'primary_key' => 'svcpart',
479 #(this should be renamed to part_pop)
482 'popnum', 'int', '', '',
483 'city', 'varchar', '', $char_d,
484 'state', 'char', '', 2,
486 'exch', 'char', '', 3,
489 'primary_key' => 'popnum',
496 'svcnum', 'int', '', '',
497 'username', 'varchar', '', $username_len, #unique (& remove dup code)
498 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
499 'popnum', 'int', 'NULL', '',
500 'uid', 'int', 'NULL', '',
501 'gid', 'int', 'NULL', '',
502 'finger', 'varchar', 'NULL', $char_d,
503 'dir', 'varchar', 'NULL', $char_d,
504 'shell', 'varchar', 'NULL', $char_d,
505 'quota', 'varchar', 'NULL', $char_d,
506 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
508 'primary_key' => 'svcnum',
510 'index' => [ ['username'] ],
515 'svcnum', 'int', '', '',
516 'domsvc', 'int', '', '',
517 'domuid', 'int', '', '',
518 'domuser', 'varchar', '', $char_d,
520 'primary_key' => 'svcnum',
522 'index' => [ ['domsvc'], ['domuid'] ],
527 # 'svcnum', 'int', '', '',
528 # 'amount', @money_type,
530 # 'primary_key' => 'svcnum',
531 # 'unique' => [ [] ],
537 'svcnum', 'int', '', '',
538 'domain', 'varchar', '', $char_d,
540 'primary_key' => 'svcnum',
541 'unique' => [ ['domain'] ],
547 # 'svcnum', 'int', '', '',
548 # 'svcnum', 'int', '', '',
549 # 'svcnum', 'int', '', '',
550 # 'worker', 'varchar', '', $char_d,
551 # '_date', @date_type,
553 # 'primary_key' => 'svcnum',
554 # 'unique' => [ [] ],