postgres 6.5 finally supports decimal(10,2)
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.21 2000-01-30 06:03:26 ivan Exp $
4 #
5 # ivan@sisd.com 97-nov-8,9
6 #
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
10 #
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
12 #
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
14 #
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
17 #
18 # finished up ivan@sisd.com 98-jun-1
19 #
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
24 #
25 # ss is 11 chars ivan@sisd.com 98-jul-20
26 #
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
28 #
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
31 #
32 # fix radius attributes ivan@sisd.com 98-sep-27
33 #
34 # $Log: fs-setup,v $
35 # Revision 1.21  2000-01-30 06:03:26  ivan
36 # postgres 6.5 finally supports decimal(10,2)
37 #
38 # Revision 1.20  2000/01/28 22:53:33  ivan
39 # track full phone number
40 #
41 # Revision 1.19  1999/07/29 08:50:35  ivan
42 # wrong type for cust_pay_batch.exp
43 #
44 # Revision 1.18  1999/04/15 22:46:30  ivan
45 # TT isn't a state!
46 #
47 # Revision 1.17  1999/04/14 07:58:39  ivan
48 # export getsecrets from FS::UID instead of calling it explicitly
49 #
50 # Revision 1.16  1999/02/28 19:44:16  ivan
51 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
52 #
53 # Revision 1.15  1999/02/27 21:06:21  ivan
54 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
55 # by Ben Leibig <leibig@colorado.edu>
56 #
57 # Revision 1.14  1999/02/07 09:59:14  ivan
58 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
59 #
60 # Revision 1.13  1999/02/04 06:09:23  ivan
61 # add AU provences
62 #
63 # Revision 1.12  1999/02/03 10:42:27  ivan
64 # *** empty log message ***
65 #
66 # Revision 1.11  1999/01/17 03:11:52  ivan
67 # remove preliminary completehost changes
68 #
69 # Revision 1.10  1998/12/16 06:05:38  ivan
70 # add table cust_main_invoice
71 #
72 # Revision 1.9  1998/12/15 04:36:29  ivan
73 # s/croak/die/; #oops
74 #
75 # Revision 1.8  1998/12/15 04:33:27  ivan
76 # dies if it isn't running as the freeside user
77 #
78 # Revision 1.7  1998/11/18 09:01:31  ivan
79 # i18n! i18n!
80 #
81 # Revision 1.6  1998/11/15 13:18:02  ivan
82 # remove debugging
83 #
84 # Revision 1.5  1998/11/15 09:43:03  ivan
85 # update for new config file syntax, new adminsuidsetup
86 #
87 # Revision 1.4  1998/10/22 15:51:23  ivan
88 # also varchar with no length specified - postgresql fix broke mysql.
89 #
90 # Revision 1.3  1998/10/22 15:46:28  ivan
91 # now smallint is illegal, so remove that too.
92 #
93
94 #to delay loading dbdef until we're ready
95 BEGIN { $FS::Record::setup_hack = 1; }
96
97 use strict;
98 use DBI;
99 use FS::dbdef;
100 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
101 use FS::Record;
102 use FS::cust_main_county;
103
104 die "Not running uid freeside!" unless checkeuid();
105
106 my $user = shift or die &usage;
107 getsecrets($user);
108
109 #needs to match FS::Record
110 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
111
112 ###
113
114 print "\nEnter the maximum username length: ";
115 my($username_len)=&getvalue;
116
117 print "\n\n", <<END, ":";
118 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
119 for each user.  Enter any additional RADIUS attributes you need to track for
120 each user, separated by whitespace.
121 END
122 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
123
124 sub getvalue {
125   my($x)=scalar(<STDIN>);
126   chop $x;
127   $x;
128 }
129
130 ###
131
132 my($char_d) = 80; #default maxlength for text fields
133
134 #my(@date_type)  = ( 'timestamp', '', ''     );
135 my(@date_type)  = ( 'int', 'NULL', ''     );
136 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
137 my @money_type = ( 'decimal',   '', '10,2' );
138
139 ###
140 # create a dbdef object from the old data structure
141 ###
142
143 my(%tables)=&tables_hash_hack;
144
145 #turn it into objects
146 my($dbdef) = new FS::dbdef ( map {  
147   my(@columns);
148   while (@{$tables{$_}{'columns'}}) {
149     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
150     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
151   }
152   FS::dbdef_table->new(
153     $_,
154     $tables{$_}{'primary_key'},
155     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
156     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
157     FS::dbdef_unique->new($tables{$_}{'unique'}),
158     FS::dbdef_index->new($tables{$_}{'index'}),
159     @columns,
160   );
161 } (keys %tables) );
162
163 #add radius attributes to svc_acct
164
165 my($svc_acct)=$dbdef->table('svc_acct');
166
167 my($attribute);
168 foreach $attribute (@attributes) {
169   $svc_acct->addcolumn ( new FS::dbdef_column (
170     'radius_'. $attribute,
171     'varchar',
172     'NULL',
173     $char_d,
174   ));
175 }
176
177 #make part_svc table (but now as object)
178
179 my($part_svc)=$dbdef->table('part_svc');
180
181 #because of svc_acct_pop
182 #foreach (grep /^svc_/, $dbdef->tables) { 
183 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
184 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
185   my($table)=$dbdef->table($_);
186   my($col);
187   foreach $col ( $table->columns ) {
188     next if $col =~ /^svcnum$/;
189     $part_svc->addcolumn( new FS::dbdef_column (
190       $table->name. '__' . $table->column($col)->name,
191       'varchar', #$table->column($col)->type, 
192       'NULL',
193       $char_d, #$table->column($col)->length,
194     ));
195     $part_svc->addcolumn ( new FS::dbdef_column (
196       $table->name. '__'. $table->column($col)->name . "_flag",
197       'char',
198       'NULL',
199       1,
200     ));
201   }
202 }
203
204 #important
205 $dbdef->save($dbdef_file);
206 &FS::Record::reload_dbdef($dbdef_file);
207
208 ###
209 # create 'em
210 ###
211
212 my($dbh)=adminsuidsetup $user;
213
214 #create tables
215 $|=1;
216
217 my($table);
218 foreach  ($dbdef->tables) {
219   my($table)=$dbdef->table($_);
220   print "Creating $_...";
221
222   my($statement);
223
224   #create table
225   foreach $statement ($table->sql_create_table(datasrc)) {
226     #print $statement, "\n"; 
227     $dbh->do( $statement )
228       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
229   }
230
231   print "\n";
232 }
233
234 #not really sample data (and shouldn't default to US)
235
236 #cust_main_county
237
238 #USPS state codes
239 foreach ( qw(
240 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
241 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 
242 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
243 ) ) {
244   my($cust_main_county)=new FS::cust_main_county({
245     'state' => $_,
246     'tax'   => 0,
247     'country' => 'US',
248   });  
249   my($error);
250   $error=$cust_main_county->insert;
251   die $error if $error;
252 }
253
254 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
255 foreach ( qw(
256 VIC NSW NT QLD TAS ACT WA SA
257 ) ) {
258   my($cust_main_county)=new FS::cust_main_county({
259     'state' => $_,
260     'tax'   => 0,
261     'country' => 'AU',
262   });  
263   my($error);
264   $error=$cust_main_county->insert;
265   die $error if $error;
266 }
267
268 #ISO 2-letter country codes (same as country TLDs) except US and AU
269 foreach ( qw(
270 AF AL DZ AS AD AO AI AQ AG AR AM AW AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 YE YU ZR ZM ZW
280 ) ) {
281   my($cust_main_county)=new FS::cust_main_county({
282     'tax'   => 0,
283     'country' => $_,
284   });  
285   my($error);
286   $error=$cust_main_county->insert;
287   die $error if $error;
288 }
289
290 $dbh->disconnect or die $dbh->errstr;
291
292 sub usage {
293   die "Usage:\n  fs-setup user\n"; 
294 }
295
296 ###
297 # Now it becomes an object.  much better.
298 ###
299 sub tables_hash_hack {
300
301   #note that s/(date|change)/_$1/; to avoid keyword conflict.
302   #put a kludge in FS::Record to catch this or? (pry need some date-handling
303   #stuff anyway also)
304
305   my(%tables)=( #yech.}
306
307     'agent' => {
308       'columns' => [
309         'agentnum', 'int',            '',     '',
310         'agent',    'varchar',           '',     $char_d,
311         'typenum',  'int',            '',     '',
312         'freq',     'int',       'NULL', '',
313         'prog',     @perl_type,
314       ],
315       'primary_key' => 'agentnum',
316       'unique' => [ [] ],
317       'index' => [ ['typenum'] ],
318     },
319
320     'agent_type' => {
321       'columns' => [
322         'typenum',   'int',  '', '',
323         'atype',     'varchar', '', $char_d,
324       ],
325       'primary_key' => 'typenum',
326       'unique' => [ [] ],
327       'index' => [ [] ],
328     },
329
330     'type_pkgs' => {
331       'columns' => [
332         'typenum',   'int',  '', '',
333         'pkgpart',   'int',  '', '',
334       ],
335       'primary_key' => '',
336       'unique' => [ ['typenum', 'pkgpart'] ],
337       'index' => [ ['typenum'] ],
338     },
339
340     'cust_bill' => {
341       'columns' => [
342         'invnum',    'int',  '', '',
343         'custnum',   'int',  '', '',
344         '_date',     @date_type,
345         'charged',   @money_type,
346         'owed',      @money_type,
347         'printed',   'int',  '', '',
348       ],
349       'primary_key' => 'invnum',
350       'unique' => [ [] ],
351       'index' => [ ['custnum'] ],
352     },
353
354     'cust_bill_pkg' => {
355       'columns' => [
356         'pkgnum',  'int', '', '',
357         'invnum',  'int', '', '',
358         'setup',   @money_type,
359         'recur',   @money_type,
360         'sdate',   @date_type,
361         'edate',   @date_type,
362       ],
363       'primary_key' => '',
364       'unique' => [ ['pkgnum', 'invnum'] ],
365       'index' => [ ['invnum'] ],
366     },
367
368     'cust_credit' => {
369       'columns' => [
370         'crednum',  'int', '', '',
371         'custnum',  'int', '', '',
372         '_date',    @date_type,
373         'amount',   @money_type,
374         'credited', @money_type,
375         'otaker',   'varchar', '', 8,
376         'reason',   'varchar', '', 255,
377       ],
378       'primary_key' => 'crednum',
379       'unique' => [ [] ],
380       'index' => [ ['custnum'] ],
381     },
382
383     'cust_main' => {
384       'columns' => [
385         'custnum',  'int',  '',     '',
386         'agentnum', 'int',  '',     '',
387 #        'titlenum', 'int',  'NULL',   '',
388         'last',     'varchar', '',     $char_d,
389 #        'middle',   'varchar', 'NULL', $char_d,
390         'first',    'varchar', '',     $char_d,
391         'ss',       'char', 'NULL', 11,
392         'company',  'varchar', 'NULL', $char_d,
393         'address1', 'varchar', '',     $char_d,
394         'address2', 'varchar', 'NULL', $char_d,
395         'city',     'varchar', '',     $char_d,
396         'county',   'varchar', 'NULL', $char_d,
397         'state',    'varchar', 'NULL', $char_d,
398         'zip',      'varchar', '',     10,
399         'country',  'char', '',     2,
400         'daytime',  'varchar', 'NULL', 20,
401         'night',    'varchar', 'NULL', 20,
402         'fax',      'varchar', 'NULL', 12,
403         'payby',    'char', '',     4,
404         'payinfo',  'varchar', 'NULL', 16,
405         #'paydate',  @date_type,
406         'paydate',  'varchar', 'NULL', 10,
407         'payname',  'varchar', 'NULL', $char_d,
408         'tax',      'char', 'NULL', 1,
409         'otaker',   'varchar', '',     8,
410         'refnum',   'int',  '',     '',
411       ],
412       'primary_key' => 'custnum',
413       'unique' => [ [] ],
414       #'index' => [ ['last'], ['company'] ],
415       'index' => [ ['last'], ],
416     },
417
418     'cust_main_invoice' => {
419       'columns' => [
420         'destnum',  'int',  '',     '',
421         'custnum',  'int',  '',     '',
422         'dest',     'varchar', '',  $char_d,
423       ],
424       'primary_key' => 'destnum',
425       'unique' => [ [] ],
426       'index' => [ ['custnum'], ],
427     },
428
429     'cust_main_county' => { #county+state+country are checked off the
430                             #cust_main_county for validation and to provide
431                             # a tax rate.
432       'columns' => [
433         'taxnum',   'int',   '',    '',
434         'state',    'varchar',  'NULL',    $char_d,
435         'county',   'varchar',  'NULL',    $char_d,
436         'country',  'char',  '', 2, 
437         'tax',      'real',  '',    '', #tax %
438       ],
439       'primary_key' => 'taxnum',
440       'unique' => [ [] ],
441   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
442       'index' => [ [] ],
443     },
444
445     'cust_pay' => {
446       'columns' => [
447         'paynum',   'int',    '',   '',
448         'invnum',   'int',    '',   '',
449         'paid',     @money_type,
450         '_date',    @date_type,
451         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
452                                          # payment type table.
453         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
454         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
455       ],
456       'primary_key' => 'paynum',
457       'unique' => [ [] ],
458       'index' => [ ['invnum'] ],
459     },
460
461     'cust_pay_batch' => { #what's this used for again?  list of customers
462                           #in current CARD batch? (necessarily CARD?)
463       'columns' => [
464         'invnum',   'int',    '',   '',
465         'custnum',   'int',    '',   '',
466         'last',     'varchar', '',     $char_d,
467         'first',    'varchar', '',     $char_d,
468         'address1', 'varchar', '',     $char_d,
469         'address2', 'varchar', 'NULL', $char_d,
470         'city',     'varchar', '',     $char_d,
471         'state',    'varchar', '',     $char_d,
472         'zip',      'varchar', '',     10,
473         'country',  'char', '',     2,
474         'trancode', 'int', '', '',
475         'cardnum',  'varchar', '',     16,
476         #'exp',      @date_type,
477         'exp',      'varchar', '',     11,
478         'payname',  'varchar', 'NULL', $char_d,
479         'amount',   @money_type,
480       ],
481       'primary_key' => '',
482       'unique' => [ [] ],
483       'index' => [ ['invnum'], ['custnum'] ],
484     },
485
486     'cust_pkg' => {
487       'columns' => [
488         'pkgnum',    'int',    '',   '',
489         'custnum',   'int',    '',   '',
490         'pkgpart',   'int',    '',   '',
491         'otaker',    'varchar', '', 8,
492         'setup',     @date_type,
493         'bill',      @date_type,
494         'susp',      @date_type,
495         'cancel',    @date_type,
496         'expire',    @date_type,
497       ],
498       'primary_key' => 'pkgnum',
499       'unique' => [ [] ],
500       'index' => [ ['custnum'] ],
501     },
502
503     'cust_refund' => {
504       'columns' => [
505         'refundnum',    'int',    '',   '',
506         'crednum',      'int',    '',   '',
507         '_date',        @date_type,
508         'refund',       @money_type,
509         'otaker',       'varchar',   '',   8,
510         'reason',       'varchar',   '',   $char_d,
511         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
512                                              # into payment type table.
513         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
514       ],
515       'primary_key' => 'refundnum',
516       'unique' => [ [] ],
517       'index' => [ ['crednum'] ],
518     },
519
520     'cust_svc' => {
521       'columns' => [
522         'svcnum',    'int',    '',   '',
523         'pkgnum',    'int',    '',   '',
524         'svcpart',   'int',    '',   '',
525       ],
526       'primary_key' => 'svcnum',
527       'unique' => [ [] ],
528       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
529     },
530
531     'part_pkg' => {
532       'columns' => [
533         'pkgpart',    'int',    '',   '',
534         'pkg',        'varchar',   '',   $char_d,
535         'comment',    'varchar',   '',   $char_d,
536         'setup',      @perl_type,
537         'freq',       'int', '', '',  #billing frequency (months)
538         'recur',      @perl_type,
539       ],
540       'primary_key' => 'pkgpart',
541       'unique' => [ [] ],
542       'index' => [ [] ],
543     },
544
545 #    'part_title' => {
546 #      'columns' => [
547 #        'titlenum',   'int',    '',   '',
548 #        'title',      'varchar',   '',   $char_d,
549 #      ],
550 #      'primary_key' => 'titlenum',
551 #      'unique' => [ [] ],
552 #      'index' => [ [] ],
553 #    },
554
555     'pkg_svc' => {
556       'columns' => [
557         'pkgpart',    'int',    '',   '',
558         'svcpart',    'int',    '',   '',
559         'quantity',   'int',    '',   '',
560       ],
561       'primary_key' => '',
562       'unique' => [ ['pkgpart', 'svcpart'] ],
563       'index' => [ ['pkgpart'] ],
564     },
565
566     'part_referral' => {
567       'columns' => [
568         'refnum',   'int',    '',   '',
569         'referral', 'varchar',   '',   $char_d,
570       ],
571       'primary_key' => 'refnum',
572       'unique' => [ [] ],
573       'index' => [ [] ],
574     },
575
576     'part_svc' => {
577       'columns' => [
578         'svcpart',    'int',    '',   '',
579         'svc',        'varchar',   '',   $char_d,
580         'svcdb',      'varchar',   '',   $char_d,
581       ],
582       'primary_key' => 'svcpart',
583       'unique' => [ [] ],
584       'index' => [ [] ],
585     },
586
587     #(this should be renamed to part_pop)
588     'svc_acct_pop' => {
589       'columns' => [
590         'popnum',    'int',    '',   '',
591         'city',      'varchar',   '',   $char_d,
592         'state',     'varchar',   '',   $char_d,
593         'ac',        'char',   '',   3,
594         'exch',      'char',   '',   3,
595         'loc',       'char',   'NULL',   4, #NULL for legacy purposes
596       ],
597       'primary_key' => 'popnum',
598       'unique' => [ [] ],
599       'index' => [ [] ],
600     },
601
602     'svc_acct' => {
603       'columns' => [
604         'svcnum',    'int',    '',   '',
605         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
606         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
607         'popnum',    'int',    'NULL',   '',
608         'uid',       'int', 'NULL',   '',
609         'gid',       'int', 'NULL',   '',
610         'finger',    'varchar',   'NULL',   $char_d,
611         'dir',       'varchar',   'NULL',   $char_d,
612         'shell',     'varchar',   'NULL',   $char_d,
613         'quota',     'varchar',   'NULL',   $char_d,
614         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
615       ],
616       'primary_key' => 'svcnum',
617       'unique' => [ [] ],
618       'index' => [ ['username'] ],
619     },
620
621     'svc_acct_sm' => {
622       'columns' => [
623         'svcnum',    'int',    '',   '',
624         'domsvc',    'int',    '',   '',
625         'domuid',    'int', '',   '',
626         'domuser',   'varchar',   '',   $char_d,
627       ],
628       'primary_key' => 'svcnum',
629       'unique' => [ [] ],
630       'index' => [ ['domsvc'], ['domuid'] ], 
631     },
632
633     #'svc_charge' => {
634     #  'columns' => [
635     #    'svcnum',    'int',    '',   '',
636     #    'amount',    @money_type,
637     #  ],
638     #  'primary_key' => 'svcnum',
639     #  'unique' => [ [] ],
640     #  'index' => [ [] ],
641     #},
642
643     'svc_domain' => {
644       'columns' => [
645         'svcnum',    'int',    '',   '',
646         'domain',    'varchar',    '',   $char_d,
647       ],
648       'primary_key' => 'svcnum',
649       'unique' => [ ['domain'] ],
650       'index' => [ [] ],
651     },
652
653     #'svc_wo' => {
654     #  'columns' => [
655     #    'svcnum',    'int',    '',   '',
656     #    'svcnum',    'int',    '',   '',
657     #    'svcnum',    'int',    '',   '',
658     #    'worker',    'varchar',   '',   $char_d,
659     #    '_date',     @date_type,
660     #  ],
661     #  'primary_key' => 'svcnum',
662     #  'unique' => [ [] ],
663     #  'index' => [ [] ],
664     #},
665
666   );
667
668   %tables;
669
670 }
671