2

I have following two migrations:

One, Add column contextual_page_number to transcripts table:

class AddContextualPageNumberToTranscripts < ActiveRecord::Migration[5.2]
  def change
    add_column :transcripts, :contextual_page_number, :integer, default: 1
  end
end

Second, changing the value of the previous added column contextual_page_number based on value of another column:

class ChangePageOffsetAndContextualPageNumberOfTranscripts < ActiveRecord::Migration[5.2]
  def up
    Firm.all.find_in_batches do |group|
      group.each do |firm|
        Apartment::Tenant.switch(firm.tenant) do
          Transcript.where.not(page_offset: 0).each do |transcript|
            transcript.update(
              contextual_page_number: ((transcript.page_offset - 1) * -1),
              page_offset: 1
            )
          end
        end
      end
    end
  end

  def down
    ..
  end
end

After running the migration, I am getting unknown attribute contextual_page_number error.

== 20211108132509 AddContextualPageNumberToTranscripts: migrating ============= -- add_column(:transcripts, :contextual_page_number, :integer, {:default=>1}) -> 0.0095s == 20211108132509 AddContextualPageNumberToTranscripts: migrated (0.0096s) ====

== 20220113095658 ChangePageOffsetAndContextualPageNumberOfTranscripts: migrating rails aborted! StandardError: An error has occurred, this and all later migrations canceled:

unknown attribute 'contextual_page_number' for Transcript.

I have even tried reset_column_information, but no luck:

Apartment::Tenant.switch(firm.tenant) do
  Transcript.connection.schema_cache.clear!
  Transcript.reset_column_information
  ..
end

Any clue would be of great help, thanks.

Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61
  • Does this answer your question? [How do I add some inserts in rails migration?](https://stackoverflow.com/questions/2667580/how-do-i-add-some-inserts-in-rails-migration) – marmeladze Jan 13 '22 at 19:08
  • Does schema.rb include contextual_page_number after you run the migrations? Also, can you access the contextual_page_number attribute in the console? – idmean Jan 16 '22 at 07:47

5 Answers5

1

As mentioned in one of the answer, I tried reset_column_information just right after the add_column, but that didn't worked. Finally, SQL to the rescue..

sql_cmd = "UPDATE transcripts
           SET contextual_page_number = ((page_offset - 1) * -1),
               page_offset = 1
           WHERE page_offset != 0"

Transcript.connection.execute(sql_cmd)
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61
0

You need two migration files. First, try running the migration and check schema.rb for the table transcripts and verify that the newly added column contextual_page_number is being added or not.

Once you are sure that your new column is added, then again create a new migration like, eg: MigrateTransriptsCloningsData, and then add the desired changes in the up block, then execute db:migrate to update the required changes.

My choice would be To add a new rake task and executing it. like bundle exec rake migrate_transcripts_data:start instead of keeping that logic in the db/migrate/your_new_migration_file, choice is yours.

Milind
  • 4,535
  • 2
  • 26
  • 58
0

reset_column_information should be the correct way to resolve this sort of problem if you want to use models in a migration. This isn't without its problems though.

I suspect the issue is that you are calling it too late somehow. Put it first thing in the up method of the second migration or after the add_column in the first migration.

matthew.tuck
  • 1,267
  • 9
  • 11
0

I may assume that the issue is in Apartment.tenant_names. In the second migration, you are switching tenants by Apartment::Tenant.switch(firm.tenant), but I do not see similar in the first migrations. Probably tenant names are in DB, not in configs. I am pretty sure that you may find samples of the appropriate add_column in your previous migrations.

Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
0

Do not use structure migrations to modify data.

Use rake tasks, or data-migrate gem instead.

Also, do not use automatic data migrations, if you not ensure, that it working as expected on production server. Always store data before modifications and write modification rollback code.

Sergio Belevskij
  • 2,478
  • 25
  • 24