From: Arthur Axel "fREW" Schmidt Date: Wed, 21 Apr 2010 22:29:49 +0000 (+0000) Subject: RT56175: allow tables to have a prefix X-Git-Tag: v0.900201~32 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5b64dcdc3bb8d13b3cd896e70a43871eeb77db07;p=dbsrgits%2FDBIx-Class-Journal.git RT56175: allow tables to have a prefix --- diff --git a/lib/DBIx/Class/Schema/Journal.pm b/lib/DBIx/Class/Schema/Journal.pm index 46ced86..6c1678a 100644 --- a/lib/DBIx/Class/Schema/Journal.pm +++ b/lib/DBIx/Class/Schema/Journal.pm @@ -17,6 +17,7 @@ __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal __PACKAGE__->mk_classdata('journal_component'); __PACKAGE__->mk_classdata('journal_components'); __PACKAGE__->mk_classdata('journal_nested_changesets'); +__PACKAGE__->mk_classdata('journal_prefix'); use strict; use warnings; @@ -38,6 +39,20 @@ sub _journal_schema_prototype { my $comp = $self->journal_component || "Journal"; + + my $prefix = $self->journal_prefix || ''; + foreach my $audit (qw(ChangeSet ChangeLog)) { + my $class = blessed($proto) . "::$audit"; + + Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit"); + + $class->journal_define_table(blessed($proto), $prefix); + + $proto->register_class($audit, $class); + + $self->register_class($audit, $class) + if $self->journal_copy_sources; + } ## Create auditlog+history per table my %j_sources = map { $_ => 1 } $self->journal_sources @@ -114,7 +129,7 @@ sub create_journal_for { Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit"); - $class->journal_define_table($source); + $class->journal_define_table($source, blessed($journal_schema)); $journal_schema->register_class($audit_source, $class); @@ -175,5 +190,4 @@ sub changeset_session { $self->_journal_schema->current_session($sessionid); } - 1; diff --git a/lib/DBIx/Class/Schema/Journal/DB.pm b/lib/DBIx/Class/Schema/Journal/DB.pm index f90054d..1cd7330 100644 --- a/lib/DBIx/Class/Schema/Journal/DB.pm +++ b/lib/DBIx/Class/Schema/Journal/DB.pm @@ -7,10 +7,10 @@ __PACKAGE__->mk_group_accessors( simple => 'current_user' ); __PACKAGE__->mk_group_accessors( simple => 'current_session' ); __PACKAGE__->mk_group_accessors( simple => '_current_changeset_container' ); -DBIx::Class::Schema::Journal::DB->load_classes(qw(ChangeSet ChangeLog)); - require DBIx::Class::Schema::Journal::DB::AuditLog; require DBIx::Class::Schema::Journal::DB::AuditHistory; +require DBIx::Class::Schema::Journal::DB::ChangeLog; +require DBIx::Class::Schema::Journal::DB::ChangeSet; sub _current_changeset { my $self = shift; diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm index b5664e9..bb6e46e 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm @@ -3,7 +3,7 @@ package DBIx::Class::Schema::Journal::DB::AuditHistory; use base 'DBIx::Class::Core'; sub journal_define_table { - my ( $class, $source ) = @_; + my ( $class, $source, $schema_class ) = @_; $class->table($source->name . '_audit_history'); @@ -40,7 +40,7 @@ sub journal_define_table { $class->add_column($column => \%hist_info); } - $class->belongs_to(change => 'DBIx::Class::Schema::Journal::DB::ChangeLog', 'audit_change_id'); + $class->belongs_to(change => "${schema_class}::ChangeLog", 'audit_change_id'); } 1; diff --git a/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm b/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm index 5498d0f..472d94b 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm @@ -3,7 +3,7 @@ package DBIx::Class::Schema::Journal::DB::AuditLog; use base 'DBIx::Class::Core'; sub journal_define_table { - my ( $class, $source ) = @_; + my ( $class, $source, $schema_class ) = @_; $class->table($source->name . '_audit_log'); @@ -26,8 +26,8 @@ sub journal_define_table { $class->set_primary_key( $source->primary_columns ); - $class->belongs_to(created => 'DBIx::Class::Schema::Journal::DB::ChangeLog', 'create_id'); - $class->belongs_to(deleted => 'DBIx::Class::Schema::Journal::DB::ChangeLog', 'delete_id'); + $class->belongs_to(created => "${schema_class}::ChangeLog", 'create_id'); + $class->belongs_to(deleted => "${schema_class}::ChangeLog", 'delete_id'); } 1; diff --git a/lib/DBIx/Class/Schema/Journal/DB/ChangeLog.pm b/lib/DBIx/Class/Schema/Journal/DB/ChangeLog.pm index 1b3dc24..04ea211 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/ChangeLog.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/ChangeLog.pm @@ -2,33 +2,37 @@ package DBIx::Class::Schema::Journal::DB::ChangeLog; use base 'DBIx::Class::Core'; -__PACKAGE__->load_components(qw/Ordered/); -__PACKAGE__->table('changelog'); - -__PACKAGE__->add_columns( - ID => { - data_type => 'integer', - is_auto_increment => 1, - is_primary_key => 1, - is_nullable => 0, - }, - changeset_id => { - data_type => 'integer', - is_nullable => 0, - is_foreign_key => 1, - }, - order_in => { - data_type => 'integer', - is_nullable => 0, - }, -); - - -__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'); - __PACKAGE__->grouping_column('changeset_id'); +sub journal_define_table { + my ( $class, $schema_class, $prefix ) = @_; + + $class->load_components(qw/Ordered/); + $class->table($prefix . 'changelog'); + + $class->add_columns( + ID => { + data_type => 'integer', + is_auto_increment => 1, + is_primary_key => 1, + is_nullable => 0, + }, + changeset_id => { + data_type => 'integer', + is_nullable => 0, + is_foreign_key => 1, + }, + order_in => { + data_type => 'integer', + is_nullable => 0, + }, + ); + + + $class->set_primary_key('ID'); + $class->add_unique_constraint('setorder', [ qw/changeset_id order_in/ ]); + $class->belongs_to('changeset', "${schema_class}::ChangeSet", 'changeset_id'); + + $class->position_column('order_in'); + $class->grouping_column('changeset_id'); +} 1; diff --git a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm index 05bb248..d631066 100644 --- a/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm +++ b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm @@ -2,44 +2,48 @@ package DBIx::Class::Schema::Journal::DB::ChangeSet; use base 'DBIx::Class::Core'; -__PACKAGE__->load_components(qw/InflateColumn::DateTime/); -__PACKAGE__->table('changeset'); - -__PACKAGE__->add_columns( - ID => { - data_type => 'integer', - is_auto_increment => 1, - is_primary_key => 1, - is_nullable => 0, - }, - user_id => { - data_type => 'integer', - is_nullable => 1, - is_foreign_key => 1, - }, - set_date => { - data_type => 'timestamp', - is_nullable => 0, - }, - session_id => { - data_type => 'varchar', - size => 255, - is_nullable => 1, - }, -); +sub journal_define_table { + my ( $class, $schema_class, $prefix ) = @_; + + $class->load_components(qw/InflateColumn::DateTime/); + $class->table($prefix . 'changeset'); + + $class->add_columns( + ID => { + data_type => 'integer', + is_auto_increment => 1, + is_primary_key => 1, + is_nullable => 0, + }, + user_id => { + data_type => 'integer', + is_nullable => 1, + is_foreign_key => 1, + }, + set_date => { + data_type => 'timestamp', + is_nullable => 0, + }, + session_id => { + data_type => 'varchar', + size => 255, + is_nullable => 1, + }, + ); + + $class->set_primary_key('ID'); +} sub new { my $self = shift->next::method(@_); - # I think we should not do the following and - # instead use DBIx::Class::TimeStamp. If I - # can think of a good way (passing a version on - # import?) to do it and retain backcompat I will. - # - # --fREW, 01-27-2010 + # I think we should not do the following and + # instead use DBIx::Class::TimeStamp. If I + # can think of a good way (passing a version on + # import?) to do it and retain backcompat I will. + # + # --fREW, 01-27-2010 $self->set_date(gmtime); return $self; } -__PACKAGE__->set_primary_key('ID'); - 1;