From: Oliver Gorwits Date: Wed, 28 Apr 2010 00:20:38 +0000 (+0000) Subject: add bootstrap_journal to make things simpler, and also make prepopulate_journal to... X-Git-Tag: v0.900201~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-Journal.git;a=commitdiff_plain;h=462d8e704db544e0a0d53db4b68b73e57e32a04c add bootstrap_journal to make things simpler, and also make prepopulate_journal to be idempotent --- diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index e6bb3c7..368fa45 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -84,7 +84,7 @@ Load the module into your L Schema Class: __PACKAGE__->load_components(qw/Schema::Journal/); -And then call C<< $schema->journal_schema_deploy >> to create all the tables +And then call C<< $schema->bootstrap_journal >> to create all the tables necessary for the journal, in your database. Optionally set where the journal is stored: @@ -240,6 +240,12 @@ journalling. =over 4 +=item bootstrap_journal + +This calls C followed by C to +create your journal tables and if necessary populate them with a snapshot of +your current original schema data. + =item journal_schema_deploy Will use L to set up the tables for journalling in diff --git a/lib/DBIx/Class/Schema/Journal.pm b/lib/DBIx/Class/Schema/Journal.pm index e20f72c..d8da1f7 100644 --- a/lib/DBIx/Class/Schema/Journal.pm +++ b/lib/DBIx/Class/Schema/Journal.pm @@ -138,19 +138,30 @@ sub create_journal_for { } } -sub prepopulate_journal { +# XXX FIXME deploy is not idempotenta :-( +sub bootstrap_journal { my $self = shift; + $self->journal_schema_deploy; + $self->prepopulate_journal; +} - my %j_sources = map { $_ => 1 } $self->journal_sources - ? @{$self->journal_sources} - : $self->sources; - +# copy data from original schema sources into the journal as inserts in one +# changeset, so that later deletes will not fail to be journalled. +sub prepopulate_journal { + my $self = shift; my $schema = $self; - my $j_schema = $self->_journal_schema; - my $changelog_rs = $j_schema->resultset('ChangeLog'); + + # woah, looks like prepopulate has already run? + return if $schema->_journal_schema->resultset('ChangeSet')->count != 0; # using our own overridden txn_do (see below) will create a changeset $schema->txn_do( sub { + my %j_sources = map { $_ => 1 } $self->journal_sources + ? @{$self->journal_sources} + : $self->sources; + + my $j_schema = $self->_journal_schema; + my $changelog_rs = $j_schema->resultset('ChangeLog'); my $chs_id = $j_schema->current_changeset; foreach my $s_name ($self->sources) {