How To Modify / Update a Table?

Ok this is a general query. How do I update or modify a table and its
accompanying files?

For example : I do a ruby script/generate scaffold TABLE_NAME
FIELD_NAME1:TYPE FIELD_NAME2:TYPE

I then do a rake db:migrate and everything is generated. Cool.
But now I realized I want to rename FIELD_NAME2 to FIELD_NAMEX ; what do
I do?

Right now I go in and

  1. delete every file generated by the previous generate and rake
  2. then do the generate with renamed field
  3. then rake all over again.

Now this almost always creates an error and it starts complaining that
rake aborted table exists or it doesnt and in this case it is giving
this error :

C:\ruby\letter4sure>rake db:migrate version=29 --trace
(in C:/ruby/letter4sure)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
uninitialized constant CreatePsoAccounts
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:266:in
load_missing_constant' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:inconst_missing’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in
const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/inflector.rb:257:inconstantize’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/string/inflections.rb:148:in
constantize' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:386:inmigration_class’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:363:in
migration_classes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:286:ininject’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:in
each' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:ininject’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:in
migration_classes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:inmigrate’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:311:in
down' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:300:inmigrate’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/tasks/databases.rake:85
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in call' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in each' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in invoke' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:insynchronize’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in invoke' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:intop_level’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in each' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:intop_level’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in
standard_exception_handling' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:intop_level’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in run' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:instandard_exception_handling’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in run' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7 c:/ruby/bin/rake:16:inload’
c:/ruby/bin/rake:16

Now this is awful. What I am doing wrong here? What I usually do when
rake starts acting up is start deleting table and going back down the
migration version as you can see above; but this one is still giving me
the same error. Why is that?

Hey,

I think an easier way would be to generate a new migration file with
“ruby script/generate migration RenameOldFieldNameFromTableName”.
Inside the “self.up” part of the migration file, you need to use the
“rename_column :TableName, :OldFieldName, :NewFieldName”. The
“self.down” part should probably do the opposite
“rename_column :TableName, :NewFieldName, :OldFieldName”.

Once you finished that migration file, you can execute the migration
with “rake db:migrate”.

The following link gives an overview of all possible migration
commands:

Falk

On 8 Mai, 17:34, Ather S. [email protected]

you get this error often if your classname doesn’t match the filename
(eg often happens if you copy from one file into another)

if your classname is:
CreatePsoAccounts

about this error complains, then your file MUST be named like

00x_create_pso_accounts.rb

(the number is variable of course, the rest not)

how to handle such changes depends on your project stage
if it’s in the beginning and no live system with real data exists, you
can work as you do, just correct the old migrations.

but if there is a db with real data, then you must use

change_column
rename_column
and so on.
rails has a full set of commands to change your db structure

(another reason not to change old migrations is if you work in a team
and other people have to use your code. then they often forget to
rebuild the db completely after updating their code.)

The classname in db/migrate is actually CreatePSOACCOUNTs!

I changed it to CreatePsoAccounts.

Then I did a rake db:migrate again and had to drop a table and this time
I get the following name creation problem. Although I typed PSOAccounts
rails decided to name it psoaccoun_ts ! what is that?

This synch. is still too inefficient, mechanical , brittle and
complicated. I would like to work with someone in upgrading the rails
plugin to automatically synchronize the model view with the db schema
and the reverse.

Is anyone working on this or would like my input or assistance. I come
from a Java/J2ee background.

Shiraz

Thorsten M. wrote:

you get this error often if your classname doesn’t match the filename
(eg often happens if you copy from one file into another)

if your classname is:
CreatePsoAccounts

about this error complains, then your file MUST be named like

00x_create_pso_accounts.rb

(the number is variable of course, the rest not)

how to handle such changes depends on your project stage
if it’s in the beginning and no live system with real data exists, you
can work as you do, just correct the old migrations.

but if there is a db with real data, then you must use

change_column
rename_column
and so on.
rails has a full set of commands to change your db structure

(another reason not to change old migrations is if you work in a team
and other people have to use your code. then they often forget to
rebuild the db completely after updating their code.)

Great its out of synch. again this time it keeps attempting to drop a
table that does not exist! ? what do I do now? I have gone back some
versions… should I create this table and see if it drops it?

This is not “joyful” , “painless” programming!

Is it?

Ather S. wrote:

I dread moving the application to the deployment server. Capistrano is
not setup because deployment is on a windows server 2003 and synching
dbs etc. is a problem.
Am in too much of a hasty blurr to remember why but always often have
trouble migrating to the production server with rakes.

I am serious about developing a useful GUI for rake. If anyone is
interested we could work on making it more resilient and not so prone to
errors.

Murphyslaw wrote:

Hey,

I think an easier way would be to generate a new migration file with
“ruby script/generate migration RenameOldFieldNameFromTableName”.
Inside the “self.up” part of the migration file, you need to use the
“rename_column :TableName, :OldFieldName, :NewFieldName”. The
“self.down” part should probably do the opposite
“rename_column :TableName, :NewFieldName, :OldFieldName”.

Once you finished that migration file, you can execute the migration
with “rake db:migrate”.

The following link gives an overview of all possible migration
commands:
ActiveRecord::Migration

Falk

I dread moving the application to the deployment server. Capistrano is
not setup because deployment is on a windows server 2003 and synching
dbs etc. is a problem.
Am in too much of a hasty blurr to remember why but always often have
trouble migrating to the production server with rakes.

I am serious about developing a useful GUI for rake. If anyone is
interested we could work on making it more resilient and not so prone to
errors.

Murphyslaw wrote:

Hey,

I think an easier way would be to generate a new migration file with
“ruby script/generate migration RenameOldFieldNameFromTableName”.
Inside the “self.up” part of the migration file, you need to use the
“rename_column :TableName, :OldFieldName, :NewFieldName”. The
“self.down” part should probably do the opposite
“rename_column :TableName, :NewFieldName, :OldFieldName”.

Once you finished that migration file, you can execute the migration
with “rake db:migrate”.

The following link gives an overview of all possible migration
commands:
ActiveRecord::Migration

Falk