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