| 1 | package DBIx::Class::DeploymentAdapter; | ||
| 2 | use 5.008001; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = "0.07"; | ||
| 7 | |||
| 8 | =encoding utf-8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | DBIx::Class::DeploymentAdapter - Deployment handler adapter to your DBIC app, which offers some candy | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use DBIx::Class::DeploymentAdapter; | ||
| 17 | |||
| 18 | my $args = { | ||
| 19 | schema => $schema, | ||
| 20 | script_directory => './share/migrations', | ||
| 21 | databases => ['MySQL'], | ||
| 22 | sql_translator_args => { mysql_enable_utf8 => 1 }, | ||
| 23 | }; | ||
| 24 | |||
| 25 | $args->{to_version} = $to_version if $to_version; | ||
| 26 | $args->{force_overwrite} = $force_overwrite if $force_overwrite; | ||
| 27 | |||
| 28 | my $da = DBIx::Class::DeploymentAdapter->new($args); | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Deployment handler adapter to your DBIC app, which offers some candy | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | use DBIx::Class::DeploymentHandler; | ||
| 37 | |||
| 38 | use Moose; | ||
| 39 | |||
| 40 | has dh_store => ( | ||
| 41 | is => "rw", | ||
| 42 | isa => "Maybe[Object]" | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub dh { | ||
| 46 | |||
| 47 | my ( $self, $args ) = @_; | ||
| 48 | |||
| 49 | if ( !$self->dh_store ) { | ||
| 50 | |||
| 51 | return unless $args && $args->{schema}; | ||
| 52 | |||
| 53 | $args->{script_directory} ||= "./share/migrations"; | ||
| 54 | $args->{databases} ||= ["MySQL"]; | ||
| 55 | $args->{sql_translator_args} ||= { mysql_enable_utf8 => 1 }; | ||
| 56 | |||
| 57 | my $dh = DBIx::Class::DeploymentHandler->new($args); | ||
| 58 | $self->dh_store( $dh ); | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | return $self->dh_store; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub BUILD { | ||
| 66 | |||
| 67 | my $self = shift; | ||
| 68 | my $args = shift; | ||
| 69 | |||
| 70 | $self->dh($args); | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 install | ||
| 74 | |||
| 75 | Installs the schema files to the given Database | ||
| 76 | |||
| 77 | $da->install; | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub install { | ||
| 82 | |||
| 83 | my ( $self ) = @_; | ||
| 84 | |||
| 85 | return unless $self->dh; | ||
| 86 | |||
| 87 | $self->dh->install; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 prepare | ||
| 91 | |||
| 92 | Summarize all prepares from L<DBIx::Class::DeploymentHandler> in one Command | ||
| 93 | |||
| 94 | $da->prepare; | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub prepare { | ||
| 99 | |||
| 100 | my ($self) = @_; | ||
| 101 | |||
| 102 | return unless $self->dh; | ||
| 103 | |||
| 104 | my $start_version = $self->dh->database_version; | ||
| 105 | my $target_version = $self->dh->schema->schema_version; | ||
| 106 | |||
| 107 | $self->dh->prepare_install; | ||
| 108 | |||
| 109 | $self->dh->prepare_upgrade( | ||
| 110 | { | ||
| 111 | from_version => $start_version, | ||
| 112 | to_version => $target_version, | ||
| 113 | } | ||
| 114 | ); | ||
| 115 | |||
| 116 | $self->dh->prepare_downgrade( | ||
| 117 | { | ||
| 118 | from_version => $target_version, | ||
| 119 | to_version => $start_version, | ||
| 120 | } | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 status | ||
| 125 | |||
| 126 | Returns the Status of database and schema versions as string | ||
| 127 | |||
| 128 | $da->status; | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub status { | ||
| 133 | |||
| 134 | my ( $self ) = @_; | ||
| 135 | |||
| 136 | return unless ref $self->dh; | ||
| 137 | |||
| 138 | my $deployed_version = $self->dh->database_version; | ||
| 139 | my $schema_version = $self->dh->schema->schema_version; | ||
| 140 | |||
| 141 | return sprintf( "Schema is %s\nDeployed database is %s\n", $schema_version, $deployed_version ); | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | =head2 upgrade_incremental | ||
| 146 | |||
| 147 | Upgrade the database version step by step, if anything wents wrong, it dies with the specific database error. | ||
| 148 | |||
| 149 | You can give a target version to the method to make it stop there | ||
| 150 | |||
| 151 | $da->upgrade_incremental; | ||
| 152 | $da->upgrade_incremental(112); | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub upgrade_incremental { | ||
| 157 | |||
| 158 | my ( $self, $to_version ) = @_; | ||
| 159 | |||
| 160 | return unless $self->dh; | ||
| 161 | |||
| 162 | my $start_version = $self->dh->database_version + 1; | ||
| 163 | my $target_version = $self->dh->schema->schema_version; | ||
| 164 | |||
| 165 | for my $version ( $start_version .. $target_version ) { | ||
| 166 | |||
| 167 | if( $to_version && $version >= $to_version ) { | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | |||
| 171 | warn "upgrading to version $version\n"; | ||
| 172 | |||
| 173 | eval { | ||
| 174 | my ( $ddl, $sql ) = @{ $self->dh->upgrade_single_step( { version_set => [ $version - 1, $version ] } ) || [] }; # from last version to desired version | ||
| 175 | $self->dh->add_database_version( | ||
| 176 | { | ||
| 177 | version => $version, | ||
| 178 | ddl => $ddl, | ||
| 179 | upgrade_sql => $sql, | ||
| 180 | } | ||
| 181 | ); | ||
| 182 | }; | ||
| 183 | |||
| 184 | if ($@) { | ||
| 185 | my $error_version = $self->dh->database_version; | ||
| 186 | warn "Database remains on version $error_version"; | ||
| 187 | die "UPGRADE ERROR - Version $error_version upgrading to $version: " . $@; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | =head1 LICENSE | ||
| 197 | |||
| 198 | Copyright (C) Patrick Kilter. | ||
| 199 | |||
| 200 | This library is free software; you can redistribute it and/or modify | ||
| 201 | it under the same terms as Perl itself. | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Patrick Kilter E<lt>pk@gassmann.itE<gt> | ||
| 206 | |||
| 207 | =cut |