rubyonjets / jets

Ruby on Jets
http://rubyonjets.com
MIT License
2.6k stars 181 forks source link

Jets::Commands::Db::Tasks.load! causes problems when using Dynamoid #443

Closed prashcr closed 11 months ago

prashcr commented 4 years ago

Checklist

My Environment

Software Version
Operating System Ubuntu 18.04
Jets 2.3.12
Ruby 2.5.6

Expected Behaviour

I can create a table with Dynamoid using rake dynamoid:create_tables and Jets doesn't interfere with the rake task.

Current Behavior

Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB. D, [2020-02-19T18:31:26.923510 #13842] DEBUG -- : list_tables | Request "{}" | Response "{\"TableNames\":[]}" D, [2020-02-19T18:31:26.923668 #13842] DEBUG -- : (5.47 ms) LIST TABLES D, [2020-02-19T18:31:26.933931 #13842] DEBUG -- : list_tables | Request "{}" | Response "{\"TableNames\":[]}" D, [2020-02-19T18:31:26.934115 #13842] DEBUG -- : (10.27 ms) LIST TABLES D, [2020-02-19T18:31:26.934167 #13842] DEBUG -- : (10.34 ms) CACHE TABLES I, [2020-02-19T18:31:26.934247 #13842] INFO -- : Creating dynamoid_Jets::Commands::Db::Tasks::Dummy_development_users table. This could take a while. D, [2020-02-19T18:31:26.971592 #13842] DEBUG -- : create_table | Request "{\"TableName\":\"dynamoid_Jets::Commands::Db::Tasks::Dummy_development_users\",\"KeySchema\":[{\"AttributeName\":\"id\",\"KeyType\":\"HASH\"}],\"AttributeDefinitions\":[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}],\"BillingMode\":\"PROVISIONED\",\"ProvisionedThroughput\":{\"ReadCapacityUnits\":100,\"WriteCapacityUnits\":20}}" | Response "{\"__type\":\"com.amazon.coral.validate#ValidationException\",\"message\":\"Invalid table/index name. Table/index names must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, '_', '-', and '.'\"}" rake aborted! Aws::DynamoDB::Errors::ValidationException: Invalid table/index name. Table/index names must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, '_', '-', and '.'

Step-by-step reproduction instructions

Code Sample

class User
  include Dynamoid::Document

end

Solution Suggestion

  1. Add a Jets config option to disable_active_record (or something more elegant), should conditionally call Jets::Commands::Db::Tasks.load! in Jets::Commands::RakeTasks, like we do for loading webpack tasks
  2. Move load File.expand_path("../environment-task.rake", __FILE__) from Jets::Commands::Db::Tasks.load and put it in Jets::Commands::RakeTasks instead
# lib/jets/commands/rake_tasks.rb

    def load!
      return if @@loaded # prevent loading twice
      # Run Bundler.setup so all project rake tasks also show up in `jets help`
      Jets::Bundle.setup

-     Jets::Commands::Db::Tasks.load!
+     Jets::Commands::Db::Tasks.load! unless Jets.config.active_record_disabled?
+     load File.expand_path("../environment-task.rake", __FILE__)
      load_webpacker_tasks

      # custom project rake tasks
      Dir.glob("#{Jets.root}/lib/tasks/*.rake").each { |r| load r }

      @@loaded = true
    end

My monkeypatch fix, for reference

# Rakefile

require 'jets'
# HACK: Avoid loading ActiveRecord tasks
class Jets::Commands::Db::Tasks
  def self.load!
    Rake::Task.define_task(:environment) { Jets.boot }
  end
end
Jets.load_tasks
require 'dynamoid/tasks'
tongueroo commented 11 months ago

Jets v5 lazy initializes ActiveRecord thanks to Jets Engines support now. So don't believe will need the monkeypatch workaround anymore. Closing out.

https://blog.boltops.com/2023/12/05/jets-5-improvements-galore/