9 #to delay loading dbdef until we're ready
10 BEGIN { $FS::Schema::setup_hack = 1; }
12 #to allow initial insert
14 $FS::part_pkg::setup_hack = 1;
15 $FS::part_pkg::setup_hack = 1;
18 use vars qw($opt_u $opt_d $opt_v $opt_q);
20 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
22 use FS::Schema qw( dbdef_dist reload_dbdef );
23 use FS::Record qw( qsearch );
25 use FS::Setup qw(create_initial_data);
28 die "Not running uid freeside!" unless checkeuid();
31 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
34 $opt_v = 1 unless $opt_q; #verbose by default now
36 my $config_dir = shift || '%%%DIST_CONF%%%' ;
37 $config_dir =~ /^([\w.:=\/]+)$/
38 or die "unacceptable configuration directory name";
43 #needs to match FS::Record
44 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
48 my $username_len = 32;
50 #print "\n\n", <<END, ":";
51 #Freeside tracks the RADIUS User-Name, check attribute Password and
52 #reply attribute Framed-IP-Address for each user. You can specify additional
53 #check and reply attributes (or you can add them later with the
54 #fs-radius-add-check and fs-radius-add-reply programs).
56 #First enter any additional RADIUS check attributes you need to track for each
57 #user, separated by whitespace.
59 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
60 # split(" ",&getvalue);
62 #print "\n\n", <<END, ":";
63 #Now enter any additional reply attributes you need to track for each user,
64 #separated by whitespace.
66 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
67 # split(" ",&getvalue);
69 #print "\n\n", <<END, ":";
70 #Do you wish to enable the tracking of a second, separate shipping/service
76 # my($x)=scalar(<STDIN>);
83 # my $x = scalar(<STDIN>);
87 #my @check_attributes = (); #add later
88 #my @attributes = (); #add later
92 # create a dbdef object from the old data structure
95 warn "Loading schema objects\n" if $opt_v;
97 my $dbdef = dbdef_dist(datasrc);
100 $dbdef->save($dbdef_file);
101 &FS::Schema::reload_dbdef($dbdef_file);
107 warn "Connecting to database\n" if $opt_v;
109 $FS::CurrentUser::upgrade_hack = 1;
110 $FS::UID::callback_hack = 1;
111 my $dbh = adminsuidsetup $opt_u; #$user;
112 $FS::UID::callback_hack = 0;
117 warn "Creating tables and indices\n" if $opt_v;
119 foreach my $statement ( $dbdef->sql($dbh) ) {
120 $dbh->do( $statement )
121 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
124 #now go back and reverse engineer the db
125 #so we pick up the correct column DEFAULTs for #oidless inserts
126 dbdef_create($dbh, $dbdef_file);
127 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
128 reload_dbdef($dbdef_file);
130 warn "Tables and indices created - commiting transaction\n" if $opt_v;
131 $dbh->commit or die $dbh->errstr;
132 $dbh->disconnect or die $dbh->errstr;
133 warn "Database schema committed successfully\n" if $opt_v;
135 warn "Initializing configuration\n" if $opt_v;
136 $FS::UID::callback_hack = 1;
137 $dbh = adminsuidsetup $opt_u;
138 $FS::UID::callback_hack = 0;
139 if (!scalar(qsearch('conf', {}))) {
140 my $error = FS::Conf::init_config($config_dir);
142 $dbh->rollback or die $dbh->errstr;
147 warn "Configuration initialized - commiting transaction\n" if $opt_v;
148 $dbh->commit or die $dbh->errstr;
149 $dbh->disconnect or die $dbh->errstr;
150 warn "Configuration committed successfully\n" if $opt_v;
152 $dbh = adminsuidsetup $opt_u;
153 create_initial_data('domain' => $opt_d);
155 warn "Database initialized - commiting transaction\n" if $opt_v;
156 $dbh->commit or die $dbh->errstr;
157 $dbh->disconnect or die $dbh->errstr;
158 warn "Database initialization committed successfully\n" if $opt_v;
160 sub dbdef_create { # reverse engineer the schema from the DB and save to file
161 my( $dbh, $file ) = @_;
162 my $dbdef = new_native DBIx::DBSchema $dbh;
167 die "Usage:\n freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
168 # [ -u user ] for devel/multi-db installs