use_namespaces upgrade is fully tested, need to implement downgrade
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 8c82515..1856903 100644 (file)
@@ -38,7 +38,6 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 dump_directory
                                 dump_overwrite
                                 really_erase_my_files
-                                use_namespaces
                                 result_namespace
                                 resultset_namespace
                                 default_resultset_class
@@ -62,6 +61,8 @@ __PACKAGE__->mk_group_accessors('simple', qw/
                                 version_to_dump
                                 schema_version_to_dump
                                 _upgrading_from
+                                _upgrading_from_load_classes
+                                use_namespaces
 /);
 
 =head1 NAME
@@ -213,7 +214,7 @@ the chunks back together.  Examples:
     ---------------------------
     luser       | Luser
     luser_group | LuserGroup
-    luser-opts  | LuserOpts
+    luser-opts  | LuserOpt
 
 =head2 inflect_plural
 
@@ -263,6 +264,9 @@ C<components> list if this option is set.
 
 =head2 use_namespaces
 
+This is now the default, to go back to L<DBIx::Class::Schema/load_classes> pass
+a C<0>.
+
 Generate result class names suitable for
 L<DBIx::Class::Schema/load_namespaces> and call that instead of
 L<DBIx::Class::Schema/load_classes>. When using this option you can also
@@ -412,6 +416,8 @@ sub new {
 
     $self->_check_back_compat;
 
+    $self->use_namespaces(1) unless defined $self->use_namespaces;
+
     $self;
 }
 
@@ -431,6 +437,8 @@ Dynamic schema detected, will run in 0.04006 mode.
 Set the 'naming' attribute or the SCHEMA_LOADER_BACKCOMPAT environment variable
 to disable this warning.
 
+Also consider setting 'use_namespaces => 1' if/when upgrading.
+
 See perldoc DBIx::Class::Schema::Loader::Manual::UpgradingFromV4 for more
 details.
 EOF
@@ -442,6 +450,13 @@ EOF
         $self->naming->{relationships} ||= 'v4';
         $self->naming->{monikers}      ||= 'v4';
 
+        if ($self->use_namespaces) {
+            $self->_upgrading_from_load_classes(1);
+        }
+        else {
+            $self->use_namespaces(0);
+        }
+
         return;
     }
 
@@ -452,9 +467,30 @@ EOF
     open(my $fh, '<', $filename)
         or croak "Cannot open '$filename' for reading: $!";
 
+    my $load_classes = 0;
+
     while (<$fh>) {
-        if (/^# Created by DBIx::Class::Schema::Loader v((\d+)\.(\d+))/) {
-            my $real_ver = $1;
+        if (/^__PACKAGE__->load_classes;/) {
+            $load_classes = 1;
+        } elsif (my ($real_ver) =
+                /^# Created by DBIx::Class::Schema::Loader v(\d+\.\d+)/) {
+
+            if ($load_classes && (not defined $self->use_namespaces)) {
+                warn <<"EOF"  unless $ENV{SCHEMA_LOADER_BACKCOMPAT};
+
+'load_classes;' static schema detected, turning off 'use_namespaces'.
+
+Set the 'use_namespaces' attribute or the SCHEMA_LOADER_BACKCOMPAT environment
+variable to disable this warning.
+
+See perldoc DBIx::Class::Schema::Loader::Manual::UpgradingFromV4 for more
+details.
+EOF
+                $self->use_namespaces(0);
+            }
+            elsif ($load_classes && $self->use_namespaces) {
+                $self->_upgrading_from_load_classes(1);
+            }
 
             # XXX when we go past .0 this will need fixing
             my ($v) = $real_ver =~ /([1-9])/;
@@ -581,7 +617,8 @@ sub _load_external {
          .qq|# for you to hand-edit.  If you do not either delete\n|
          .qq|# this section or remove that file from \@INC, this section\n|
          .qq|# will be repeated redundantly when you re-create this\n|
-         .qq|# file again via Loader!\n|
+         .qq|# file again via Loader!  See skip_load_external to disable\n|
+         .qq|# this feature.\n|
         );
         chomp $code;
         $self->_ext_stmt($class, $code);
@@ -598,7 +635,7 @@ sub _load_external {
 # These lines were loaded from '$old_real_inc_path',
 # based on the Result class name that would have been created by an 0.04006
 # version of the Loader. For a static schema, this happens only once during
-# upgrade.
+# upgrade. See skip_load_external to disable this feature.
 EOF
 
         my $code = do {
@@ -1061,8 +1098,14 @@ sub _make_src_class {
     }
     my $table_class = join(q{::}, @result_namespace, $table_moniker);
 
-    if (my $upgrading_v = $self->_upgrading_from) {
-        local $self->naming->{monikers} = $upgrading_v;
+    if ((my $upgrading_v = $self->_upgrading_from)
+            || $self->_upgrading_from_load_classes) {
+        local $self->naming->{monikers} = $upgrading_v
+            if $upgrading_v;
+
+        my @result_namespace = @result_namespace;
+        @result_namespace = ($schema_class)
+            if $self->_upgrading_from_load_classes;
 
         my $old_class = join(q{::}, @result_namespace,
             $self->_table2moniker($table));