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 => (
46 isa => 'DBIx::Class::Storage',
51 method _build_storage {
52 my $s = $self->schema->storage;
53 $s->_determine_driver;
63 has do_diff_on_init => (
70 isa => 'DBIx::Class::ResultSet',
73 handles => [qw( is_installed db_version )],
76 method _build_version_rs {
77 $self->schema->set_us_up_the_bomb;
78 $self->schema->resultset('__VERSION')
83 isa => 'DBIx::Class::DeploymentHandler::Databases',
85 default => sub { [qw( MySQL SQLite PostgreSQL )] },
91 default => sub { {} },
95 my $schema = $self->schema;
97 my $sqltargs = $self->sqltargs;
98 my $dir = $self->upgrade_directory;
99 my $storage = $self->storage;
103 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
104 $storage->_query_start($line);
106 # do a dbh_do cycle here, as we need some error checking in
107 # place (even though we will ignore errors)
108 $storage->dbh_do (sub { $_[1]->do($line) });
111 carp "$_ (running '${line}')"
113 $storage->_query_end($line);
115 my @statements = $self->deployment_statements();
116 if (@statements > 1) {
117 foreach my $statement (@statements) {
118 $deploy->( $statement );
121 elsif (@statements == 1) {
122 foreach my $line ( split(";\n", $statements[0])) {
128 method install($new_version) {
129 carp 'Install not possible as versions table already exists in database'
130 if $self->is_installed;
132 $new_version ||= $self->schema_version;
137 $self->version_rs->create({
138 version => $new_version,
140 # upgrade_sql => $upgrade_sql,
145 method ordered_schema_versions { undef }
148 my $db_version = $self->db_version;
149 my $schema_version = $self->schema_version;
151 unless ($db_version) {
153 carp 'Upgrade not possible as database is unversioned. Please call install first.';
157 if ( $db_version eq $schema_version ) {
159 carp "Upgrade not necessary\n";
163 my @version_list = $self->ordered_schema_versions ||
164 ( $db_version, $schema_version );
166 # remove all versions in list above the required version
167 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
171 # remove all versions in list below the current version
172 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
176 # check we have an appropriate list of versions
177 die if @version_list < 2;
180 while ( @version_list >= 2 ) {
181 $self->upgrade_single_step( $version_list[0], $version_list[1] );
186 __PACKAGE__->meta->make_immutable;
192 vim: ts=2,sw=2,expandtab