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
34 #to delay loading dbdef until we're ready
35 BEGIN { $FS::Record::setup_hack = 1; }
40 use FS::UID qw(adminsuidsetup datasrc);
42 use FS::cust_main_county;
44 #needs to match FS::Record
45 my($dbdef_file) = "/var/spool/freeside/dbdef.". datasrc;
49 print "\nEnter the maximum username length: ";
50 my($username_len)=&getvalue;
52 print "\n\n", <<END, ":";
53 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
54 for each user. Enter any additional RADIUS attributes you need to track for
55 each user, separated by whitespace.
57 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
60 my($x)=scalar(<STDIN>);
67 my($char_d) = 80; #default maxlength for text fields
69 #my(@date_type) = ( 'timestamp', '', '' );
70 my(@date_type) = ( 'int', 'NULL', '' );
71 my(@perl_type) = ( 'long varchar', 'NULL', '' );
73 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
74 @money_type = ( 'money', '', '' );
76 @money_type = ( 'decimal', '', '10,2' );
80 # create a dbdef object from the old data structure
83 my(%tables)=&tables_hash_hack;
86 my($dbdef) = new FS::dbdef ( map {
88 while (@{$tables{$_}{'columns'}}) {
89 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
90 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
94 $tables{$_}{'primary_key'},
95 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
96 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
97 FS::dbdef_unique->new($tables{$_}{'unique'}),
98 FS::dbdef_index->new($tables{$_}{'index'}),
103 #add radius attributes to svc_acct
105 my($svc_acct)=$dbdef->table('svc_acct');
108 foreach $attribute (@attributes) {
109 $svc_acct->addcolumn ( new FS::dbdef_column (
110 'radius_'. $attribute,
117 #make part_svc table (but now as object)
119 my($part_svc)=$dbdef->table('part_svc');
121 #because of svc_acct_pop
122 #foreach (grep /^svc_/, $dbdef->tables) {
123 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
124 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
125 my($table)=$dbdef->table($_);
127 foreach $col ( $table->columns ) {
128 next if $col =~ /^svcnum$/;
129 $part_svc->addcolumn( new FS::dbdef_column (
130 $table->name. '__' . $table->column($col)->name,
131 'varchar', #$table->column($col)->type,
133 $char_d, #$table->column($col)->length,
135 $part_svc->addcolumn ( new FS::dbdef_column (
136 $table->name. '__'. $table->column($col)->name . "_flag",
145 $dbdef->save($dbdef_file);
146 FS::Record::reload_dbdef;
152 my($dbh)=adminsuidsetup;
158 foreach ($dbdef->tables) {
159 my($table)=$dbdef->table($_);
160 print "Creating $_...";
165 foreach $statement ($table->sql_create_table(datasrc)) {
166 #print $statement, "\n";
167 $dbh->do( $statement )
168 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
174 #not really sample data (and shouldn't default to US)
178 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
179 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
180 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
182 my($cust_main_county)=create FS::cust_main_county({
187 $error=$cust_main_county->insert;
188 die $error if $error;
191 $dbh->disconnect or die $dbh->errstr;
194 # Now it becomes an object. much better.
196 sub tables_hash_hack {
198 #note that s/(date|change)/_$1/; to avoid keyword conflict.
199 #put a kludge in FS::Record to catch this or? (pry need some date-handling
202 my(%tables)=( #yech.}
206 'agentnum', 'int', '', '',
207 'agent', 'varchar', '', $char_d,
208 'typenum', 'int', '', '',
209 'freq', 'smallint', 'NULL', '',
212 'primary_key' => 'agentnum',
214 'index' => [ ['typenum'] ],
219 'typenum', 'int', '', '',
220 'atype', 'varchar', '', $char_d,
222 'primary_key' => 'typenum',
229 'typenum', 'int', '', '',
230 'pkgpart', 'int', '', '',
233 'unique' => [ ['typenum', 'pkgpart'] ],
234 'index' => [ ['typenum'] ],
239 'invnum', 'int', '', '',
240 'custnum', 'int', '', '',
242 'charged', @money_type,
244 'printed', 'int', '', '',
246 'primary_key' => 'invnum',
248 'index' => [ ['custnum'] ],
253 'pkgnum', 'int', '', '',
254 'invnum', 'int', '', '',
255 'setup', @money_type,
256 'recur', @money_type,
261 'unique' => [ ['pkgnum', 'invnum'] ],
262 'index' => [ ['invnum'] ],
267 'crednum', 'int', '', '',
268 'custnum', 'int', '', '',
270 'amount', @money_type,
271 'credited', @money_type,
272 'otaker', 'varchar', '', 8,
273 'reason', 'varchar', '', 255,
275 'primary_key' => 'crednum',
277 'index' => [ ['custnum'] ],
282 'custnum', 'int', '', '',
283 'agentnum', 'int', '', '',
284 'last', 'varchar', '', $char_d,
285 'first', 'varchar', '', $char_d,
286 'ss', 'char', 'NULL', 11,
287 'company', 'varchar', 'NULL', $char_d,
288 'address1', 'varchar', '', $char_d,
289 'address2', 'varchar', 'NULL', $char_d,
290 'city', 'varchar', '', $char_d,
291 'county', 'varchar', 'NULL', $char_d,
292 'state', 'char', '', 2,
293 'zip', 'varchar', '', 10,
294 'country', 'char', '', 2,
295 'daytime', 'varchar', 'NULL', 20,
296 'night', 'varchar', 'NULL', 20,
297 'fax', 'varchar', 'NULL', 12,
298 'payby', 'char', '', 4,
299 'payinfo', 'varchar', 'NULL', 16,
300 'paydate', @date_type,
301 'payname', 'varchar', 'NULL', $char_d,
302 'tax', 'char', 'NULL', 1,
303 'otaker', 'varchar', '', 8,
304 'refnum', 'int', '', '',
306 'primary_key' => 'custnum',
308 #'index' => [ ['last'], ['company'] ],
309 'index' => [ ['last'], ],
312 'cust_main_county' => { #county+state are checked off the cust_main_county
313 #table for validation and to provide a tax rate.
316 'taxnum', 'int', '', '',
317 'state', 'char', '', 2, #two letters max in US... elsewhere?
318 'county', 'varchar', '', $char_d,
319 'tax', 'real', '', '', #tax %
321 'primary_key' => 'taxnum',
323 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
329 'paynum', 'int', '', '',
330 'invnum', 'int', '', '',
333 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
334 # payment type table.
335 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
336 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
338 'primary_key' => 'paynum',
340 'index' => [ ['invnum'] ],
343 'cust_pay_batch' => { #what's this used for again? list of customers
344 #in current CARD batch? (necessarily CARD?)
346 'invnum', 'int', '', '',
347 'custnum', 'int', '', '',
348 'last', 'varchar', '', $char_d,
349 'first', 'varchar', '', $char_d,
350 'address1', 'varchar', '', $char_d,
351 'address2', 'varchar', 'NULL', $char_d,
352 'city', 'varchar', '', $char_d,
353 'state', 'char', '', 2,
354 'zip', 'varchar', '', 10,
355 'country', 'char', '', 2,
356 'trancode', 'TINYINT', '', '',
357 'cardnum', 'varchar', '', 16,
359 'payname', 'varchar', 'NULL', $char_d,
360 'amount', @money_type,
364 'index' => [ ['invnum'], ['custnum'] ],
369 'pkgnum', 'int', '', '',
370 'custnum', 'int', '', '',
371 'pkgpart', 'int', '', '',
372 'otaker', 'varchar', '', 8,
376 'cancel', @date_type,
377 'expire', @date_type,
379 'primary_key' => 'pkgnum',
381 'index' => [ ['custnum'] ],
386 'refundnum', 'int', '', '',
387 'crednum', 'int', '', '',
389 'refund', @money_type,
390 'otaker', 'varchar', '', 8,
391 'reason', 'varchar', '', $char_d,
392 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
393 # into payment type table.
394 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
396 'primary_key' => 'refundnum',
398 'index' => [ ['crednum'] ],
403 'svcnum', 'int', '', '',
404 'pkgnum', 'int', '', '',
405 'svcpart', 'int', '', '',
407 'primary_key' => 'svcnum',
409 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
414 'pkgpart', 'int', '', '',
415 'pkg', 'varchar', '', $char_d,
416 'comment', 'varchar', '', $char_d,
418 'freq', 'smallint', '', '', #billing frequency (months)
421 'primary_key' => 'pkgpart',
428 'pkgpart', 'int', '', '',
429 'svcpart', 'int', '', '',
430 'quantity', 'int', '', '',
433 'unique' => [ ['pkgpart', 'svcpart'] ],
434 'index' => [ ['pkgpart'] ],
439 'refnum', 'int', '', '',
440 'referral', 'varchar', '', $char_d,
442 'primary_key' => 'refnum',
449 'svcpart', 'int', '', '',
450 'svc', 'varchar', '', $char_d,
451 'svcdb', 'varchar', '', $char_d,
453 'primary_key' => 'svcpart',
458 #(this should be renamed to part_pop)
461 'popnum', 'int', '', '',
462 'city', 'varchar', '', $char_d,
463 'state', 'char', '', 2,
465 'exch', 'char', '', 3,
468 'primary_key' => 'popnum',
475 'svcnum', 'int', '', '',
476 'username', 'varchar', '', $username_len, #unique (& remove dup code)
477 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
478 'popnum', 'int', 'NULL', '',
479 'uid', 'bigint', 'NULL', '',
480 'gid', 'bigint', 'NULL', '',
481 'finger', 'varchar', 'NULL', $char_d,
482 'dir', 'varchar', 'NULL', $char_d,
483 'shell', 'varchar', 'NULL', $char_d,
484 'quota', 'varchar', 'NULL', $char_d,
485 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
487 'primary_key' => 'svcnum',
489 'index' => [ ['username'] ],
494 'svcnum', 'int', '', '',
495 'domsvc', 'int', '', '',
496 'domuid', 'bigint', '', '',
497 'domuser', 'varchar', '', $char_d,
499 'primary_key' => 'svcnum',
501 'index' => [ ['domsvc'], ['domuid'] ],
506 # 'svcnum', 'int', '', '',
507 # 'amount', @money_type,
509 # 'primary_key' => 'svcnum',
510 # 'unique' => [ [] ],
516 'svcnum', 'int', '', '',
517 'domain', 'varchar', '', $char_d,
519 'primary_key' => 'svcnum',
520 'unique' => [ ['domain'] ],
526 # 'svcnum', 'int', '', '',
527 # 'svcnum', 'int', '', '',
528 # 'svcnum', 'int', '', '',
529 # 'worker', 'varchar', '', $char_d,
530 # '_date', @date_type,
532 # 'primary_key' => 'svcnum',
533 # 'unique' => [ [] ],