I was told that I should follow the below steps if make new migrations in my branch and have merge conflicts with master in the db/structure.sql file.
In the branch, bundle exec rake db:drop
In the branch, bundle exec rake db:create
In master, bundle exec rake db:structure:load
In the branch, git merge master
In the branch, bundle exec rake db:migrate
What are steps 1-3 necessary if all I want to do is align the db/structure.sql file? By merging in master, don't I get the new migrations I haven't ran yet and then by running them, it will update my db/structure.sql?
You're right, dropping and recreating the database to solve a conflict in db/structure.sql (or db/schema.rb for that matter) is a little ridiculous. You should be able to simply run the new migrations and get an updated structure.sql from the db:migrate.
The db/stucture.sql file is simply the database's structure as the database sees it (whereas db/schema.rb is the database's structure in the limited view of it that ActiveRecord has). If there is a conflict in structure.sql, that simply means:
The merge involves new migrations which change the database structure.
The branch you're merging in has run the migrations in a different order so the schema's don't quite match up even though they may be functionally equivalent.
(1) is solved by running the new migrations and possibly fixing any places where the migrations themselves are in conflict. A quick bin/rake db:migrate should fix this and leave you with a new non-conflicted db/structure.sql.
(2) is solved the same way. You could also do a a manual bin/rake db:structure:dump to rebuild db/structure.sql but you'd only do this if you're certain that you really do have this situation; but really, db:migrate will take care of it so there's no reason not to just db:migrate.
Conflicts in db/structure.sql (or db/schema.rb) don't indicate a problem with the db/structure.sql itself, they indicate problems with the database that git can't directly see. The solution to the conflicts is to fix the database.
You can just run bundle exec rake db:structure:dump to re-generate the db/structure.sql file.
For schema.rb I found https://stackoverflow.com/a/3815807/6003161
Related
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...)
How do I undo bundle exec rake db:setup?
I ran it in the wrong rails app. I ran it in the blogger when I should have ran it in the blogger_advanced app.
You can do rake db:drop
.
It will drop all tables (thats any tables created by setup, any migrations run by setup and any seeds created by setup)
You may use rake db:rollback and it'll rollback migrations one by one, but if yours apps have same named models (User for example) it'll be lost. So you need to dump this tables first and recreate them later using database administrator tool.
And also it'll broke your schema_migrations table and you'll need to refill it by hands from your migration file names.
So if there is not much useful info, it's better to use #Alexphys approach.
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
After rake db:migrate:rollback STEP=1, rake db:migrate:reset, rake db:migrate:setup, rake db:migrate:up VERSION=XXXXXXXXX I get the following entry:
Status Migration ID Migration Name
------------------------------------------------------
up 0 *********NO FILE**********
up 20120209023430 Create tasks
How can I get rid of the orphaned entry? I have encountered this problem a few times after raking the db similar to above. Could somebody please explain what exactly causes this.
Thx in advance.
Shahram
You could use rake db:migrate:reset db:seed. It's a little less verbose and will completely wipe your database, re-run all migrations, and then seed your database from the db/seeds.rb file.
I've just released this gem that can solve this issue for good.
The idea of that gem is simple. It keeps all migrated migrations inside tmp folder so that Git ignores them. It's just only your local story. These files are needed to roll back the "unknown" migrations being in another branch. Now, whenever you have an inconsistent DB schema due to running migrations in some other branch just run rails db:migrate inside the current branch and it will fix the issue automatically. The gem does all this magic automatically for you.
I created a migration, ran rake db:migrate, which bumped my db/schema.rb version number. Then I did a git fetch origin master and saw that there were changes from my team members. So I did a git stash and a git rebase FETCH_HEAD, followed by a git stash pop. This resulted in a conflict in db/schema.rb over the version number.
Upstream>>>
ActiveRecord::Schema.define(:version => 20110930179257) do
===========
ActiveRecord::Schema.define(:version => 20110930161932) do
<<<Stashed
I think the appropriate fix would be to manually increment the version number to something higher than the upstream.
Is this sensible, or bad news?
Thanks,
Max
If your current database has the correct schema, you should:
Run pending migrations (if any)
rake db:migrate
Overwrite your schema.rb from your current database schema
rake db:schema:dump
And commit
When I find myself with this conflict, I simply migrate the database. Whether there are pending migrations or not, the conflict will be corrected.
tldr
Accept the Upstream version and run rake db:migrate as you'd normally do.
why is that the way to go
Don't worry about the migrations you've created (which are below Upstream version 20110930179257). ActiveRecord uses a table schema_migrations where it puts all of the migrations that have been run. If your migrations aren't on the list but in db/migrate directory, then ActiveRecord will run them.
Here's the table so you can visualise it better:
It's tempting to think that it's actually this line:
ActiveRecord::Schema.define(:version => 20110930179257)
that defines latest migration run, so no migrations with version below it are going to be run. This is fortunately not true. Rails will run any migrations that are in db/migrate folder and not yet in the schema_migrations table.
According to this answer, a conflict is guaranteed. The user has to to manually merge, and set the version as the higher of the two.
Here's what I do when merging master into my feature branch fails over conflicts in db/schema.rb:
$ git merge --abort
$ git checkout master
$ rake db:drop db:create db:migrate
$ git checkout -- db/schema.rb
$ git checkout my_feature_branch
$ rake db:migrate
$ git add db/schema.rb
$ git commit -m 'Updated schema'
$ git merge master
~/bin/update-schema-rb:
#!/usr/bin/env bash
git co master
bin/rake db:reset db:seed
git co -
bin/rake db:migrate
An option is to have a manual version script which is additive only. There you want conflicts. Schema is something that will bite you hard if you don't keep on top of it. So once bitten, twice shy, I don't mind keeping schema and lookup info (list of customer types) with an additive only change management scheme.
I feel the best approach is to do rake db:drop db:create db:migrate to regenerate schema using migrations that exist only on the current branch. That way migrations from other branches won't leak into your schema file and give you headache later - in case if you switch branches a lot.