Initial back-compat code
Dagfinn Ilmari Mannsåker [Wed, 26 Aug 2009 23:09:44 +0000 (23:09 +0000)]
lib/DBIx/Class/Schema/Loader/Base.pm
lib/DBIx/Class/Schema/Loader/Compat/v0_040.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Loader/RelBuilder.pm
lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm [new file with mode: 0644]

index f674dcd..7514b59 100644 (file)
@@ -269,12 +269,36 @@ sub new {
                                                      TMPDIR  => 1,
                                                      CLEANUP => 1,
                                                    );
+    $self->_check_back_compat;
+    $self;
+}
 
-    $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
-        $self->schema, $self->inflect_plural, $self->inflect_singular
-    ) if !$self->{skip_relationships};
+sub _check_back_compat {
+    my ($self) = @_;
 
-    $self;
+    my $filename = $self->_get_dump_filename($self->schema_class);
+    return unless -e $filename;
+
+    open(my $fh, '<', $filename)
+        or croak "Cannot open '$filename' for reading: $!";
+
+    while (<$fh>) {
+        if (/^# Created by DBIx::Class::Schema::Loader (v\d+)\.(\d+)/) {
+            my $ver = "${1}_${2}";
+            while (1) {
+                my $compat_class = "DBIx::Class::Schema::Loader::Compat::${ver}";
+                if ($self->load_optional_class($compat_class)) {
+                    no strict 'refs';
+                    my $class = ref $self || $self;
+                    unshift @{"${class}::ISA"}, $compat_class;
+                    last;
+                }
+                $ver =~ s/\d\z// or last;
+            }
+            last;
+        }
+    }
+    close $fh;
 }
 
 sub _find_file_in_inc {
@@ -360,7 +384,7 @@ sub rescan {
     my ($self, $schema) = @_;
 
     $self->{schema} = $schema;
-    $self->{relbuilder}{schema} = $schema;
+    $self->_relbuilder->{schema} = $schema;
 
     my @created;
     my @current = $self->_tables_list;
@@ -375,6 +399,13 @@ sub rescan {
     return map { $self->monikers->{$_} } @$loaded;
 }
 
+sub _relbuilder {
+    my ($self) = @_;
+    $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new(
+        $self->schema, $self->inflect_plural, $self->inflect_singular
+    );
+}
+
 sub _load_tables {
     my ($self, @tables) = @_;
 
@@ -760,7 +791,7 @@ sub _load_relationships {
     my $tbl_uniq_info = $self->_table_uniq_info($table);
 
     my $local_moniker = $self->monikers->{$table};
-    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info, $tbl_uniq_info);
+    my $rel_stmts = $self->_relbuilder->generate_code($local_moniker, $tbl_fk_info, $tbl_uniq_info);
 
     foreach my $src_class (sort keys %$rel_stmts) {
         my $src_stmts = $rel_stmts->{$src_class};
diff --git a/lib/DBIx/Class/Schema/Loader/Compat/v0_040.pm b/lib/DBIx/Class/Schema/Loader/Compat/v0_040.pm
new file mode 100644 (file)
index 0000000..3073159
--- /dev/null
@@ -0,0 +1,22 @@
+package DBIx::Class::Schema::Loader::Compat::v0_040;
+
+use strict;
+use warnings;
+use Class::C3;
+use DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040;
+
+# Make a moniker from a table
+sub _default_table2moniker {
+    my ($self, $table) = @_;
+
+    return join '', map ucfirst, split /[\W_]+/, lc $table;
+}
+
+sub _relbuilder {
+       my ($self) = @_;
+    $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder::v04Compat->new(
+        $self->schema, $self->inflect_plural, $self->inflect_singular
+    );
+}
+
+1;
index 8c30912..a2b587d 100644 (file)
@@ -2,6 +2,7 @@ package DBIx::Class::Schema::Loader::RelBuilder;
 
 use strict;
 use warnings;
+use Class::C3;
 use Carp::Clan qw/^DBIx::Class/;
 use Lingua::EN::Inflect::Number ();
 
diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm
new file mode 100644 (file)
index 0000000..3d12a25
--- /dev/null
@@ -0,0 +1,34 @@
+package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040;
+
+use strict;
+use warnings;
+use Class::C3;
+
+use base 'DBIx::Class::Schema::Loader::RelBuilder';
+
+sub _uniq_fk_rel {
+    my ($self, $local_moniker, $local_relname, $local_cols, $uniqs) = @_;
+
+    return ('has_many', $local_relname);
+}
+
+sub _remote_attrs { }
+
+sub _remote_relname {
+    my ($self, $remote_table, $cond) = @_;
+
+    my $remote_relname;
+    # for single-column case, set the remote relname to the column
+    # name, to make filter accessors work
+    if(scalar keys %{$cond} == 1) {
+        $remote_relname = $self->_inflect_singular(values %{$cond});
+    }
+    else {
+        $remote_relname = $self->_inflect_singular(lc $remote_table);
+    }
+
+    return $remote_relname;
+}
+
+
+1;