Database strong structure change: migrations

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?

  1. 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

Finaly the docs have it, so this is obsolete.

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: