1 package DBIx::Class::DeploymentHandler;
4 use Method::Signatures::Simple;
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
8 use Carp::Clan '^DBIx::Class::DeploymentHandler';
10 require SQL::Translator::Diff;
13 with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
16 use Moose::Util::TypeConstraints;
17 subtype 'DBIx::Class::DeploymentHandler::Databases'
18 => as 'ArrayRef[Str]';
20 coerce 'DBIx::Class::DeploymentHandler::Databases'
23 no Moose::Util::TypeConstraints;
27 isa => 'DBIx::Class::Schema',
30 handles => [qw( ddl_filename schema_version )],
33 has upgrade_directory => (
40 has backup_directory => (
43 predicate => 'has_backup_directory',
47 isa => 'DBIx::Class::Storage',
52 method _build_storage {
53 my $s = $self->schema->storage;
54 $s->_determine_driver;
64 has do_diff_on_init => (
71 isa => 'DBIx::Class::ResultSet',
74 handles => [qw( is_installed db_version )],
77 method _build_version_rs {
78 $self->schema->set_us_up_the_bomb;
79 $self->schema->resultset('__VERSION')
84 isa => 'DBIx::Class::DeploymentHandler::Databases',
86 default => sub { [qw( MySQL SQLite PostgreSQL )] },
92 default => sub { {} },
96 my $schema = $self->schema;
98 my $sqltargs = $self->sqltargs;
99 my $dir = $self->upgrade_directory;
100 my $storage = $self->storage;
104 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
105 $storage->_query_start($line);
107 # do a dbh_do cycle here, as we need some error checking in
108 # place (even though we will ignore errors)
109 $storage->dbh_do (sub { $_[1]->do($line) });
112 carp "$_ (running '${line}')"
114 $storage->_query_end($line);
116 my @statements = $self->deployment_statements();
117 if (@statements > 1) {
118 foreach my $statement (@statements) {
119 $deploy->( $statement );
122 elsif (@statements == 1) {
123 foreach my $line ( split(";\n", $statements[0])) {
129 method install($new_version) {
130 carp 'Install not possible as versions table already exists in database'
131 if $self->is_installed;
133 $new_version ||= $self->schema_version;
138 $self->version_rs->create({
139 version => $new_version,
141 # upgrade_sql => $upgrade_sql,
146 method ordered_schema_versions { undef }
149 my $db_version = $self->db_version;
150 my $schema_version = $self->schema_version;
152 unless ($db_version) {
154 carp 'Upgrade not possible as database is unversioned. Please call install first.';
158 if ( $db_version eq $schema_version ) {
160 carp "Upgrade not necessary\n";
164 my @version_list = $self->ordered_schema_versions ||
165 ( $db_version, $schema_version );
167 # remove all versions in list above the required version
168 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
172 # remove all versions in list below the current version
173 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
177 # check we have an appropriate list of versions
178 die if @version_list < 2;
181 while ( @version_list >= 2 ) {
182 $self->upgrade_single_step( $version_list[0], $version_list[1] );
187 __PACKAGE__->meta->make_immutable;
193 vim: ts=2,sw=2,expandtab