Catalyst-Plugin-Authorization-Abilities
view release on metacpan or search on metacpan
t/lib/Schema/Utils.pm view on Meta::CPAN
}
my $merge = Hash::Merge->new( 'LEFT_PRECEDENT' );
my $allattrs = $merge->merge( $unicode_option, $attrs );
return $dsn, $user, $password, $allattrs;
}
sub _build_schema {
my $self = shift;
my $schema_class = $self->model->{schema_class};
$schema_class->require or die $@;
my ($dsn, $user, $pass, $args ) = $self->_connect_info;
return $schema_class->connect($dsn, $user, $pass, $args )
or die "Failed to connect to database";
}
sub schema_class {
my $self = shift;
return $self->model->{'connect_info'}->{schema_class};
}
sub dsn {
my $self = shift;
my $dsn = ${$self->model->{'connect_info'}}[0]
or croak "dsn is not defined in " . $self->conf;
return $dsn
}
=head2 init_schema
use Schema::Utils;
my $schema = Schema::Utils->schema( conf => $conf );
$schema->init_schema(populate => 1);
This method creates a fresh test database. If the C<populate> flag is true,
it will call L</populate_schema>.
=cut
sub init_schema {
my $self = shift;
my %args = @_;
my $schema = $self->schema;
# if add_drop_table has been specified, it will try to drop tables beforehand, but not "IF EXISTS",
# due to a BUG in SQL::Translator: https://rt.cpan.org/Ticket/Display.html?id=48688
# This will cause failures if the tables don't exist (i.e. when you first deploy):
# ("DBI Exception: DBD::$driver::db do failed: no such table")
#
#-mxh This is fragile because it relies on fixed output in the regex.
# Recently, the output changed to include a "\n" and broke this code.
# I added the s (and i) regex modifiers, but it still needs a better implementation.
local $SIG{__WARN__} = sub {
die @_ unless $_[0] =~ /no such table.*DROP TABLE/is;
};
# Check if database is already deployed by
# examining if the table User exists and has a record.
eval { $schema->resultset('User')->count };
if (!$@) {
die "You have already deployed your database\n";
}
$schema->deploy( $attrs );
$schema->populate_schema if $args{populate};
return $self;
}
1;
( run in 0.610 second using v1.01-cache-2.11-cpan-364913b4093 )