make DeployMethod deploy methods public for unversioned use-case
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Dad.pm
1 package DBIx::Class::DeploymentHandler::Dad;
2
3 use Moose;
4 use Method::Signatures::Simple;
5 use DBIx::Class::DeploymentHandler::Types;
6 require DBIx::Class::Schema;    # loaded for type constraint
7 require DBIx::Class::ResultSet; # loaded for type constraint
8 use Carp::Clan '^DBIx::Class::DeploymentHandler';
9
10 has schema => (
11   isa      => 'DBIx::Class::Schema',
12   is       => 'ro',
13   required => 1,
14   handles => ['schema_version'],
15 );
16
17 has upgrade_directory => ( # configuration
18   isa      => 'Str',
19   is       => 'ro',
20   required => 1,
21   default  => 'sql',
22 );
23
24 has backup_directory => ( # configuration
25   isa => 'Str',
26   is  => 'ro',
27   predicate  => 'has_backup_directory',
28 );
29
30 has to_version => ( # configuration
31   is         => 'ro',
32   lazy_build => 1,
33 );
34
35 sub _build_to_version { $_[0]->schema->schema_version }
36
37 has databases => ( # configuration
38   coerce  => 1,
39   isa     => 'DBIx::Class::DeploymentHandler::Databases',
40   is      => 'ro',
41   default => sub { [qw( MySQL SQLite PostgreSQL )] },
42 );
43
44 has sqltargs => ( # configuration
45   isa => 'HashRef',
46   is  => 'ro',
47   default => sub { {} },
48 );
49
50 method install {
51   croak 'Install not possible as versions table already exists in database'
52     if $self->version_storage_is_installed;
53
54   my $ddl = $self->deploy;
55
56   $self->version_storage->add_database_version({
57     version     => $self->to_version,
58     ddl         => $ddl,
59   });
60 }
61
62 sub upgrade {
63   my $self = shift;
64   while ( my $version_list = $self->next_version_set ) {
65     my ($ddl, $upgrade_sql) = @{$self->upgrade_single_step($version_list)||[]};
66
67     $self->add_database_version({
68       version     => $version_list->[-1],
69       ddl         => $ddl,
70       upgrade_sql => $upgrade_sql,
71     });
72   }
73 }
74
75 sub downgrade {
76   my $self = shift;
77   while ( my $version_list = $self->previous_version_set ) {
78     $self->downgrade_single_step($version_list);
79
80     # do we just delete a row here?  I think so but not sure
81     $self->delete_database_version({ version => $version_list->[-1] });
82   }
83 }
84
85 method backup { $self->storage->backup($self->backup_directory) }
86
87 __PACKAGE__->meta->make_immutable;
88
89 1;
90
91 =pod
92
93 =attr schema
94
95 =attr upgrade_directory
96
97 =attr backup_directory
98
99 =attr to_version
100
101 =attr databases
102
103 =method install
104
105 =method upgrade
106
107 =method backup
108
109 __END__
110
111 vim: ts=2 sw=2 expandtab