When debugging production data, I sometimes will use the Rails console to inspect and query data.
I prefer the Rails (ActiveRecord) console to a raw SQL client.
Is there a way to tell Rails to not write to the DB during my console session to avoid accidentally modifying production data?
Probably you are looking rails console --sandbox
Rails documentation
Related
I want to run some code around a rails console session (e.g. add log tagging, set database connection). For the rails server I can do this using middle ware, but can this also be done for the console?
see also: Rails7 MultiDB switch shard for console
I need to get all active sessions' data from my Rails application. I am using Redis for session store.
I tried REDIS.keys and then REDIS.get("SESSION_KEY")but it seems like encrypted data.
Is there any painless solution to get live sessions' data on Rails?
Thanks.
Decrypt the Rails session data from Redis
Ruby on Rails using Marshal method for saving objects to cache.
For example:
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/rack_cache.rb#L17
You can unmarshalling this data as follows:
Marshal.load(REDIS.get("SESSION_KEY"))
As part of the security procedures for our Rails application, we want to have an audit log of the commands that were run from rails console in our production environment.
This audit log should be persisted somewhere, such as a database, a file in an S3 bucket, or similar. If it could be written to the standard rails log, that might be OK too, as we already have a way to persist that.
We're currently hosted on Heroku. Heroku will log the event of firing up a console, but (a) Heroku provides no functionality for logging commands run from an active console, and (b) we'd like a more general-purpose solution.
Are there any pre-existing solutions for this out there?
If there is none, I'm trying to figure out how to monkey-patch IRB or Rails Console.
I've discovered that the data I need is already in
Readline::HISTORY
but I'm struggling to figure out where/how to hook into it.
Ideally, I'd like to capture each entry as (before?) it is sent to the interpreter, and quickly persist it somewhere (we already have Resque, so that may be a good solution). FWIW, sending the contents of Readline::HISTORY on Kernel.exit seems like it may be unreliable. For example, if a SIGKILL stops the process, then the history contents wouldn't be saved.
I have a ruby on rails app deployed on heroku with a database and a follower database.
I typically do "heroku run rails console" to look around the data, and I'm wondering if there's a way that I can do it on the follower database so I don't accidentally write/delete things. I know this should be simple, but I couldn't seem to find documentation on it.
Thanks!
You can do this from your local console. Just fire it up:
rails console
Then you need the connection information for your follower database on heroku. You can retrieve this from https://postgres.heroku.com/databases/. Just click the relevant database and assign a hash with the following info:
follower_db = {:adapter => "postgresql",
:host => "myhost",
:username => "myuser",
:password => "mypass",
:database => "somedatabase"}
Now connect to this remote database from the console:
ActiveRecord::Base.establish_connection(follower_db)
Now any query you enter in the console will be run on this DB.
This will use the follower database credentials from the FOLLOWER_DATABASE_URL environment variable in your Heroku app to start up a Rails console that connects directly to the follower instead of the promoted database. Normally Rails is set up to read from DATABASE_URL by default, and this just sets it to a different value for the duration of your console session.
heroku run 'DATABASE_URL=$FOLLOWER_DATABASE_URL rails console'
If you're worried about accidentally deleting stuff, run console with the sandbox flag
heroku run console --sandbox --app app-name
Your changes will be rolled back when your session ends.
The console certainly has its place, but if I want to check what data is in a database then I'd prefer to look directly at it without ActiveRecord etc being in the way. I've found many instances where using the console requires that you check the generated SQL anyway to ensure that there are no scopes (for example) slipping in.
I'd suggest that you try to get into the habit of using a SQL interface (PgAdminIII perhaps) instead of the console -- an Heroku dataclip will even work if you don't want to trouble about connecting from the client (though the psql connection string is given to you from the database page).
A database follower is a read-only copy of the master database that stays up-to-date with the master database data. As writes and other data modifications are committed in the master database the changes are streamed, in real-time, to the follower databases.
Source - https://devcenter.heroku.com/articles/heroku-postgres-follower-databases
The main question is: Can I read Rails session data in Node?
More details:
I have a project that is written in Ruby on Rails. It works but I want to add to it and eventually replace it using NodeJS. Both are running on the same server, just on different ports.
As of now RoR will serve up all the HTML files (and continue handeling the existing functionality) and then I'll connect to the Node server via AJAX. Node will just dish up JSON for the time being.
The problem is, how can I work with session variables between the two? More specifically, can I get to RoRs session variables in Node? Mostly I just need to know which user is logged in.
If it matters, I am running Rails 2.3.5, Ruby 1.8.7, and Node 0.8.17.
I haven't tried exactly same stuff, myself, but, we did something similar but with Sinatra and Java.
I wouldn't comment about your approach on application design, but, in case you don't mind using Memcached session store in your rails application, yes it is possible. Configuring Memcached with Ruby app is explained on Heroku Doc
In Node application you can use Memcached Client like 3rd-Eden and access session variable from memcache
You would have to explicitly pass session id generated by rails to Node.