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