3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
6 #to allow initial insert
7 $FS::part_pkg::setup_hack = 1;
8 $FS::part_pkg::setup_hack = 1;
11 use vars qw($opt_u $opt_d $opt_v);
13 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
15 use FS::Schema qw( dbdef_dist reload_dbdef );
16 use FS::Record qw( qsearch );
18 use FS::Setup qw(create_initial_data);
21 die "Not running uid freeside!" unless checkeuid();
24 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
27 my $config_dir = shift || '%%%DIST_CONF%%%' ;
28 $config_dir =~ /^([\w.:=\/]+)$/
29 or die "unacceptable configuration directory name";
34 #needs to match FS::Record
35 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
39 my $username_len = 32;
41 #print "\n\n", <<END, ":";
42 #Freeside tracks the RADIUS User-Name, check attribute Password and
43 #reply attribute Framed-IP-Address for each user. You can specify additional
44 #check and reply attributes (or you can add them later with the
45 #fs-radius-add-check and fs-radius-add-reply programs).
47 #First enter any additional RADIUS check attributes you need to track for each
48 #user, separated by whitespace.
50 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
51 # split(" ",&getvalue);
53 #print "\n\n", <<END, ":";
54 #Now enter any additional reply attributes you need to track for each user,
55 #separated by whitespace.
57 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
58 # split(" ",&getvalue);
60 #print "\n\n", <<END, ":";
61 #Do you wish to enable the tracking of a second, separate shipping/service
67 # my($x)=scalar(<STDIN>);
74 # my $x = scalar(<STDIN>);
78 #my @check_attributes = (); #add later
79 #my @attributes = (); #add later
83 # create a dbdef object from the old data structure
86 my $dbdef = dbdef_dist(datasrc);
89 $dbdef->save($dbdef_file);
90 &FS::Schema::reload_dbdef($dbdef_file);
96 $FS::CurrentUser::upgrade_hack = 1;
97 $FS::UID::callback_hack = 1;
98 my $dbh = adminsuidsetup $opt_u; #$user;
99 $FS::UID::callback_hack = 0;
104 foreach my $statement ( $dbdef->sql($dbh) ) {
105 $dbh->do( $statement )
106 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
109 #now go back and reverse engineer the db
110 #so we pick up the correct column DEFAULTs for #oidless inserts
111 dbdef_create($dbh, $dbdef_file);
112 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
113 reload_dbdef($dbdef_file);
115 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
117 $dbh->commit or die $dbh->errstr;
118 $dbh->disconnect or die $dbh->errstr;
120 warn "Database schema committed successfully\n" if $opt_v;
122 warn "Initializing freeside configuration\n" if $opt_v;
123 $FS::UID::callback_hack = 1;
124 $dbh = adminsuidsetup $opt_u;
125 $FS::UID::callback_hack = 0;
126 if (!scalar(qsearch('conf', {}))) {
127 my $error = FS::Conf::init_config($config_dir);
129 $dbh->rollback or die $dbh->errstr;
134 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
136 $dbh->commit or die $dbh->errstr;
137 $dbh->disconnect or die $dbh->errstr;
139 warn "Freeside configuration committed successfully\n" if $opt_v;
141 $dbh = adminsuidsetup $opt_u;
142 create_initial_data('domain' => $opt_d);
144 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
146 $dbh->commit or die $dbh->errstr;
147 $dbh->disconnect or die $dbh->errstr;
149 warn "Database initialization committed successfully\n" if $opt_v;
151 sub dbdef_create { # reverse engineer the schema from the DB and save to file
152 my( $dbh, $file ) = @_;
153 my $dbdef = new_native DBIx::DBSchema $dbh;
158 die "Usage:\n freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
159 # [ -u user ] for devel/multi-db installs