Restoring / deleting soft deleted items - eve

I am using Python Eve with the soft_delete enabled for some resources of my REST backend and I meet a problem to really delete from the Mongo DB a soft deleted item.
My application workflow involves:
- creating an item: POST /item with unique name
- deleting an item: DELETE /item
- creating an item: POST /item with the same unique name
On the third step, the item is still existing in the DB but it is _deleted: True. How should I restore this item when the user POST /item ?
I know that a PUT /item or PATCH /item will do the trick but my user do not know that he must PUT/PATCH rather than POSTing ...
I tried to use the deleteitem_internal function inside an on_pre_POST_... hook function but it raises a 404 error.
I suppose that I should patch_internal my item with the POSTed data and then abort the request. Is this solution the best one to implement when one wants to restore a soft deleted item ?

Related

Cannot delete a mnesia table that I know exists

I have a mnesia table that I am trying to delete. However, when I try to run :mnesia.delete(TableName) I get this error back {:aborted, {:no_exists, TableName}}
When I try to create the same table by running :mnesia.create_table(TableName, [attributes: [:id, :att1, :att2], disc_copies: [Node.self()]]) I get this back {:aborted, {:already_exists, TableName}}
I can still see the .DCD file for the table after a delete, what causes this and how can I fix it?
Note: The code is in an Elixir codebase.
Edit: When my application starts, I try to delete and recreate that table, even if it exists.
:mnesia.delete/1 looks for a key to delete in the given table (and takes a tuple {Table, Key}).
You probably want :mnesia.delete_table/1 which will delete the table itself.
Docs for more: http://erlang.org/doc/man/mnesia.html#delete_table-1

how to tell active record to make an update instead of insert on a (not) new record after dup(ing) an other one?

Or: How does Active record decide what to use update or insert?
I am using UUIDs as keys.
I generate UUIDs client side for new objects to allow asynch work with the server/db.
Example: The user adds a new triangle to his drawing, I create this object set a uuid (lets say AAA), place it in editor and the user can work further. Then I send the update to the server. So the user does not have to wait the RTT.
The user fills the triangle green, I draw that -> AJAX to server (Update (AAA))
The use makes a copy of the green triangle, I copy the object set a new UUID and send an AJAX copy AAA to BBB
the user hits undo - remove BBB from drawing send AJAX remove BBB (but its not deleted its just marked for deletion as a standard in my app)
the user hits redo -> I copy the object set a new UUID (BBB again) and send an AJAX copy AAA to BBB
BANG duplicate key
Server side I make a dup, set the id and save
new_rec=old_rec.dup
new_rec.id=new_id #BBB
new_rec.save (BANG because insert)
So what I need is something like
new_rec=old_rec.dup
new_rec.id=new_id #BBB
new_rec.set_method_to_update if Record.exist?(new_id)
new_rec.save (BANG because insert)
I know, that there are workarounds but is it possible the easy way?
AFAIK rails doesn't let you change the persisted characteristic of a record. You'll need to retrieve the record if it exists and update it.
new_rec = Record.find(new_id)
if new_rec
new_rec.deleted = false
else
new_rec = old_rec.dup
new_rec.id = new_id
end
new_rec.save
Regarding the duplicate entries on server end, considering only the primary keys as identifiers in the DB objects. Fetch the data with ID and create one if not found.
Assuming you are using Rails 4, you can use this method find_or_create_by
As i see your question, on a different note: first thing you can do is instead of making ajax calls on every or specific operations. Put a sync of say 5-10 seconds of work done on the interface, using something like angular/backbone to handle the UI level model hierarchy which you have designed. Replace the temporary UUID's on the browser from the server ID's on sync or keep a mapping of both which could be slightly helpful but somewhat messy to maintain.
What I needed - found accidently: #new_record
Set to false and all is fine ...
Usage in Record (model.rb)
def clear_new_if_not
#new_record=!self.class.exists?(self.id)
end
and logic above:
new_rec=old_rec.dup
new_rec.id=new_id #BBB
new_rec.clear_new_if_not()
new_rec.save (no bang)

Rails, Soulmate, Redis remove record

I use Soulmate to autocomplete search results, however I want to be able to delete records after a while so they don't show up in the searchfield again. To reload the list with Soulmate seems a bit hacky and unnecessary.
I have used json to load and I have a unique record "id"
{"id":1547,"term":"Foo Baar, Baaz","score":85}
How can I delete that record from redis so it wont show up in the search results again?
It is not trivial to do it directly from Redis, using redis-cli commands.
Looking at soulmate code, the data structure is as follows:
a soulmate-index:[type] set containing all the prefixes
a soulmate-data:[type] hash object containing the association between the id and the json object.
per prefix, a soulmate-index:[type]:[prefix] sorted set (with score and id)
So to delete an item, you need to:
Retrieve the json object from its id (you already did it) -> id 1547
HDEL soulmate-data:[type] 1547
Generate all the possible prefixes from "Foo Baar, Baaz"
For each prefix:
SREM soulmate-data:[type] [prefix]
ZREM soulmate-index:[type]:[prefix] 1547
Probably it would be easier to directly call the remove method provided in the Soulmate.Loader class from a Ruby script, which automates everything for you.
https://github.com/seatgeek/soulmate/blob/master/lib/soulmate/loader.rb

Update Contour form(records) using the record ID

I can successfully create entries in contour programmatically(C#) but I am not able to update the created record using the record ID. After digging my head around can’t find a reason why the following code doesn’t work. It’s very basic and all That I am trying to do is get the record that exist in the contour.
RecordStorage recordStorage = new RecordStorage();
Record r = recordStorage.GetRecord(new Guid("15d654cb-a7c6-4f1f-8b55-0ecd7d19b0e3"));
recordStorage.Dispose();
Just to start with the update process, I am trying to get the record object using it’s id but can’t proceed further as it throws a weird error “An item with the same key has already been added.” I can’t understand while it’s trying to set the value when I call the “storage.GetRecord()”. Following is the stack trace
**An item with the same key has already been added.**
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Umbraco.Forms.Data.Storage.RecordFieldStorage.GetAllRecordFields(Record record)
at Umbraco.Forms.Data.Storage.RecordStorage.GetRecord(Object id)
at MauriceBlackburn.Service.ContourFormService.InsertRecord(ContourFormFields unionContourForm)
Any thoughts, have I missed something, I have been digging all day around and still not able to figure this out. Thanks in advance.
Much Appreciated.
First off, try deleting the workflow and re-adding it.
You could also create two simple workflows, one that will write the record and a second to manipulate it (using the id when written).
Make sure that there are no records with the same ID in database. You might have inserted them before.

Active Record - Get the second, third.. item in a database (without ID)

How to I retrieve the second, third .. entries in a database. I don't want to use the auto incrementing id generated by rails.
As I am deleting entries from my database, so the ID continues to increase.
So my DB would have
id : 210 (Even thought it's currently the first value)
id : 211 (Even thought it's currently the second value)
I know "Phone.first" will return the first how do I get the second.
Sub Question-
Destroy_all/delete_all - only removes the item, Can I remove all entries from the DB and have the DB id's start from one without dropping the table. As I ran into problems.
Say you want the fourth user:
#users = User.limit(4)
#fourth_user = #users[3]
Concerning your second question, destroy_all should do the trick since it triggers all callbacks. Be sure to add :dependent => :destroy in your relationships.
what you need is
#n=2,3,4 etc
.limit(1).offset(n)
Model.second was added to Rails 4.1.8.
So you can use it on Rails versions equal to or greater than that.
.third, .fourth and .fifth were also added to ActiveRecord::FinderMethods.

Resources