From: Luke Saunders Date: Thu, 28 Feb 2008 10:28:31 +0000 (+0000) Subject: changed versioning table from SchemaVersions to dbix_class_schema_versions with trans... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b4b1e91cbe7dfd356c74205e1c3393525b1f37d5;p=dbsrgits%2FDBIx-Class-Historic.git changed versioning table from SchemaVersions to dbix_class_schema_versions with transition ability --- diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index 197f4ac..8095067 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -4,7 +4,7 @@ use strict; use warnings; __PACKAGE__->load_components(qw/ Core/); -__PACKAGE__->table('SchemaVersions'); +__PACKAGE__->table('dbix_class_schema_versions'); __PACKAGE__->add_columns ( 'Version' => { @@ -28,6 +28,11 @@ __PACKAGE__->add_columns ); __PACKAGE__->set_primary_key('Version'); +package DBIx::Class::Version::TableCompat; +use base 'DBIx::Class::Version::Table'; + +__PACKAGE__->table('SchemaVersions'); + package DBIx::Class::Version; use base 'DBIx::Class::Schema'; use strict; @@ -35,6 +40,13 @@ use warnings; __PACKAGE__->register_class('Table', 'DBIx::Class::Version::Table'); +package DBIx::Class::VersionCompat; +use base 'DBIx::Class::Schema'; +use strict; +use warnings; + +__PACKAGE__->register_class('TableCompat', 'DBIx::Class::Version::TableCompat'); + # --------------------------------------------------------------------------- @@ -61,7 +73,7 @@ classes, to enable them to upgrade to newer schema layouts. To use this module, you need to have called C on your Schema to create your upgrade files to include with your delivery. -A table called I is created and maintained by the +A table called I is created and maintained by the module. This contains two fields, 'Version' and 'Installed', which contain each VERSION of your Schema, and the date+time it was installed. @@ -126,7 +138,7 @@ sub schema_version { =head2 get_db_version Returns the version that your database is currently at. This is determined by the values in the -SchemaVersions table that $self->upgrade writes to. +dbix_class_schema_versions table that $self->upgrade writes to. =cut @@ -259,7 +271,7 @@ sub upgrade # db unversioned unless ($db_version) { - # set version in SchemaVersions table, can't actually upgrade as we don 't know what version the DB is at + # set version in dbix_class_schema_versions table, can't actually upgrade as we don 't know what version the DB is at $self->_create_db_to_schema_diff() if ($self->do_diff_on_init); # create versions table and version row @@ -291,7 +303,7 @@ sub upgrade $self->backup() if($self->do_backup); $self->txn_do(sub { $self->do_upgrade() }); - # set row in SchemaVersions table + # set row in dbix_class_schema_versions table $self->_set_db_version; } @@ -386,6 +398,18 @@ sub _on_connect { my ($self) = @_; $self->{vschema} = DBIx::Class::Version->connect(@{$self->storage->connect_info()}); + my $vtable = $self->{vschema}->resultset('Table'); + + # check for legacy versions table and move to new if exists + my $vschema_compat = DBIx::Class::VersionCompat->connect(@{$self->storage->connect_info()}); + unless ($self->_source_exists($vtable)) { + my $vtable_compat = $vschema_compat->resultset('TableCompat'); + if ($self->_source_exists($vtable_compat)) { + $self->{vschema}->deploy; + map { $vtable->create({$_->get_columns}) } $vtable_compat->all; + $self->storage->dbh->do("DROP TABLE " . $vtable_compat->result_source->from); + } + } my $pversion = $self->get_db_version(); diff --git a/t/94versioning.t b/t/94versioning.t index 86dd11b..3667c52 100644 --- a/t/94versioning.t +++ b/t/94versioning.t @@ -18,14 +18,18 @@ BEGIN { eval "use DBD::mysql; use SQL::Translator 0.08;"; plan $@ ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.08 for testing' ) - : ( tests => 9 ); + : ( tests => 13 ); } +my $version_table_name = 'dbix_class_schema_versions'; +my $old_table_name = 'SchemaVersions'; + use lib qw(t/lib); use_ok('DBICVersionOrig'); my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass); -eval { $schema_orig->storage->dbh->do('drop table SchemaVersions') }; +eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) }; +eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) }; is($schema_orig->ddl_filename('MySQL', 't/var', '1.0'), File::Spec->catfile('t', 'var', 'DBICVersion-Schema-1.0-MySQL.sql'), 'Filename creation working'); unlink('t/var/DBICVersion-Schema-1.0-MySQL.sql') if (-e 't/var/DBICVersion-Schema-1.0-MySQL.sql'); @@ -56,3 +60,26 @@ eval "use DBICVersionNew"; }; is($@, '', 'new column created'); } + +{ + my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); + eval { + $schema_version->storage->dbh->do('select * from ' . $version_table_name); + }; + is($@, '', 'version table exists'); + + eval { + $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name"); + $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name"); + }; + is($@, '', 'versions table renamed to old style table'); + + $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); + is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay'); + + eval { + $schema_version->storage->dbh->do('select * from ' . $old_table_name); + }; + ok($@, 'old version table gone'); + +}