aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-07-17 14:07:49 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-08-03 10:21:23 +0100
commitac2e17b025eee17ab7fc0bcb0906faf2af7a3a41 (patch)
tree4999964b32a7d36193edfb7f58d56d447d70c81d
parentf3994a336c33b585e116674baeb19fd918eb3ab0 (diff)
Allow update-schema to run on empty database.
-rwxr-xr-xbin/install-as-user6
-rwxr-xr-xbin/update-schema53
2 files changed, 32 insertions, 27 deletions
diff --git a/bin/install-as-user b/bin/install-as-user
index 2730fe404..e42401758 100755
--- a/bin/install-as-user
+++ b/bin/install-as-user
@@ -98,12 +98,8 @@ if ! psql -l | egrep "^ *$DB_NAME *\|" > /dev/null
then
createdb --owner "$UNIX_USER" "$DB_NAME"
echo 'CREATE LANGUAGE plpgsql;' | psql -U "$UNIX_USER" "$DB_NAME" || true
- psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/schema.sql
- psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/alert_types.sql
- psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/generate_secret.sql
-else
- bin/update-schema --commit
fi
+bin/update-schema --commit
echo $DONE_MSG
# Generate po and mo files (these invocations taken from Kagee's script):
diff --git a/bin/update-schema b/bin/update-schema
index cf9a6175c..573cfc19c 100755
--- a/bin/update-schema
+++ b/bin/update-schema
@@ -40,19 +40,12 @@ BEGIN {
}
use FixMyStreet;
-use mySociety::DBHandle qw(dbh);
+use FixMyStreet::DB;
use mySociety::MaPit;
use Getopt::Long;
use Pod::Usage;
-my %args = (
- Name => FixMyStreet->config('FMS_DB_NAME'),
- User => FixMyStreet->config('FMS_DB_USER'),
- Password => FixMyStreet->config('FMS_DB_PASS'),
-);
-$args{Host} = FixMyStreet->config('FMS_DB_HOST', undef) if FixMyStreet->config('FMS_DB_HOST');
-$args{Port} = FixMyStreet->config('FMS_DB_PORT', undef) if FixMyStreet->config('FMS_DB_PORT');
-mySociety::DBHandle::configure( %args );
+my $db = FixMyStreet::DB->storage;
my ($commit, $version, $downgrade, $help);
@@ -106,15 +99,24 @@ sub get_statements {
}
sub run_statements {
+ $db->txn_begin;
foreach my $st (@_) {
print ".";
- dbh()->do($st);
+ $db->dbh->do($st);
}
- dbh()->do('COMMIT');
+ $db->txn_commit;
print "\n";
}
-if ($upgrade) {
+if ($upgrade && $current_version eq 'EMPTY') {
+ print "* Database empty, loading in whole schema\n";
+ $nothing = 0;
+ if ($commit) {
+ run_statements(get_statements("$bin_dir/../db/schema.sql"));
+ run_statements(get_statements("$bin_dir/../db/generate_secret.sql"));
+ run_statements(get_statements("$bin_dir/../db/alert_types.sql"));
+ }
+} elsif ($upgrade) {
if ($version) {
die "Not currently implemented";
}
@@ -136,13 +138,13 @@ if ($upgrade) {
if ( $commit && $current_version lt '0028' ) {
$nothing = 0;
print "Bodies created, fetching names from mapit\n";
- my $area_ids = dbh()->selectcol_arrayref('SELECT area_id FROM body_areas');
+ my $area_ids = $db->dbh->selectcol_arrayref('SELECT area_id FROM body_areas');
if ( @$area_ids ) {
my $areas = mySociety::MaPit::call('areas', $area_ids);
foreach (values %$areas) {
- dbh()->do('UPDATE body SET name=? WHERE id=?', {}, $_->{name}, $_->{id});
+ $db->dbh->do('UPDATE body SET name=? WHERE id=?', {}, $_->{name}, $_->{id});
}
- dbh()->do('COMMIT');
+ $db->txn_commit;
}
}
}
@@ -194,6 +196,7 @@ else {
# By querying the database schema, we can see where we're currently at
# (assuming schema change files are never half-applied, which should be the case)
sub get_db_version {
+ return 'EMPTY' if ! table_exists('problem');
return '0051' if column_exists('contacts', 'state');
return '0050' if table_exists('defect_types');
return '0049' if column_exists('response_priorities', 'external_id');
@@ -245,42 +248,48 @@ sub get_db_version {
return '0006' if 0;
return '0005-add_council_user_flag' if column_exists('users', 'from_council');
return '0005-add_abuse_flags_to_users_and_reports' if column_exists('problem', 'flagged');
- return '0000';
+ return '0004' if column_exists('comment', 'user_id');
+ return '0003' if column_exists('alert', 'user_id');
+ return '0002' if column_exists('problem', 'user_id');
+ return '0001' if table_exists('sessions');
+ return '0000' if table_exists('problem');
+ die "Database schema issue!";
}
# Returns true if a table exists
sub table_exists {
my $table = shift;
- return dbh()->selectrow_array('select count(*) from pg_tables where tablename = ?', {}, $table);
+ return $db->dbh->selectrow_array('select count(*) from pg_tables where tablename = ?', {}, $table);
}
# Returns true if a column of table exists
sub column_exists {
my ( $table, $column ) = @_;
- return dbh()->selectrow_array('select count(*) from pg_class, pg_attribute WHERE pg_class.relname=? AND pg_attribute.attname=? AND pg_class.oid=pg_attribute.attrelid AND pg_attribute.attnum > 0', {}, $table, $column);
+ return $db->dbh->selectrow_array('select count(*) from pg_class, pg_attribute WHERE pg_class.relname=? AND pg_attribute.attname=? AND pg_class.oid=pg_attribute.attrelid AND pg_attribute.attnum > 0', {}, $table, $column);
}
# Returns true if a column of a row in a table contains some text
sub column_like {
my ( $table, $where, $column, $contents ) = @_;
- return dbh()->selectrow_array("select count(*) from $table WHERE $where AND $column LIKE ?", {}, "%$contents%");
+ return $db->dbh->selectrow_array("select count(*) from $table WHERE $where AND $column LIKE ?", {}, "%$contents%");
}
# Returns true if a check constraint on a table exists
sub constraint_exists {
my ( $constraint ) = @_;
- return dbh()->selectrow_array('select count(*) from pg_constraint where conname = ?', {}, $constraint);
+ return $db->dbh->selectrow_array('select count(*) from pg_constraint where conname = ?', {}, $constraint);
}
# Returns true if a check constraint contains a certain string
sub constraint_contains {
my ( $constraint, $check ) = @_;
- my ($consrc) = dbh()->selectrow_array('select consrc from pg_constraint where conname = ?', {}, $constraint);
+ my ($consrc) = $db->dbh->selectrow_array('select consrc from pg_constraint where conname = ?', {}, $constraint);
+ return unless $consrc;
return $consrc =~ /$check/;
}
# Returns true if a function exists
sub function_exists {
my $fn = shift;
- return dbh()->selectrow_array('select count(*) from pg_proc where proname = ?', {}, $fn);
+ return $db->dbh->selectrow_array('select count(*) from pg_proc where proname = ?', {}, $fn);
}