Experiments in versioning..
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
index 0b9b969..40caff6 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 
 use Carp::Clan qw/^DBIx::Class/;
+use Scalar::Util qw/weaken/;
+use File::Spec;
 
 use base qw/DBIx::Class/;
 
@@ -20,7 +22,7 @@ DBIx::Class::Schema - composable schemas
 
   package Library::Schema;
   use base qw/DBIx::Class::Schema/;
-  
+
   # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
   __PACKAGE__->load_classes(qw/CD Book DVD/);
 
@@ -36,7 +38,7 @@ DBIx::Class::Schema - composable schemas
     $password,
     { AutoCommit => 0 },
   );
-  
+
   my $schema2 = Library::Schema->connect($coderef_returning_dbh);
 
   # fetch objects using Library::Schema::DVD
@@ -63,7 +65,7 @@ particular which module inherits off which.
 
 =back
 
-Registers a class which isa L<DBIx::Class::ResultSourceProxy>. Equivalent to
+Registers a class which isa DBIx::Class::ResultSourceProxy. Equivalent to
 calling:
 
   $schema->register_source($moniker, $component_class->result_source_instance);
@@ -94,6 +96,7 @@ sub register_source {
   $reg{$moniker} = $source;
   $self->source_registrations(\%reg);
   $source->schema($self);
+  weaken($source->{schema}) if ref($self);
   if ($source->result_class) {
     my %map = %{$self->class_mappings};
     $map{$source->result_class} = $moniker;
@@ -219,15 +222,15 @@ Example:
 
 sub load_classes {
   my ($class, @params) = @_;
-  
+
   my %comps_for;
-  
+
   if (@params) {
     foreach my $param (@params) {
       if (ref $param eq 'ARRAY') {
         # filter out commented entries
         my @modules = grep { $_ !~ /^#/ } @$param;
-        
+
         push (@{$comps_for{$class}}, @modules);
       }
       elsif (ref $param eq 'HASH') {
@@ -267,7 +270,10 @@ sub load_classes {
           die $@ unless $@ =~ /Can't locate.+$comp_class\.pm\sin\s\@INC/;
           warn $@ if $@;
         }
-        push(@to_register, [ $comp, $comp_class ]);
+
+        $comp_class->source_name($comp) unless $comp_class->source_name;
+
+        push(@to_register, [ $comp_class->source_name, $comp_class ]);
       }
     }
   }
@@ -454,6 +460,7 @@ sub connection {
   my $storage = $storage_class->new;
   $storage->connect_info(\@info);
   $self->storage($storage);
+  $self->on_connect() if($self->can('on_connect'));
   return $self;
 }
 
@@ -711,6 +718,82 @@ sub deploy {
   $self->storage->deploy($self, undef, $sqltargs);
 }
 
+=head2 create_ddl_dir (EXPERIMENTAL)
+
+=over 4
+
+=item Arguments: \@databases, $version, $directory, $preversion, $sqlt_args
+
+=back
+
+Creates an SQL file based on the Schema, for each of the specified
+database types, in the given directory. Given a previous version number,
+this will also create a file containing the ALTER TABLE statements to
+transform the previous schema into the current one. Note that these
+statements may contain DROP TABLE or DROP COLUMN statements that can
+potentially destroy data.
+
+The file names are created using the C<ddl_filename> method below, please
+override thus method in your schema if you would like a different file
+name format. For the ALTER file, the same format is used, replacing
+$version in the name with "$preversion-$version".
+
+If no arguments are passed, then the following default values are used:
+
+=over 4
+
+=item databases  - ['MySQL', 'SQLite', 'PostgreSQL']
+
+=item version    - $schema->VERSION
+
+=item directory  - './'
+
+=item preversion - <none>
+
+=back 
+Note that this feature is currently EXPERIMENTAL and may not work correctly
+across all databases, or fully handle complex relationships.
+
+WARNING: Please check all SQL files created, before applying them.
+
+=cut
+
+sub create_ddl_dir
+{
+  my $self = shift;
+
+  $self->throw_exception("Can't create_ddl_dir without storage") unless $self->storage;
+  $self->storage->create_ddl_dir($self, @_);
+}
+
+=head2 ddl_filename
+
+=over 4
+
+=item Arguments: $directory, $database-type, $version
+
+=back
+
+This method is called by C<create_ddl_dir> to compose a file name out of
+the supplied directory, database type and version number. The default file
+name format is: "$filename-$version-$type.sql".
+
+You may override this method in your schema if you wish to use a different
+format.
+
+=cut
+
+sub ddl_filename
+{
+    my ($self, $dir, $type, $version) = @_;
+
+    my $filename = ref($self);
+    $filename =~ s/^.*:://;
+    $filename = File::Spec->catpath($dir, "$filename-$version-$type.sql");
+
+    return $filename;
+}
+
 1;
 
 =head1 AUTHORS