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 # the \nCOMMIT below is entirely to make the tests quieter,
95 # there is surely a better way to fix it (/m breaks everything)
96 return if(!$line || $line =~ /^(--|BEGIN TRANSACTION|\nCOMMIT|\s+$)/);
97 $storage->_query_start($line);
99 # do a dbh_do cycle here, as we need some error checking in
100 # place (even though we will ignore errors)
101 $storage->dbh_do (sub { $_[1]->do($line) });
104 carp "$_ (running '${line}')"
106 $storage->_query_end($line);
108 my @statements = $self->deployment_statements;
109 if (@statements > 1) {
110 foreach my $statement (@statements) {
111 $deploy->( $statement );
114 elsif (@statements == 1) {
115 foreach my $line ( split(";\n", $statements[0])) {
121 method install($new_version) {
122 carp 'Install not possible as versions table already exists in database'
123 if $self->is_installed;
125 $new_version ||= $self->schema_version;
130 $self->version_rs->create({
131 version => $new_version,
133 # upgrade_sql => $upgrade_sql,
138 method ordered_schema_versions { undef }
141 my $db_version = $self->db_version;
142 my $schema_version = $self->schema_version;
144 unless ($db_version) {
146 carp 'Upgrade not possible as database is unversioned. Please call install first.';
150 if ( $db_version eq $schema_version ) {
152 carp "Upgrade not necessary\n";
156 my @version_list = $self->ordered_schema_versions ||
157 ( $db_version, $schema_version );
159 # remove all versions in list above the required version
160 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
164 # remove all versions in list below the current version
165 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
169 # check we have an appropriate list of versions
170 die if @version_list < 2;
173 while ( @version_list >= 2 ) {
174 $self->upgrade_single_step( $version_list[0], $version_list[1] );
179 __PACKAGE__->meta->make_immutable;
185 vim: ts=2 sw=2 expandtab