From e99f5ee410b32b222fe74156a998fe0b94da5a3a Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Wed, 4 Feb 2015 21:43:44 +0000 Subject: [PATCH] [#199] Missing schema changes when user has no permissions on DB This fixes many glitches with the schema deployment: - silently ignoring real failure modes such as user not having correct permissions on the DB - a couple of broken upgrade steps - adds --reset so schema can be fully redeployed --- Netdisco/Changes | 6 ++++ Netdisco/bin/netdisco-db-deploy | 33 +++++++++++++++---- Netdisco/lib/App/Netdisco/DB.pm | 2 +- .../App-Netdisco-DB-17-18-PostgreSQL.sql | 2 ++ .../App-Netdisco-DB-38-39-PostgreSQL.sql | 2 -- .../App-Netdisco-DB-39-40-PostgreSQL.sql | 17 ++++++++++ .../lib/App/Netdisco/Manual/Deployment.pod | 7 ++++ .../lib/App/Netdisco/Manual/ReleaseNotes.pod | 20 ++++++++++- 8 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-39-40-PostgreSQL.sql diff --git a/Netdisco/Changes b/Netdisco/Changes index 9cda62a2..fdbec3f9 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -1,3 +1,9 @@ +2.031003 - 2015-02-04 + + [BUG FIXES] + + * [#199] Missing schema changes when user has no permissions on DB + 2.031002 - 2015-02-04 [BUG FIXES] diff --git a/Netdisco/bin/netdisco-db-deploy b/Netdisco/bin/netdisco-db-deploy index 77875807..5a3cef55 100755 --- a/Netdisco/bin/netdisco-db-deploy +++ b/Netdisco/bin/netdisco-db-deploy @@ -50,15 +50,20 @@ netdisco-db-deploy - Database deployment for Netdisco This script upgrades or initialises a Netdisco database schema. + ~netdisco/bin/netdisco-db-deploy [--reset] + +This script connects to the database and runs without user interaction. If +there's no Nedisco schema, it is deployed. If there's an unversioned schema +then versioning is added, and updates applied. Otherwise only necessary +updates are applied to an already versioned schema. + Pre-existing requirements are that there be a database table created and a user with rights to create tables in that database. Both the table and user name must match those configured in your environment YAML file (default C<~/environments/deployment.yml>). -Simply run this script, which connects to the database and runs without user -interaction. If there's no Nedisco schema, it is deployed. If there's an -unversioned schema then versioning is added, and updates applied. Otherwise -only necessary updates are applied to an already versioned schema. +If you wish to force the redeployment of all database configuration, pass the +C<--reset> argument on the command line. =head1 VERSIONS @@ -74,7 +79,11 @@ Version 2 is the "classic" Netdisco database schema as of Netdisco 1.1 =item * -Version 5 (and onwards) adds patches for Netdisco 1.2 +Versions 5 to 16 add patches for Netdisco 1.2 + +=item * + +Version 17 onwards deploys schema upgrades for Netdisco 2 =back @@ -82,6 +91,15 @@ Version 5 (and onwards) adds patches for Netdisco 1.2 my $schema = schema('netdisco'); +if (scalar @ARGV and $ARGV[0] and $ARGV[0] eq '--reset') { + $schema->storage->dbh_do( + sub { + my ($storage, $dbh, @args) = @_; + $dbh->do('DROP TABLE dbix_class_schema_versions'); + }, + ); +} + # installs the dbix_class_schema_versions table with version "1" # which corresponds to an empty schema if (not $schema->get_db_version) { @@ -112,8 +130,9 @@ for (my $i = $db_version; $i < $target_version; $i++) { $schema->upgrade_single_step($i, $i + 1); } catch { - $schema->_set_db_version({version => $i + 1}); - } + warn "Error: $_" + if $_ !~ m/(does not exist|already exists)/; + }; } exit 0; diff --git a/Netdisco/lib/App/Netdisco/DB.pm b/Netdisco/lib/App/Netdisco/DB.pm index e0d6a45c..f0e99f84 100644 --- a/Netdisco/lib/App/Netdisco/DB.pm +++ b/Netdisco/lib/App/Netdisco/DB.pm @@ -11,7 +11,7 @@ __PACKAGE__->load_namespaces( ); our # try to hide from kwalitee - $VERSION = 39; # schema version used for upgrades, keep as integer + $VERSION = 40; # schema version used for upgrades, keep as integer use Path::Class; use File::Basename; diff --git a/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-17-18-PostgreSQL.sql b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-17-18-PostgreSQL.sql index 1f5ebea3..1a9535a1 100644 --- a/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-17-18-PostgreSQL.sql +++ b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-17-18-PostgreSQL.sql @@ -1,5 +1,7 @@ BEGIN; +ALTER TABLE admin DROP CONSTRAINT IF EXISTS admin_pkey; + ALTER TABLE admin ADD PRIMARY KEY (job); COMMIT; diff --git a/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-38-39-PostgreSQL.sql b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-38-39-PostgreSQL.sql index 686b2f38..578614b7 100644 --- a/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-38-39-PostgreSQL.sql +++ b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-38-39-PostgreSQL.sql @@ -12,6 +12,4 @@ BEGIN; -DELETE n1.* FROM node n1 INNER JOIN (SELECT mac, switch, port from node GROUP BY mac, switch, port HAVING count(*) > 1) n2 ON n1.mac = n2.mac AND n1.switch = n2.switch AND n1.port = n2.port AND n1.vlan = '0'; - COMMIT; diff --git a/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-39-40-PostgreSQL.sql b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-39-40-PostgreSQL.sql new file mode 100644 index 00000000..a274134b --- /dev/null +++ b/Netdisco/lib/App/Netdisco/DB/schema_versions/App-Netdisco-DB-39-40-PostgreSQL.sql @@ -0,0 +1,17 @@ +-- clean up node table where vlan = 0 and vlan = +-- +-- DELETE n1.* +-- FROM node n1 INNER JOIN +-- (SELECT mac, switch, port from node +-- GROUP BY mac, switch, port +-- HAVING count(*) > 1) n2 +-- ON n1.mac = n2.mac +-- AND n1.switch = n2.switch +-- AND n1.port = n2.port +-- AND n1.vlan = '0'; + +BEGIN; + +DELETE FROM node AS n1 USING (SELECT mac, switch, port from node GROUP BY mac, switch, port HAVING count(*) > 1) n2 WHERE n1.mac = n2.mac AND n1.switch = n2.switch AND n1.port = n2.port AND n1.vlan = '0'; + +COMMIT; diff --git a/Netdisco/lib/App/Netdisco/Manual/Deployment.pod b/Netdisco/lib/App/Netdisco/Manual/Deployment.pod index 2917750a..e208a952 100644 --- a/Netdisco/lib/App/Netdisco/Manual/Deployment.pod +++ b/Netdisco/lib/App/Netdisco/Manual/Deployment.pod @@ -177,6 +177,13 @@ following commands into a shell script and call it nightly from C: This will keep 30 days of backups. You don't need to stop Netdisco during the backup. +=head1 Database Schema Redeployment + +The database schema can be fully redeployed (even over an existing +installation, in a safe way) using the following command: + + ~netdisco/bin/netdisco-db-deploy --reset + =head1 Further Reading... Other ways to run and host the web application can be found in the diff --git a/Netdisco/lib/App/Netdisco/Manual/ReleaseNotes.pod b/Netdisco/lib/App/Netdisco/Manual/ReleaseNotes.pod index d4fa9831..dace2383 100644 --- a/Netdisco/lib/App/Netdisco/Manual/ReleaseNotes.pod +++ b/Netdisco/lib/App/Netdisco/Manual/ReleaseNotes.pod @@ -36,7 +36,25 @@ but they are backwards compatible. =back -=head1 2.031000 +=head1 2.031003 + +=head2 Health Advice + +This release will I remove from the database spurious Node +(workstation, printer, etc) entries on vlan 0, which were causing dupliate +entries in the web interface. We advise that you back up the database prior to +upgrade: + + /usr/bin/pg_dump -F p --create -f netdisco-pgsql.dump netdisco + +=head2 General Notices + +=head1 2.031003 + +The database schema can be fully redeployed (even over an existing +installation, in a safe way) using the following command: + + ~netdisco/bin/netdisco-db-deploy --reset =head2 General Notices