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.3 1998-10-22 15:46:28 ivan
36 # now smallint is illegal, so remove that too.
39 #to delay loading dbdef until we're ready
40 BEGIN { $FS::Record::setup_hack = 1; }
45 use FS::UID qw(adminsuidsetup datasrc);
47 use FS::cust_main_county;
49 #needs to match FS::Record
50 my($dbdef_file) = "/var/spool/freeside/dbdef.". datasrc;
54 print "\nEnter the maximum username length: ";
55 my($username_len)=&getvalue;
57 print "\n\n", <<END, ":";
58 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
59 for each user. Enter any additional RADIUS attributes you need to track for
60 each user, separated by whitespace.
62 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
65 my($x)=scalar(<STDIN>);
72 my($char_d) = 80; #default maxlength for text fields
74 #my(@date_type) = ( 'timestamp', '', '' );
75 my(@date_type) = ( 'int', 'NULL', '' );
76 my(@perl_type) = ( 'varchar', 'NULL', '' );
78 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
79 @money_type = ( 'money', '', '' );
81 @money_type = ( 'decimal', '', '10,2' );
85 # create a dbdef object from the old data structure
88 my(%tables)=&tables_hash_hack;
91 my($dbdef) = new FS::dbdef ( map {
93 while (@{$tables{$_}{'columns'}}) {
94 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
95 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
99 $tables{$_}{'primary_key'},
100 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
101 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
102 FS::dbdef_unique->new($tables{$_}{'unique'}),
103 FS::dbdef_index->new($tables{$_}{'index'}),
108 #add radius attributes to svc_acct
110 my($svc_acct)=$dbdef->table('svc_acct');
113 foreach $attribute (@attributes) {
114 $svc_acct->addcolumn ( new FS::dbdef_column (
115 'radius_'. $attribute,
122 #make part_svc table (but now as object)
124 my($part_svc)=$dbdef->table('part_svc');
126 #because of svc_acct_pop
127 #foreach (grep /^svc_/, $dbdef->tables) {
128 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
129 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
130 my($table)=$dbdef->table($_);
132 foreach $col ( $table->columns ) {
133 next if $col =~ /^svcnum$/;
134 $part_svc->addcolumn( new FS::dbdef_column (
135 $table->name. '__' . $table->column($col)->name,
136 'varchar', #$table->column($col)->type,
138 $char_d, #$table->column($col)->length,
140 $part_svc->addcolumn ( new FS::dbdef_column (
141 $table->name. '__'. $table->column($col)->name . "_flag",
150 $dbdef->save($dbdef_file);
151 FS::Record::reload_dbdef;
157 my($dbh)=adminsuidsetup;
163 foreach ($dbdef->tables) {
164 my($table)=$dbdef->table($_);
165 print "Creating $_...";
170 foreach $statement ($table->sql_create_table(datasrc)) {
171 #print $statement, "\n";
172 $dbh->do( $statement )
173 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
179 #not really sample data (and shouldn't default to US)
183 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
184 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
185 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
187 my($cust_main_county)=create FS::cust_main_county({
192 $error=$cust_main_county->insert;
193 die $error if $error;
196 $dbh->disconnect or die $dbh->errstr;
199 # Now it becomes an object. much better.
201 sub tables_hash_hack {
203 #note that s/(date|change)/_$1/; to avoid keyword conflict.
204 #put a kludge in FS::Record to catch this or? (pry need some date-handling
207 my(%tables)=( #yech.}
211 'agentnum', 'int', '', '',
212 'agent', 'varchar', '', $char_d,
213 'typenum', 'int', '', '',
214 'freq', 'int', 'NULL', '',
217 'primary_key' => 'agentnum',
219 'index' => [ ['typenum'] ],
224 'typenum', 'int', '', '',
225 'atype', 'varchar', '', $char_d,
227 'primary_key' => 'typenum',
234 'typenum', 'int', '', '',
235 'pkgpart', 'int', '', '',
238 'unique' => [ ['typenum', 'pkgpart'] ],
239 'index' => [ ['typenum'] ],
244 'invnum', 'int', '', '',
245 'custnum', 'int', '', '',
247 'charged', @money_type,
249 'printed', 'int', '', '',
251 'primary_key' => 'invnum',
253 'index' => [ ['custnum'] ],
258 'pkgnum', 'int', '', '',
259 'invnum', 'int', '', '',
260 'setup', @money_type,
261 'recur', @money_type,
266 'unique' => [ ['pkgnum', 'invnum'] ],
267 'index' => [ ['invnum'] ],
272 'crednum', 'int', '', '',
273 'custnum', 'int', '', '',
275 'amount', @money_type,
276 'credited', @money_type,
277 'otaker', 'varchar', '', 8,
278 'reason', 'varchar', '', 255,
280 'primary_key' => 'crednum',
282 'index' => [ ['custnum'] ],
287 'custnum', 'int', '', '',
288 'agentnum', 'int', '', '',
289 'last', 'varchar', '', $char_d,
290 'first', 'varchar', '', $char_d,
291 'ss', 'char', 'NULL', 11,
292 'company', 'varchar', 'NULL', $char_d,
293 'address1', 'varchar', '', $char_d,
294 'address2', 'varchar', 'NULL', $char_d,
295 'city', 'varchar', '', $char_d,
296 'county', 'varchar', 'NULL', $char_d,
297 'state', 'char', '', 2,
298 'zip', 'varchar', '', 10,
299 'country', 'char', '', 2,
300 'daytime', 'varchar', 'NULL', 20,
301 'night', 'varchar', 'NULL', 20,
302 'fax', 'varchar', 'NULL', 12,
303 'payby', 'char', '', 4,
304 'payinfo', 'varchar', 'NULL', 16,
305 'paydate', @date_type,
306 'payname', 'varchar', 'NULL', $char_d,
307 'tax', 'char', 'NULL', 1,
308 'otaker', 'varchar', '', 8,
309 'refnum', 'int', '', '',
311 'primary_key' => 'custnum',
313 #'index' => [ ['last'], ['company'] ],
314 'index' => [ ['last'], ],
317 'cust_main_county' => { #county+state are checked off the cust_main_county
318 #table for validation and to provide a tax rate.
321 'taxnum', 'int', '', '',
322 'state', 'char', '', 2, #two letters max in US... elsewhere?
323 'county', 'varchar', 'NULL', $char_d,
324 'tax', 'real', '', '', #tax %
326 'primary_key' => 'taxnum',
328 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
334 'paynum', 'int', '', '',
335 'invnum', 'int', '', '',
338 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
339 # payment type table.
340 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
341 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
343 'primary_key' => 'paynum',
345 'index' => [ ['invnum'] ],
348 'cust_pay_batch' => { #what's this used for again? list of customers
349 #in current CARD batch? (necessarily CARD?)
351 'invnum', 'int', '', '',
352 'custnum', 'int', '', '',
353 'last', 'varchar', '', $char_d,
354 'first', 'varchar', '', $char_d,
355 'address1', 'varchar', '', $char_d,
356 'address2', 'varchar', 'NULL', $char_d,
357 'city', 'varchar', '', $char_d,
358 'state', 'char', '', 2,
359 'zip', 'varchar', '', 10,
360 'country', 'char', '', 2,
361 'trancode', 'int', '', '',
362 'cardnum', 'varchar', '', 16,
364 'payname', 'varchar', 'NULL', $char_d,
365 'amount', @money_type,
369 'index' => [ ['invnum'], ['custnum'] ],
374 'pkgnum', 'int', '', '',
375 'custnum', 'int', '', '',
376 'pkgpart', 'int', '', '',
377 'otaker', 'varchar', '', 8,
381 'cancel', @date_type,
382 'expire', @date_type,
384 'primary_key' => 'pkgnum',
386 'index' => [ ['custnum'] ],
391 'refundnum', 'int', '', '',
392 'crednum', 'int', '', '',
394 'refund', @money_type,
395 'otaker', 'varchar', '', 8,
396 'reason', 'varchar', '', $char_d,
397 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
398 # into payment type table.
399 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
401 'primary_key' => 'refundnum',
403 'index' => [ ['crednum'] ],
408 'svcnum', 'int', '', '',
409 'pkgnum', 'int', '', '',
410 'svcpart', 'int', '', '',
412 'primary_key' => 'svcnum',
414 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
419 'pkgpart', 'int', '', '',
420 'pkg', 'varchar', '', $char_d,
421 'comment', 'varchar', '', $char_d,
423 'freq', 'int', '', '', #billing frequency (months)
426 'primary_key' => 'pkgpart',
433 'pkgpart', 'int', '', '',
434 'svcpart', 'int', '', '',
435 'quantity', 'int', '', '',
438 'unique' => [ ['pkgpart', 'svcpart'] ],
439 'index' => [ ['pkgpart'] ],
444 'refnum', 'int', '', '',
445 'referral', 'varchar', '', $char_d,
447 'primary_key' => 'refnum',
454 'svcpart', 'int', '', '',
455 'svc', 'varchar', '', $char_d,
456 'svcdb', 'varchar', '', $char_d,
458 'primary_key' => 'svcpart',
463 #(this should be renamed to part_pop)
466 'popnum', 'int', '', '',
467 'city', 'varchar', '', $char_d,
468 'state', 'char', '', 2,
470 'exch', 'char', '', 3,
473 'primary_key' => 'popnum',
480 'svcnum', 'int', '', '',
481 'username', 'varchar', '', $username_len, #unique (& remove dup code)
482 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
483 'popnum', 'int', 'NULL', '',
484 'uid', 'int', 'NULL', '',
485 'gid', 'int', 'NULL', '',
486 'finger', 'varchar', 'NULL', $char_d,
487 'dir', 'varchar', 'NULL', $char_d,
488 'shell', 'varchar', 'NULL', $char_d,
489 'quota', 'varchar', 'NULL', $char_d,
490 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
492 'primary_key' => 'svcnum',
494 'index' => [ ['username'] ],
499 'svcnum', 'int', '', '',
500 'domsvc', 'int', '', '',
501 'domuid', 'int', '', '',
502 'domuser', 'varchar', '', $char_d,
504 'primary_key' => 'svcnum',
506 'index' => [ ['domsvc'], ['domuid'] ],
511 # 'svcnum', 'int', '', '',
512 # 'amount', @money_type,
514 # 'primary_key' => 'svcnum',
515 # 'unique' => [ [] ],
521 'svcnum', 'int', '', '',
522 'domain', 'varchar', '', $char_d,
524 'primary_key' => 'svcnum',
525 'unique' => [ ['domain'] ],
531 # 'svcnum', 'int', '', '',
532 # 'svcnum', 'int', '', '',
533 # 'svcnum', 'int', '', '',
534 # 'worker', 'varchar', '', $char_d,
535 # '_date', @date_type,
537 # 'primary_key' => 'svcnum',
538 # 'unique' => [ [] ],