diff --git a/Netdisco/bin/create_netdisco_schema_version b/Netdisco/bin/nd-dbic-versions similarity index 89% rename from Netdisco/bin/create_netdisco_schema_version rename to Netdisco/bin/nd-dbic-versions index e45d71f5..84e56b1c 100755 --- a/Netdisco/bin/create_netdisco_schema_version +++ b/Netdisco/bin/nd-dbic-versions @@ -9,8 +9,24 @@ use Dancer::Plugin::DBIC 'schema'; use Netdisco::DB; use Getopt::Long; +=head1 create_netdisco_schema_version + +This script creates SQL DDL files of the Netdisco database schema. + +If called without any CLI options, it makes one SQL DDL file which will +initialize the complete schema to the current DBIx::Class specification. + +If called with the "-p " option, upgrade SQL DDL command files +are created between the specified version and the current DBIx::Class +specification. + +=cut + +schema('netdisco')->create_ddl_dir( + 'PostgreSQL', $version, $sql_dir, $preversion ); + my $sql_dir = $Netdisco::DB::schema_versions_dir; -my $version = schema->schema_version(); +my $version = schema('netdisco')->schema_version; my ( $preversion, $help ); GetOptions( @@ -33,18 +49,3 @@ $sql_dir ENDHELP exit(1); }; - -schema->create_ddl_dir( 'PostgreSQL', $version, $sql_dir, $preversion ); - -=head1 create_netdisco_schema_version - -This script creates SQL DDL files of the Netdisco database schema. - -If called without any CLI options, it makes one SQL DDL file which will -initialize the complete schema to the current DBIx::Class specification. - -If called with the "-p " option, upgrade SQL DDL command files -are created between the specified version and the current DBIx::Class -specification. - -=cut diff --git a/Netdisco/bin/upgrade_netdisco_schema_version b/Netdisco/bin/netdisco-db-deploy similarity index 61% rename from Netdisco/bin/upgrade_netdisco_schema_version rename to Netdisco/bin/netdisco-db-deploy index 4be0d93f..858713fb 100755 --- a/Netdisco/bin/upgrade_netdisco_schema_version +++ b/Netdisco/bin/netdisco-db-deploy @@ -7,27 +7,6 @@ use Dancer ':script'; use Dancer::Plugin::DBIC 'schema'; use Try::Tiny; -use feature 'say'; - -if (not schema->get_db_version()) { - # installs the dbix_class_schema_versions table with version "1" - # which corresponds to an empty schema - say 'Installing DBIx::Class versioning to Netdisco::DB schema...'; - schema->txn_do(sub { schema->install("1") }); - exec $0; -} - -try { - # if schema exists, assume it's current public Netdisco version - if (schema->get_db_version() == 1 - and my $count = schema->resultset('Device')->count()) { - schema->txn_do(sub { schema->_set_db_version("2") }); - } -}; - -# upgrades from whatever dbix_class_schema_versions says, to $VERSION -say 'Upgrading Netdisco::DB schema...'; -schema->txn_do(sub { schema->upgrade() }); =head1 upgrade_netdisco_schema_version @@ -66,3 +45,29 @@ Version 4 (not yet created) B =back =cut + +my $schema = schema('netdisco'); + +# installs the dbix_class_schema_versions table with version "1" +# which corresponds to an empty schema +if (not $schema->get_db_version) { + $schema->txn_do(sub { $schema->install(1) }); + $schema->storage->disconnect; +} + +# test for existing schema at public release version, set v=2 if so +try { + $schema->storage->dbh_do(sub { + my ($storage, $dbh) = @_; + $dbh->selectrow_arrayref("SELECT * FROM device WHERE 0 = 1"); + }); + + $schema->_set_db_version(2) + if $schema->get_db_version == 1; + $schema->storage->disconnect; +}; + +# upgrade from whatever dbix_class_schema_versions says, to $VERSION +$schema->txn_do(sub { $schema->upgrade }); + +exit 0; diff --git a/Netdisco/lib/Netdisco/Daemon/Worker/Manager.pm b/Netdisco/lib/Netdisco/Daemon/Worker/Manager.pm index 12bcbf07..a957661e 100644 --- a/Netdisco/lib/Netdisco/Daemon/Worker/Manager.pm +++ b/Netdisco/lib/Netdisco/Daemon/Worker/Manager.pm @@ -20,11 +20,11 @@ sub worker_begin { $dbh->selectrow_arrayref("SELECT * FROM admin WHERE 0 = 1"); }); } - catch { $daemon->deploy }; + catch { $daemon->txn_do( $daemon->deploy ) }; $daemon->storage->disconnect; if ($daemon->get_db_version < $daemon->schema_version) { - $daemon->upgrade; + $daemon->txn_do( $daemon->upgrade ); } # on start, any jobs previously grabbed by a daemon on this host