From 772fc261f686a569e0339aa5d89a19b03114aed6 Mon Sep 17 00:00:00 2001 From: Thomas Berezansky Date: Thu, 18 Aug 2011 12:06:46 -0400 Subject: [PATCH] Add --create-database option to eg_db_config.pl Uses create_database.sql and superuser rights to do all database creation steps other than making a usable superuser account to begin with. Signed-off-by: Thomas Berezansky --- Open-ILS/src/sql/Pg/create_database.sql | 30 ++++++++++++++ Open-ILS/src/support-scripts/eg_db_config.pl | 41 +++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/sql/Pg/create_database.sql diff --git a/Open-ILS/src/sql/Pg/create_database.sql b/Open-ILS/src/sql/Pg/create_database.sql new file mode 100644 index 0000000000..b3be0f0da0 --- /dev/null +++ b/Open-ILS/src/sql/Pg/create_database.sql @@ -0,0 +1,30 @@ +-- This file is intended to be called by eg_db_config.pl + +-- If manually calling: +-- Connect to the postgres database initially +-- Specify the database to create as -vdb_name=DATABASE +-- Specify the postgres contrib directory as -vcontrib_dir=CONTRIBDIR +-- You can get the contrib directory using pg_config --sharedir and adding a /contrib to it + +-- NOTE: This file does not do transactions +-- This is intentional. Please do not wrap in BEGIN/COMMIT. +DROP DATABASE IF EXISTS :db_name; + +CREATE DATABASE :db_name TEMPLATE template0 ENCODING 'UNICODE' LC_COLLATE 'C' LC_CTYPE 'C'; + +\connect :db_name + +CREATE LANGUAGE plperl; +CREATE LANGUAGE plperlu; + +-- This dance is because :variable/blah doesn't seem to work when doing \i +-- But it does when doing \set +-- So we \set to a single variable, then use that single variable with \i +\set load_file :contrib_dir/tablefunc.sql +\i :load_file +\set load_file :contrib_dir/tsearch2.sql +\i :load_file +\set load_file :contrib_dir/pgxml.sql +\i :load_file +\set load_file :contrib_dir/hstore.sql +\i :load_file diff --git a/Open-ILS/src/support-scripts/eg_db_config.pl b/Open-ILS/src/support-scripts/eg_db_config.pl index 498eaf5b01..7a3c2a8f97 100755 --- a/Open-ILS/src/support-scripts/eg_db_config.pl +++ b/Open-ILS/src/support-scripts/eg_db_config.pl @@ -31,6 +31,8 @@ my $build_db_sh = ''; my $offline_file = ''; my $prefix = ''; my $sysconfdir = ''; +my $pg_contribdir = ''; +my $create_db_sql = ''; my @services; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); @@ -124,6 +126,20 @@ sub get_settings { $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw); } +=item create_database() - Creates the database using create_database.sql +=cut +sub create_database { + my $settings = shift; + + $ENV{'PGUSER'} = $settings->{user}; + $ENV{'PGPASSWORD'} = $settings->{pw}; + $ENV{'PGPORT'} = $settings->{port}; + $ENV{'PGHOST'} = $settings->{host}; + my $cmd = 'psql -vdb_name=' . $settings->{db} . ' -vcontrib_dir=' . $pg_contribdir . + ' -d postgres -f ' . $create_db_sql; + system($cmd); +} + =item create_schema() - Creates the database schema by calling build-db.sh =cut sub create_schema { @@ -164,15 +180,21 @@ sub set_admin_account { } my $offline; +my $cdatabase; my $cschema; my $uconfig; +my $pgconfig; my %settings; GetOptions("create-schema" => \$cschema, + "create-database" => \$cdatabase, "create-offline" => \$offline, "update-config" => \$uconfig, "config-file=s" => \$config_file, "build-db-file=s" => \$build_db_sh, + "pg-contrib-dir=s" => \$pg_contribdir, + "create-db-sql=s" => \$create_db_sql, + "pg-config" => \$pgconfig, "admin-user=s" => \$admin_user, "admin-password=s" => \$admin_pw, "service=s" => \@services, @@ -207,25 +229,38 @@ if (!$build_db_sh) { $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh'); } +if (!$pg_contribdir) { + $pgconfig = 'pg_config' if(!$pgconfig); + my @temp = `$pgconfig --sharedir`; + chomp $temp[0]; + $pg_contribdir = File::Spec->catdir($temp[0], 'contrib'); +} + +if (!$create_db_sql) { + $create_db_sql = File::Spec->catfile($script_dir, '../sql/Pg/create_database.sql'); +} + if (!$offline_file) { $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl'); } unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; } unless (-e $config_file) { die "Error: $config_file does not exist. \n"; } +unless (-d $pg_contribdir || !$cdatabase) { die "Error: $pg_contribdir does not exist. \n"; } if ($uconfig) { update_config(\@services, \%settings); } # Get our settings from the config file get_settings(\%settings); +if ($cdatabase) { create_database(\%settings); } if ($cschema) { create_schema(\%settings); } if ($admin_user && $admin_pw) { set_admin_account($admin_user, $admin_pw, \%settings); } if ($offline) { create_offline_config($offline_file, \%settings); } -if ((!$cschema && !$uconfig && !$offline && !$admin_pw) || $help) { +if ((!$cdatabase && !$cschema && !$uconfig && !$offline && !$admin_pw) || $help) { print <