我最终这样做了,其中 $schema 是一个全局变量。由于使用了show tables
.
sub tableExists {
my $table = shift;
my $dbh = $schema->storage->dbh; # Get our DBI handle.
my $sql = "show tables like '$table'"; #MySQL only
my $sth = $dbh->prepare($sql);
my $exists = undef;
if ( $sth->execute() ) {
while ( my $t = $sth->fetchrow_array() ) {
print $t, $/;
if ( $t =~ /^$table$/ ) { $exists = 1; }
}
}
if ( defined $exists ) {
print "found\n";
return $exists;
}
else {
print "not found\n";
return $exists;
}
}
我这样称呼它:
$schema = MyApp::Schema->connect(
$dsn,
$user,
$password,
);
my $table_exists = tableExists("mytable");
if ( !defined $table_exists ) {
print "Deploying schema...", $/;
$schema->deploy();
print "Done", $/;
}