[Dbix-class] Defaulting a column by configuration

Bill Moseley moseley at hank.org
Fri Jun 1 13:43:42 GMT 2012


I have a time_zone column, but sadly it allows nulls.  Fixing the database
would be the correct approach, but for now I'm looking at defaulting the
column.  But, the default value depends on which database we are connected
to.

The result class is used in both a Catalyst app and in cron jobs.  Both the
Catalyst app and the cron jobs pull the connect_info from a single config
config, so the only thing I really have in common for all uses is
connect_info.

So, in the result class I can use "around time_zone =3D> sub {}" to set the
default

One option is to just do a match on the database name in the dsn:

$self->result_source->storage->connect_info->[0]->{dsn}.


but I can't be sure someone won't change that later.

Now I'm looking at just adding a "default_time_zone" to connect_info and
then inspecting:

$self->result_source->storage->connect_info->[0]->{default_time_zone}.

And then do:

around time_zone =3D> sub {
    my $orig =3D shift;
    my $self =3D shift;
    return $self->$orig( @_ )
|| $self->result_source->storage->connect_info->[0]->{default_time_zone};
};

Is there a more elegant solution to this?

I don't like stuffing non-database args into connect_info, of course, but
it seems like the only thing I have available in all my environments.

-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20120601/37b=
154af/attachment.htm


More information about the DBIx-Class mailing list