Forum: Ruby on Rails Database strong structure change: migrations

C32b9373f23914f8448630fe28ac928e?d=identicon&s=25 Martin (Guest)
on 2014-04-16 19:15
(Received via mailing list)
Hi there,

I have some major structure change issues with an ruby on rails
application, and I'd like to ask you about how i can handle that.  I
will
make some dummy problems for you to show what i want to reach:

1. Having a Client model with first_name and last_name should now come
together as name:
For the model it would be easy to have just

def name
   first_name + ' ' + last_name
end

That works, but now I want to change the database this way, so we dont
need
the first_name and last_name anymore:

I could create a migration having a name:string:index field.
but after that - how can I make all the first_name and last_name applied
there, and remove the first_name and last_name fields?

2. same for appointments:
We had a start_datetime and an end_datetime which until now was enaugh.
but now we want to change it to
planned_start_time end actual_start_time (same for end time)

on the migration all the old data from start_datetime should be put into
both (for historical data) planned end actual times.  For the future
this
will differ.

How can I make this migrations?

If I know about that later there will be some table changes, but maybe I
understand the routines for that.
Is there also any sql way doing it with rails?  For sql itself it would
be
easy with INSERT..SELECT, so maybe I could generate migrations with sql
code?

thanks,
Martin
C32b9373f23914f8448630fe28ac928e?d=identicon&s=25 Martin (Guest)
on 2014-04-16 20:13
(Received via mailing list)
Finaly the docs have it, so this is obsolete.
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2014-04-16 20:39
Martin wrote in post #1143301:
> Hi there,
>
> I have some major structure change issues with an ruby on rails
> application, and I'd like to ask you about how i can handle that.  I
> will
> make some dummy problems for you to show what i want to reach:
>
> 1. Having a Client model with first_name and last_name should now come
> together as name:
> For the model it would be easy to have just
>
> def name
>    first_name + ' ' + last_name
> end
>
> That works, but now I want to change the database this way, so we dont
> need
> the first_name and last_name anymore:

IMHO that is a bad idea. Chances are likely that you would come to
regret that decision. If you ever use either first_name or last_name
separately from each other then you violate First Normal Form (1NF). The
bigger issue is that once you decide to concatenate these two values in
to a single field there's no going back. There is no reliable algorithm
that can separate the two distinct values.

I say if you already have separate first and last names keep it that way
and add full_name method to the model. That costs you nothing really and
preserves flexibility in your app.

> I could create a migration having a name:string:index field.
> but after that - how can I make all the first_name and last_name applied
> there, and remove the first_name and last_name fields?

Whatever you decide, you are not limited to the ActiveRecord helpers in
your migrations. You can run any arbitrary SQL you want using the
execute method:

http://guides.rubyonrails.org/migrations.html#when...
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.