The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Class::Schema::Versioned - 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/');

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 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.

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.

So you'll probably want to write a script which generates your DDLs and diffs and another which executes the upgrade.

NB: At the moment, only SQLite and MySQL are supported. This is due to spotty behaviour in the SQL::Translator producers, please help us by them.

METHODS

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.

get_db_version

Returns the version that your database is currently at. This is determined by the values in the dbix_class_schema_versions table that $self->upgrade writes to.

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..

upgrade

Call this to attempt to upgrade your database from the version it is at to the version this DBIC schema is at.

It requires an SQL diff file to exist in $schema->upgrade_directory, normally you will have created this using $schema->create_ddl_dir.

do_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.

Will run the whole file as it is by default.

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. Probably won't work unless called from the overridable do_upgrade method.

connection

Overloaded method. This checks the DBIC schema version against the DB version and warns if they are not the same or if the DB is unversioned. It also provides compatibility between the old versions table (SchemaVersions) and the new one (dbix_class_schema_versions).

To avoid the checks on connect, set the env var DBIC_NO_VERSION_CHECK. This can be useful for scripts.

AUTHORS

Jess Robinson <castaway@desert-island.demon.co.uk> Luke Saunders <luke@shadowcatsystems.co.uk>

LICENSE

You may distribute this code under the same terms as Perl itself.