-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENV variable usage in migrations #60
Comments
I'm not sure. Apologies, we seem to have bungled the 3.0 release a bit - I've just pushed the tag for it. Here's the diff between the versions: v2.0.0...v3.0.0 From here it looks like we only applied Rubocop style adjustments and updated some of the dependencies, but clearly something else has gone awry... Looking at the code snippet: does |
Hello @nickcampbell18 . Thanks for the fast reply. I did try some options without success:
All of them failed with the result being nil which explodes on the gem verification for As my first thought i went into figuring out how rails Dunno what it could be as it seems weirdo to me what is happening exactly. If you give me some directions i can try to give it a try. EDIT: Could it be related to the way ruby define instance and class methods besides the new rails loading mechanism? |
OK so it turns out there were two separate issues here! Your first code was broken as I expected, because The code in your follow-up comments would have worked except for one crucial change - you need to cast your environment variable to an integer ( Here is some code which works (I ran it locally to test on a fresh Rails install): class Testing < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
set_statement_timeout(
ENV
.fetch("MIGRATION_20201014221845", 250)
.to_i
)
def up
create_table :foos
end
def down
drop_table :foos
end
end
|
Cool. Will check it out asap! Thanks for the detailed explanation. |
Gonna mark this issue as closed but please reopen if you encounter further problems! |
@nickcampbell18 I just checked what you suggested with no luck. What you said about casting to It seems to be related to how the method So first i thought about Tried changing to It does not matter if i define as a class method or as an instance method, it always raise So in the end what differs from version Ideas? EDIT: Can't reopen the issue. It seems this repo is configured to not allow it. |
Ok, i figured out how to fix my issue. As i stated in my last comment Since i was always defining a class method called class AddUserIdentifiersCompanyUniqueIndex < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
set_statement_timeout(statement_timeout)
def up
return if index_exists?(:user_identifiers, %i[company_id value])
add_index :user_identifiers, %i[company_id value], algorithm: :concurrently, unique: true, where: 'subsidiary_id is null'
end
def down
return unless index_exists?(:user_identifiers, %i[company_id value])
remove_index :user_identifiers, %i[company_id value]
end
def self.statement_timeout
ENV.fetch('MIGRATION_20201014221845', 250)
end
end it was not getting my method definition but rather the attribute reader of When i removed the DSL, it worked correctly. Now i'm just not sure if it is running in the context of The solution (which i don't like much because seems to much of magic going on) is to remove the DSL: class AddUserIdentifiersCompanyUniqueIndex < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
return if index_exists?(:user_identifiers, %i[company_id value])
add_index :user_identifiers, %i[company_id value], algorithm: :concurrently, unique: true, where: 'subsidiary_id is null'
end
def down
return unless index_exists?(:user_identifiers, %i[company_id value])
remove_index :user_identifiers, %i[company_id value]
end
def self.statement_timeout
ENV.fetch('MIGRATION_20201014221845', 250)
end
end |
BTW, i got it working without type casting ENV var to integer @nickcampbell18. I did some diggings to try to understand the behaviour and could not reproduce the method being executed from the context of the parent: class Vehicle
def self.wheels(wheels)
puts self.name
@wheels = wheels
end
def wheels
puts self.class.name
self.class.instance_variable_get(:@wheels)
end
end
class Car < Vehicle
wheels 4
end
# Car
# => 4
class Motorcycle < Vehicle
wheels 2
end
# Motorcycle
# => 2
Car.new.wheels # Car
Motorcycle.new.wheels # Motorcycle Is this behaviour of executing in the context of |
So, i just upgraded to version
3.0.0
and got an issue that does not exist on version2.0.0
.We have a codebase with thousands of migrations and several tables. Over the years we got to a solution to help us maintain all those migrations and facilitate deployments and migrations through heroku for different database sizes (Yes, we have multiple databases as some big clients asked and payed for it)
So we got to work with different table sizes using ENV vars like so:
Any ideas on why it does not work on version
3.0.0
?The text was updated successfully, but these errors were encountered: