Browse files

Add triggers to prevent desk asset drift.

There are two disconnected places in the databases where the assets on desks
are tracked. One is the `desk__id` column in the `story`, `media`, and
`template` tables. The other is "asset groups." These are kind of nasty, but
necessary in order for permissions to work properly.

Unfortunately, these two sources can drift from one another. So this commit
adds a migration that corrects any existing drift, and then adds triggers
to prevent it in the future. This required a few other changes:

* The installer now installs PL/pgSQL, which is required for the trigger
  functions. If the database is created in advance, it must include PL/pgSQL
  or the installation will fail.
* Bric::Biz::Workflow::Parts::Desk now has to save its corresponding asset
  group before it saves the assets in that group with new IDs. This won't
  usually be necessary, since the trigger executes at transaction commmit
  time, but tests run under autocommit, and so generated quite a lot of
  errors. By making the order of execution a little saner, the errors go away
  and all tests pass.

For now, there are no corresponding triggers for MySQL. I *think* MySQL 5 can
do the same thing (or close to it), but I don't have the energy to figure out
how just now.

[#272 state:resolved]
  • Loading branch information...
theory committed Oct 12, 2011
1 parent 6d45e11 commit ddffd06403d76695ee728529c8b5799887845a8b
@@ -57,6 +57,7 @@ =head1 See Also
$DBDEFDB = 'template1';
+ create_plpgsql();
} else {
# Set environment variables for psql.
@@ -128,6 +129,21 @@ sub create_db {
print "Database created.\n";
+# create PL/pgSQL
+sub create_plpgsql {
+ print "Creating PL/pgSQL in $DB->{db_name}...\n";
+ my $err = exec_sql(qq{CREATE LANGUAGE plpgsql}, 0, $DB->{db_name});
+ if ($err && $err !~ /language "plpgsql" already exists/) {
+ # There was an error.
+ hard_fail(
+ "Failed to create PL/pgSQL. The database error was\n\n",
+ "$err\n"
+ );
+ }
+ print "PL/pgSQL created.\n";
# create SYS_USER, optionally dropping an existing syst
sub create_user {
my $user = $DB->{sys_user};
Oops, something went wrong.

0 comments on commit ddffd06

Please sign in to comment.