We have this gem that recently got a version boost which included a lot of new migrations.
What seems to be the consensus when running new migrations on apps with existing installations without having to wipe the database and start again?
At the moment our install_generator just copies in the updated migration.rb file but immediately conflicts with apps that already have the old tables.
So I'm looking for the cleanest way to handle gem updates that include new migrations.
Migrations inside gems are supposed to be written in a way such that while running new migrations, you don't have to delete old tables. Which gem are you using?
Related
After upgrading from rails 5.1 to rails 6.1 the entries are getting deleted from the schema_migrations table except for the last entry.
I am facing this issue in all the environments. For the test environment, I fixed the test environment by commenting on this line ActiveRecord::Migration.maintain_test_schema! but in the development and non-production environment, I am still facing the issue.
I try to google it but I don't see any article related to it. Could anyone give some insight on it?
Rails 6.1.0
ruby 3.0.0
activerecord-oracle_enhanced-adapter 6.1.4
Database Oracle
Just delete the migrations if you have no pending changes.
https://edgeguides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you
Migrations, mighty as they may be, are not the authoritative source
for your database schema. Your database remains the authoritative
source. By default, Rails generates db/schema.rb which attempts to
capture the current state of your database schema.
It tends to be faster and less error prone to create a new instance of
your application's database by loading the schema file via bin/rails
db:schema:load than it is to replay the entire migration history. Old
migrations may fail to apply correctly if those migrations use
changing external dependencies or rely on application code which
evolves separately from your migrations.
I have made quite a large upgrade to my app. I have older version deployed on Heroku at the moment. Problem is that I have added/removed quite few migrations in the process of making my app more modular. I do not want to lose my registered user table that is already up on Heroku while deploying the update. Are there any tips someone can offer on how to preserve my user table while upgrading the app? I do have backup add-on installed but I have no clue what to do with that file.
You should then setup your migrations more carefully since that one, which is last presented on the heroku server. So, when:
you are adding a column, just setup defualt value
you remove the column, you shell export data to (for example) YAML, and store it in the tmp/.
you are reconstructing parts of the colunms by adding and remove some of them, in migration carefully copy required data from older column (prepared to deletion) to newly created ones.
If you're that concerned about deploying I think you might need to declare migration bankruptcy.
Create a new migration that drops the current schema and copy the new database schema into it
Dump out the contents of your database as CSV ( https://coderwall.com/p/jwtxjg/simple-export-to-csv-with-postgres ) or using Rails to dump in some other way (YAML etc.)
Write a Rake task to parse the data dump into your new schema
Set up a new database on Heroku Postgres (this does not delete the old data)
Do a dry run locally to make sure it all works
Promote the new database to DATABASE_URL
Deploy, migrate, run the Rake task
Not very nice and not something you can roll back from easily if it goes wrong.
When a rails project grows a lot, you can find yourself having trouble with fixes for the data in the production database.
I have normally used migrations or specific rake tasks for this, but I was wondering if a system similar to migrations existed for keeping the database fixes and run them when needed.
I know you probably figured this out by now but there IS a gem for this... it is called datafix
https://github.com/Casecommons/datafix
basically you create a datafix, like a migration, and a spec for it, then you can run it as needed on the server.
The following gems can be also used for this purpose:
nondestructive_migrations
datafix
migrake
migration-data
data-migrate
I prefer nondestructive_migrations and datafix they are very similar - nondestructive_migrations simpler implementation building on rails migrations.
Is there an easy way to drop tables created by a gem when I am installing it? For my specific case I want to uninstall the gem and reinstall it but the old tables and data are still there. Right now I plan on dropping the created tables manually. Is there a way to see all the tables created by a gem?
There is no easy way to see all the tables created by a gem, unless they're namespaced within something sensible. For example, Forem namespaces all the tables with a forem_ prefix.
You would need to create a migration and drop the tables manually.
This is probably a stupidly easy question for a Rails person but is causing me no end of confusion. I downloaded several open source Rails projects but am not able to run them. Usually, are you supposed to do a db:migrate before you try to run a Rails project? I thought they were supposed to just run.
I guess it depends on how the database is configured. If it's pointing to a sqlite db, then its probably all ready to go, otherwise if its a full blown RDBMS, then yes the database would need to be migrated assuming of course that the settings in database.yml are configured correctly.