Migrations

Just wondering if I can clean up some of migrations and merge them with others.

I've added a few things here and there to my database tables over time.

Thanks,
Joe

People might have different opinions about this, but this is what I do:

If website is not deployed yet, then whatever, you can edit or delete existing migrations, then destroy current database and run rails db:migrate to rebuild database schema. I do this from time to time when trying out different things.

If website is deployed to production, you really shouldn’t edit your existing migrations, because that goes against the whole idea of database migrations. Also those migrations simply won’t run (that migration you edited, its version would already be in schema_migrations table).

But I think it’s fine to delete old migrations (that are deployed and do not cause any issues). Your database schema file (db/schema.rb) is what defines your current database state, so after migrations are ran there are really no use for them anymore. In the worst case you’ll still have those migrations in version control (if you use one - I hope you do).

Thanks, I do have some junk in my schema that I should clean up by removing some older migrations that I’m not using in my current web app.

No, you should clean that unneeded stuff in your current schema by
*creating* migrations which remove the superflous tables/columns.

This way you can incrementaly move from any DB-schema version to any
other in either direction.

Well, I wasn’t going to touch the schema file directly. I thought about merging some migrations and deleting some that aren’t being used.

If I deleted the database and re-ran the migrations wouldn’t it update the schema?

Yes, but it would bring the databases of everyone else out of sync.
Migrations are a history of changes made to your schema to (re-)create
the actual one.

Its just like version controll for your code. You won't delete your .git
just because you had a type somewhere, won't you?

the database isn’t in production, it has a bunch of faker data in it.

I do not care… You are taking huge risks by changing old migrations and
re-running from scratch. This way you might have deleted to much and
therefore your model might get out of sync with the table.

While when you add a single migration which is responsible to clean up
your schema, you can easily roll it back if you realise that it didn't
work as expected. This way back is not easily possible when you alter
your migrations history.

oh ok, I didn’t realize that.

I do have one migration that I need to run that alters the name of the field in a table. I accidentally capitalized the first character, and it caused all sorts of issues. I can’t seem to run that migration.

Thanks,

Joe

ok, I recreated the migration and ran it. It fixed my problem. (but I have 2 that do the same thing).

Why would you have two migrations doing the same thing? If the
first didn't produce the result you want...

Right, the first one was commented out.