[dancer-users] Trouble with DBIC in Plugins
Matthew Mallard
matt at Q-technologies.com.au
Sat May 2 12:34:21 BST 2015
Andy,
Thanks again for your help. This code works:
my $schema;
on_plugin_import {
my $dsl = shift;
# Grab a handle to the Plugin::DBIC schema
die "No schema method in app. Did you load DBIC::Plugin::DBIC before Custom::Plugin?" unless $dsl->can('schema');
$schema = $dsl->schema;
$dsl->debug( "**** Got Schema **** ");
};
Having said that, I realise the original code would have worked too but too many things were changing at the same time and it caught me out. I moved from mysql to postgresql and the case sensitivity was causing a problem, there were rather radical changes in the way Dancer2 was requiring me to structure my code and to top it off the "DEPRECATED: Custom::Plugin calls 'dsl' instead of '$dsl->dsl'. at /perf/perl/lib/perl5/Dancer2/Plugin/DBIC.pm line 13.” was making me think I was on the wrong track. But this warning remains even with the new code.
Turning statement logging on in postgres helped me track down the case mismatch.
Cheers,
Matt
On 2 May 2015, at 7:47 pm, Andrew Beverley <andy at andybev.com> wrote:
> On Sat, 2015-05-02 at 12:50 +1000, Matthew Mallard wrote:
>>>> I’m getting a bit confused with plugins. I’m sure that I was able to
>>>> use the DBIC Plugin at one point from one of my plugins, but I can’t
>>>> seem to get it to work in 0.159002 or 0.160000. DBIC seems to be
>>>> okay outside of plugins.
>>>
>>> Firstly, there is change going on in the world of plugins, which I
>>> suspect will fix this, but I haven't read up on the full details myself:
>>>
>>> http://lists.preshweb.co.uk/pipermail/dancer-users/2015-April/004512.html
>>>
>>>> * how do I get DBIC to print the SQL it’s executing so I can double
>>>> check it’s what I’m expecting?
>>>> * am I doing something else wrong in the way I’m trying to use DBIC?
>>>
>>> This is a bit more tricky. With the current plugin architecture,
>>> "schema" is not available in the DSL when you call it from another
>>> plugin as you are doing. You can workaround this by capturing it during
>>> plugin initialisation, as long as you load the DBIC plugin before yours.
>>> See Plugin::Auth::Extensible::Provider::DBIC for an example:
>>
>> So do I create a sub in my custom plugin along these lines:
>> sub new {
>> my ($class, $dsl) = @_;
>>
>> # Grab a handle to the Plugin::DBIC schema
>> die "No schema method in app. Did you load DBIC::Plugin::DBIC before My::Custom::Module?"
>> unless $dsl->can('schema');
>> my $schema = $dsl->schema;
>
> Yes, similar to that, but using on_plugin_import. See this example:
>
> https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible/blob/de84a4a5baf5b56a83906355de075678c485fc51/lib/Dancer2/Plugin/Auth/Extensible.pm#L1158
>
>> my $self = {
>> dsl_local => $dsl,
>> schema => $schema,
>> };
>> return bless $self => $class;
>
> I don't think you need to bless your class, as it's already blessed. You
> just need to store the schema in a global in your module, a bit like
> $settings here:
>
> https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible/blob/de84a4a5baf5b56a83906355de075678c485fc51/lib/Dancer2/Plugin/Auth/Extensible.pm#L13
>
>> Then in my routes module I call plugins in this order:
>> use Dancer2::Plugin::DBIC
>> use My::Custom::Module
>
> Yes.
>
>> How does 'new' get called and what do I use to reference the schema in my custom plugin.
>
> See above.
>
>> Let me know if there is some other background reading I should be doing
>> for how this work,
>
> To be honest, it's a dirty hack, so you won't find any documentation on
> it. You might be better waiting for the new plugin architecture if you
> can.
>
> Andy
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
More information about the dancer-users
mailing list