minor fixups
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Admin.pm
index a8f52ee..862143f 100644 (file)
@@ -1,59 +1,38 @@
-#
-#===============================================================================
-#
-#         FILE:  Admin.pm
-#
-#  DESCRIPTION:  Administrative functions for DBIx::Class Schemata
-#
-#        FILES:  ---
-#         BUGS:  ---
-#        NOTES:  ---
-#       AUTHOR:  Gordon Irving (), <Gordon.irving@sophos.com>
-#      VERSION:  1.0
-#      CREATED:  28/11/09 12:27:15 GMT
-#     REVISION:  ---
-#===============================================================================
-
 package DBIx::Class::Admin;
 
+# check deps
+BEGIN {
+    my @_deps = qw(
+        Moose MooseX::Types MooseX::Types::JSON MooseX::Types::Path::Class
+        Try::Tiny parent JSON::Any Class::C3::Componentised
+        namespace::autoclean
+    );
+
+    my @_missing_deps;
+    foreach my $dep (@_deps) {
+      eval "require $dep";
+      if ($@) {
+        push @_missing_deps, $dep;
+      }
+    }
+
+    if (@_missing_deps > 0) {
+      die "The following dependecies are missing " . join ",", @_missing_deps;
+    }
+}
+
 use Moose;
-use MooseX::Types -declare => [qw( DBICConnectInfo )];
-use MooseX::Types::Moose qw/Int HashRef ArrayRef Str Any/;
+use parent 'DBIx::Class::Schema';
+use Carp::Clan qw/^DBIx::Class/;
+
+use MooseX::Types::Moose qw/Int HashRef ArrayRef Str Any Bool/;
+use DBIx::Class::Admin::Types qw/DBICConnectInfo/;
 use MooseX::Types::JSON qw(JSON);
 use MooseX::Types::Path::Class qw(Dir File);
 use Try::Tiny;
-
-use parent 'Class::C3::Componentised';
-
 use JSON::Any;
-
 use namespace::autoclean;
 
-my @_deps = qw(Moose MooseX::Types MooseX::Types::JSON MooseX::Types::Path::Class Try::Tiny parent JSON::Any Class::C3::Componentised namespace::autoclean);
-
-coerce ArrayRef,
-  from JSON,
-  via { _json_to_data ($_) };
-
-coerce HashRef,
-  from JSON,
-  via { _json_to_data($_) };
-
-subtype DBICConnectInfo,
-  as ArrayRef;
-
-coerce DBICConnectInfo,
-  from JSON,
-   via { return _json_to_data($_) } ;
-
-coerce DBICConnectInfo,
-  from Str,
-    via { return _json_to_data($_) };
-
-coerce DBICConnectInfo,
-  from HashRef,
-   via { [ $_->{dsn}, $_->{user}, $_->{password} ]  };
-
 
 =head1 NAME
 
@@ -84,25 +63,6 @@ DBIx::Class::Admin - Administration object for schemas
 
 =head1 Attributes
 
-=head2 lib
-
-add a library search path
-
-=cut
-
-has lib => (
-  is    => 'ro',
-  isa    => Dir,
-  coerce  => 1,
-  trigger => \&_set_inc,
-);
-
-sub _set_inc {
-  my ($self, $lib) = @_;
-  push @INC, $lib->stringify;
-}
-
-
 =head2 schema_class
 
 the class of the schema to load
@@ -111,7 +71,7 @@ the class of the schema to load
 
 has 'schema_class' => (
   is    => 'ro',
-  isa    => 'Str',
+  isa    => Str,
   coerce  => 1,
 );
 
@@ -231,7 +191,7 @@ designed for use with catalyst config files
 
 has 'config_stanza' => (
   is      => 'ro',
-  isa      => 'Str',
+  isa      => Str,
 );
 
 
@@ -250,7 +210,7 @@ has config => (
 
 sub _build_config {
   my ($self) = @_;
-  try { require Config::Any } catch { die "Config::Any is required to parse the config file"; };
+  try { require Config::Any } catch { $self->throw_exception( "Config::Any is required to parse the config file"); };
 
   my $cfg = Config::Any->load_files ( {files => [$self->config_file], use_ext =>1, flatten_to_hash=>1});
 
@@ -281,7 +241,7 @@ Used for install, the version which will be 'installed' in the schema
 
 has version => (
   is      => 'rw',
-  isa      => 'Str',
+  isa      => Str,
 );
 
 
@@ -293,7 +253,7 @@ Previouse version of the schema to create an upgrade diff for, the full sql for
 
 has preversion => (
   is      => 'rw',
-  isa      => 'Str',
+  isa      => Str,
 );
 
 
@@ -305,7 +265,7 @@ Try and force certain operations.
 
 has force => (
   is      => 'rw',
-  isa      => 'Bool',
+  isa      => Bool,
 );
 
 
@@ -317,12 +277,12 @@ Be less verbose about actions
 
 has quiet => (
   is      => 'rw',
-  isa      => 'Bool',
+  isa      => Bool,
 );
 
 has '_confirm' => (
   is    => 'bare',
-  isa    => 'Bool',
+  isa    => Bool,
 );
 
 
@@ -376,7 +336,7 @@ sub upgrade {
   my $schema = $self->schema();
   if (!$schema->get_db_version()) {
     # schema is unversioned
-    die "could not determin current schema version, please either install or deploy";
+    $self->throw_exception ("could not determin current schema version, please either install or deploy");
   } else {
     my $ret = $schema->upgrade();
     return $ret;
@@ -411,14 +371,13 @@ sub install {
     print "retun is $ret\n";
   }
   elsif ($schema->get_db_version() and $self->force ) {
-    warn "Forcing install may not be a good idea";
+    carp "Forcing install may not be a good idea";
     if($self->_confirm() ) {
-      # FIXME private api
       $self->schema->_set_db_version({ version => $version});
     }
   }
   else {
-    die "schema already has a version not installing, try upgrade instead";
+    $self->throw_exception ("schema already has a version not installing, try upgrade instead");
   }
 
 }
@@ -443,18 +402,12 @@ sub deploy {
   if (!$schema->get_db_version() ) {
     # schema is unversioned
     $schema->deploy( $args, $self->sql_dir)
-      or die "could not deploy schema";
+      or $self->throw_exception ("could not deploy schema");
   } else {
-    die "there already is a database with a version here, try upgrade instead";
+    $self->throw_exception("there already is a database with a version here, try upgrade instead");
   }
 }
 
-
-# FIXME ensure option spec compatability
-#die('Do not use the where option with the insert op') if ($where);
-#die('Do not use the attrs option with the insert op') if ($attrs);
-
-
 =head2 insert
 
 =over 4
@@ -509,9 +462,6 @@ sub update {
   }
 }
 
-# FIXME
-#die('Do not use the set option with the delete op') if ($set);
-
 
 =head2 delete
 
@@ -599,38 +549,12 @@ sub _find_stanza {
       $cfg = $cfg->{$path};
     }
     else {
-      die "could not find $stanza in config, $path did not seem to exist";
+      $self->throw_exception("could not find $stanza in config, $path did not seem to exist");
     }
   }
   return $cfg;
 }
 
-sub _json_to_data {
-  my ($json_str) = @_;
-  my $json = JSON::Any->new(allow_barekey => 1, allow_singlequote => 1, relaxed=>1);
-  my $ret = $json->jsonToObj($json_str);
-  return $ret;
-}
-
-
-{  # deps check
-
-my @_missing_deps;
-foreach my $dep (@_deps) {
-  eval "require $dep";
-  if ($@) {
-    push @_missing_deps, $dep;
-  }
-}
-
-if (@_missing_deps > 0) {
-  die "The following dependecies are missing " . join ",", @_missing_deps;
-}
-
-
-}
-
-
 =head1 AUTHORS
 
 See L<DBIx::Class/CONTRIBUTORS>.