rename Change to ChangeLog to avoid reserved word problem
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal / DB.pm
1 package DBIx::Class::Schema::Journal::DB;
2
3 use base 'DBIx::Class::Schema';
4
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 DBIx::Class::Schema::Journal::DB->load_classes(qw/
11                                                ChangeSet
12                                                ChangeLog
13                                                AuditLog
14                                                AuditHistory
15                                                /);
16
17
18 sub _current_changeset {
19     my $self = shift;
20     my $ref = $self->_current_changeset_container;
21     $ref && $ref->{changeset};
22 }
23
24 # this is for localization of the current changeset
25 sub current_changeset {
26     my ( $self, @args ) = @_;
27
28     $self->throw_exception("setting current_changeset is not supported, use txn_do to create a new changeset") if @args;
29
30     my $id = $self->_current_changeset;
31
32     $self->throw_exception("Can't call current_changeset outside of a transaction") unless $id;
33
34     return $id;
35 }
36
37 sub 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
59 1;