if journalling schema is not the main schema we need to call txn_do on that too
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal / DB.pm
CommitLineData
f0f14c64 1package DBIx::Class::Schema::Journal::DB;
2
3use base 'DBIx::Class::Schema';
4
aba93491 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' );
9
10# this is for localization of the current changeset
11sub current_changeset {
12 my ( $self, @args ) = @_;
13
14 $self->throw_error("setting current_changeset is not supported, use txn_do to create a new changeset") if @args;
15
16 my $ref = $self->_current_changeset_container;
17
18 return $ref && $ref->{changeset};
19}
74f04ccc 20
b5851590 21DBIx::Class::Schema::Journal::DB->load_classes(qw/
22 ChangeSet
23 Change
c5fba518 24 AuditLog
25 AuditHistory
b5851590 26 /);
f0f14c64 27
aba93491 28sub journal_create_changeset {
29 my ( $self, @args ) = @_;
30
31 my %changesetdata = ( @args, ID => undef );
32
33 delete $changesetdata{parent_id} unless $self->nested_changesets;
34
35 if( defined( my $user = $self->current_user() ) )
36 {
37 $changesetdata{user_id} = $user;
38 }
39 if( defined( my $session = $self->current_session() ) )
40 {
41 $changesetdata{session_id} = $session;
42 }
43
44 ## Create a new changeset, then run $code as a transaction
45 my $cs = $self->resultset('ChangeSet');
46
47 $cs->create({ %changesetdata });
48}
49
f0f14c64 501;