1 package DBIx::Class::DeploymentHandler;
4 use Method::Signatures::Simple;
5 require DBIx::Class::Schema; # loaded for type constraint
6 require DBIx::Class::Storage; # loaded for type constraint
7 require DBIx::Class::ResultSet; # loaded for type constraint
8 use Carp::Clan '^DBIx::Class::DeploymentHandler';
10 require SQL::Translator::Diff;
13 with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
16 use Moose::Util::TypeConstraints;
17 subtype 'DBIx::Class::DeploymentHandler::Databases'
18 => as 'ArrayRef[Str]';
20 coerce 'DBIx::Class::DeploymentHandler::Databases'
23 no Moose::Util::TypeConstraints;
27 isa => 'DBIx::Class::Schema',
30 handles => [qw( ddl_filename schema_version )],
33 has upgrade_directory => ( # configuration
40 has backup_directory => ( # configuration
43 predicate => 'has_backup_directory',
47 isa => 'DBIx::Class::Storage',
52 method _build_storage {
53 my $s = $self->schema->storage;
54 $s->_determine_driver;
58 has do_backup => ( # configuration
65 isa => 'DBIx::Class::ResultSet',
68 handles => [qw( is_installed db_version )],
71 method _build_version_rs {
72 $self->schema->set_us_up_the_bomb;
73 $self->schema->resultset('__VERSION')
76 has databases => ( # configuration
78 isa => 'DBIx::Class::DeploymentHandler::Databases',
80 default => sub { [qw( MySQL SQLite PostgreSQL )] },
83 has sqltargs => ( # configuration
86 default => sub { {} },
89 method install($new_version) {
90 carp 'Install not possible as versions table already exists in database'
91 if $self->is_installed;
93 $new_version ||= $self->schema_version;
98 $self->version_rs->create({
99 version => $new_version,
101 # upgrade_sql => $upgrade_sql,
106 method ordered_schema_versions { undef }
109 my $db_version = $self->db_version;
110 my $schema_version = $self->schema_version;
112 unless ($db_version) {
114 carp 'Upgrade not possible as database is unversioned. Please call install first.';
118 if ( $db_version eq $schema_version ) {
120 carp "Upgrade not necessary\n";
124 my @version_list = $self->ordered_schema_versions ||
125 ( $db_version, $schema_version );
127 # remove all versions in list above the required version
128 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
132 # remove all versions in list below the current version
133 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
137 # check we have an appropriate list of versions
138 die if @version_list < 2;
141 while ( @version_list >= 2 ) {
142 $self->upgrade_single_step( $version_list[0], $version_list[1] );
147 __PACKAGE__->meta->make_immutable;
153 vim: ts=2 sw=2 expandtab