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