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