[Bast-commits] r4105 - in DBIx-Class/0.08/branches/versioned_enhancements: lib/DBIx/Class/Schema t

captainL at dev.catalyst.perl.org captainL at dev.catalyst.perl.org
Thu Feb 28 10:28:31 GMT 2008


Author: captainL
Date: 2008-02-28 10:28:31 +0000 (Thu, 28 Feb 2008)
New Revision: 4105

Modified:
   DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema/Versioned.pm
   DBIx-Class/0.08/branches/versioned_enhancements/t/94versioning.t
Log:
changed versioning table from SchemaVersions to dbix_class_schema_versions with transition ability

Modified: DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema/Versioned.pm
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema/Versioned.pm	2008-02-27 15:12:12 UTC (rev 4104)
+++ DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema/Versioned.pm	2008-02-28 10:28:31 UTC (rev 4105)
@@ -4,7 +4,7 @@
 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__->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,7 +40,14 @@
 
 __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');
+
+
 # ---------------------------------------------------------------------------
 
 =head1 NAME
@@ -61,7 +73,7 @@
 module, you need to have called C<create_ddl_dir> on your Schema to
 create your upgrade files to include with your delivery.
 
-A table called I<SchemaVersions> is created and maintained by the
+A table called I<dbix_class_schema_versions> 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 @@
 =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 @@
 
   # 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 @@
   $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,7 +398,19 @@
 {
   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();
 
   if($pversion eq $self->schema_version)

Modified: DBIx-Class/0.08/branches/versioned_enhancements/t/94versioning.t
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/t/94versioning.t	2008-02-27 15:12:12 UTC (rev 4104)
+++ DBIx-Class/0.08/branches/versioned_enhancements/t/94versioning.t	2008-02-28 10:28:31 UTC (rev 4105)
@@ -18,14 +18,18 @@
     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 @@
   };
   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');
+
+}




More information about the Bast-commits mailing list