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.4 1998-10-22 15:51:23 ivan
36 # also varchar with no length specified - postgresql fix broke mysql.
38 # Revision 1.3 1998/10/22 15:46:28 ivan
39 # now smallint is illegal, so remove that too.
42 #to delay loading dbdef until we're ready
43 BEGIN { $FS::Record::setup_hack = 1; }
48 use FS::UID qw(adminsuidsetup datasrc);
50 use FS::cust_main_county;
52 #needs to match FS::Record
53 my($dbdef_file) = "/var/spool/freeside/dbdef.". datasrc;
57 print "\nEnter the maximum username length: ";
58 my($username_len)=&getvalue;
60 print "\n\n", <<END, ":";
61 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
62 for each user. Enter any additional RADIUS attributes you need to track for
63 each user, separated by whitespace.
65 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
68 my($x)=scalar(<STDIN>);
75 my($char_d) = 80; #default maxlength for text fields
77 #my(@date_type) = ( 'timestamp', '', '' );
78 my(@date_type) = ( 'int', 'NULL', '' );
79 my(@perl_type) = ( 'varchar', 'NULL', 255 );
81 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
82 @money_type = ( 'money', '', '' );
84 @money_type = ( 'decimal', '', '10,2' );
88 # create a dbdef object from the old data structure
91 my(%tables)=&tables_hash_hack;
94 my($dbdef) = new FS::dbdef ( map {
96 while (@{$tables{$_}{'columns'}}) {
97 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
98 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
100 FS::dbdef_table->new(
102 $tables{$_}{'primary_key'},
103 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
104 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
105 FS::dbdef_unique->new($tables{$_}{'unique'}),
106 FS::dbdef_index->new($tables{$_}{'index'}),
111 #add radius attributes to svc_acct
113 my($svc_acct)=$dbdef->table('svc_acct');
116 foreach $attribute (@attributes) {
117 $svc_acct->addcolumn ( new FS::dbdef_column (
118 'radius_'. $attribute,
125 #make part_svc table (but now as object)
127 my($part_svc)=$dbdef->table('part_svc');
129 #because of svc_acct_pop
130 #foreach (grep /^svc_/, $dbdef->tables) {
131 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
132 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
133 my($table)=$dbdef->table($_);
135 foreach $col ( $table->columns ) {
136 next if $col =~ /^svcnum$/;
137 $part_svc->addcolumn( new FS::dbdef_column (
138 $table->name. '__' . $table->column($col)->name,
139 'varchar', #$table->column($col)->type,
141 $char_d, #$table->column($col)->length,
143 $part_svc->addcolumn ( new FS::dbdef_column (
144 $table->name. '__'. $table->column($col)->name . "_flag",
153 $dbdef->save($dbdef_file);
154 FS::Record::reload_dbdef;
160 my($dbh)=adminsuidsetup;
166 foreach ($dbdef->tables) {
167 my($table)=$dbdef->table($_);
168 print "Creating $_...";
173 foreach $statement ($table->sql_create_table(datasrc)) {
174 #print $statement, "\n";
175 $dbh->do( $statement )
176 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
182 #not really sample data (and shouldn't default to US)
186 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
187 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
188 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
190 my($cust_main_county)=create FS::cust_main_county({
195 $error=$cust_main_county->insert;
196 die $error if $error;
199 $dbh->disconnect or die $dbh->errstr;
202 # Now it becomes an object. much better.
204 sub tables_hash_hack {
206 #note that s/(date|change)/_$1/; to avoid keyword conflict.
207 #put a kludge in FS::Record to catch this or? (pry need some date-handling
210 my(%tables)=( #yech.}
214 'agentnum', 'int', '', '',
215 'agent', 'varchar', '', $char_d,
216 'typenum', 'int', '', '',
217 'freq', 'int', 'NULL', '',
220 'primary_key' => 'agentnum',
222 'index' => [ ['typenum'] ],
227 'typenum', 'int', '', '',
228 'atype', 'varchar', '', $char_d,
230 'primary_key' => 'typenum',
237 'typenum', 'int', '', '',
238 'pkgpart', 'int', '', '',
241 'unique' => [ ['typenum', 'pkgpart'] ],
242 'index' => [ ['typenum'] ],
247 'invnum', 'int', '', '',
248 'custnum', 'int', '', '',
250 'charged', @money_type,
252 'printed', 'int', '', '',
254 'primary_key' => 'invnum',
256 'index' => [ ['custnum'] ],
261 'pkgnum', 'int', '', '',
262 'invnum', 'int', '', '',
263 'setup', @money_type,
264 'recur', @money_type,
269 'unique' => [ ['pkgnum', 'invnum'] ],
270 'index' => [ ['invnum'] ],
275 'crednum', 'int', '', '',
276 'custnum', 'int', '', '',
278 'amount', @money_type,
279 'credited', @money_type,
280 'otaker', 'varchar', '', 8,
281 'reason', 'varchar', '', 255,
283 'primary_key' => 'crednum',
285 'index' => [ ['custnum'] ],
290 'custnum', 'int', '', '',
291 'agentnum', 'int', '', '',
292 'last', 'varchar', '', $char_d,
293 'first', 'varchar', '', $char_d,
294 'ss', 'char', 'NULL', 11,
295 'company', 'varchar', 'NULL', $char_d,
296 'address1', 'varchar', '', $char_d,
297 'address2', 'varchar', 'NULL', $char_d,
298 'city', 'varchar', '', $char_d,
299 'county', 'varchar', 'NULL', $char_d,
300 'state', 'char', '', 2,
301 'zip', 'varchar', '', 10,
302 'country', 'char', '', 2,
303 'daytime', 'varchar', 'NULL', 20,
304 'night', 'varchar', 'NULL', 20,
305 'fax', 'varchar', 'NULL', 12,
306 'payby', 'char', '', 4,
307 'payinfo', 'varchar', 'NULL', 16,
308 'paydate', @date_type,
309 'payname', 'varchar', 'NULL', $char_d,
310 'tax', 'char', 'NULL', 1,
311 'otaker', 'varchar', '', 8,
312 'refnum', 'int', '', '',
314 'primary_key' => 'custnum',
316 #'index' => [ ['last'], ['company'] ],
317 'index' => [ ['last'], ],
320 'cust_main_county' => { #county+state are checked off the cust_main_county
321 #table for validation and to provide a tax rate.
324 'taxnum', 'int', '', '',
325 'state', 'char', '', 2, #two letters max in US... elsewhere?
326 'county', 'varchar', 'NULL', $char_d,
327 'tax', 'real', '', '', #tax %
329 'primary_key' => 'taxnum',
331 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
337 'paynum', 'int', '', '',
338 'invnum', 'int', '', '',
341 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
342 # payment type table.
343 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
344 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
346 'primary_key' => 'paynum',
348 'index' => [ ['invnum'] ],
351 'cust_pay_batch' => { #what's this used for again? list of customers
352 #in current CARD batch? (necessarily CARD?)
354 'invnum', 'int', '', '',
355 'custnum', 'int', '', '',
356 'last', 'varchar', '', $char_d,
357 'first', 'varchar', '', $char_d,
358 'address1', 'varchar', '', $char_d,
359 'address2', 'varchar', 'NULL', $char_d,
360 'city', 'varchar', '', $char_d,
361 'state', 'char', '', 2,
362 'zip', 'varchar', '', 10,
363 'country', 'char', '', 2,
364 'trancode', 'int', '', '',
365 'cardnum', 'varchar', '', 16,
367 'payname', 'varchar', 'NULL', $char_d,
368 'amount', @money_type,
372 'index' => [ ['invnum'], ['custnum'] ],
377 'pkgnum', 'int', '', '',
378 'custnum', 'int', '', '',
379 'pkgpart', 'int', '', '',
380 'otaker', 'varchar', '', 8,
384 'cancel', @date_type,
385 'expire', @date_type,
387 'primary_key' => 'pkgnum',
389 'index' => [ ['custnum'] ],
394 'refundnum', 'int', '', '',
395 'crednum', 'int', '', '',
397 'refund', @money_type,
398 'otaker', 'varchar', '', 8,
399 'reason', 'varchar', '', $char_d,
400 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
401 # into payment type table.
402 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
404 'primary_key' => 'refundnum',
406 'index' => [ ['crednum'] ],
411 'svcnum', 'int', '', '',
412 'pkgnum', 'int', '', '',
413 'svcpart', 'int', '', '',
415 'primary_key' => 'svcnum',
417 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
422 'pkgpart', 'int', '', '',
423 'pkg', 'varchar', '', $char_d,
424 'comment', 'varchar', '', $char_d,
426 'freq', 'int', '', '', #billing frequency (months)
429 'primary_key' => 'pkgpart',
436 'pkgpart', 'int', '', '',
437 'svcpart', 'int', '', '',
438 'quantity', 'int', '', '',
441 'unique' => [ ['pkgpart', 'svcpart'] ],
442 'index' => [ ['pkgpart'] ],
447 'refnum', 'int', '', '',
448 'referral', 'varchar', '', $char_d,
450 'primary_key' => 'refnum',
457 'svcpart', 'int', '', '',
458 'svc', 'varchar', '', $char_d,
459 'svcdb', 'varchar', '', $char_d,
461 'primary_key' => 'svcpart',
466 #(this should be renamed to part_pop)
469 'popnum', 'int', '', '',
470 'city', 'varchar', '', $char_d,
471 'state', 'char', '', 2,
473 'exch', 'char', '', 3,
476 'primary_key' => 'popnum',
483 'svcnum', 'int', '', '',
484 'username', 'varchar', '', $username_len, #unique (& remove dup code)
485 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
486 'popnum', 'int', 'NULL', '',
487 'uid', 'int', 'NULL', '',
488 'gid', 'int', 'NULL', '',
489 'finger', 'varchar', 'NULL', $char_d,
490 'dir', 'varchar', 'NULL', $char_d,
491 'shell', 'varchar', 'NULL', $char_d,
492 'quota', 'varchar', 'NULL', $char_d,
493 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
495 'primary_key' => 'svcnum',
497 'index' => [ ['username'] ],
502 'svcnum', 'int', '', '',
503 'domsvc', 'int', '', '',
504 'domuid', 'int', '', '',
505 'domuser', 'varchar', '', $char_d,
507 'primary_key' => 'svcnum',
509 'index' => [ ['domsvc'], ['domuid'] ],
514 # 'svcnum', 'int', '', '',
515 # 'amount', @money_type,
517 # 'primary_key' => 'svcnum',
518 # 'unique' => [ [] ],
524 'svcnum', 'int', '', '',
525 'domain', 'varchar', '', $char_d,
527 'primary_key' => 'svcnum',
528 'unique' => [ ['domain'] ],
534 # 'svcnum', 'int', '', '',
535 # 'svcnum', 'int', '', '',
536 # 'svcnum', 'int', '', '',
537 # 'worker', 'varchar', '', $char_d,
538 # '_date', @date_type,
540 # 'primary_key' => 'svcnum',
541 # 'unique' => [ [] ],