=cut
sub create_upgrade_path {
- ## override this method
+ ## override this method
}
+=head2 ordered_schema_versions
+
+=over 4
+
+=item Returns: a list of version numbers, ordered from lowest to highest
+
+=back
+
+Virtual method that should be overriden to return an ordered list
+of schema versions. This is then used to produce a set of steps to
+upgrade through to achieve the required schema version.
+
+You may want the db_version retrieved via $self->get_db_version
+and the schema_version which is retrieved via $self->schema_version
+
+=cut
+
+sub ordered_schema_versions {
+ ## override this method
+}
+
=head2 upgrade
-Call this to attempt to upgrade your database from the version it is at to the version
-this DBIC schema is at. If they are the same it does nothing.
+Call this to attempt to upgrade your database from the version it
+is at to the version this DBIC schema is at. If they are the same
+it does nothing.
-It requires an SQL diff file to exist in you I<upgrade_directory>, normally you will
-have created this using L<DBIx::Class::Schema/create_ddl_dir>.
+It will call L</ordered_schema_versions> to retrieve an ordered
+list of schema versions (if ordered_schema_versions returns nothing
+then it is assumed you can do the upgrade as a single step). It
+then iterates through the list of versions between the current db
+version and the schema version applying one update at a time until
+all relvant updates are applied.
-If successful the dbix_class_schema_versions table is updated with the current
-DBIC schema version.
+The individual update steps are performed by using
+L</upgrade_single_step>, which will apply the update and also
+update the dbix_class_schema_versions table.
=cut
my $version_table_name = 'dbix_class_schema_versions';
my $old_table_name = 'SchemaVersions';
- my $ddl_dir = File::Spec->catdir ('t', 'var');
+ my $ddl_dir = dir ('t', 'var');
my $fn = {
- v1 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-MySQL.sql'),
- v2 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-MySQL.sql'),
- v3 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-3.0-MySQL.sql'),
- trans_v12 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-2.0-MySQL.sql'),
- trans_v23 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-3.0-MySQL.sql'),
+ v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
+ v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
- trans => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
++ v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
++ trans_v12 => $ddl_dir-> ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
++ trans_v23 => $ddl_dir-> ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
};
use lib qw(t/lib);
# loading a new module defining a new version of the same table
DBICVersion::Schema->_unregister_source ('Table');
-eval "use DBICVersionNew";
+eval "use DBICVersion_v2";
-my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
+my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
{
unlink($fn->{v2});
- unlink($fn->{trans});
+ unlink($fn->{trans_v12});
- is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
- is($schema_upgrade->schema_version, '2.0', 'schema version ok');
- $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
- ok(-f $fn->{trans}, 'Created DDL file');
+ is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
+ is($schema_v2->schema_version, '2.0', 'schema version ok');
+ $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
+ ok(-f $fn->{trans_v12}, 'Created DDL file');
- {
- my $w;
- local $SIG{__WARN__} = sub { $w = shift };
-
- $schema_v2->upgrade();
- like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
- }
+ sleep 1; # remove this when TODO below is completed
+ warnings_like (
- sub { $schema_upgrade->upgrade() },
++ sub { $schema_v2->upgrade() },
+ qr/DB version .+? is lower than the schema version/,
+ 'Warn before upgrade',
+ );
- is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded');
+ is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
- eval {
+ lives_ok ( sub {
- $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
+ $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
- };
- is($@, '', 'new column created');
-
- # should overwrite files and warn about it
- my @w;
- local $SIG{__WARN__} = sub {
- if ($_[0] =~ /Overwriting existing/) {
- push @w, $_[0];
- }
- else {
- warn @_;
- }
- };
- $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
-
- is (2, @w, 'A warning generated for both the DDL and the diff');
- like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
- like ($w[1], qr/Overwriting existing diff file - $fn->{trans_v12}/, 'Upgrade diff overwrite warning');
+ }, 'new column created' );
+
+ warnings_exist (
- sub { $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
++ sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
+ [
+ qr/Overwriting existing DDL file - $fn->{v2}/,
+ qr/Overwriting existing diff file - $fn->{trans}/,
+ ],
+ 'An overwrite warning generated for both the DDL and the diff',
+ );
}
{