From: Jess Robinson Date: Sat, 28 Apr 2007 13:44:05 +0000 (+0000) Subject: Initial commit, not tested, may not work ;) X-Git-Tag: v0.900201~112 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f0f14c64c00b1e4970c8aecf5cd1231e118cca61;p=dbsrgits%2FDBIx-Class-Journal.git Initial commit, not tested, may not work ;) --- f0f14c64c00b1e4970c8aecf5cd1231e118cca61 diff --git a/NOTES b/NOTES new file mode 100644 index 0000000..9c8ed98 --- /dev/null +++ b/NOTES @@ -0,0 +1 @@ +http://stikkit.com/stikkits/403253 diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm new file mode 100644 index 0000000..8da929e --- /dev/null +++ b/lib/DBIx/Class/Journal.pm @@ -0,0 +1,12 @@ +package DBIx::Class::Journal; + +use base qw/DBIx::Class/; +__PACKAGE__->load_components(qw/AccessorGroup/); + +__PACKAGE__->mk_group_accessors('simple' => qw/ + journal_dsn + journal_sources + /); + + +1; diff --git a/lib/DBIx/Class/Schema/Journal.pm b/lib/DBIx/Class/Schema/Journal.pm new file mode 100644 index 0000000..e66120c --- /dev/null +++ b/lib/DBIx/Class/Schema/Journal.pm @@ -0,0 +1,89 @@ +package DBIx::Class::Schema::Journal; + +use base qw/DBIx::Class/; + +__PACKAGE__->mk_classdata('journal_dsn'); +__PACKAGE__->mk_classdata('journal_sources'); ## [ source names ] +__PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ] +__PACKAGE__->mk_classdata('_journal_schema'); + +sub load_classes +{ + my $self = shift; + $self->next::method(@_); + + $self->_journal_schema((__PACKAGE__ . '::DB')->connect($self->journal_dsn || $self->storage->connect_info)); + + my %j_sources = @{$self->journal_sources} ? map { $_ => 1 } @{$self->journal_sources} : map { $_ => 1 } $self->sources; + foreach my $s_name ($self->sources) + { + next unless($j_sources{$s_name}); + $self->create_journal_for($s_name); + } + + ## Set up relationship between changeset->user_id and this schema's user + if(!@{$self->journal_user}) + { + warn "No Journal User set!"; + return; + } + + DBIx::Class::Schema::Journal::DB::ChangeSet->belongs_to('user', @{$self->journal_user}); +} + +sub get_audit_log_class_name +{ + my ($self, $sourcename) = @_; + + return __PACKAGE__ . "::DB::${sourcename}AuditLog"; +} + +sub get_audit_history_class_name +{ + my ($self, $sourcename) = @_; + + return __PACKAGE__ . "::DB::${sourcename}AuditHistory"; +} + +sub create_journal_for +{ + my ($self, $s_name) = @_; + + 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'); + $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'); + $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'); + +} + +1; diff --git a/lib/DBIx/Class/Schema/Journal/DB.pm b/lib/DBIx/Class/Schema/Journal/DB.pm new file mode 100644 index 0000000..89e7387 --- /dev/null +++ b/lib/DBIx/Class/Schema/Journal/DB.pm @@ -0,0 +1,7 @@ +package DBIx::Class::Schema::Journal::DB; + +use base 'DBIx::Class::Schema'; + +DBIx::Class::Schema::Journal::DB->load_classes(); + +1; diff --git a/lib/DBIx/Class/Schema/Journal/DB/Change.pm b/lib/DBIx/Class/Schema/Journal/DB/Change.pm new file mode 100644 index 0000000..dc4aafa --- /dev/null +++ b/lib/DBIx/Class/Schema/Journal/DB/Change.pm @@ -0,0 +1,32 @@ +package DBIx::Class::Schema::Journal::DB::Change; + +use base 'DBIx::Class'; + +__PACKAGE__->load_components(qw/Ordered Core/); +__PACKAGE__->table('change'); + +__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__->belongs_to('changeset', 'DBIx::Class::Schema::Journal::DB::ChangeSet', 'changeset_id'); + +__PACKAGE__->position_column('order_in'); +__PACKAGE__->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 new file mode 100644 index 0000000..f269227 --- /dev/null +++ b/lib/DBIx/Class/Schema/Journal/DB/ChangeSet.pm @@ -0,0 +1,34 @@ +package DBIx::Class::Schema::Journal::DB::ChangeSet; + +use base 'DBIx::Class'; + +__PACKAGE__->load_components(qw/InflateColumn::DateTime Core/); +__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 => 0, + is_foreign_key => 1, + }, + set_date => { + data_type => 'timestamp', + is_nullable => 0, + default_value => 'NOW()', + }, + session_id => { + data_type => 'varchar', + size => 255, + is_nullable => 1, + }, + ); + +__PACKAGE__->set_primary_key('ID'); + +1;