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 $storage = $self->storage;
100 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
101 $storage->_query_start($line);
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) });
108 carp "$_ (running '${line}')"
110 $storage->_query_end($line);
112 my @statements = $self->deployment_statements();
113 if (@statements > 1) {
114 foreach my $statement (@statements) {
115 $deploy->( $statement );
118 elsif (@statements == 1) {
119 foreach my $line ( split(";\n", $statements[0])) {
125 method install($new_version) {
126 carp 'Install not possible as versions table already exists in database'
127 if $self->is_installed;
129 $new_version ||= $self->schema_version;
134 $self->version_rs->create({
135 version => $new_version,
137 # upgrade_sql => $upgrade_sql,
142 method ordered_schema_versions { undef }
145 my $db_version = $self->db_version;
146 my $schema_version = $self->schema_version;
148 unless ($db_version) {
150 carp 'Upgrade not possible as database is unversioned. Please call install first.';
154 if ( $db_version eq $schema_version ) {
156 carp "Upgrade not necessary\n";
160 my @version_list = $self->ordered_schema_versions ||
161 ( $db_version, $schema_version );
163 # remove all versions in list above the required version
164 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
168 # remove all versions in list below the current version
169 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
173 # check we have an appropriate list of versions
174 die if @version_list < 2;
177 while ( @version_list >= 2 ) {
178 $self->upgrade_single_step( $version_list[0], $version_list[1] );
183 __PACKAGE__->meta->make_immutable;
189 vim: ts=2,sw=2,expandtab