Why does rake throw this Rails migration error?

I have two cars ... a development machine and a production machine. When I first brought the rails application to a production server, I had no problems. I just imported schema.rb by running rake db: schema: load RAILS_ENV = production. All was good.

So, then on my development machine, I made a few changes and another migration, and then copied the new application to the production machine. Then I tried updating the database by doing rake db: migrate RAILS_ENV = production. I get the following error: "The database already has an object named" schema_migrations ".

I think to myself, I'm not joking, Rake ... you created it! I ran after the rake, and it seems that the rake thinks this is the first time he has ever run. However, by analyzing the schema_migrations table on my development machine and my production machine, you can see that there is a difference in one migration, namely the one I want to migrate.

I also tried to explicitly determine the version number, but this does not work either.

Any ideas on how I can upgrade my production server?

Update:

Let me start by saying that I cannot just drop the database. This is a production server with just over 100 thousand records. What happens if a similar problem arises in the future? Am I just throwing a table every time there is a database problem? This may work this time, but it doesn't seem like a practical long-term solution to every database problem. I doubt that my problem is unique to me right now.

  • It looks like the schema_info table and the schema_migrations table are the same. In my setup, I only have "schema_migrations". As stated earlier, the difference between the schema_migrations table on the production server and the development machine is just one record. That is, a record containing the version number of the change I want to transfer.

  • From the book I read “Simply Rails 2,” it says that when you first go to the production server, instead of running rake db: migrate, you just need to run rake: db: schema: load.

  • If that matters, I'm using Rails version 2.1.

+4
source share
9 answers

This is a guess, I admit: I think that since you first ran db: schema: load instead of db: migrate in your production environment, you got the structure of your db, but not the data that is being transferred is populated into your schema_info table. So, now when you start the migration in the production environment, there is no data in the schema_info, so migrate believes that it is not running yet (because it does not).

That says ... you say that you looked at the schema_migrations table and that there is a difference in one version from the developer to production ... I have not heard about this table, although I did it on my rails a few months ago. Perhaps you could try creating a schema_info table in a production environment with one version column and adding a row with the version in which you think your production environment is turned on.

+1
source

Regarding your update:

  • I do not understand what is the difference between your production_migrations schema and the dev version. Is there a record in both tables (there should be only one column, "version", on the right) or is there one record in dev DB and zero records in production? If the worksheet has null entries, do the following:

    ActiveRecord::Base.connection.execute("INSERT schema_migrations (version) VALUES(#{my version number that production is supposedly on})")

  • Alternatively, you can try to completely exclude the schema_migrations table when creating:

    ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations")

    Then run rake db:migrate RAILS_ENV=production again. This will trigger migrations starting from version 1, although this is probably not what you need.

  • Alternatively, you can start an IRB session in your production environment, either perform a “request” or “download” (I cannot remember which or if it matters) the migration file you want to download, and then call MyMigrationClass.up . After that, you will need to manually set the version number in the schema_migrations table, since you will still have a problem in the future, but this will work as a quick hack type.

+1
source

If you get "The database already has an object named" schema_migrations "." error message, then I suspect that you are using MS SQLServer as a database? (Since this looks like an MS SQL Server error message)

If so, which ActiveRecord database adapter are you using? (What is your database.yml file, what stones are installed to access the MS SQL Server database?)

Currently, it seems that Rails does not find the schema_migrations table in the production diagram and therefore tries to create it, and this creation fails with a database error message. This is probably because the uppercase / lowercase characters in the schema_migrations table name are, as I understand it, MS SQL Server identifiers are case sensitive.

+1
source

Depending on the system used in production, I saw examples in which the following does not work:

 rake db:migrate RAILS_ENV=production 

But where does it work:

 RAILS_ENV=production rake db:migrate 

Bizarre, I know, but it's worth a try to make that change.

+1
source

I would just reset the database, add it again and run rake rb: migrate. Brad is right that when he started loading the schema, he did not put any records in the schema_migrations table.

This is more difficult, of course, if there is data that you cannot lose on the production server. You can get rake backup tasks (not sure if this is part of the kernel or not), then run rake db: backup: write to your production database, and then after you upgrade the migration to production, run rake db : backup:. read

0
source

schema_info is an old version of Rails. schema_migrations - the new child on the block. You should be able to delete the schema_info table as it will no longer be used. You will probably want to find any problems associated with this name change.

0
source

rake db: schema: load will load the database structure from schema.rb. This file is the current view of the database structure. It is used when you have an empty schema (database) that requires the creation of all tables and indexes. This eliminates the need to run all migrations. If you have an existing production database with data, you do not want to run it. As others said, that would be bad!

0
source

I know that this message was some time ago, but I stumbled upon it and it was not answered. How does this google go here.

When you did rake db: schema: dump (or when it was done for you by build scripts), it would put the definition of the migration table in schema.rb. At the end of the script, the process will try to create the table again, however, it obviously already exists. Just delete the migration table from schema.rb before running rake: schema: load and there will be no error message.

You will need to set the version number in the migration table to start the migration later. Therefore, it is important to know which version your schema.rb belongs to, or to delete all old migrations (are they safe in your SCM correctly?)

0
source
 rake db:migrate RAILS_ENV=production 

Use the db:schema:load task only for the first creation, incremental changes must be applied.

-1
source

All Articles