multi column PK support
[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
fd45c476 10DBIx::Class::Schema::Journal::DB->load_classes(qw/
11 ChangeSet
12 ChangeLog
fd45c476 13 AuditHistory
14 /);
15
30a4f241 16require DBIx::Class::Schema::Journal::DB::AuditLog;
fd45c476 17
18sub _current_changeset {
19 my $self = shift;
20 my $ref = $self->_current_changeset_container;
21 $ref && $ref->{changeset};
22}
23
aba93491 24# this is for localization of the current changeset
25sub current_changeset {
26 my ( $self, @args ) = @_;
27
fd45c476 28 $self->throw_exception("setting current_changeset is not supported, use txn_do to create a new changeset") if @args;
aba93491 29
fd45c476 30 my $id = $self->_current_changeset;
aba93491 31
fd45c476 32 $self->throw_exception("Can't call current_changeset outside of a transaction") unless $id;
74f04ccc 33
fd45c476 34 return $id;
35}
f0f14c64 36
aba93491 37sub journal_create_changeset {
38 my ( $self, @args ) = @_;
39
40 my %changesetdata = ( @args, ID => undef );
41
42 delete $changesetdata{parent_id} unless $self->nested_changesets;
43
44 if( defined( my $user = $self->current_user() ) )
45 {
46 $changesetdata{user_id} = $user;
47 }
48 if( defined( my $session = $self->current_session() ) )
49 {
50 $changesetdata{session_id} = $session;
51 }
52
53 ## Create a new changeset, then run $code as a transaction
54 my $cs = $self->resultset('ChangeSet');
55
56 $cs->create({ %changesetdata });
57}
58
f0f14c64 591;