simplify most of the code significantly thanks to version handling code
[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';
e70a1600 10with 'DBIx::Class::DeploymentHandler::WithDatabaseToSchemaVersions';
2e68a8e1 11
cf400f48 12BEGIN {
13 use Moose::Util::TypeConstraints;
14 subtype 'DBIx::Class::DeploymentHandler::Databases'
15 => as 'ArrayRef[Str]';
16
17 coerce 'DBIx::Class::DeploymentHandler::Databases'
18 => from 'Str'
19 => via { [$_] };
20 no Moose::Util::TypeConstraints;
21}
22
b974984a 23has schema => (
61847972 24 isa => 'DBIx::Class::Schema',
25 is => 'ro',
26 required => 1,
4ea147c6 27 handles => [qw( ddl_filename schema_version )],
b974984a 28);
29
d3b45f46 30has upgrade_directory => ( # configuration
61847972 31 isa => 'Str',
32 is => 'ro',
33 required => 1,
4ea147c6 34 default => 'sql',
b974984a 35);
36
d3b45f46 37has backup_directory => ( # configuration
61847972 38 isa => 'Str',
39 is => 'ro',
8bf3eee1 40 predicate => 'has_backup_directory',
b974984a 41);
42
d3b45f46 43has do_backup => ( # configuration
61847972 44 isa => 'Bool',
45 is => 'ro',
46 default => undef,
b974984a 47);
48
12fdd461 49has version_rs => (
61847972 50 isa => 'DBIx::Class::ResultSet',
51 is => 'ro',
52 lazy_build => 1,
53 handles => [qw( is_installed db_version )],
12fdd461 54);
55
38bd9956 56has to_version => (
57 is => 'ro',
58 lazy_build => 1,
59);
60
9e1c29c2 61method _build_version_rs {
62 $self->schema->set_us_up_the_bomb;
63 $self->schema->resultset('__VERSION')
64}
65
d3b45f46 66has databases => ( # configuration
cf400f48 67 coerce => 1,
68 isa => 'DBIx::Class::DeploymentHandler::Databases',
69 is => 'ro',
9e401dc2 70 default => sub { [qw( MySQL SQLite PostgreSQL )] },
71);
72
d3b45f46 73has sqltargs => ( # configuration
ecc3b6be 74 isa => 'HashRef',
75 is => 'ro',
76 default => sub { {} },
77);
78
24f4524b 79method install {
12fdd461 80 carp 'Install not possible as versions table already exists in database'
ceef4ff5 81 if $self->is_installed;
b974984a 82
24f4524b 83 my $new_version = $self->to_version;
b974984a 84
85 if ($new_version) {
d3b45f46 86 $self->deploy;
12fdd461 87
88 $self->version_rs->create({
61847972 89 version => $new_version,
90 # ddl => $ddl,
91 # upgrade_sql => $upgrade_sql,
12fdd461 92 });
b974984a 93 }
94}
95
b974984a 96method upgrade {
38bd9956 97 while ( my $version_list = $self->next_version_set ) {
24f4524b 98 $self->upgrade_single_step($version_list);
b974984a 99 }
100}
101
2e68a8e1 102__PACKAGE__->meta->make_immutable;
103
b974984a 1041;
61847972 105
106__END__
107
2eaf903b 108vim: ts=2 sw=2 expandtab