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 => ( # configuration
40 has backup_directory => ( # configuration
43 predicate => 'has_backup_directory',
47 isa => 'DBIx::Class::Storage',
52 method _build_storage {
53 my $s = $self->schema->storage;
54 $s->_determine_driver;
58 has do_backup => ( # configuration
65 isa => 'DBIx::Class::ResultSet',
68 handles => [qw( is_installed db_version )],
71 method _build_version_rs {
72 $self->schema->set_us_up_the_bomb;
73 $self->schema->resultset('__VERSION')
76 has databases => ( # configuration
78 isa => 'DBIx::Class::DeploymentHandler::Databases',
80 default => sub { [qw( MySQL SQLite PostgreSQL )] },
83 has sqltargs => ( # configuration
86 default => sub { {} },
90 my $storage = $self->storage;
94 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
95 $storage->_query_start($line);
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) });
102 carp "$_ (running '${line}')"
104 $storage->_query_end($line);
106 my @statements = $self->deployment_statements;
107 if (@statements > 1) {
108 foreach my $statement (@statements) {
109 $deploy->( $statement );
112 elsif (@statements == 1) {
113 foreach my $line ( split(";\n", $statements[0])) {
119 method install($new_version) {
120 carp 'Install not possible as versions table already exists in database'
121 if $self->is_installed;
123 $new_version ||= $self->schema_version;
128 $self->version_rs->create({
129 version => $new_version,
131 # upgrade_sql => $upgrade_sql,
136 method ordered_schema_versions { undef }
139 my $db_version = $self->db_version;
140 my $schema_version = $self->schema_version;
142 unless ($db_version) {
144 carp 'Upgrade not possible as database is unversioned. Please call install first.';
148 if ( $db_version eq $schema_version ) {
150 carp "Upgrade not necessary\n";
154 my @version_list = $self->ordered_schema_versions ||
155 ( $db_version, $schema_version );
157 # remove all versions in list above the required version
158 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
162 # remove all versions in list below the current version
163 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
167 # check we have an appropriate list of versions
168 die if @version_list < 2;
171 while ( @version_list >= 2 ) {
172 $self->upgrade_single_step( $version_list[0], $version_list[1] );
177 __PACKAGE__->meta->make_immutable;
183 vim: ts=2,sw=2,expandtab