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