<commit_before>require "micrate"
require "pg"
require "mysql"
require "sqlite3"

module Kemalyst::Generator
  class MainCommand < Cli::Supercommand
    command "m", aliased: "migrate"
    command "micrate", aliased: "migrate"

    class Migrate < Cli::Command
      command_name "migrate"
      class Options
        arg "command", desc: "up, down, redo, status, dbversion", required: true
      end

      def run
        Micrate::Cli.setup_logger
        Micrate::DB.connection_url = database_url

        begin
          case args.command
          when "up"
            Micrate::Cli.run_up
          when "down"
            Micrate::Cli.run_down
          when "redo"
            Micrate::Cli.run_redo
          when "status"
            Micrate::Cli.run_status
          when "dbversion"
            Micrate::Cli.run_dbversion
          else
            Micrate::Cli.print_help
          end
        rescue e : Micrate::UnorderedMigrationsException
          Micrate::Cli.report_unordered_migrations(e.versions)
          exit 1
        rescue e : Exception
          puts e.message
          exit 1
        end
      end

      def database_url
        yaml_file = File.read("config/database.yml")
        yaml = YAML.parse(yaml_file)
        db = yaml.first.to_s
        settings = yaml[db]
        env(settings["database"].to_s)
      end

      private def env(value)
        env_var = value.gsub("${", "").gsub("}", "")
        if ENV.has_key? env_var
          return ENV[env_var]
        else<commit_msg>show db connection errors.  use env DATABASE_URL<commit_after>require "micrate"
require "pg"
require "mysql"
require "sqlite3"

module Kemalyst::Generator
  class MainCommand < Cli::Supercommand
    command "m", aliased: "migrate"
    command "micrate", aliased: "migrate"

    class Migrate < Cli::Command
      command_name "migrate"
      class Options
        arg "command", desc: "up, down, redo, status, dbversion", required: true
      end

      def run
        Micrate::DB.connection_url = database_url
        begin
          case args.command
          when "up"
            Micrate::Cli.run_up
          when "down"
            Micrate::Cli.run_down
          when "redo"
            Micrate::Cli.run_redo
          when "status"
            Micrate::Cli.run_status
          when "dbversion"
            Micrate::Cli.run_dbversion
          else
            Micrate::Cli.print_help
          end
        rescue e : Micrate::UnorderedMigrationsException
          Micrate::Cli.report_unordered_migrations(e.versions)
          exit 1
        rescue e : DB::ConnectionRefused
          puts "Connection refused: #{Micrate::DB.connection_url}"
          exit 1
        rescue e : Exception
          puts e.message
          exit 1
        end
      end

      def database_url
        ENV["DATABASE_URL"]? || begin
          yaml_file = File.read("config/database.yml")
          yaml = YAML.parse(yaml_file)
          db = yaml.first.to_s
          settings = yaml[db]
          env(settings["database"].to_s)
        end
      end

      private def env(value)
        env_var = value.gsub("${", "").gsub("}", "")
        if ENV.has_key? env_var
          return ENV[env_var]
        else
{'fraction_non_alphanumeric': 0.09418713086726764, 'fraction_numerical': 0.002175940317065589, 'mean_word_length': 1.8757819481680071, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6190824', 'n_tokens_mistral': 987, 'n_tokens_neox': 953, 'n_words': 224}