migrate unneeded code out of main class and into delegate class
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler.pm
CommitLineData
b974984a 1package DBIx::Class::DeploymentHandler;
2
3use Moose;
4use Method::Signatures::Simple;
12fdd461 5require DBIx::Class::Schema; # loaded for type constraint
12fdd461 6require DBIx::Class::ResultSet; # loaded for type constraint
e1f67607 7use Carp::Clan '^DBIx::Class::DeploymentHandler';
b974984a 8
334bced5 9with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
2e68a8e1 10
cf400f48 11BEGIN {
12 use Moose::Util::TypeConstraints;
13 subtype 'DBIx::Class::DeploymentHandler::Databases'
14 => as 'ArrayRef[Str]';
15
16 coerce 'DBIx::Class::DeploymentHandler::Databases'
17 => from 'Str'
18 => via { [$_] };
19 no Moose::Util::TypeConstraints;
20}
21
b974984a 22has schema => (
61847972 23 isa => 'DBIx::Class::Schema',
24 is => 'ro',
25 required => 1,
4ea147c6 26 handles => [qw( ddl_filename schema_version )],
b974984a 27);
28
d3b45f46 29has upgrade_directory => ( # configuration
61847972 30 isa => 'Str',
31 is => 'ro',
32 required => 1,
4ea147c6 33 default => 'sql',
b974984a 34);
35
d3b45f46 36has backup_directory => ( # configuration
61847972 37 isa => 'Str',
38 is => 'ro',
8bf3eee1 39 predicate => 'has_backup_directory',
b974984a 40);
41
d3b45f46 42has do_backup => ( # configuration
61847972 43 isa => 'Bool',
44 is => 'ro',
45 default => undef,
b974984a 46);
47
12fdd461 48has version_rs => (
61847972 49 isa => 'DBIx::Class::ResultSet',
50 is => 'ro',
51 lazy_build => 1,
52 handles => [qw( is_installed db_version )],
12fdd461 53);
54
9e1c29c2 55method _build_version_rs {
56 $self->schema->set_us_up_the_bomb;
57 $self->schema->resultset('__VERSION')
58}
59
d3b45f46 60has databases => ( # configuration
cf400f48 61 coerce => 1,
62 isa => 'DBIx::Class::DeploymentHandler::Databases',
63 is => 'ro',
9e401dc2 64 default => sub { [qw( MySQL SQLite PostgreSQL )] },
65);
66
d3b45f46 67has sqltargs => ( # configuration
ecc3b6be 68 isa => 'HashRef',
69 is => 'ro',
70 default => sub { {} },
71);
72
b974984a 73method install($new_version) {
12fdd461 74 carp 'Install not possible as versions table already exists in database'
ceef4ff5 75 if $self->is_installed;
b974984a 76
12fdd461 77 $new_version ||= $self->schema_version;
b974984a 78
79 if ($new_version) {
d3b45f46 80 $self->deploy;
12fdd461 81
82 $self->version_rs->create({
61847972 83 version => $new_version,
84 # ddl => $ddl,
85 # upgrade_sql => $upgrade_sql,
12fdd461 86 });
b974984a 87 }
88}
89
8636376a 90method ordered_schema_versions { undef }
b974984a 91
92method upgrade {
12fdd461 93 my $db_version = $self->db_version;
94 my $schema_version = $self->schema_version;
b974984a 95
b974984a 96 unless ($db_version) {
61847972 97 # croak?
98 carp 'Upgrade not possible as database is unversioned. Please call install first.';
99 return;
b974984a 100 }
101
12fdd461 102 if ( $db_version eq $schema_version ) {
61847972 103 # croak?
104 carp "Upgrade not necessary\n";
105 return;
b974984a 106 }
107
12fdd461 108 my @version_list = $self->ordered_schema_versions ||
109 ( $db_version, $schema_version );
b974984a 110
111 # remove all versions in list above the required version
12fdd461 112 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
61847972 113 pop @version_list;
b974984a 114 }
115
116 # remove all versions in list below the current version
12fdd461 117 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
61847972 118 shift @version_list;
b974984a 119 }
120
121 # check we have an appropriate list of versions
12fdd461 122 die if @version_list < 2;
b974984a 123
124 # do sets of upgrade
12fdd461 125 while ( @version_list >= 2 ) {
61847972 126 $self->upgrade_single_step( $version_list[0], $version_list[1] );
127 shift @version_list;
b974984a 128 }
129}
130
2e68a8e1 131__PACKAGE__->meta->make_immutable;
132
b974984a 1331;
61847972 134
135__END__
136
2eaf903b 137vim: ts=2 sw=2 expandtab