rails c not using the same database as rails db - ruby-on-rails

I just realized that when I run rails c I'm getting a different database than when I run rails db.
I initially though the console wasn't connecting to the database at all because it kept returning empty sets, for instance when running User.all, despite having seed data. I then inserted data into the database via the console and did a query from the SQLite console. The new data did not appear even after reloading the console.
My confusion is further exacerbated by the fact that I only have the db/development.sqlite3 file and rails r "puts Rails.env" returns Development.
So where might I have gone wrong here? Why is one using the development database and the other seemingly using something else? Could rake db:seed be pushing to Test perhaps? Is there a way to check the SQLite console for the environment?

Turns out Spring was causing the problem. I had a previous app with the same name and it was loading that when I opened the rails console despite being in the newer app directory.
I've disabled Spring with the DISABLE_SPRING variable and now things are working as expected.

Related

Is there any chance of testing the PostgreSQL database in this scenario without duplicating the database in the development environment?

“I have a Rails application with PostgreSQL database. My database contains 1000 records in it.
I have written a rake task which will manipulate the records in the database.
If the rake task went wrong i want to revert back the changes to the original in the database.
is there any chance of testing the database in this scenario without duplicating the database in the development environment ?”
In Rails itself having the feature sandbox
If you wish to test out some code without changing any data, you can do that by invoking rails console --sandbox.
bin/rails console --sandbox
Loading development environment in sandbox
Any modifications you make will be rolled back on exit
irb(main):001:0>
You can paste your ruby code here and can see the changes you wanted to see. while closing this console, All your changes will be reverted back(running as a transactional)
For more informations you can refer this link
http://edgeguides.rubyonrails.org/command_line.html#rails-console
you can duplicate a database in same env.
create database test with template original_db;
Will create cop of existing db, on which you can run your tests, specifying test instead of original db name in connections string

Rebuild lost database in Ruby on Rails app

I have an existing ruby on rails app that was on a VPS. I pulled the app off the VPS but never got the database data backed up. I was looking through the source code and see that there is no schema.rb file but I do have all the migration files.
My question is, will there be a way to rebuild the entire database the way it was before?
rake db:migrate should give you back the table schema, but you don't get any of the data.

Rails -- working with an existing db, cannot run migrations without losing data

I am a beginner working with Rails 4.1. I am trying to integrate an existing sqlite3 db with my app in the development environment.
To try to get this to work, I've followed the steps listed at Joel Berghoff's blog (for MySQL):
Reference the db in config/database.yml
Run “rake db:schema:dump”
Convert schema.rb into db/migrate/001_create_database.rb
The issue I am facing is, whenever I run "rake db:migrate" the entire db refreshes and I lose all the pre-populated data. I got around this for awhile by running migrations first, then replacing the blank db that was generated with my pre-populated copy -- this allowed me to play around with my models in the rails console and see the data. However when I try to boot up the server on my local machine, I get a message that migrations are pending.
I am not quite sure what to do here...I've read that I should be seeding the db from "rake db:seed", but my existing db is quite large -- almost 1mm records, and when I attempted this (in albeit clumsy fashion) it ran for over 3 hours before I gave up. Any guidance on how to proceed is much appreciated!
Migrates should be used to create and changes the tables and fields, not load data, as you can see here Ruby on Rails Guides
If you want to import data you could do it on the seeds, but in your specific case it seems to me that you should create a dump from your origin database and load it on the target database.
Here is a tutorial sqlite3: how to import/export data from/to a file
sqllite3 and MySQL are different things. My guess is you think you are connected to sqllite db but in reality you are connected to an empty MySQL db.
Look into ActiveRecord migrations.
Here is a rails doc on ActiveRecord Migrations: http://guides.rubyonrails.org/migrations.html

Rails console can't connect to database

In last days i update my OS X to Maverics. Today when i try to create new project like this:
rails new abc
there were many problems but i install xcode and now it's work. Right now i open rails console like this:
rails console
and then whatever i write i only see:
Loading development environment (Rails 4.0.1)
1.9.3p448 :001 > Link
=> Link(no database connection)
What is wrong? Mysql is running, database exist. When i do rake db:migrate everything works fine.
The console probably does have a database connection but is reporting that it doesn't.
To see if that's true, make a query in the console.
Link.count
That fixed the false positive warning for me and a colleague.
In the past, referencing an ActiveRecord model immediately after loading the console would return its column information. Rails now only returns this information if it has been explicitly coerced to connect to the database by a method like connection, first, all, count, etc.
You can restore the previous behavior by adding this to your config/application.rb
console do
ActiveRecord::Base.connection
end
According to the issues #12804 on rails/rails.
The message just tells you that AR has not yet connected to the database and therefore does not know the column information. I think it's desired not to connect to the database unless we need to fetch data.
For now you could use Model.connection to establish a connection.
Try using reload! on the console and ensure that you have records in the specified model if not create records for the relations etc..
I had the same problem on ubuntu. I used reload! in rails console and added records in the database.
I'm having the same issue with Rails 4.0.1. It's occurring on the Linux server I'm deploying to as well as my Mavericks development machine.
The server works, specs work, but the console doesn't have a database connection.
Reverting to Rails 4.0.0 fixes the issue with the console.
I haven't found any other mention of this issue. There's probably an issue with the changes for 4.0.1 and the Postgres adapter, maybe? (Are you using Postgres?)

Ruby: SQLite3::BusyException: database is locked:

Ran into this error message whilst developing tonight: SQLite3::BusyException: database is locked:
I have two models:
Podcasts have many Tracks
Tracks belong to Podcasts.
Podcast files are hosted on mixcloud.
To create a Podcast:
user submits a url for a podcast on mixcloud
rails app grabs json feed associated with url
json is used to set attributes (title, image etc) on the new Podcast object
I'm trying to get my rails app to take advantage of the fact that the json feed also details the names (and artists) of the Tracks that belong to this Podcast.
I thought the following before_validation method would automatically create all associated Tracks whenever we create a new Podcast.
class Podcast < ActiveRecord::Base
attr_accessible :mixcloud_url, :lots, :of, :other, :attrs
has_many :tracks
before_validation :create_tracks
def create_tracks
json = Hashie::Mash.new HTTParty.get(self.json_url)
json.sections.each do |section|
if section.section_type=="track"
Track.create(:name=>section.track.name, :podcast_id=>self.id)
end
end
end
end
How can I get round this? It looks like rails (or sqlite3) doesn't like me creating new instances of an associated model in this way. How else can I do this? I suspect this is as much a rails problem as an sqlite3 one. I can post more code if it's gonna help.
For anyone else encountering this issue with SQLite locking in development when a Rails console is open, try this:
Just run the following:
ActiveRecord::Base.connection.execute("BEGIN TRANSACTION; END;")
For me anyway, it appears to clear any transaction that the console was holding onto and frees up the database.
This is especially a problem for me when running delayed_job, which seems to fail at closing the transaction quite often.
SQLite is not really supposed to be used for concurrent access which is the issue you are running into here. You can try increasing the timeout in your database.yml file which may be a workaround for you in this case. However, I would recommend you switch to another database that supports multiple connections like MySQL or PgSQL.
For me...the issue I had was that it seemed that the Rails Console I had open for a while was locking up a connection with SQLite.
So once I exited that console, and restarted my webserver (Thin) it worked perfectly.
I tried #trisweb's suggestion but it didn't work for me.
I had the same
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is
locked: INSERT INTO "users" ("created_at", "email", "name",
"password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)"
issue. I tried every way around found in Google and I failed. The problem was solved for me when I closed my SQLite Database Browser.
Make sure you don't have 2 guards or several consoles running.
If you want make sure desperately see the "No Name's" answer above.
You can also try increasing pool:
for example:
change test section in your config/database.yml as below
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 50
timeout: 5000
Probably you have a Rails console open on another bash, if so you have to close it (ctrl+D).
actually for me, I found killing rails help to fix this problem.
use "ps aux | grep rails" to find out ongoing rails process id.
then use
"kill -9 [rails-pid]"
to kill processes.
Then it will work
It is most likely not related to rails code. Using at the same time the console with the sandbox option (rails console --sandbox), makes the problem systematic with SQLite, since the console is basically waiting to quit to rollback everything.
The solution above from #trisweb will not work in this case, but quitting the console will.
my trouble is: I opened a database management program named "DB Browser for SQlite". Closed this database management program, and problem solved.
Yes this is an old question and there are many answers on here already. But none of them worked for me, meaning it took me a long time to finally figure out the problem. I found what worked and will share it in case it is what might be causing the problem for you too.
I was using the SQLITE Browser (its a GUI database browser). I will refer to it as "GUI" here (to prevent confusion with the word browser being your localhost::8000 chrome browser or whatever.
http://sqlitebrowser.org/
I was monitoring what was being written to the database and had the GUI open while my rails app ran in my chrome browser. I would refresh the GUI to see if it was adding the data as I expected it to.
As a matter of debugging I had decided to delete a row from the SQLite GUI so I could see if my app would react appropriately to the row being missing now.
As it turns out, the SQLite Browser does not actually delete the row (causing confusion on my end as to why my app was acting like the row was still there, even though it was visually missing on the GUI). Anyway after 30 minutes of frustration I closed the SQLite GUI and then got a notice that asked if i wanted to save any changes to the database that I made. I naively clicked "No" and closed the app.
Apparently what happens is that the GUI then locked the database because there were rows in my database that had been sort of "soft-deleted" without committing to the delete. So the GUI was (for lack of a better term) holding the database in Limbo.
This explains why a) my app wasnt acting like the row was missing, because it hadn't actually been deleted yet, and B) explains why the database locked up. It was still waiting for me to commit the deletes.
So to solve the problem, I simply opened up the GUI once again and deleted the same row and then closed the GUI and this time I clicked "Yes" when asking to save changes to the database. It saved the delete and unlocked the database and now my app works!
I hope this helps someone else that might be having the same issue but was using the SQLite Browser GUI interface. This might be what is locking your database.
I had the same issue. For those with SQLite Database Browser. I did not need to close SQLite Database Browser. I only had to click the "Write Changes" button. It is highlighted and needs to not be highlighted.
SQLite has troubles with concurrency.
I changed sqlite on Postgresql and the issue is gone
This happens when you make any changes manually directly into the SQlite DB Browser (like delete a row or change the value of any column) and forget to save those changes. Any changes made need to be saved (ctrl + s). If not saved, SQLite locks the Database until u save those changes.
I did the same and my issue got resolved!
I was using DB Browser for SQLite and rails console simultaneously. Closing the DB Browser for SQLite fixed the issue for me.
try restarting the server
or closing any running rails console,
worked for me
Your .sqlite3 file must be saved.
Go to DB Browser for SQlite and make ctrl+s or File->Write Changes.
Try wrap cycle in a single transaction:
def create_tracks
json = Hashie::Mash.new HTTParty.get(self.json_url)
Track.transaction do
json.sections.each do |section|
if section.section_type=="track"
Track.create(:name=>section.track.name, :podcast_id=>self.id)
end
end
end
end
(see Track.transaction)
You may also have enabled threads when parallelizing your tests. Remember to disable the option in test/test_helper.rb :
parallelize(workers: :number_of_processors, with: :threads)
to
parallelize(workers: :number_of_processors)
https://edgeguides.rubyonrails.org/testing.html#parallel-testing-with-threads
1. bin/rails console
2. exit
Go into the rails console and type exit, and press enter.
Done!
Sadly, many of these solutions did not work for me.
I was lucky. Since this was happening while I was running tests, I just did a simple DROP and CREATE on the DB.
$ rake db:drop RAILS_ENV=test
Dropped database 'db/test.sqlite3'
$ rake db:create RAILS_ENV=test
Created database 'db/test.sqlite3'
$ rake db:migrate RAILS_ENV=test
== 20220310061725 Tables: migrating ======================================
....
== 20220310061725 Tables: migrated (0.0027s) =============================
...etc
Not the best solution, but it works in a pinch.
If this was in your development environment, and you HAD to do this, I would seriously invest in creating a data seed for your DBs so you can get up to speed again.
rake db:drop
rake db:create
rake db:migrate
rake db:seed
I'd like to talk about the case of rails controller opened.
In my own case, I had opened rails controller with the --sandbox option.
After reading that I had to close the rails console. Closing alone didn't fix it. I had to restart my PC to make sure all processes got terminated. That resolved it.

Resources