I am trying to create mnesia table from Erlang shell. I got error and the same error for the below syntax copied from mnesia help documents.
mnesia:create_table(employee,
[{attributes, record_info(fields, employee)}]).
got the error
record employee undefined
tried various combinations, getting same error. mnesia already started.
You need to have the record employee defined before you can do record_info on it. In the shell use can use rr(FileName). command which will find all the record definitions in the file and remember them. In a module you would either define the record directly in the module or include a file which contains the record definition.
The reason for having to do this special handling in the shell is that records are purely a compile-time feature so a record definition doesn't really "exist" anywhere.
EDIT:
If you want to define the record directly in shell then you can't use the standard -record(...). syntax. That is only valid in modules. The shell sees that as a call to the record/2 function. You need to use the rd shell command. It your case it would become:
3> rd(employee, {emp_no, name, salary, sex, phone, room_no}).
employee
4> record_info(fields, employee).
[emp_no,name,salary,sex,phone,romm_no]
5>
Then record_info works. If you already have the record definitions in a file then use the shell rr(File). command instead as it is easier. I think.
You can try
rd(employee, {emp_no, name, salary, sex, phone, room_no}). in the erlang shell.
rd(RecordName, RecordDefinition) Defines a record in the shell.
RecordName is an atom and RecordDefinition lists the field names and
the default values. Usually record definitions are made known to the
shell by use of the rr commands described below, but sometimes it is
handy to define records on the fly.
Please see this link:http://www.erlang.org/doc/man/shell.html
Related
The CREATE INDEX <indexName> command is not idempotent and will cause an error if the given index already exists. I'm new to neo4j, and can't find a predicate that avoids this error. I've tried various permutations of ANY(...), and they all barf at "db.indexes()".
Since CREATE INDEX ... fails if the index exists and DROP INDEX ... fails if it doesn't, I don't know how to write a .cypher file that creates the index only if needed.
A short form might be something like CREATE INDEX indexName FOR (c:SomeLabel) ON (c.someProperty) IF NOT EXISTS, but of course that short form doesn't exist.
Is there some way to do this with a predicate, subquery or some such expression?
As of Neo4j 4.1.3, a new index creation syntax has been introduced to do just that
CREATE INDEX myIndex IF NOT EXISTS FOR (t:Test) ON (t.id)
Indexes for search performance
You can use the apoc.schema.node.indexExists function to check whether an index exists before creating it.
For example, this query will create the :Foo(id) index if it does not already exist:
WITH 1 AS ignored
WHERE NOT apoc.schema.node.indexExists('Foo', ['id'])
CALL db.createIndex('index_name', ['Foo'], ['id'], 'native-btree-1.0') YIELD name, labels, properties
RETURN name, labels, properties
For some reason, the Cypher planner currently is not able to parse the normal CREATE INDEX index_name ... syntax after the above WHERE clause, so this query uses the db.createIndex procedure instead.
There is also a much more powerful APOC procedure, apoc.schema.assert, but it may be overkill for your requirements.
By default, the command is ignored if the index exists.
Can you test the following?
CREATE (n:Car {id: 1});
Added 1 label, created 1 node, set 1 property, completed after 23 ms.
CREATE INDEX ON :Car (id);
1st execution: Added 1 index, completed after 6 ms.
2nd execution : (no changes, no records)
I tried both suggestions, and neither solves my issue. I don't have time to discover, through trial-and-error, how to install APOC in my environment.
The first line of mbh86's answer is inaccurate, at least in my system. The command is not ignored, it fails with an error. So if anything else is in the same cypher script, it will fail.
The best I can do is apparently to wrap the CREATE INDEX in a command-line string, run that string from either a bash or python script, run it, and check the return code from the calling program.
I appreciate the effort of both commentators, and I didn't want to leave either hanging.
I want to execute a rather nasty recursive update query in rails. This means I want to write some raw postgres sql, with parameters, and execute it inside a rails controller.
How do I do that? I can't find a PreparedStatement class in activerecord, there don't seem to be any methods named 'native', I have tried ActiveRecord::Base.connection.exec_delete, I have looked through the source - just cannot, cannot work it out.
I've looked everywhere - the documentation goes in circles.
How would I tell postgres to execute
delete from foo where foo.bar=?
bind 'baz' to the q-mark, and do it without using the active record objects, finders, you-beaut subset thingies and all the rest.
I just want to execute a prepared statement with some bindings. How hard can it be?
(PS, and no: I don't want to jam the parameters into the string myself and execute it as unparameterised sql. It's wrong and it means I have to worry about sanitising the data.)
See the discussion of PreparedStatements in Rails ('Using Prepared Statements') here - http://blog.daniel-azuma.com/archives/216 . Shows you which methods to call, and how to format your arguments.
UPDATE:
Paraphrased from the post:
For the delete method arguments use the template first, followed by a query name (which can be nil) and then an array of values to inject into the statement. So like this:
row_count = connection.delete("DELETE FROM foo WHERE foo.bar=$1", nil, [[nil, 'baz']])
I am new to postgres. In mysql we can check whether the database is in read-only mode by triggering the below query.
SELECT ##global.read_only
Likewise can anyone pls help me with the query to do the same in postgres? I tried few things like below
SELECT schemaname||'.'||tablename FROM pg_tables
WHERE
has_table_privilege ( 'postgres', schemaname||'.'||tablename, 'select' )
AND schemaname NOT IN ( 'pg_catalog','information_schema');
But it is listing like below which I am not expecting.
?column?
----------------------------------------
public.schema_migrations
public.credential_methods
public.notifications
public.site_defaults
public.apis
public.client_applications
public.api_groups
public.operations
public.client_application_labels
public.client_application_label_values
public.roles
public.users
public.sdm_user_roles
public.permissions_roles
public.keys
public.o_two_access_tokens
public.settings
public.sdm_users
public.permissions
public.audits
public.oauth_requesttokens
public.oauth_access_tokens
public.oauth_verifiers
public.logged_exceptions
public.api_call_details
public.api_access_roles
public.api_access_users
public.login_attempts
public.system_scopes
public.keys_system_scopes
public.o_two_auth_codes
public.o_two_refresh_tokens
public.service_profiles
public.error_traces
I also tried "\du" but this one is working only in terminal but not from a ruby file.
query=ActiveRecord::Base.connection.execute("\du;")
ActiveRecord::StatementInvalid: PGError: ERROR: syntax error at or near "du"
LINE 1: du;
Thanks,
Rafiu
You probably want something of the has_*_privilege() family function for relevant tables and relevant privileges. See here. Other than that I'm not sure if postgres has a concept of read-only mode.
Well, there's also show transaction_read_only inside a read-only transaction, but that doesn't seem to be like what you're asking for. And I don't think that transaction being readonly affects privileges of the user.
I'm not sure what you expect from your query, but if you want something boolean, as in whether you have access anywhere, you can use count(*)!=0 (and, probably, not select).
If you have a multi-node instance cluster, and you have the hot standby configuration. The output of SELECT pg_is_in_recovery() can tell you if the cluster is in the read-only mode.
I would like to let my users create Ruby scripts that do computation on some data residing on the web server and then outputs results. The scripts are executed on the server. Is there any way to do this securely?
More specifically, I would like to:
restrict the resources the script can use (memory and cpu), and limit its running time
restrict which core classes the script can use (e.g. String, Fixnum, Float, Math etc)
let the script access and return data
output any errors to the user
Are there any libraries or projects that do what I'm asking for? If not in Ruby, maybe some other language?
You can use a "blank slate" as a clean room, and a sandbox in which to set the safe level to 4.
A blank slate an object you've stripped all the methods from:
class BlankSlate
instance_methods.each do |name|
class_eval do
unless name =~ /^__|^instance_eval$|^binding$|^object_id$/
undef_method name
end
end
end
end
A clean room is an object in which context you evaluate other code:
clean_room = BlankSlate.new
Read a command from an untrusted source, then untaint it. Unless untainted, Ruby will refuse to eval the string in a sandbox.
command = gets
command.untaint
Now execute the string in a sandbox, cranking the safe level up as high as it will go. The $SAFE level will go back to normal when the proc ends. We execute the command in the context of the clean room's binding, so that it can only see the methods and variables that the clean room can see (remember, though, that like any object, the clean room can see anything in global scape).
result = proc do
$SAFE = 4
clean_room.instance_eval do
binding
end.eval(command)
end.call
print the result:
p result
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).