Sha256: 64cc483e9a22e35b417fe4a047bee3e6dd577192940d043a0c1c926fe95da8c9

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

require 'rails/generators'

class Planetscale
  class InstallGenerator < Rails::Generators::Base    
    class_option :organization, type: :string, default: ''

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

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

    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 

    def create_planetscale_config
      create_file "config/planetscale.rb", "PlanetScale.start(org: '#{@org}')\n"
      inject_into_file "config/environment.rb", after: "require_relative \"application\"\n" 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. 
    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
  end 
end

class String
  def bold; "\e[1m#{self}\e[22m" end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
planetscale-0.2.0 lib/generators/planetscale/install_generator.rb
planetscale-0.1.0 lib/generators/planetscale/install_generator.rb