The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades

SYNOPSIS

  package Library::Schema;
  use base qw/DBIx::Class::Schema/;   
  # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
  __PACKAGE__->load_classes(qw/CD Book DVD/);

  __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Versioned/);
  __PACKAGE__->upgrade_directory('/path/to/upgrades/');
  __PACKAGE__->backup_directory('/path/to/backups/');

  sub backup
  {
    my ($self) = @_;
    # my special backup process
  }

  sub upgrade
  {
    my ($self) = @_;

    ## overridable sub, per default just runs all the commands.

    $self->run_upgrade(qr/create/i);
    $self->run_upgrade(qr/alter table .*? add/i);
    $self->run_upgrade(qr/alter table .*? (?!drop)/i);
    $self->run_upgrade(qr/alter table .*? drop/i);
    $self->run_upgrade(qr/drop/i);
    $self->run_upgrade(qr//i);   
  }

DESCRIPTION

This module is a component designed to extend DBIx::Class::Schema classes, to enable them to upgrade to newer schema layouts. To use this module, you need to have called create_ddl_dir on your Schema to create your upgrade files to include with your delivery.

A table called SchemaVersions 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.

If you would like to influence which levels of version change need upgrades in your Schema, you can override the method ddl_filename in DBIx::Class::Schema. Return a false value if there is no upgrade path between the two versions supplied. By default, every change in your VERSION is regarded as needing an upgrade.

The actual upgrade is called manually by calling upgrade on your schema object. Code is run at connect time to determine whether an upgrade is needed, if so, a warning "Versions out of sync" is produced.

NB: At the moment, SQLite upgrading is rather spotty, as SQL::Translator::Diff returns SQL statements that SQLite does not support.

METHODS

backup

This is an overwritable method which is called just before the upgrade, to allow you to make a backup of the database. Per default this method attempts to call $self->storage->backup, to run the standard backup on each database type.

This method should return the name of the backup file, if appropriate.

backup is called from upgrade, make sure you call it, if you write your own <upgrade> method.

upgrade

This is an overwritable method used to run your upgrade. The freeform method allows you to run your upgrade any way you please, you can call run_upgrade any number of times to run the actual SQL commands, and in between you can sandwich your data upgrading. For example, first run all the CREATE commands, then migrate your data from old to new tables/formats, then issue the DROP commands when you are finished.

run_upgrade

 $self->run_upgrade(qr/create/i);

Runs a set of SQL statements matching a passed in regular expression. The idea is that this method can be called any number of times from your upgrade method, running whichever commands you specify via the regex in the parameter.

upgrade_directory

Use this to set the directory your upgrade files are stored in.

backup_directory

Use this to set the directory you want your backups stored in.

schema_version

Returns the current schema class' $VERSION; does -not- use $schema->VERSION since that varies in results depending on if version.pm is installed, and if so the perl or XS versions. If you want this to change, bug the version.pm author to make vpp and vxs behave the same.

AUTHOR

Jess Robinson <castaway@desert-island.demon.co.uk>