I have been mucking with my development database and am getting ready to move it to production. I made some edits outside of rails, so I don't have a migration for all of my changes. I know I can rake db:schema:dump to generate a schema.rb file, but can I apply that to an already populated production db without wiping the data? Something similar to rake db:schema:load without wiping the data?
If not, do I just need to manually create the migrations that would catch the production db up? If I do make those migrations, won't all future calls to rake db:migrate on the dev box fail because the change in the migration already exists in the dev db?
The problem with rake db:schema:load is that it will forcefully create the tables, which you can see in db/schema.rb:
create_table :users, :force => true do |t|
# etc
end
What I would recommend is that you do create the missing migrations. You can fix your local dev database by adding the timestamps to the schema_migrations table manually. That is the consequence of changing your schema by hand.
I personally always make sure that rake db:migrate:reset (drop all tables and migrate from scratch) will produce the same db/schema.rb as rake db:schema:dump would. Any change in the database schema must be automated by a migration. You could even make it part of your CI script, by running rake db:migrate:reset and than asserting that db/schema.rb didn't change from what is in source control.
Related
I'm using a postgres feature that rails schema.rb file doesn't support so I've set config.active_record.schema_format = :sql to ensure that all aspects of my database schema are recorded. However, when I set this option rails seems to stop updating the schema.rb file.
Since schema.rb is far far easier to read and I really only need structure.sql to capture the array_to_string function needed to generate a certain fulltext index I'd like to have migrations automatically update schema.rb as well as structure.sql.
Is there any way to get rails to update both when I run db:migrate without (as I've been doing) changing the config option by hand and then rerunning db:migrate?
Ohh, I'm being an idiot. There are rake tasks db:schema:dump and db:structure:dump. All I have to do is to create a new task that has dependencies on db:migrate and both db:structure:dump db:schema:dump or just run them both on the command line.
I don`t really understand what the difference is between a schema and a migration. Is a schema created by running a migration?
Rails schema.rb file represents the current state of the database.
It is generated and update when you run migrations with
rake db:migrate
Schema.rb is used when you do rake db:test:prepare or rake db:schema:dump, rake db:schema:load
Migrations are a convenient way for you to alter your database in a structured and organized manner. Active Record Migrations
I recently started using Rails, and created a few Models using the CLI which in turn created some migrations.
I ran the rake db:migrate command after adding all my columns in there, and then realized that I'd left out the associations.
So what did I do?
I went ahead and edited the migrations to include those keys.
I ran rake db:migrate again, and nothing changed in the schema.
Then I ran rake db:reset and then rake db:setup.
When that didn't work, I deleted my schema.rb (the darn thing wouldn't get updated!) and tried recreating it. When I realized that didn't work, I dropped the database, and killed the schema.
Now I'm stuck with some manually modified migrations, no schema.rb and no database.
How do I get the modified migrations to generate a schema, and play nice with Rails?
In development it does not matter to drop and rebuild your database. I do it often and I even have a rake task for that. The 3 command to chain are:
rake db:drop
rake db:create
rake db:migrate
# And a 4rth optional command to rebuild your test database
rake db:test:prepare
With this you should be good
Next time you need to modify a migration manually after migrating it, you should process by:
rake db:rollback
edit your migration
rake db:migrate
Following those steps will save you some headaches
Bonus info:
After you deployed your migration to your production server you cannot manually modify it, hence you must write another migration that will perform the modification (adding columns, etc...)
In my endless stupidness I changed the mysql db using mysql and not migrations, so now the db is out of sync with migration.
my question is, if it's possible to generate the missing migrations (step) and a new schema.db without loosing data and the changes* in the db?
*changes like adding tables, columns.
thx
As far as recreating your migrations, you're out of luck but you can recreate the schema
rake db:schema:dump
If you go this route, when creating a new db (for a new environment etc...) you'll want to do
RAILS_ENV=some_env rake db:schema:load # specify the env if not development
instead of
rake db:migrate
since your migrations do not align with the current schema.
Be careful when running schema:load as it recreates the db from scratch. i.e. you'll lose all data.
It will be good to create the missing migrations. You can fix your local dev database by adding the timestamps to the schema_migrations table manually. That is the consequence of changing your schema by hand.
Also make sure that rake db:migrate:reset (drop all tables and migrate from scratch) will produce the same db/schema.rb as rake db:schema:dump would. Any change in the database schema must be automated by a migration.
The problem with rake db:schema:load is that it will forcefully create the tables.
Due to some deployment issues I stopped tracking schema.rb in git. Somehow I have stuffed this up and somewhere along the way my schema.rb file has disappeared.
Is there a way of regenerating schema.rb from the database or from the migrations? I would prefer not to lose the existing data.
If you run a rake -T it will list all possible rake tasks for your Rails project. One of them is db:schema:dump which will recreate the schema.rb for the Rails app from the database.
bundle exec rake db:schema:dump
Careful,
rake db:schema:dump
will dump the current DB schema FROM the DB. This means that if you made any changes to your migrations, they will NOT be reflected in schema.rb file which is not what you want IMO.
If you want to re-create the schema from the migrations, do the following:
rake db:drop # ERASES THE DATABASE !!!!
rake db:create
rake db:migrate
RAILS 5 Way:
rails db:schema:dump
or if you Encounter Gem::LoadError then:
bundle exec rails db:schema:dump
Note:
in rails 5 it is recommended that task are generated/executed by using rails instead of rake, this is just to remember, rails generated task are of extension .rake see in lib/tasks/myTask.rake. which means these task can also be executed by prepending rake.
rake db:schema:dump
I think this is still valid in Rails 3 - it regenerates the schema.rb from the database.
Directly from the schema.rb file itself:
If you need to create the application database on another
system, you should be using db:schema:load, not running all the migrations
from scratch. The latter is a flawed and unsustainable approach (the more migrations
you'll amass, the slower it'll run and the greater likelihood for issues).
So do NOT do the suggestion of rake db:migrate, which was suggested in the - at the time of this writing - lowest rated answer.
If you regenerate schema.rb locally, you should be alright. It simply holds a representation of the structure of your database tables. The data itself is not contained in this file.
To regenerate your schema.rb file, run:
bundle exec rake db:schema:dump
Then simply commit the new schema.rb file and you should be in good shape!
I also had a similar problem where my old schema was not refreshing even if I deleted migration.
So, what I did was dropping all existing tables in the database and migrating them again. Then running "db:schema:load" command gave me a fresh schema.rb.
drop table my_table_name // deleted them individually
rake db:migrate
rake db:schema:dump // re-created a new schema