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 "    require_relative \"planetscale\"\n  RUBY\n  end\nend\n"

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 =
  "  development:\n    <<: *default\n    username: root\n    host: 127.0.0.1\n    port: 3305\n    database: \#{@database}\n  EOS\n\n\n  db_url = \"mysql2://root:@127.0.0.1:3305/\#{@database}\"\n  puts \"Installed!\\n\\nConfigure your database.yaml like so:\\n\".bold\n  puts d\n  puts \"\\nOr set DATABASE_URL=\#{db_url}\"\nend\n"

#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