"Deletion" of records by setting status to 'deleted'

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

"Deletion" of records by setting status to 'deleted'

Ilya Chesnokov
Hi,

I have a "service" table in my database and want to mark services as
deleted by setting service.status = 'deleted' instead of actually
deleting them (for business reasons).

I do it in a working project where DBIC is used as a database
interaction layer, and it is preferable to make these changes as
transparent  to the surrounding code as possible. Our codebase is
(mostly) covered with tests, so it's generally easy to understand
whether there is a regression or not.

First I tried to override search_rs() method in a custom resultset
with something like this:
https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
work as expected on prefetches with condition in them.

Then I tried a method with resultset_attributes (see
https://metacpan.org/pod/DBIx::Class::ResultSource#resultset_attributes),
but it didn't work because it's impossible to get
"current_source_alias" from ResultSource.

Currently I'm using something based on the behavior of
resultset_attributes (see
https://gist.github.com/ichesnokov/e5fb7e70cb6d497ea5a0), and it
works, but requires fiddling with internal state of a resultset, which
seems a bit wrong.

Is there any better way for achieving the same?

Thanks.
--
Best regards,
Ilya Chesnokov

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Bob MacCallum
I am not an expert, but shouldn't it just work with

__PACKAGE__->resultset_attributes({ where => { status => { '!=' => 'deleted' } } });

?

However, I've only ever used order_by in resultset_attributes so YMMV.



On Mon, Mar 9, 2015 at 6:04 PM, Ilya Chesnokov <[hidden email]> wrote:
Hi,

I have a "service" table in my database and want to mark services as
deleted by setting service.status = 'deleted' instead of actually
deleting them (for business reasons).

I do it in a working project where DBIC is used as a database
interaction layer, and it is preferable to make these changes as
transparent  to the surrounding code as possible. Our codebase is
(mostly) covered with tests, so it's generally easy to understand
whether there is a regression or not.

First I tried to override search_rs() method in a custom resultset
with something like this:
https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
work as expected on prefetches with condition in them.

Then I tried a method with resultset_attributes (see
https://metacpan.org/pod/DBIx::Class::ResultSource#resultset_attributes),
but it didn't work because it's impossible to get
"current_source_alias" from ResultSource.

Currently I'm using something based on the behavior of
resultset_attributes (see
https://gist.github.com/ichesnokov/e5fb7e70cb6d497ea5a0), and it
works, but requires fiddling with internal state of a resultset, which
seems a bit wrong.

Is there any better way for achieving the same?

Thanks.
--
Best regards,
Ilya Chesnokov

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Dmitry Latin
In reply to this post by Ilya Chesnokov
Hi!

> First I tried to override search_rs() method in a custom resultset
> with something like this:
> https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
> work as expected on prefetches with condition in them.
>
I think this way doesn't work on prefetches because it shouldn't.

In addition to overriding search_rs, you have to provide conditions into
relations (https://metacpan.org/pod/DBIx::Class::Relationship::Base#Custom-join-conditions)

--
//wbr, Dmitry Latin

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Ilya Chesnokov
In reply to this post by Bob MacCallum
2015-03-10 12:26 GMT+03:00 Bob MacCallum <[hidden email]>:
> I am not an expert, but shouldn't it just work with
>
> __PACKAGE__->resultset_attributes({ where => { status => { '!=' => 'deleted'
> } } });
>
>
> ?

This doesn't work when there is more than one table with 'status'
column in it: we need to provide an alias for that table along with a
column name (which can be retrieved with
$resultset->current_source_alias).

> However, I've only ever used order_by in resultset_attributes so YMMV.
>
>
>
> On Mon, Mar 9, 2015 at 6:04 PM, Ilya Chesnokov <[hidden email]>
> wrote:
>>
>> Hi,
>>
>> I have a "service" table in my database and want to mark services as
>> deleted by setting service.status = 'deleted' instead of actually
>> deleting them (for business reasons).
>>
>> I do it in a working project where DBIC is used as a database
>> interaction layer, and it is preferable to make these changes as
>> transparent  to the surrounding code as possible. Our codebase is
>> (mostly) covered with tests, so it's generally easy to understand
>> whether there is a regression or not.
>>
>> First I tried to override search_rs() method in a custom resultset
>> with something like this:
>> https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
>> work as expected on prefetches with condition in them.
>>
>> Then I tried a method with resultset_attributes (see
>> https://metacpan.org/pod/DBIx::Class::ResultSource#resultset_attributes),
>> but it didn't work because it's impossible to get
>> "current_source_alias" from ResultSource.
>>
>> Currently I'm using something based on the behavior of
>> resultset_attributes (see
>> https://gist.github.com/ichesnokov/e5fb7e70cb6d497ea5a0), and it
>> works, but requires fiddling with internal state of a resultset, which
>> seems a bit wrong.
>>
>> Is there any better way for achieving the same?
>>
>> Thanks.
>> --
>> Best regards,
>> Ilya Chesnokov
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive:
>> http://www.grokbase.com/group/dbix-class@...
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...



--
Best regards,
Ilya Chesnokov

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Ilya Chesnokov
In reply to this post by Dmitry Latin
2015-03-10 12:45 GMT+03:00 Dmitry Latin <[hidden email]>:
> Hi!
>
>> First I tried to override search_rs() method in a custom resultset
>> with something like this:
>> https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
>> work as expected on prefetches with condition in them.
>>
> I think this way doesn't work on prefetches because it shouldn't.

Why?

> In addition to overriding search_rs, you have to provide conditions into
> relations (https://metacpan.org/pod/DBIx::Class::Relationship::Base#Custom-join-conditions)

This seems irrelevant to my issue: I don't need custom joins, I only
want to have what DBIx::Class provides by default.

By "prefetches with condition" I mean expressions with both "prefetch"
and "where" attributes, similar to this:

$customer->find(
    $id,
    {
        prefetch => 'services',
        where => { 'services.entity' => 'abcd' },
    }
);

> --
> //wbr, Dmitry Latin
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@...



--
Best regards,
Ilya Chesnokov

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Dmitry Latin
On Tuesday 10 of March 2015 17:31:27 Ilya Chesnokov wrote:

> 2015-03-10 12:45 GMT+03:00 Dmitry Latin <[hidden email]>:
> > Hi!
> >
> >> First I tried to override search_rs() method in a custom resultset
> >> with something like this:
> >> https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
> >> work as expected on prefetches with condition in them.
> >
> > I think this way doesn't work on prefetches because it shouldn't.
>
> Why?
>
Because search_rs works only with current resultset, not with related.


> > In addition to overriding search_rs, you have to provide conditions into
> > relations
> > (https://metacpan.org/pod/DBIx::Class::Relationship::Base#Custom-join-con
> > ditions)
> This seems irrelevant to my issue: I don't need custom joins, I only
> want to have what DBIx::Class provides by default.
>
> By "prefetches with condition" I mean expressions with both "prefetch"
> and "where" attributes, similar to this:
>
> $customer->find(
>     $id,
>     {
>         prefetch => 'services',
>         where => { 'services.entity' => 'abcd' },
>     }
> );
>
I thought that you need to fetch customer and in addition fetch all not
deleted services for that customer - here at least you get customer without
services.

But your example tells: "fetch customer and in addition fetch all services,
where services not deleted" - here it is possible to get empty result.

So, probably I misunderstood your problem.

--
//wbr, Dmitry Latin

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: "Deletion" of records by setting status to 'deleted'

Ilya Chesnokov
In reply to this post by Ilya Chesnokov
OK, solved my problem by creating a separate view in my database that only includes services with proper statuses. The "solution" I tried to use first didn't work properly as well, don't try to use it (without modification, at least)!

От: [hidden email]
Отправлено: ‎09.‎03.‎2015 21:04
Кому: [hidden email]
Тема: "Deletion" of records by setting status to 'deleted'

Hi,

I have a "service" table in my database and want to mark services as
deleted by setting service.status = 'deleted' instead of actually
deleting them (for business reasons).

I do it in a working project where DBIC is used as a database
interaction layer, and it is preferable to make these changes as
transparent  to the surrounding code as possible. Our codebase is
(mostly) covered with tests, so it's generally easy to understand
whether there is a regression or not.

First I tried to override search_rs() method in a custom resultset
with something like this:
https://gist.github.com/ichesnokov/1d4236f79c69fd14ebbe, but it didn't
work as expected on prefetches with condition in them.

Then I tried a method with resultset_attributes (see
https://metacpan.org/pod/DBIx::Class::ResultSource#resultset_attributes),
but it didn't work because it's impossible to get
"current_source_alias" from ResultSource.

Currently I'm using something based on the behavior of
resultset_attributes (see
https://gist.github.com/ichesnokov/e5fb7e70cb6d497ea5a0), and it
works, but requires fiddling with internal state of a resultset, which
seems a bit wrong.

Is there any better way for achieving the same?

Thanks.
--
Best regards,
Ilya Chesnokov

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...