Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect after schema version check. #57

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/DBIx/Class/Schema/Versioned.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
Expand All @@ -273,6 +273,32 @@ 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;
}

=head2 sqlt_deploy_hook

Hook to exclude the 'dbix_class_schema_versions' table from being modified
during schema deployment.

=cut

sub sqlt_deploy_hook {
my ($self, $sqlt_schema) = @_;

$sqlt_schema->drop_table('dbix_class_schema_versions');
}

=head2 create_upgrade_path

=over 4
Expand Down Expand Up @@ -525,7 +551,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')
Expand Down Expand Up @@ -589,22 +615,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');
}
}

Expand Down Expand Up @@ -694,7 +721,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 !!!
Expand Down
2 changes: 1 addition & 1 deletion t/94versioning.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down