1690096f84cec5eca3c9645085fc199b2d886176
[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,
47 );
48
49 sub _build_to_version { $_[0]->schema->schema_version }
50
51 has databases => ( # configuration
52   coerce  => 1,
53   isa     => 'DBIx::Class::DeploymentHandler::Databases',
54   is      => 'ro',
55   default => sub { [qw( MySQL SQLite PostgreSQL )] },
56 );
57
58 has sqltargs => ( # configuration
59   isa => 'HashRef',
60   is  => 'ro',
61   default => sub { {} },
62 );
63
64 method install {
65   carp 'Install not possible as versions table already exists in database'
66     if $self->version_storage_is_installed;
67
68   my $new_version = $self->to_version;
69
70   if ($new_version) {
71     $self->_deploy;
72
73     $self->version_storage->add_database_version({
74       version     => $new_version,
75       # ddl         => $ddl,
76       # upgrade_sql => $upgrade_sql,
77     });
78   }
79 }
80
81 sub upgrade {
82   my $self = shift;
83   while ( my $version_list = $self->next_version_set ) {
84     $self->_upgrade_single_step($version_list);
85
86     $self->add_database_version({
87       version     => $version_list->[-1],
88       # ddl         => $ddl,
89       # upgrade_sql => $upgrade_sql,
90     });
91   }
92 }
93
94 method backup { $self->storage->backup($self->backup_directory) }
95
96 __PACKAGE__->meta->make_immutable;
97
98 1;
99
100 __END__
101
102 vim: ts=2 sw=2 expandtab