tests for component (and therefor bugfixes!)
[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
973d060d 9with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod',
10 'DBIx::Class::DeploymentHandler::WithDatabaseToSchemaVersions',
11 'DBIx::Class::DeploymentHandler::WithStandardVersionStorage';
2e68a8e1 12
cf400f48 13BEGIN {
14 use Moose::Util::TypeConstraints;
15 subtype 'DBIx::Class::DeploymentHandler::Databases'
16 => as 'ArrayRef[Str]';
17
18 coerce 'DBIx::Class::DeploymentHandler::Databases'
19 => from 'Str'
20 => via { [$_] };
21 no Moose::Util::TypeConstraints;
22}
23
b974984a 24has schema => (
61847972 25 isa => 'DBIx::Class::Schema',
26 is => 'ro',
27 required => 1,
b539a216 28 handles => ['schema_version'],
b974984a 29);
30
d3b45f46 31has upgrade_directory => ( # configuration
61847972 32 isa => 'Str',
33 is => 'ro',
34 required => 1,
4ea147c6 35 default => 'sql',
b974984a 36);
37
d3b45f46 38has backup_directory => ( # configuration
61847972 39 isa => 'Str',
40 is => 'ro',
8bf3eee1 41 predicate => 'has_backup_directory',
b974984a 42);
43
e217d19c 44has to_version => ( # configuration
38bd9956 45 is => 'ro',
6bbb6ce7 46 lazy_build => 1,
38bd9956 47);
48
b539a216 49sub _build_to_version { $_[0]->schema->schema_version }
50
d3b45f46 51has databases => ( # configuration
cf400f48 52 coerce => 1,
53 isa => 'DBIx::Class::DeploymentHandler::Databases',
54 is => 'ro',
9e401dc2 55 default => sub { [qw( MySQL SQLite PostgreSQL )] },
56);
57
d3b45f46 58has sqltargs => ( # configuration
ecc3b6be 59 isa => 'HashRef',
60 is => 'ro',
61 default => sub { {} },
62);
63
24f4524b 64method install {
12fdd461 65 carp 'Install not possible as versions table already exists in database'
973d060d 66 if $self->version_storage_is_installed;
b974984a 67
24f4524b 68 my $new_version = $self->to_version;
b974984a 69
70 if ($new_version) {
8a7847f1 71 $self->_deploy;
12fdd461 72
6bbb6ce7 73 $self->version_storage->add_database_version({
61847972 74 version => $new_version,
75 # ddl => $ddl,
76 # upgrade_sql => $upgrade_sql,
12fdd461 77 });
b974984a 78 }
79}
80
7521a845 81sub upgrade {
c3aec7c9 82 my $self = shift;
83 while ( my $version_list = $self->next_version_set ) {
84 $self->_upgrade_single_step($version_list);
85
973d060d 86 $self->add_database_version({
c3aec7c9 87 version => $version_list->[-1],
88 # ddl => $ddl,
89 # upgrade_sql => $upgrade_sql,
90 });
b974984a 91 }
92}
93
e217d19c 94method backup { $self->storage->backup($self->backup_directory) }
95
2e68a8e1 96__PACKAGE__->meta->make_immutable;
97
b974984a 981;
61847972 99
100__END__
101
2eaf903b 102vim: ts=2 sw=2 expandtab