1 package DBIx::Class::Schema::Journal::DB;
3 use base 'DBIx::Class::Schema';
5 __PACKAGE__->mk_classdata('nested_changesets');
6 __PACKAGE__->mk_group_accessors( simple => 'current_user' );
7 __PACKAGE__->mk_group_accessors( simple => 'current_session' );
8 __PACKAGE__->mk_group_accessors( simple => '_current_changeset_container' );
10 DBIx::Class::Schema::Journal::DB->load_classes(qw(ChangeSet ChangeLog));
12 require DBIx::Class::Schema::Journal::DB::AuditLog;
13 require DBIx::Class::Schema::Journal::DB::AuditHistory;
15 sub _current_changeset {
17 my $ref = $self->_current_changeset_container;
18 $ref && $ref->{changeset};
21 # this is for localization of the current changeset
22 sub current_changeset {
23 my ( $self, @args ) = @_;
25 $self->throw_exception(
26 'setting current_changeset is not supported, use txn_do to create a new changeset'
29 my $id = $self->_current_changeset;
31 $self->throw_exception(
32 q{Can't call current_changeset outside of a transaction}
38 sub journal_create_changeset {
39 my ( $self, @args ) = @_;
41 my %changesetdata = ( @args, ID => undef );
43 delete $changesetdata{parent_id} unless $self->nested_changesets;
45 if( defined( my $user = $self->current_user() ) ) {
46 $changesetdata{user_id} = $user;
48 if( defined( my $session = $self->current_session() ) ) {
49 $changesetdata{session_id} = $session;
52 ## Create a new changeset, then run $code as a transaction
53 my $cs = $self->resultset('ChangeSet');
55 $cs->create({ %changesetdata });
58 sub journal_create_change {
60 $self->resultset('ChangeLog')->create({
61 changeset_id => $self->current_changeset
65 sub journal_update_or_create_log_entry {
66 my ($self, $row, @cols) = @_;
68 my $s_name = $row->result_source->source_name;
70 my %id = map { $_ => $row->get_column($_)} $row->primary_columns;
72 $self->resultset("${s_name}AuditLog")->update_or_create({ @cols, %id });
75 sub journal_record_in_history {
76 my ($self, $row, @cols) = @_;
78 my $s_name = $row->result_source->source_name;
80 $self->resultset("${s_name}AuditHistory")->create({ $row->get_columns, @cols });