Migrations

36 views
Skip to first unread message

Joe Guerra

unread,
Sep 5, 2017, 8:03:56 AM9/5/17
to Ruby on Rails: Talk
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

Rolandas Barysas

unread,
Sep 5, 2017, 8:35:17 AM9/5/17
to rubyonra...@googlegroups.com
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).



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/58c11dac-ec57-48cd-9fea-7eb4928cdcb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Rolandas

Joe Guerra

unread,
Sep 5, 2017, 9:31:28 AM9/5/17
to Ruby on Rails: Talk
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.  



On Tuesday, September 5, 2017 at 8:35:17 AM UTC-4, Rolandas Barysas wrote:
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).

On Tue, Sep 5, 2017 at 3:03 PM, Joe Guerra <JGu...@jginfosys.com> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.



--
Rolandas

Norbert Melzer

unread,
Sep 5, 2017, 11:09:22 AM9/5/17
to rubyonra...@googlegroups.com
On 09/05/2017 03:31 PM, Joe Guerra wrote:
> 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.



signature.asc

Joe Guerra

unread,
Sep 5, 2017, 11:16:43 AM9/5/17
to Ruby on Rails: Talk
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?  

Norbert Melzer

unread,
Sep 5, 2017, 11:49:10 AM9/5/17
to rubyonra...@googlegroups.com
On 09/05/2017 05:16 PM, Joe Guerra wrote:
> 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?

signature.asc

Joe Guerra

unread,
Sep 5, 2017, 11:52:18 AM9/5/17
to rubyonra...@googlegroups.com
the database isn't in production, it has a bunch of faker data in it.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ab4fafd2-e39c-6c28-5b1e-30849dc83cd0%40gmail.com.

Norbert Melzer

unread,
Sep 5, 2017, 12:03:02 PM9/5/17
to rubyonra...@googlegroups.com
On 09/05/2017 05:51 PM, Joe Guerra wrote:
> 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.

signature.asc

Joe Guerra

unread,
Sep 5, 2017, 1:03:14 PM9/5/17
to rubyonra...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.

Joe Guerra

unread,
Sep 5, 2017, 4:38:49 PM9/5/17
to Ruby on Rails: Talk
ok, I recreated the migration and ran it.  It fixed my problem.  (but I have 2 that do the same thing).

Hassan Schroeder

unread,
Sep 5, 2017, 5:11:55 PM9/5/17
to rubyonrails-talk
On Tue, Sep 5, 2017 at 1:38 PM, Joe Guerra <JGu...@jginfosys.com> wrote:
>
> 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...

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Joe Guerra

unread,
Sep 5, 2017, 7:26:30 PM9/5/17
to rubyonra...@googlegroups.com
Right,  the first one was commented out.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages