Created
November 17, 2015 09:45
-
-
Save mrenvoize/db676953f1174fff4f3b to your computer and use it in GitHub Desktop.
Mojolicious DBIx::Class::DeploymentHandler Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Rebus::Command::db; | |
use Mojo::Base 'Mojolicious::Command'; | |
use DBIx::Class::DeploymentHandler; | |
use Getopt::Long qw(GetOptions :config pass_through); | |
use File::Path qw(remove_tree); | |
has description => ''; | |
has usage => ''; | |
sub run { | |
my ($self) = @_; | |
my $cmd = defined($ARGV[1]) ? $ARGV[1] : 'status'; | |
my $from_version; | |
my $to_version; | |
my $version; | |
my $force_overwrite = 0; | |
GetOptions( | |
'from-version=i' => \$from_version, | |
'to-version=i' => \$to_version, | |
'version=i' => \$version, | |
'force=s' => \$force_overwrite | |
); | |
my $schema = $self->app->db; | |
my $script_dir = $self->app->home->rel_dir('share/migrations'); | |
my $dbic_dh = DBIx::Class::DeploymentHandler->new( | |
{ | |
schema => $schema, | |
script_directory => $script_dir, | |
databases => 'PostgreSQL', | |
force_overwrite => $force_overwrite | |
} | |
); | |
if ($cmd eq 'prepare') { | |
my $schema_version = $dbic_dh->schema_version || die "Your Schema has no version!"; | |
# Prepare Upgrade/Downgrade | |
if (my $previous = _has_previous_version($schema_version)) { | |
$dbic_dh->version_storage_is_installed || die "No Database to create upgrades from!"; | |
if ($dbic_dh->database_version < $schema_version) { | |
print "Preparing upgrade files\n"; | |
$dbic_dh->prepare_deploy; | |
$dbic_dh->prepare_upgrade({from_version => $previous, to_version => $schema_version}); | |
# Clean up after ourselves | |
remove_tree("$script_dir/PostgreSQL/deploy/$schema_version"); | |
} | |
else { | |
print "Your Database version must be lower than than your schema version in order to prepare upgrades\n"; | |
} | |
} | |
else { | |
print "There is no current database deployed, so I can't prepare upgrades\n"; | |
} | |
} | |
elsif ($cmd eq 'install') { | |
$dbic_dh->install({version => 1}); | |
$dbic_dh->upgrade; | |
} | |
elsif ($cmd eq 'upgrade') { | |
$dbic_dh->upgrade; | |
} | |
elsif ($cmd eq 'status') { | |
print "Schema is ${\$dbic_dh->schema_version}\n"; | |
if ($dbic_dh->version_storage_is_installed) { | |
print "Deployed database is ${\$dbic_dh->database_version}\n"; | |
} | |
else { | |
print "Database is not currently installed\n"; | |
} | |
} | |
else { | |
print "Schema is ${\$dbic_dh->schema_version}\n"; | |
if ($dbic_dh->version_storage_is_installed) { | |
print "Deployed database is ${\$dbic_dh->database_version}\n"; | |
} | |
else { | |
print "Database is not currently installed\n"; | |
} | |
} | |
} | |
sub _has_previous_version { $_[0] ? $_[0] - 1 : 0 } | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment