VersionHandler no longer needs access to other components, *much* cleaner tests for...
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler.pm
1 package DBIx::Class::DeploymentHandler;
2
3 use Moose;
4 use Method::Signatures::Simple;
5 require DBIx::Class::Schema;    # loaded for type constraint
6 require DBIx::Class::ResultSet; # loaded for type constraint
7 use Carp::Clan '^DBIx::Class::DeploymentHandler';
8
9 with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod',
10      'DBIx::Class::DeploymentHandler::WithDatabaseToSchemaVersions',
11      'DBIx::Class::DeploymentHandler::WithStandardVersionStorage';
12
13 BEGIN {
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
24 has schema => (
25   isa      => 'DBIx::Class::Schema',
26   is       => 'ro',
27   required => 1,
28   handles => ['schema_version'],
29 );
30
31 has upgrade_directory => ( # configuration
32   isa      => 'Str',
33   is       => 'ro',
34   required => 1,
35   default  => 'sql',
36 );
37
38 has backup_directory => ( # configuration
39   isa => 'Str',
40   is  => 'ro',
41   predicate  => 'has_backup_directory',
42 );
43
44 has to_version => ( # configuration
45   is         => 'ro',
46   lazy_build => 1, # builder comes from another role...
47                    # which is... probably not how we want it
48 );
49
50 sub _build_to_version { $_[0]->schema->schema_version }
51
52 has databases => ( # configuration
53   coerce  => 1,
54   isa     => 'DBIx::Class::DeploymentHandler::Databases',
55   is      => 'ro',
56   default => sub { [qw( MySQL SQLite PostgreSQL )] },
57 );
58
59 has sqltargs => ( # configuration
60   isa => 'HashRef',
61   is  => 'ro',
62   default => sub { {} },
63 );
64
65 method install {
66   carp 'Install not possible as versions table already exists in database'
67     if $self->version_storage_is_installed;
68
69   my $new_version = $self->to_version;
70
71   if ($new_version) {
72     $self->_deploy;
73
74     $self->add_database_version({
75       version     => $new_version,
76       # ddl         => $ddl,
77       # upgrade_sql => $upgrade_sql,
78     });
79   }
80 }
81
82 sub upgrade {
83   my $self = shift;
84   while ( my $version_list = $self->next_version_set ) {
85     $self->_upgrade_single_step($version_list);
86
87     $self->add_database_version({
88       version     => $version_list->[-1],
89       # ddl         => $ddl,
90       # upgrade_sql => $upgrade_sql,
91     });
92   }
93 }
94
95 method backup { $self->storage->backup($self->backup_directory) }
96
97 __PACKAGE__->meta->make_immutable;
98
99 1;
100
101 __END__
102
103 vim: ts=2 sw=2 expandtab