Cannot delete a mnesia table that I know exists - erlang

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

Related

Restoring / deleting soft deleted items

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 ?

Can't view newly created records after database sequence reset

So I had some problems with my database. I have an import functionality which I used to import a few thousand records. Now I was developing locally (SQLite) and everything worked fine. I pushed my updates to heroku where I have a PostgreSQL database. I ran my import there and it appeared to work well too. Now, I'm trying to manually create an other record in that table and I kept getting the error that the unique ID was a duplicate. So, after some online searching I created this extension to active record:
def self.reset_pk_sequence
case ActiveRecord::Base.connection.adapter_name
when 'SQLite'
new_max = maximum(primary_key) || 0
update_seq_sql = "update sqlite_sequence set seq = #{new_max} where name = '#{table_name}';"
ActiveRecord::Base.connection.execute(update_seq_sql)
when 'PostgreSQL'
ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
else
raise "Task not implemented for this DB adapter"
end
end
I ran it on my table, and the ID is now counting up from where it should (3000 something instead of 1,2,3, etc).
My problem now is that I am able to create new records in my table, and I see them in my index page. However, when I try to open that specific record, I get this error:
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
The link from my index is just to the ID of the record. When I check if this ID exists in the console, I'm perfectly able to find the record. What's going wrong here?
I figured it out. I had some empty associations that I was trying to display in the view. The error threw me off since it appeared like something was wrong with my record.

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.

Mnesia write fails

I defined a record named log. I want to create an mnesia table with name log_table. When I try to write a record to table, I get bad_type error as follows:
(node1#kitt)4> mnesia:create_table(log_table, [{ram_copies, [node()]},
{attributes, record_info(fields, log)}]).
{atomic,ok}
(node1#kitt)5> mnesia:dirty_write(log_table, #log{id="hebelek"}).
** exception exit: {aborted,{bad_type,#log{id = "hebelek"}}}
in function mnesia:abort/1
What am I missing?
By default the record name is assumed to be the same as the table name.
To fix this you should either name your table just log or append the option {record_name, log} in your table options (as you've done in your fix).
It is usually good practice to let your record and table be named the same thing, it makes the code easier to read and debug. You can also then use the mnesia:write/1 function with just your record as only argument. Mnesia then figures out which table to put the record in by looking at the name.
I've found it. When I changed mnesia:create_table call to this
mnesia:create_table(log_table, [{ram_copies, [node()]},
{record_name, log},
{attributes, record_info(fields, log)}]).
everything works OK.
How does your definition of the log-records look? Do you get the same error if you create a new table from scratch (i.e. remove the Mnesia# directory first).

Resources