Skip to content
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

ActiveRecord 7.2+ Migration Error #2298

Open
richpeck opened this issue Dec 18, 2024 · 0 comments
Open

ActiveRecord 7.2+ Migration Error #2298

richpeck opened this issue Dec 18, 2024 · 0 comments

Comments

@richpeck
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

padrino ar:migrate crashes out citing create_table function not found when using ActiveRecord 7.2+

What is the expected behavior?

padrino ar:migrate will process the migrations as per ActiveRecord's standard functionality

Which versions of Ruby, Padrino, Sinatra, Rack, OS are you using? Did this work in previous versions?

Ruby 3.3.6, Padrino 16.pre.3


migrate_task

** Invoke ar:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke skeleton (first_time)
** Execute skeleton
** Execute ar:migrate
rake aborted!
NoMethodError: undefined method `create_table' for class ActiveRecord::SchemaMigration (NoMethodError)

      @schema_migration.create_table
                       ^^^^^^^^^^^^^
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.1/lib/active_record/migration.rb:1431:in `initialize'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.1/lib/active_record/migration.rb:1261:in `new'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.1/lib/active_record/migration.rb:1261:in `up'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.1/lib/active_record/migration.rb:1236:in `migrate'
/var/www/vhosts/xxxxxxxxx/httpdocs/lib/tasks/ar_migrations_fix.rake:44:in `block (2 levels) in '
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `block in execute'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `each'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `execute'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:188:in `invoke'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:188:in `invoke_task'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block (2 levels) in top_level'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `each'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block in top_level'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:147:in `run_with_threads'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:132:in `top_level'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:83:in `block in run'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:214:in `standard_exception_handling'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:80:in `run'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/exe/rake:27:in `'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/bin/rake:25:in `load'
/var/www/vhosts/xxxxxxxxx/httpdocs/vendor/bundle/ruby/3.3.0/bin/rake:25:in `
'
Tasks: TOP => ar:migrate

Upon further investigation, the issue is caused by the following: -

ActiveRecord::MigrationContext.new("db/migrate/", ActiveRecord::SchemaMigration).migrate(env_migration_version)

This is incorrect for ActiveRecord 7.2, it should be the same as AR 6.0: -

ActiveRecord::MigrationContext.new("db/migrate/").migrate(env_migration_version)

I've managed to monkeypatch a solution by overriding the default migrate task and am happy to submit a PR if needed: -

## RPECK 10/12/2024 - Fix to the ActiveRecord Migrate task in Padrino
## Any migrations performed have to pass the correct values to ActiveRecord
## --
## For versions of AR > 7.1, there seems to be an issue with the core migrate task, leading to an error citing:
## undefined method `create_table' for class ActiveRecord::SchemaMigration (NoMethodError)
## -
## The solution is to ensure that the correct values are passed to ActiveRecord, which we've achieved here with a monkeypatch
## Ref: ./padrino-gen/lib/padrino-gen/padrino-tasks/activerecord.rb
namespace :ar do

	task :migrate => :environment do

		Rake::Task["ar:migrate"].clear

		if PadrinoTasks.load?(:activerecord, defined?(ActiveRecord))
		
 			ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true

			if less_than_active_record_5_2?
				ActiveRecord::Migrator.migrate("db/migrate/", env_migration_version)
			elsif less_than_active_record_6_0? || greater_than_active_record_7_1? ## RPECK 18/12/2024 -> added logic here 
				ActiveRecord::MigrationContext.new("db/migrate/").migrate(env_migration_version)
			else
				ActiveRecord::MigrationContext.new("db/migrate/", ActiveRecord::SchemaMigration).migrate(env_migration_version)
			end

			if less_than_active_record_7_0?
				Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
			else
				Rake::Task["ar:schema:dump"].invoke if ActiveRecord.schema_format == :ruby
			end

		end

	end
	
	def greater_than_active_record_7_1?
		ActiveRecord.version > Gem::Version.create("7.1.0")
	end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant