Class: Planetscale::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/planetscale/install_generator.rb

Constant Summary collapse

APPLICATION_REQUIRE_REGEX =
/(require_relative ("|')application("|')\n)/.freeze

Instance Method Summary collapse

Instance Method Details

#check_orgObject



17
18
19
20
21
22
23
24
# File 'lib/generators/planetscale/install_generator.rb', line 17

def check_org
  if options[:organization].empty? && @org.nil?
    puts "Usage: bundle exec rails g planetscale:install --organization ORG_NAME"
    abort
  end

  @org ||= options[:organization]
end

#create_planetscale_configObject



28
29
30
31
32
33
34
# File 'lib/generators/planetscale/install_generator.rb', line 28

def create_planetscale_config
  create_file "config/planetscale.rb", "PlanetScale.start(org: '#{@org}')\n"
  inject_into_file "config/environment.rb", after: APPLICATION_REQUIRE_REGEX do <<~'RUBY'
    require_relative "planetscale"
  RUBY
  end
end

todo(nickvanw): When we get rid of DB passwords, this can mostly go away, and we can just return the ‘DATABSE_URL` that the user should use.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/planetscale/install_generator.rb', line 38

def print_database_yaml
  d =
  <<~EOS
  development:
    <<: *default
    username: root
    host: 127.0.0.1
    port: 3305
    database: #{@database}
  EOS


  db_url = "mysql2://root:@127.0.0.1:3305/#{@database}"
  puts "Installed!\n\nConfigure your database.yaml like so:\n".bold
  puts d
  puts "\nOr set DATABASE_URL=#{db_url}"
end

#read_configObject



7
8
9
10
11
12
13
14
15
# File 'lib/generators/planetscale/install_generator.rb', line 7

def read_config
  @database = "<db_name>"
  file_path = File.join(Rails.root, PlanetScale::Proxy::PLANETSCALE_FILE)
  return unless File.exist?(file_path)

  data = YAML.safe_load(File.read(file_path))
  @database = data['database']
  @org = data['org']
end