now smallint is illegal, so remove that too.
[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.3  1998-10-22 15:46:28  ivan
36 # now smallint is illegal, so remove that too.
37 #
38
39 #to delay loading dbdef until we're ready
40 BEGIN { $FS::Record::setup_hack = 1; }
41
42 use strict;
43 use DBI;
44 use FS::dbdef;
45 use FS::UID qw(adminsuidsetup datasrc);
46 use FS::Record;
47 use FS::cust_main_county;
48
49 #needs to match FS::Record
50 my($dbdef_file) = "/var/spool/freeside/dbdef.". datasrc;
51
52 ###
53
54 print "\nEnter the maximum username length: ";
55 my($username_len)=&getvalue;
56
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.
61 END
62 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
63
64 sub getvalue {
65   my($x)=scalar(<STDIN>);
66   chop $x;
67   $x;
68 }
69
70 ###
71
72 my($char_d) = 80; #default maxlength for text fields
73
74 #my(@date_type)  = ( 'timestamp', '', ''     );
75 my(@date_type)  = ( 'int', 'NULL', ''     );
76 my(@perl_type) = ( 'varchar', 'NULL', ''   ); 
77 my(@money_type);
78 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
79   @money_type = ( 'money',   '', '' );
80 } else {
81   @money_type = ( 'decimal',   '', '10,2' );
82 }
83
84 ###
85 # create a dbdef object from the old data structure
86 ###
87
88 my(%tables)=&tables_hash_hack;
89
90 #turn it into objects
91 my($dbdef) = new FS::dbdef ( map {  
92   my(@columns);
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 );
96   }
97   FS::dbdef_table->new(
98     $_,
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'}),
104     @columns,
105   );
106 } (keys %tables) );
107
108 #add radius attributes to svc_acct
109
110 my($svc_acct)=$dbdef->table('svc_acct');
111
112 my($attribute);
113 foreach $attribute (@attributes) {
114   $svc_acct->addcolumn ( new FS::dbdef_column (
115     'radius_'. $attribute,
116     'varchar',
117     'NULL',
118     $char_d,
119   ));
120 }
121
122 #make part_svc table (but now as object)
123
124 my($part_svc)=$dbdef->table('part_svc');
125
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($_);
131   my($col);
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, 
137       'NULL',
138       $char_d, #$table->column($col)->length,
139     ));
140     $part_svc->addcolumn ( new FS::dbdef_column (
141       $table->name. '__'. $table->column($col)->name . "_flag",
142       'char',
143       'NULL',
144       1,
145     ));
146   }
147 }
148
149 #important
150 $dbdef->save($dbdef_file);
151 FS::Record::reload_dbdef;
152
153 ###
154 # create 'em
155 ###
156
157 my($dbh)=adminsuidsetup;
158
159 #create tables
160 $|=1;
161
162 my($table);
163 foreach  ($dbdef->tables) {
164   my($table)=$dbdef->table($_);
165   print "Creating $_...";
166
167   my($statement);
168
169   #create table
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";
174   }
175
176   print "\n";
177 }
178
179 #not really sample data (and shouldn't default to US)
180
181 #cust_main_county
182 foreach ( qw(
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
186 ) ) {
187   my($cust_main_county)=create FS::cust_main_county({
188     'state' => $_,
189     'tax'   => 0,
190   });  
191   my($error);
192   $error=$cust_main_county->insert;
193   die $error if $error;
194 }
195
196 $dbh->disconnect or die $dbh->errstr;
197
198 ###
199 # Now it becomes an object.  much better.
200 ###
201 sub tables_hash_hack {
202
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
205   #stuff anyway also)
206
207   my(%tables)=( #yech.}
208
209     'agent' => {
210       'columns' => [
211         'agentnum', 'int',            '',     '',
212         'agent',    'varchar',           '',     $char_d,
213         'typenum',  'int',            '',     '',
214         'freq',     'int',       'NULL', '',
215         'prog',     @perl_type,
216       ],
217       'primary_key' => 'agentnum',
218       'unique' => [ [] ],
219       'index' => [ ['typenum'] ],
220     },
221
222     'agent_type' => {
223       'columns' => [
224         'typenum',   'int',  '', '',
225         'atype',     'varchar', '', $char_d,
226       ],
227       'primary_key' => 'typenum',
228       'unique' => [ [] ],
229       'index' => [ [] ],
230     },
231
232     'type_pkgs' => {
233       'columns' => [
234         'typenum',   'int',  '', '',
235         'pkgpart',   'int',  '', '',
236       ],
237       'primary_key' => '',
238       'unique' => [ ['typenum', 'pkgpart'] ],
239       'index' => [ ['typenum'] ],
240     },
241
242     'cust_bill' => {
243       'columns' => [
244         'invnum',    'int',  '', '',
245         'custnum',   'int',  '', '',
246         '_date',     @date_type,
247         'charged',   @money_type,
248         'owed',      @money_type,
249         'printed',   'int',  '', '',
250       ],
251       'primary_key' => 'invnum',
252       'unique' => [ [] ],
253       'index' => [ ['custnum'] ],
254     },
255
256     'cust_bill_pkg' => {
257       'columns' => [
258         'pkgnum',  'int', '', '',
259         'invnum',  'int', '', '',
260         'setup',   @money_type,
261         'recur',   @money_type,
262         'sdate',   @date_type,
263         'edate',   @date_type,
264       ],
265       'primary_key' => '',
266       'unique' => [ ['pkgnum', 'invnum'] ],
267       'index' => [ ['invnum'] ],
268     },
269
270     'cust_credit' => {
271       'columns' => [
272         'crednum',  'int', '', '',
273         'custnum',  'int', '', '',
274         '_date',    @date_type,
275         'amount',   @money_type,
276         'credited', @money_type,
277         'otaker',   'varchar', '', 8,
278         'reason',   'varchar', '', 255,
279       ],
280       'primary_key' => 'crednum',
281       'unique' => [ [] ],
282       'index' => [ ['custnum'] ],
283     },
284
285     'cust_main' => {
286       'columns' => [
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',  '',     '',
310       ],
311       'primary_key' => 'custnum',
312       'unique' => [ [] ],
313       #'index' => [ ['last'], ['company'] ],
314       'index' => [ ['last'], ],
315     },
316
317     'cust_main_county' => { #county+state are checked off the cust_main_county
318                             #table for validation and to provide a tax rate.
319                             #add country?
320       'columns' => [
321         'taxnum',   'int',   '',    '',
322         'state',    'char',  '',    2,  #two letters max in US... elsewhere?
323         'county',   'varchar',  'NULL',    $char_d,
324         'tax',      'real',  '',    '', #tax %
325       ],
326       'primary_key' => 'taxnum',
327       'unique' => [ [] ],
328   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
329       'index' => [ [] ],
330     },
331
332     'cust_pay' => {
333       'columns' => [
334         'paynum',   'int',    '',   '',
335         'invnum',   'int',    '',   '',
336         'paid',     @money_type,
337         '_date',    @date_type,
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.
342       ],
343       'primary_key' => 'paynum',
344       'unique' => [ [] ],
345       'index' => [ ['invnum'] ],
346     },
347
348     'cust_pay_batch' => { #what's this used for again?  list of customers
349                           #in current CARD batch? (necessarily CARD?)
350       'columns' => [
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,
363         'exp',      @date_type,
364         'payname',  'varchar', 'NULL', $char_d,
365         'amount',   @money_type,
366       ],
367       'primary_key' => '',
368       'unique' => [ [] ],
369       'index' => [ ['invnum'], ['custnum'] ],
370     },
371
372     'cust_pkg' => {
373       'columns' => [
374         'pkgnum',    'int',    '',   '',
375         'custnum',   'int',    '',   '',
376         'pkgpart',   'int',    '',   '',
377         'otaker',    'varchar', '', 8,
378         'setup',     @date_type,
379         'bill',      @date_type,
380         'susp',      @date_type,
381         'cancel',    @date_type,
382         'expire',    @date_type,
383       ],
384       'primary_key' => 'pkgnum',
385       'unique' => [ [] ],
386       'index' => [ ['custnum'] ],
387     },
388
389     'cust_refund' => {
390       'columns' => [
391         'refundnum',    'int',    '',   '',
392         'crednum',      'int',    '',   '',
393         '_date',        @date_type,
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
400       ],
401       'primary_key' => 'refundnum',
402       'unique' => [ [] ],
403       'index' => [ ['crednum'] ],
404     },
405
406     'cust_svc' => {
407       'columns' => [
408         'svcnum',    'int',    '',   '',
409         'pkgnum',    'int',    '',   '',
410         'svcpart',   'int',    '',   '',
411       ],
412       'primary_key' => 'svcnum',
413       'unique' => [ [] ],
414       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
415     },
416
417     'part_pkg' => {
418       'columns' => [
419         'pkgpart',    'int',    '',   '',
420         'pkg',        'varchar',   '',   $char_d,
421         'comment',    'varchar',   '',   $char_d,
422         'setup',      @perl_type,
423         'freq',       'int', '', '',  #billing frequency (months)
424         'recur',      @perl_type,
425       ],
426       'primary_key' => 'pkgpart',
427       'unique' => [ [] ],
428       'index' => [ [] ],
429     },
430
431     'pkg_svc' => {
432       'columns' => [
433         'pkgpart',    'int',    '',   '',
434         'svcpart',    'int',    '',   '',
435         'quantity',   'int',    '',   '',
436       ],
437       'primary_key' => '',
438       'unique' => [ ['pkgpart', 'svcpart'] ],
439       'index' => [ ['pkgpart'] ],
440     },
441
442     'part_referral' => {
443       'columns' => [
444         'refnum',   'int',    '',   '',
445         'referral', 'varchar',   '',   $char_d,
446       ],
447       'primary_key' => 'refnum',
448       'unique' => [ [] ],
449       'index' => [ [] ],
450     },
451
452     'part_svc' => {
453       'columns' => [
454         'svcpart',    'int',    '',   '',
455         'svc',        'varchar',   '',   $char_d,
456         'svcdb',      'varchar',   '',   $char_d,
457       ],
458       'primary_key' => 'svcpart',
459       'unique' => [ [] ],
460       'index' => [ [] ],
461     },
462
463     #(this should be renamed to part_pop)
464     'svc_acct_pop' => {
465       'columns' => [
466         'popnum',    'int',    '',   '',
467         'city',      'varchar',   '',   $char_d,
468         'state',     'char',   '',   2,
469         'ac',        'char',   '',   3,
470         'exch',      'char',   '',   3,
471         #rest o' number?
472       ],
473       'primary_key' => 'popnum',
474       'unique' => [ [] ],
475       'index' => [ [] ],
476     },
477
478     'svc_acct' => {
479       'columns' => [
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.
491       ],
492       'primary_key' => 'svcnum',
493       'unique' => [ [] ],
494       'index' => [ ['username'] ],
495     },
496
497     'svc_acct_sm' => {
498       'columns' => [
499         'svcnum',    'int',    '',   '',
500         'domsvc',    'int',    '',   '',
501         'domuid',    'int', '',   '',
502         'domuser',   'varchar',   '',   $char_d,
503       ],
504       'primary_key' => 'svcnum',
505       'unique' => [ [] ],
506       'index' => [ ['domsvc'], ['domuid'] ], 
507     },
508
509     #'svc_charge' => {
510     #  'columns' => [
511     #    'svcnum',    'int',    '',   '',
512     #    'amount',    @money_type,
513     #  ],
514     #  'primary_key' => 'svcnum',
515     #  'unique' => [ [] ],
516     #  'index' => [ [] ],
517     #},
518
519     'svc_domain' => {
520       'columns' => [
521         'svcnum',    'int',    '',   '',
522         'domain',    'varchar',    '',   $char_d,
523       ],
524       'primary_key' => 'svcnum',
525       'unique' => [ ['domain'] ],
526       'index' => [ [] ],
527     },
528
529     #'svc_wo' => {
530     #  'columns' => [
531     #    'svcnum',    'int',    '',   '',
532     #    'svcnum',    'int',    '',   '',
533     #    'svcnum',    'int',    '',   '',
534     #    'worker',    'varchar',   '',   $char_d,
535     #    '_date',     @date_type,
536     #  ],
537     #  'primary_key' => 'svcnum',
538     #  'unique' => [ [] ],
539     #  'index' => [ [] ],
540     #},
541
542   );
543
544   %tables;
545
546 }
547