How to create database from schema.rb without initializing Rails? - ruby-on-rails

I am trying to create all my tables from schema.rb
I used the command: "rake db:schema:load"
However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty)
I could comment out these lines, and then run schema:load again, but is there an alternative?

Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load:
Create a new rails app: rails new (app)-fixer
Copy your gemfile (unless there are specific exceptions) to the fixer app.
Copy your database.yml config to the fixer app.
Copy your schema.rb file to the fixer app.
Do all appropriate "bundle install" commands as needed for your app.
Then run "rake db:drop db:create db:schema:load"
That will build up a new database from scratch, based on your current schema.

You can add a check for the table existance in your initializer.
if TheModel.table_exists?
// do something with the model
end

Related

Can Rails schema table be outside the database?

We have a legacy PostgreSQL database that is perfectly accessible from a Rails app except that when the vendor provides an update one of the consistency checks they perform against the database fails because Rails has built a "new" table there, the schema migrations table. Is it possible to direct creation of this table elsewhere? Or is it possible to use the schema cache in Rails 4 to effect this? The release notes section 3.3 on General things says "Schema cache dump (commit) - To improve Rails boot time, instead of loading the schema directly from the database, load the schema from a dump file."
I found an old blog post about this last time I tried it here. Copying the relevant parts:
To make a dump of your schema, execute the following rake task:
RAILS_ENV=production bundle exec rake db:schema:cache:dump
This will generate a file db/schema_cache.dump, that Rails will use to load the internal state of the SchemaCache instance.
To disable the schema cache dump, add the following to your config/production.rb file:
config.active_record.use_schema_cache_dump = false
If you would like to clear the schema cache, execute:
RAILS_ENV=production bundle exec rake db:schema:cache:clear

Rails: running an initializer after migrate

I have some code which I run from an initializer and it works fine. (It saves default settings from a yaml file to the database for the rails-settings-cache gem.)
But when I run this on Travis CI, since it is doing a migration from scratch, the initializer fails because the table does not exist yet.
Is there a way of running code after the migration but before the application starts?
So while I don't love doing this, an easy way to prevent the initializer from running during db:migrate, but running on application start or test run is to wrap it in a clause testing if the table exists. So if you take your existing initializer code and wrap it in
if ActiveRecord::Base.connection.table_exists? 'table_name'
....
end
where 'table_name' is the name of the missing table, then both rake db:migrate and the spec run should be able to complete successfully.

Rails: compile migrations into one file

We've been developing an application for a year and there are some errors in the migrations. Is there a way to compile them into one file so that we can eliminate any errors and the like without sorting through hundreds of individual files? Or can we reset the migrations so the version running right now is the first for new installations?
Migrations are already "compile" into one file call schema.rb located into the db folder of your rails application.
You can load the schema from the following rake task:
rake db:schema:load

How to load the data from a .sqlite3 file to a Ruby on Rails Application

I have a production.sqlite3 file which I want to import its data to the current rails project, the database schema is match between the file and my current project. I did copy the content to the development.sqlite3 file but this does not work. The only way I know to insert data to the database is by loading some yml file or use the seed command. Is there any magic command or other ways to let the rails loading the data from .sqlite file? Because from what I disover, the behavior of rails is that it only creates the .sqlite3 file when you do rake db:create &migrate, but it will ignore what ever manual changes happen in that file. Please help!
It looks like renaming your .sqlite3 file should work. Make sure you have restarted your server, and that the server is running in the correct environment. If you move both the .sqlite3 files into a completely different folder, and restart the server, an error should occur. If it doesn't, your server didn't properly restart - kill all ruby processes and try again.
In answer to your question about migrating data, I had a similar problem recently, where I had to migrate from sqlite3 to mysql. Best solution I found was yaml_db. It seemed to do the job beautifully - add it to the project, then do
rake db:data:dump
Swap the database configurations (or migrate a new database file, or change your development environment, whatever it is you need to do to make an empty but structured database active), then:
rake db:data:load

Is it possible to run a SQL-only file from a "rake db:create"?

I'm trying to install a software called Teambox in my Dreamhost shared account, following these instructions.
I have no experience with Rails. I just want to install the software in the shared hosting.
In this shared hosting, all dependencies are ok, but I have to create the dabatase from their panel. I can't create in command line (ssh).
So, when I run "rake db:create" these's an error, because the db already exists (because I created in panel).
I've already contacted support. They can't change this policy.
How do I populate my tables "by hand" in this case? Which files should I look inside Teambox's folder...
Thanks!
rake db:create should create your database and that's all. It doesn't creates your tables.
There are several rake tasks to do that :
rake db:migrate which will execute your migrations. So create all your tables.
rake db:seed which will add the original data of your application. See Database Seeding
You don't need to be able to create the database to do any of these.

Resources