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;
46 my $username_len = 32;
49 # create a dbdef object from the old data structure
52 warn "Loading schema objects\n" if $opt_v;
54 my $dbdef = dbdef_dist(datasrc);
57 $dbdef->save($dbdef_file);
58 &FS::Schema::reload_dbdef($dbdef_file);
64 warn "Connecting to database\n" if $opt_v;
66 $FS::CurrentUser::upgrade_hack = 1;
67 $FS::UID::callback_hack = 1;
68 my $dbh = adminsuidsetup $opt_u; #$user;
69 $FS::UID::callback_hack = 0;
74 warn "Creating tables and indices\n" if $opt_v;
76 foreach my $statement ( $dbdef->sql($dbh) ) {
77 $dbh->do( $statement )
78 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
81 #now go back and reverse engineer the db
82 #so we pick up the correct column DEFAULTs for #oidless inserts
83 dbdef_create($dbh, $dbdef_file);
84 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
85 reload_dbdef($dbdef_file);
87 warn "Tables and indices created - commiting transaction\n" if $opt_v;
88 $dbh->commit or die $dbh->errstr;
89 $dbh->disconnect or die $dbh->errstr;
90 warn "Database schema committed successfully\n" if $opt_v;
92 warn "Initializing configuration\n" if $opt_v;
93 $FS::UID::callback_hack = 1;
94 $dbh = adminsuidsetup $opt_u;
95 $FS::UID::callback_hack = 0;
96 if (!scalar(qsearch('conf', {}))) {
97 my $error = FS::Conf::init_config($config_dir);
99 $dbh->rollback or die $dbh->errstr;
104 warn "Configuration initialized - commiting transaction\n" if $opt_v;
105 $dbh->commit or die $dbh->errstr;
106 $dbh->disconnect or die $dbh->errstr;
107 warn "Configuration committed successfully\n" if $opt_v;
109 $dbh = adminsuidsetup $opt_u;
110 create_initial_data('domain' => $opt_d);
112 warn "Database initialized - commiting transaction\n" if $opt_v;
113 $dbh->commit or die $dbh->errstr;
114 $dbh->disconnect or die $dbh->errstr;
115 warn "Database initialization committed successfully\n" if $opt_v;
117 sub dbdef_create { # reverse engineer the schema from the DB and save to file
118 my( $dbh, $file ) = @_;
119 my $dbdef = new_native DBIx::DBSchema $dbh;
124 die "Usage:\n freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
125 # [ -u user ] for devel/multi-db installs