Navigation Menu

Skip to content

Commit

Permalink
Fixes for dbicdh.
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
jberger committed Oct 13, 2014
1 parent 7ef516e commit 59018b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
48 changes: 25 additions & 23 deletions lib/Galileo/Command/setup.pm
Expand Up @@ -7,26 +7,17 @@ use Mojolicious::Routes;
use Mojo::JSON 'j';
use Mojo::Util 'spurt';

use Galileo::DB::Deploy;

has description => "Configure your Galileo CMS via a web interface\n";

sub run {
my ($self, @args) = @_;

my $app = $self->app;
$app->plugin('Galileo::Plugin::Deploy'); # provides dh helper

my $r = Mojolicious::Routes->new;
$app->routes($r); # remove all routes

push @{ $app->renderer->classes }, __PACKAGE__;

$app->helper( dh => sub {
my $self = shift;
state $dh = Galileo::DB::Deploy->new( schema => $self->app->schema );
$dh;
});

$app->helper( 'control_group' => sub {
my $self = shift;
my $contents = pop;
Expand Down Expand Up @@ -63,17 +54,15 @@ sub run {
my $self = shift;

my $dh = $self->dh;
my $schema = $dh->schema;

my $available = $schema->schema_version;

# Nothing installed
unless ( $dh->has_admin_user ) {
my $installed = $dh->installed_version;
unless ( $installed ) {
return $self->render( 'setup/database' );
}

# Something is installed, check for a version
my $installed = $dh->installed_version || $dh->setup_unversioned;
# Something is installed, check for upgrades
my $available = $dh->schema->schema_version;

# Do nothing if version is current
if ( $installed == $available ) {
Expand All @@ -95,30 +84,43 @@ sub run {
return $self->redirect_to('database');
}

my $dh = $self->dh;
my $user = $self->param('user');
my $full = $self->param('full');

eval { $dh->schema->deploy };
eval { $dh->do_install };
eval { $dh->inject_sample_data($user, $pw1, $full) };
my $dh = $self->dh;

eval {
$dh->do_install;
$dh->inject_sample_data($user, $pw1, $full);
}

if ($@) {
my $error = "$@";
chomp $error;
$self->humane_flash( $error );
return $self->redirect_to('database');
}

$self->flash( 'galileo.message' => 'Database has been setup' );
$self->flash( 'galileo.message' => 'Database has been setup.' );
$self->redirect_to('finish');
});

$r->any('/finish' => sub {
my $self = shift;
my $message = $self->flash( 'galileo.message' );

# check that an admin user exists
if ( $self->app->dh->has_admin_user ) {
my $dh = $self->dh;
my $installed = $dh->version_installed;
my $available = $dh->schema->schema_version;
my $has_admin = $dh->has_admin_user;

if ($installed) {
unless ($has_admin) {
$message .= ' No administration user was created.';
}
unless ($installed == $available) {
$message .= " Installed database version ($installed) is older than the newest available ($available).";
}
$self->stash( 'galileo.success' => 1 );
$self->stash( 'galileo.message' => $message );
} else {
Expand Down
15 changes: 1 addition & 14 deletions lib/Galileo/DB/Deploy.pm
Expand Up @@ -67,24 +67,11 @@ sub installed_version {
return eval{ $self->database_version }
}

sub setup_unversioned {
my $self = shift;

unless ( $self->version_storage_is_installed ) {
$self->prepare_version_storage_install;
$self->install_version_storage;
}

$self->add_database_version({ version => 1 });

return 1;
}

sub do_install {
my $self = shift;

$self->prepare_install;
$self->install;
$self->install( @_ ? {version => shift} : () );
}

sub do_upgrade {
Expand Down
17 changes: 17 additions & 0 deletions lib/Galileo/Plugin/Deploy.pm
@@ -0,0 +1,17 @@
package Galileo::Plugin::Deploy;

use Mojo::Base 'Mojolicious::Plugin';

use Galileo::DB::Deploy;

sub register {
my ($plugin, $app) = @_;

my $dh = Galileo::DB::Deploy->new( schema => $app->schema );
$app->helper( dh => sub { $dh } );

return $dh;
}

1;

0 comments on commit 59018b7

Please sign in to comment.