git rid of some unnecesary test warnings
[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
6require DBIx::Class::Storage; # loaded for type constraint
7require DBIx::Class::ResultSet; # loaded for type constraint
e1f67607 8use Carp::Clan '^DBIx::Class::DeploymentHandler';
ecc3b6be 9use SQL::Translator;
9e1c29c2 10require SQL::Translator::Diff;
d76abae0 11use Try::Tiny;
b974984a 12
334bced5 13with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
2e68a8e1 14
cf400f48 15BEGIN {
16 use Moose::Util::TypeConstraints;
17 subtype 'DBIx::Class::DeploymentHandler::Databases'
18 => as 'ArrayRef[Str]';
19
20 coerce 'DBIx::Class::DeploymentHandler::Databases'
21 => from 'Str'
22 => via { [$_] };
23 no Moose::Util::TypeConstraints;
24}
25
b974984a 26has schema => (
61847972 27 isa => 'DBIx::Class::Schema',
28 is => 'ro',
29 required => 1,
4ea147c6 30 handles => [qw( ddl_filename schema_version )],
b974984a 31);
32
d3b45f46 33has upgrade_directory => ( # configuration
61847972 34 isa => 'Str',
35 is => 'ro',
36 required => 1,
4ea147c6 37 default => 'sql',
b974984a 38);
39
d3b45f46 40has backup_directory => ( # configuration
61847972 41 isa => 'Str',
42 is => 'ro',
8bf3eee1 43 predicate => 'has_backup_directory',
b974984a 44);
45
46has storage => (
61847972 47 isa => 'DBIx::Class::Storage',
48 is => 'ro',
49 lazy_build => 1,
b974984a 50);
51
7eec7eb7 52method _build_storage {
61847972 53 my $s = $self->schema->storage;
54 $s->_determine_driver;
55 $s
7eec7eb7 56}
12fdd461 57
d3b45f46 58has do_backup => ( # configuration
61847972 59 isa => 'Bool',
60 is => 'ro',
61 default => undef,
b974984a 62);
63
12fdd461 64has version_rs => (
61847972 65 isa => 'DBIx::Class::ResultSet',
66 is => 'ro',
67 lazy_build => 1,
68 handles => [qw( is_installed db_version )],
12fdd461 69);
70
9e1c29c2 71method _build_version_rs {
72 $self->schema->set_us_up_the_bomb;
73 $self->schema->resultset('__VERSION')
74}
75
d3b45f46 76has databases => ( # configuration
cf400f48 77 coerce => 1,
78 isa => 'DBIx::Class::DeploymentHandler::Databases',
79 is => 'ro',
9e401dc2 80 default => sub { [qw( MySQL SQLite PostgreSQL )] },
81);
82
d3b45f46 83has sqltargs => ( # configuration
ecc3b6be 84 isa => 'HashRef',
85 is => 'ro',
86 default => sub { {} },
87);
88
741620a6 89method deploy {
46a47cd4 90 my $storage = $self->storage;
741620a6 91
92 my $deploy = sub {
93 my $line = shift;
d76abae0 94 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
741620a6 95 $storage->_query_start($line);
d76abae0 96 try {
741620a6 97 # do a dbh_do cycle here, as we need some error checking in
98 # place (even though we will ignore errors)
99 $storage->dbh_do (sub { $_[1]->do($line) });
d76abae0 100 }
101 catch {
102 carp "$_ (running '${line}')"
741620a6 103 }
104 $storage->_query_end($line);
105 };
d3b45f46 106 my @statements = $self->deployment_statements;
741620a6 107 if (@statements > 1) {
108 foreach my $statement (@statements) {
109 $deploy->( $statement );
110 }
111 }
112 elsif (@statements == 1) {
113 foreach my $line ( split(";\n", $statements[0])) {
114 $deploy->( $line );
115 }
116 }
117}
118
b974984a 119method install($new_version) {
12fdd461 120 carp 'Install not possible as versions table already exists in database'
ceef4ff5 121 if $self->is_installed;
b974984a 122
12fdd461 123 $new_version ||= $self->schema_version;
b974984a 124
125 if ($new_version) {
d3b45f46 126 $self->deploy;
12fdd461 127
128 $self->version_rs->create({
61847972 129 version => $new_version,
130 # ddl => $ddl,
131 # upgrade_sql => $upgrade_sql,
12fdd461 132 });
b974984a 133 }
134}
135
8636376a 136method ordered_schema_versions { undef }
b974984a 137
138method upgrade {
12fdd461 139 my $db_version = $self->db_version;
140 my $schema_version = $self->schema_version;
b974984a 141
b974984a 142 unless ($db_version) {
61847972 143 # croak?
144 carp 'Upgrade not possible as database is unversioned. Please call install first.';
145 return;
b974984a 146 }
147
12fdd461 148 if ( $db_version eq $schema_version ) {
61847972 149 # croak?
150 carp "Upgrade not necessary\n";
151 return;
b974984a 152 }
153
12fdd461 154 my @version_list = $self->ordered_schema_versions ||
155 ( $db_version, $schema_version );
b974984a 156
157 # remove all versions in list above the required version
12fdd461 158 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
61847972 159 pop @version_list;
b974984a 160 }
161
162 # remove all versions in list below the current version
12fdd461 163 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
61847972 164 shift @version_list;
b974984a 165 }
166
167 # check we have an appropriate list of versions
12fdd461 168 die if @version_list < 2;
b974984a 169
170 # do sets of upgrade
12fdd461 171 while ( @version_list >= 2 ) {
61847972 172 $self->upgrade_single_step( $version_list[0], $version_list[1] );
173 shift @version_list;
b974984a 174 }
175}
176
2e68a8e1 177__PACKAGE__->meta->make_immutable;
178
b974984a 1791;
61847972 180
181__END__
182
183vim: ts=2,sw=2,expandtab