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 $opt_q);
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 $opt_v = 1 unless $opt_q; #verbose by default now
30 my $config_dir = shift || '%%%DIST_CONF%%%' ;
31 $config_dir =~ /^([\w.:=\/]+)$/
32 or die "unacceptable configuration directory name";
37 #needs to match FS::Record
38 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
40 my $username_len = 32;
43 # create a dbdef object from the old data structure
46 warn "Loading schema objects\n" if $opt_v;
48 my $dbdef = dbdef_dist(datasrc);
51 $dbdef->save($dbdef_file);
52 &FS::Schema::reload_dbdef($dbdef_file);
58 warn "Connecting to database\n" if $opt_v;
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;
68 warn "Creating tables and indices\n" if $opt_v;
70 foreach my $statement ( $dbdef->sql($dbh) ) {
71 $dbh->do( $statement )
72 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
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);
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;
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);
93 $dbh->rollback or die $dbh->errstr;
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;
103 $dbh = adminsuidsetup $opt_u;
104 create_initial_data('domain' => $opt_d);
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;
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;
118 die "Usage:\n freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
119 # [ -u user ] for devel/multi-db installs