fatkodima / online_migrations

Catch unsafe PostgreSQL migrations in development and run them easier in production (code helpers for table/column renaming, changing column type, adding columns with default, background migrations, etc).
https://rubydoc.info/github/fatkodima/online_migrations/
MIT License
628 stars 19 forks source link

rename_table_create_view for long table names #59

Closed dudleysr closed 2 months ago

dudleysr commented 2 months ago

We've been getting an ArgumentError: Table name 'too_long_name' is too long; the limit is 63 characters for a initialize_column_rename migration recently where the table does exist in our Postgres DB. I've been able to reproduce the error in a test. You can see the table name has 62 characters. I'm hoping to have time to do something along the lines of taking a substring of _column_rename based on 63 - table_name.length (would need to handle an exact 63 length table, too) but wanted to raise the issue!

# frozen_string_literal: true

require "test_helper"

module SchemaStatements
  class RenamingColumnsTest < Minitest::Test

    class AccountingIntegrationQuickBooksDesktopIntegrationDetails < ActiveRecord::Base
    end

    def setup
      @connection = ActiveRecord::Base.connection
      @schema_cache = @connection.schema_cache
      @connection.create_table(:accounting_integration_quick_books_desktop_integration_details, force: true) do |t|
        t.string :fname, index: true
        t.string :lname, index: true
      end
    end

    def teardown
      # Reset table/column renames.
      OnlineMigrations.config.column_renames.clear
      @schema_cache.clear!

      @connection.execute("DROP VIEW accounting_integration_quick_books_desktop_integration_details") rescue nil
      @connection.drop_table(:accounting_integration_quick_books_desktop_integration_details, if_exists: true)
      @connection.drop_table(:accounting_integration_quick_books_desktop_integration_details_column_rename, if_exists: true)
    end

    def test_old_code_uses_original_table_for_metadata_long_name
      long_column_renames("fname" => "first_name")

      @connection.initialize_column_rename(:accounting_integration_quick_books_desktop_integration_details, :fname, :first_name)
      AccountingIntegrationQuickBooksDesktopIntegrationDetails.reset_column_information
      @schema_cache.clear!

      assert_equal "id", AccountingIntegrationQuickBooksDesktopIntegrationDetails.primary_key

      assert_not_empty AccountingIntegrationQuickBooksDesktopIntegrationDetails.columns
      assert_not_empty AccountingIntegrationQuickBooksDesktopIntegrationDetails.columns_hash
      assert_not_empty @schema_cache.indexes("accounting_integration_quick_books_desktop_integration_details")
    end

    private

    def long_column_renames(renames)
      OnlineMigrations.config.column_renames["accounting_integration_quick_books_desktop_integration_details"] = renames
    end
  end
end
fatkodima commented 2 months ago

@dudleysr Can you please confirm that the code from master actually fixed the issue?

dudleysr commented 2 months ago

@fatkodima Working for me! Thanks for the quick turnaround!

fatkodima commented 2 months ago

Nice! Will push one more fix and release a new version tomorrow or before Monday.