Various improvements from mst
Jess Robinson [Sat, 28 Apr 2007 22:21:44 +0000 (22:21 +0000)]
lib/DBIx/Class/Schema/Journal.pm
lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Journal/DB/AuduitLog.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Journal/DB/Base.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Journal/DB/Change.pm

index e66120c..46f6963 100644 (file)
@@ -2,7 +2,10 @@ package DBIx::Class::Schema::Journal;
 
 use base qw/DBIx::Class/;
 
-__PACKAGE__->mk_classdata('journal_dsn');
+use Scalar::Util 'blessed';
+
+__PACKAGE__->mk_classdata('journal_storage_type');
+__PACKAGE__->mk_classdata('journal_connection');
 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
 __PACKAGE__->mk_classdata('_journal_schema');
@@ -12,7 +15,14 @@ sub load_classes
     my $self = shift;
     $self->next::method(@_);
 
-    $self->_journal_schema((__PACKAGE__ . '::DB')->connect($self->journal_dsn || $self->storage->connect_info));
+    my $journal_schema = (__PACKAGE__ . '::DB')->connect($self->journal_connection || $self->storage->connect_info);
+    if($self->journal_storage_type)
+    {
+        $journal_schema->storage_type($self->journal_storage_type);
+    }
+
+    ## get our own private version of the journaling sources
+    $self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal'));
 
     my %j_sources = @{$self->journal_sources} ? map { $_ => 1 } @{$self->journal_sources} : map { $_ => 1 } $self->sources;
     foreach my $s_name ($self->sources)
@@ -28,6 +38,8 @@ sub load_classes
         return;
     }
 
+    ## get our own private version of the journaling sources
+    $self->_journal_schema->compose_namespace(blessed($self) . '::Journal');
     DBIx::Class::Schema::Journal::DB::ChangeSet->belongs_to('user', @{$self->journal_user});
 }
 
@@ -35,14 +47,14 @@ sub get_audit_log_class_name
 {
     my ($self, $sourcename) = @_;
 
-    return __PACKAGE__ . "::DB::${sourcename}AuditLog";
+    return blessed($self->_journal_schema) . "::${sourcename}AuditLog";
 }
 
 sub get_audit_history_class_name
 {
     my ($self, $sourcename) = @_;
 
-    return __PACKAGE__ . "::DB::${sourcename}AuditHistory";
+    return blessed($self->_journal_schema) . "::${sourcename}AuditHistory";
 }
 
 sub create_journal_for
@@ -51,38 +63,16 @@ sub create_journal_for
 
     my $source = $self->source($s_name);
     my $newclass = $self->get_audit_log_class_name($s_name);
-    DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class');
-    $newclass->load_components('Core');
+    DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog');
     $newclass->table(lc($s_name) . "_audit_log");
-    $newclass->add_columns(
-                           ID => {
-                               data_type => 'integer',
-                               is_nullable => 0,
-                           },
-                           create_id => {
-                               data_type => 'integer',
-                               is_nullable => 0,
-                           },
-                           delete_id => {
-                               data_type => 'integer',
-                               is_nullable => 1,
-                           });
-    $newclass->belongs_to('created', 'DBIx::Class::Schema::Journal::DB::Change', 'create_id');
-    $newclass->belongs_to('deleted', 'DBIx::Class::Schema::Journal::DB::Change', 'delete_id');
                            
 
     my $histclass = $self->get_audit_hisory_class_name($s_name);
-    DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class');
-    $histclass->load_components('Core');
+    DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory');
     $histclass->table(lc($s_name) . "_audit_hisory");
     $histclass->add_columns(
-                           change_id => {
-                               data_type => 'integer',
-                               is_nullable => 0,
-                           },
                             map { $_ => $source->column_info($_) } $source->columns
                            );
-    $histclass->belongs_to('change', 'DBIx::Class::Schema::Journal::DB::Change', 'change_id');
                            
 }
 
diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm
new file mode 100644 (file)
index 0000000..2a8825a
--- /dev/null
@@ -0,0 +1,23 @@
+package DBIx::Class::Schema::Journal::DB::AuditHistory;
+
+use base 'DBIx::Class::Schema::Journal::DB::Base';
+
+__PACKAGE__->add_columns(
+                           change_id => {
+                               data_type => 'integer',
+                               is_nullable => 0,
+                           });
+__PACKAGE__->belongs_to('change', 'DBIx::Class::Schema::Journal::DB::Change', 'change_id');
+
+sub new
+{
+    my ($self, $data, @rest) = @_;
+
+    $data->{change} = {
+        changeset_id => $self->result_source->schema->_journal_schema->current_changeset,
+    };
+
+    $self->next::method($data, @rest);
+}                           
+
+1;
diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm
new file mode 100644 (file)
index 0000000..3026b80
--- /dev/null
@@ -0,0 +1,34 @@
+package DBIx::Class::Schema::Journal::DB::AuditLog;
+
+use base 'DBIx::Class::Schema::Journal::DB::Base';
+
+__PACKAGE__->add_columns(
+                           ID => {
+                               data_type => 'integer',
+                               is_nullable => 0,
+                           },
+                           create_id => {
+                               data_type => 'integer',
+                               is_nullable => 0,
+                               is_foreign_key => 1,
+                           },
+                           delete_id => {
+                               data_type => 'integer',
+                               is_nullable => 1,
+                               is_foreign_key => 1,
+                           });
+  __PACKAGE__->belongs_to('created', 'DBIx::Class::Schema::Journal::DB::Change', 'create_id');
+  __PACKAGE__->belongs_to('deleted', 'DBIx::Class::Schema::Journal::DB::Change', 'delete_id');
+
+sub new
+{
+    my ($self, $data, @rest) = @_;
+
+    $data->{created} = {
+        changeset_id => $self->result_source->schema->_journal_schema->current_changeset,
+    };
+
+    $self->next::method($data, @rest);
+}                           
+
+1;
diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuduitLog.pm b/lib/DBIx/Class/Schema/Journal/DB/AuduitLog.pm
new file mode 100644 (file)
index 0000000..83d354b
--- /dev/null
@@ -0,0 +1,9 @@
+package DBIx::Class::Schema::Journal::DB::Base;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('');
+
+
+1;
diff --git a/lib/DBIx/Class/Schema/Journal/DB/Base.pm b/lib/DBIx/Class/Schema/Journal/DB/Base.pm
new file mode 100644 (file)
index 0000000..83d354b
--- /dev/null
@@ -0,0 +1,9 @@
+package DBIx::Class::Schema::Journal::DB::Base;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('');
+
+
+1;
index dc4aafa..f5c5dc1 100644 (file)
@@ -25,6 +25,7 @@ __PACKAGE__->add_columns(
 
 
 __PACKAGE__->set_primary_key('ID');
+__PACKAGE__->add_unique_constraint('setorder', [ qw/changeset_id order_in/ ]);
 __PACKAGE__->belongs_to('changeset', 'DBIx::Class::Schema::Journal::DB::ChangeSet', 'changeset_id');
 
 __PACKAGE__->position_column('order_in');