From 054dfcbb4e70905a3857e3fc0b9e9f2e4c2e5389 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Sun, 24 Aug 2014 22:54:36 -0400 Subject: [PATCH 1/5] Disconnect after schema version check. --- lib/DBIx/Class/Schema/Versioned.pm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 95adc66b0..0a25485b6 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -613,17 +613,20 @@ sub _on_connect if($pversion eq $self->schema_version) { #carp "This version is already installed"; + $self->{vschema}->storage->disconnect; return 1; } if(!$pversion) { carp "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB."; + $self->{vschema}->storage->disconnect; return 1; } carp "Versions out of sync. This is " . $self->schema_version . ", your database contains version $pversion, please call upgrade on your Schema."; + $self->{vschema}->storage->disconnect; } # is this just a waste of time? if not then merge with DBI.pm From 05a39bd44b9281522cd6aa3ab48ef1bd47c865d8 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Fri, 29 Aug 2014 21:16:31 -0400 Subject: [PATCH 2/5] Revert "Disconnect after schema version check." This reverts commit 054dfcbb4e70905a3857e3fc0b9e9f2e4c2e5389. --- lib/DBIx/Class/Schema/Versioned.pm | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 0a25485b6..95adc66b0 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -613,20 +613,17 @@ sub _on_connect if($pversion eq $self->schema_version) { #carp "This version is already installed"; - $self->{vschema}->storage->disconnect; return 1; } if(!$pversion) { carp "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB."; - $self->{vschema}->storage->disconnect; return 1; } carp "Versions out of sync. This is " . $self->schema_version . ", your database contains version $pversion, please call upgrade on your Schema."; - $self->{vschema}->storage->disconnect; } # is this just a waste of time? if not then merge with DBI.pm From bf537231a640f6c38ea21d986ce181e085889c38 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Fri, 29 Aug 2014 21:35:07 -0400 Subject: [PATCH 3/5] Register the DBIx::Class::Version::Table class rather than creating a schema object in _on_connect to prevent doubling database connections on versioned schemas. --- lib/DBIx/Class/Schema/Versioned.pm | 38 +++++++++++++++++++++++------- t/94versioning.t | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 95adc66b0..f43059f45 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -256,7 +256,7 @@ sub install if ($new_version) { # create versions table and version row - $self->{vschema}->deploy; + $self->_deploy_version_table; $self->_set_db_version({ version => $new_version }); } } @@ -273,6 +273,25 @@ sub deploy { $self->install(); } +sub _deploy_version_table { + my $self = shift; + + my $conn_info = $self->storage->connect_info; + $self->{vschema} = DBIx::Class::Version->connect(@$conn_info); + + my $vtable = $self->resultset('Version::Table'); + unless ($self->_source_exists($vtable)) { + $self->{vschema}->deploy; + } + $self->{vschema}->storage->disconnect; +} + +sub sqlt_deploy_hook { + my ($self, $sqlt_schema) = @_; + + $sqlt_schema->drop_table('dbix_class_schema_versions'); + } + =head2 create_upgrade_path =over 4 @@ -525,7 +544,7 @@ sub get_db_version { my ($self, $rs) = @_; - my $vtable = $self->{vschema}->resultset('Table'); + my $vtable = $self->resultset('Version::Table'); my $version = try { $vtable->search({}, { order_by => { -desc => 'installed' }, rows => 1 } ) ->get_column ('version') @@ -589,22 +608,23 @@ sub _on_connect { my ($self) = @_; - my $conn_info = $self->storage->connect_info; - $self->{vschema} = DBIx::Class::Version->connect(@$conn_info); - my $conn_attrs = $self->{vschema}->storage->_dbic_connect_attributes || {}; + my $conn_attrs = $self->storage->_dbic_connect_attributes || {}; - my $vtable = $self->{vschema}->resultset('Table'); + $self->register_class('Version::Table', 'DBIx::Class::Version::Table'); + my $vtable = $self->resultset('Version::Table'); # useful when connecting from scripts etc return if ($conn_attrs->{ignore_version} || ($ENV{DBIC_NO_VERSION_CHECK} && !exists $conn_attrs->{ignore_version})); # check for legacy versions table and move to new if exists unless ($self->_source_exists($vtable)) { - my $vtable_compat = DBIx::Class::VersionCompat->connect(@$conn_info)->resultset('TableCompat'); + $self->register_class('Version::TableCompat', 'DBIx::Class::Version::TableCompat'); + my $vtable_compat = $self->resultset('Version::TableCompat'); if ($self->_source_exists($vtable_compat)) { - $self->{vschema}->deploy; + $self->_deploy_version_table; map { $vtable->new_result({ installed => $_->Installed, version => $_->Version })->insert } $vtable_compat->all; $self->storage->_get_dbh->do("DROP TABLE " . $vtable_compat->result_source->from); + $self->unregister_source('Version::TableCompat'); } } @@ -694,7 +714,7 @@ sub _set_db_version { $params ||= {}; my $version = $params->{version} ? $params->{version} : $self->schema_version; - my $vtable = $self->{vschema}->resultset('Table'); + my $vtable = $self->resultset('Version::Table'); ############################################################################## # !!! NOTE !!! diff --git a/t/94versioning.t b/t/94versioning.t index 93fcca7fa..2bfd87525 100644 --- a/t/94versioning.t +++ b/t/94versioning.t @@ -66,7 +66,7 @@ $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir); ok(-f $fn->{v1}, 'Created DDL file'); $schema_v1->deploy({ add_drop_table => 1 }); -my $tvrs = $schema_v1->{vschema}->resultset('Table'); +my $tvrs = $schema_v1->resultset('Version::Table'); is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file'); # loading a new module defining a new version of the same table From d8114498208eab47e161645d8a03a5fc21ef5e87 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Fri, 29 Aug 2014 21:51:51 -0400 Subject: [PATCH 4/5] Document sqlt_deploy_hook. --- lib/DBIx/Class/Schema/Versioned.pm | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index f43059f45..f6d1c898a 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -286,6 +286,13 @@ sub _deploy_version_table { $self->{vschema}->storage->disconnect; } +=head2 sqlt_deploy_hook + +Hook to exlude the 'dbix_class_schema_versions' table from being modified +deployment of the schema. + +=cut + sub sqlt_deploy_hook { my ($self, $sqlt_schema) = @_; From ef5fa4a13918bdc494af187168b68ce88ae3090f Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Fri, 29 Aug 2014 22:45:30 -0400 Subject: [PATCH 5/5] Fix POD typos --- lib/DBIx/Class/Schema/Versioned.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index f6d1c898a..2bd443360 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -288,8 +288,8 @@ sub _deploy_version_table { =head2 sqlt_deploy_hook -Hook to exlude the 'dbix_class_schema_versions' table from being modified -deployment of the schema. +Hook to exclude the 'dbix_class_schema_versions' table from being modified +during schema deployment. =cut