Reputation: 3172
I have a Rails-API app that I'm going to re-deploy after a long time. The app is non-production, but I am ready to deploy the production version. I want to basically delete all the migration files and start from scratch using the schema, is there any problem with this approach? Assuming I can do this, what do I need to alter the schema.rb to? Is this a common practice? Seems reasonable to me.
# what do I change the version param value to?
ActiveRecord::Schema.define(version: 20171129023538)
Upvotes: 1
Views: 902
Reputation: 584
To clarify, I am aware that the content of the rake task comes directly from the source, but I don't see the need for the [ ! -f src ]
conditional.
Perhaps I am missing something, but this should be enough:
# lib/tasks/migration_archive.rake
namespace :db do
namespace :migrate do
desc "Archives old DB migration files"
task archive: :environment do
sh "mkdir -p db/migrate/archive"
sh "mv db/migrate/*.rb db/migrate/archive"
end
end
end
Upvotes: 0
Reputation: 1
You do not need to keep old migrations. As per Rails own schema.rb file (in the comments), the DB should be recreated from the schema.rb and not from migrations.
You can clean out old migrations with a Rake task like this:
# lib/tasks/migration_archive.rake
namespace :db do
namespace :migrate do
desc 'Archives old DB migration files'
task :archive do
sh 'mkdir -p db/migrate/archive'
sh '[ ! -f src ] || mv db/migrate/*.rb db/migrate/archive'
end
end
end
Source: https://codewithrails.com/clean-up-db-migrations
Upvotes: 0
Reputation: 434575
Migrations are used to update your database or to rollback to previous versions. Once migrations have been run in all environments, they're never looked at again (unless you're manually messing around with the schema_migrations
table of course). Once all your migrations have been run, the current state of the database is in db/schema.rb
(or db/structure.sql
) so schema.rb
is all you need to start with a fresh database.
I tend to clear out old migrations every couple months just to cut down on clutter.
As far as the version goes, you can leave it alone. As long as it represent a time in the past (such as 2017-11-29 02:35:38) and it is lower than any migrations that need to be run (which doesn't apply as you're deleting them all) then it doesn't matter what it is.
Go ahead and clear out all the migrations, they're already reflected in schema.rb
and you can always dig them out of revision control if you really need them.
Upvotes: 3