delete fees, RT#81713
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -w
2
3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
5
6 #to allow initial insert
7 use FS::part_pkg;
8 $FS::part_pkg::setup_hack = 1;
9 $FS::part_pkg::setup_hack = 1;
10
11 use strict;
12 use vars qw($opt_u $opt_d $opt_v $opt_q);
13 use Getopt::Std;
14 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
15 use FS::CurrentUser;
16 use FS::Schema qw( dbdef_dist reload_dbdef );
17 use FS::Record qw( qsearch );
18 #use FS::raddb;
19 use FS::Setup qw(create_initial_data);
20 use FS::Conf;
21
22 die "Not running uid freeside!" unless checkeuid();
23
24 #my %attrib2db =
25 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
26
27 getopts("u:vqd:");
28 $opt_v = 1 unless $opt_q; #verbose by default now
29
30 my $config_dir = shift || '%%%DIST_CONF%%%' ;
31 $config_dir =~ /^([\w.:=\/]+)$/
32   or die "unacceptable configuration directory name";
33 $config_dir = $1;
34
35 getsecrets();
36
37 #needs to match FS::Record
38 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
39
40 my $username_len = 32;
41
42 ###
43 # create a dbdef object from the old data structure
44 ###
45
46 warn "Loading schema objects\n" if $opt_v;
47
48 my $dbdef = dbdef_dist(datasrc);
49
50 #important
51 $dbdef->save($dbdef_file);
52 &FS::Schema::reload_dbdef($dbdef_file);
53
54 ###
55 # create 'em
56 ###
57
58 warn "Connecting to database\n" if $opt_v;
59
60 $FS::CurrentUser::upgrade_hack = 1;
61 $FS::UID::callback_hack = 1;
62 my $dbh = adminsuidsetup $opt_u; #$user;
63 $FS::UID::callback_hack = 0;
64
65 #create tables
66 $|=1;
67
68 warn "Creating tables and indices\n" if $opt_v;
69
70 foreach my $statement ( $dbdef->sql($dbh) ) {
71   $dbh->do( $statement )
72     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
73 }
74
75 #now go back and reverse engineer the db
76 #so we pick up the correct column DEFAULTs for #oidless inserts
77 dbdef_create($dbh, $dbdef_file);
78 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
79 reload_dbdef($dbdef_file);
80
81 warn "Tables and indices created - commiting transaction\n" if $opt_v;
82 $dbh->commit or die $dbh->errstr;
83 $dbh->disconnect or die $dbh->errstr;
84 warn "Database schema committed successfully\n" if $opt_v;
85
86 warn "Initializing configuration\n" if $opt_v;
87 $FS::UID::callback_hack = 1;
88 $dbh = adminsuidsetup $opt_u;
89 $FS::UID::callback_hack = 0;
90 if (!scalar(qsearch('conf', {}))) {
91   my $error = FS::Conf::init_config($config_dir);
92   if ($error) {
93     $dbh->rollback or die $dbh->errstr;
94     die $error;
95   }
96 }
97
98 warn "Configuration initialized - commiting transaction\n" if $opt_v;
99 $dbh->commit or die $dbh->errstr;
100 $dbh->disconnect or die $dbh->errstr;
101 warn "Configuration committed successfully\n" if $opt_v;
102
103 $dbh = adminsuidsetup $opt_u;
104 create_initial_data('domain' => $opt_d);
105
106 warn "Database initialized - commiting transaction\n" if $opt_v;
107 $dbh->commit or die $dbh->errstr;
108 $dbh->disconnect or die $dbh->errstr;
109 warn "Database initialization committed successfully\n" if $opt_v;
110
111 sub dbdef_create { # reverse engineer the schema from the DB and save to file
112   my( $dbh, $file ) = @_;
113   my $dbdef = new_native DBIx::DBSchema $dbh;
114   $dbdef->save($file);
115 }
116
117 sub usage {
118   die "Usage:\n  freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
119   # [ -u user ] for devel/multi-db installs
120 }
121
122 1;
123
124