<commit_before># frozen_string_literal: true

module Starter
  module Templates
    module ActiveRecord
      def model_klass
        'ActiveRecord::Base'
      end

      def initializer
        <<-FILE.strip_heredoc
        require 'yaml'
        require 'erb'
        require 'active_record'

        config_content = File.read(File.join('config', 'database.yml'))

        db_conf = YAML.safe_load(ERB.new(config_content).result)
        env = ENV['RACK_ENV'] || 'development'

        ActiveRecord::Base.establish_connection db_conf[env]
        logger = if %w[development test].include? env
                   log_dir = File.join(Dir.getwd, 'log')
                   log_file = File.join(log_dir, 'db.log')
                   FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
                   Logger.new(File.open(log_file, 'a'))
                 else
                   Logger.new(STDOUT)
                 end

        ActiveRecord::Base.logger = logger

        # Middleware
        module ActiveRecord
          module Rack
            # ActiveRecord >= 5 removes the Pool management
            class ConnectionManagement
              def initialize(app)
                @app = app
              end

              def call(env)
                response = @app.call(env)
                response[2] = ::Rack::BodyProxy.new(response[2]) do
                  ActiveRecord::Base.clear_active_connections!
                end

                return response
              rescue StandardError
                ActiveRecord::Base.clear_active_connections!
                raise
              end
            end
          end
        end
        FILE
      end

      def rakefile
        <<-FILE.strip_heredoc
        # ActiveRecord DB tasks

        task :load_config do
          config_dir = File.expand_path('../config', __FILE__)
          config_content = File.read(File.join(config_dir, 'database.yml'))

          DatabaseTasks.env = ENV['RACK_ENV'] || 'development'
          DatabaseTasks.db_dir = db_dir
          config = YAML.safe_load(ERB.new(config_content).result)
          DatabaseTasks.database_configuration = config
          DatabaseTasks.migrations_paths = File.join(db_dir, 'migrate')

          ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
          ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
        end

        # Loads AR tasks like db:create, db:drop etc..
        load 'active_record/railties/databases.rake'
        FILE
      end

      def gemfile
        <<-FILE.strip_heredoc
        # DB stack
        gem 'activerecord', '~> 5.1'
        gem 'sqlite3'
        FILE
      end

      def migration(klass_name, resource)
        <<-FILE.strip_heredoc
        class Create#{klass_name} < ActiveRecord::Migration[5.1]
          def change
            create_table :#{resource} do |t|

              t.timestamps
            end
          end
        end<commit_msg>Specifies sqlite version for AR.<commit_after># frozen_string_literal: true

module Starter
  module Templates
    module ActiveRecord
      def model_klass
        'ActiveRecord::Base'
      end

      def initializer
        <<-FILE.strip_heredoc
        require 'yaml'
        require 'erb'
        require 'active_record'

        config_content = File.read(File.join('config', 'database.yml'))

        db_conf = YAML.safe_load(ERB.new(config_content).result)
        env = ENV['RACK_ENV'] || 'development'

        ActiveRecord::Base.establish_connection db_conf[env]
        logger = if %w[development test].include? env
                   log_dir = File.join(Dir.getwd, 'log')
                   log_file = File.join(log_dir, 'db.log')
                   FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
                   Logger.new(File.open(log_file, 'a'))
                 else
                   Logger.new(STDOUT)
                 end

        ActiveRecord::Base.logger = logger

        # Middleware
        module ActiveRecord
          module Rack
            # ActiveRecord >= 5 removes the Pool management
            class ConnectionManagement
              def initialize(app)
                @app = app
              end

              def call(env)
                response = @app.call(env)
                response[2] = ::Rack::BodyProxy.new(response[2]) do
                  ActiveRecord::Base.clear_active_connections!
                end

                return response
              rescue StandardError
                ActiveRecord::Base.clear_active_connections!
                raise
              end
            end
          end
        end
        FILE
      end

      def rakefile
        <<-FILE.strip_heredoc
        # ActiveRecord DB tasks

        task :load_config do
          config_dir = File.expand_path('../config', __FILE__)
          config_content = File.read(File.join(config_dir, 'database.yml'))

          DatabaseTasks.env = ENV['RACK_ENV'] || 'development'
          DatabaseTasks.db_dir = db_dir
          config = YAML.safe_load(ERB.new(config_content).result)
          DatabaseTasks.database_configuration = config
          DatabaseTasks.migrations_paths = File.join(db_dir, 'migrate')

          ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
          ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
        end

        # Loads AR tasks like db:create, db:drop etc..
        load 'active_record/railties/databases.rake'
        FILE
      end

      def gemfile
        <<-FILE.strip_heredoc
        # DB stack
        gem 'activerecord', '~> 5.2'
        gem 'sqlite3', '~> 1.3', '< 1.4'
        FILE
      end

      def migration(klass_name, resource)
        <<-FILE.strip_heredoc
        class Create#{klass_name} < ActiveRecord::Migration[5.1]
          def change
            create_table :#{resource} do |t|

              t.timestamps
            end
          end
        end
{'fraction_non_alphanumeric': 0.08599261001007726, 'fraction_numerical': 0.003359086328518643, 'mean_word_length': 1.6945701357466063, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 8, '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': '2003053', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1585, 'n_words': 333}