3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
6 #to allow initial insert
8 $FS::part_pkg::setup_hack = 1;
9 $FS::part_pkg::setup_hack = 1;
12 use vars qw($opt_u $opt_d $opt_v);
14 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
16 use FS::Schema qw( dbdef_dist reload_dbdef );
17 use FS::Record qw( qsearch );
19 use FS::Setup qw(create_initial_data);
22 die "Not running uid freeside!" unless checkeuid();
25 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
28 my $config_dir = shift || '%%%DIST_CONF%%%' ;
29 $config_dir =~ /^([\w.:=\/]+)$/
30 or die "unacceptable configuration directory name";
35 #needs to match FS::Record
36 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
40 my $username_len = 32;
42 #print "\n\n", <<END, ":";
43 #Freeside tracks the RADIUS User-Name, check attribute Password and
44 #reply attribute Framed-IP-Address for each user. You can specify additional
45 #check and reply attributes (or you can add them later with the
46 #fs-radius-add-check and fs-radius-add-reply programs).
48 #First enter any additional RADIUS check attributes you need to track for each
49 #user, separated by whitespace.
51 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
52 # split(" ",&getvalue);
54 #print "\n\n", <<END, ":";
55 #Now enter any additional reply attributes you need to track for each user,
56 #separated by whitespace.
58 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
59 # split(" ",&getvalue);
61 #print "\n\n", <<END, ":";
62 #Do you wish to enable the tracking of a second, separate shipping/service
68 # my($x)=scalar(<STDIN>);
75 # my $x = scalar(<STDIN>);
79 #my @check_attributes = (); #add later
80 #my @attributes = (); #add later
84 # create a dbdef object from the old data structure
87 my $dbdef = dbdef_dist(datasrc);
90 $dbdef->save($dbdef_file);
91 &FS::Schema::reload_dbdef($dbdef_file);
97 $FS::CurrentUser::upgrade_hack = 1;
98 $FS::UID::callback_hack = 1;
99 my $dbh = adminsuidsetup $opt_u; #$user;
100 $FS::UID::callback_hack = 0;
105 foreach my $statement ( $dbdef->sql($dbh) ) {
106 $dbh->do( $statement )
107 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
110 #now go back and reverse engineer the db
111 #so we pick up the correct column DEFAULTs for #oidless inserts
112 dbdef_create($dbh, $dbdef_file);
113 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
114 reload_dbdef($dbdef_file);
116 warn "Freeside schema initialized - commiting transaction\n" if $opt_v;
118 $dbh->commit or die $dbh->errstr;
119 $dbh->disconnect or die $dbh->errstr;
121 warn "Database schema committed successfully\n" if $opt_v;
123 warn "Initializing freeside configuration\n" if $opt_v;
124 $FS::UID::callback_hack = 1;
125 $dbh = adminsuidsetup $opt_u;
126 $FS::UID::callback_hack = 0;
127 if (!scalar(qsearch('conf', {}))) {
128 my $error = FS::Conf::init_config($config_dir);
130 $dbh->rollback or die $dbh->errstr;
135 warn "Freeside configuration initialized - commiting transaction\n" if $opt_v;
137 $dbh->commit or die $dbh->errstr;
138 $dbh->disconnect or die $dbh->errstr;
140 warn "Freeside configuration committed successfully\n" if $opt_v;
142 $dbh = adminsuidsetup $opt_u;
143 create_initial_data('domain' => $opt_d);
145 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
147 $dbh->commit or die $dbh->errstr;
148 $dbh->disconnect or die $dbh->errstr;
150 warn "Database initialization committed successfully\n" if $opt_v;
152 sub dbdef_create { # reverse engineer the schema from the DB and save to file
153 my( $dbh, $file ) = @_;
154 my $dbdef = new_native DBIx::DBSchema $dbh;
159 die "Usage:\n freeside-setup -d domain.name [ -v ] [ config/dir ]\n"
160 # [ -u user ] for devel/multi-db installs