<commit_before>require 'ostruct'

describe 'PostgreSQL Database migrations' do
  let(:migrator) do
    Hanami::Model::Migrator.new(configuration: configuration)
  end

  let(:random) { SecureRandom.hex(4) }

  # General variables
  let(:migrations)     { Pathname.new(__dir__ + '/../fixtures/migrations') }
  let(:schema)         { nil }
  let(:config)         { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema) }
  let(:configuration)  { Hanami::Model::Configuration.new(config) }

  # Variables for `apply` and `prepare`
  let(:root)              { Pathname.new("#{__dir__}/../../tmp").expand_path }
  let(:source_migrations) { Pathname.new("#{__dir__}/../fixtures/migrations") }
  let(:target_migrations) { root.join("migrations-#{random}") }

  after do
    migrator.drop rescue nil # rubocop:disable Style/RescueModifier
  end

  describe 'PostgreSQL' do
    let(:database) { "#{name.gsub(/[^\w]/, '_')}_#{random}" }

    let(:url) do
      db = database

      Platform.match do
        engine(:ruby)  { "postgresql://127.0.0.1/#{db}?user=#{ENV['HANAMI_DATABASE_USERNAME']}" }
        engine(:jruby) { "jdbc:postgresql://127.0.0.1/#{db}?user=#{ENV['HANAMI_DATABASE_USERNAME']}" }
      end
    end

    describe 'create' do
      before do
        migrator.create
      end

      it 'creates the database' do
        connection = Sequel.connect(url)
        connection.tables.must_be :empty?
      end

      it 'raises error if database is busy' do
        Sequel.connect(url).tables
        exception = -> { migrator.create }.must_raise Hanami::Model::MigrationError
        exception.message.must_include 'createdb: database creation failed'
        exception.message.must_include 'There is 1 other session using the database'
      end

      describe "when a command isn't available" do
        before do
          # We accomplish having a command not be available by setting PATH
          # to an empty string, which means *no commands* are available.
          @original_path = ENV['PATH']
          ENV['PATH']    = ''
        end

        after do
          ENV['PATH'] = @original_path
        end

        it 'raises MigrationError on create' do
          message = Platform.match do<commit_msg>Fixed message expectation for Postgres migrator test<commit_after>require 'ostruct'

describe 'PostgreSQL Database migrations' do
  let(:migrator) do
    Hanami::Model::Migrator.new(configuration: configuration)
  end

  let(:random) { SecureRandom.hex(4) }

  # General variables
  let(:migrations)     { Pathname.new(__dir__ + '/../fixtures/migrations') }
  let(:schema)         { nil }
  let(:config)         { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema) }
  let(:configuration)  { Hanami::Model::Configuration.new(config) }

  # Variables for `apply` and `prepare`
  let(:root)              { Pathname.new("#{__dir__}/../../tmp").expand_path }
  let(:source_migrations) { Pathname.new("#{__dir__}/../fixtures/migrations") }
  let(:target_migrations) { root.join("migrations-#{random}") }

  after do
    migrator.drop rescue nil # rubocop:disable Style/RescueModifier
  end

  describe 'PostgreSQL' do
    let(:database) { "#{name.gsub(/[^\w]/, '_')}_#{random}" }

    let(:url) do
      db = database

      Platform.match do
        engine(:ruby)  { "postgresql://127.0.0.1/#{db}?user=#{ENV['HANAMI_DATABASE_USERNAME']}" }
        engine(:jruby) { "jdbc:postgresql://127.0.0.1/#{db}?user=#{ENV['HANAMI_DATABASE_USERNAME']}" }
      end
    end

    describe 'create' do
      before do
        migrator.create
      end

      it 'creates the database' do
        connection = Sequel.connect(url)
        connection.tables.must_be :empty?
      end

      it 'raises error if database is busy' do
        Sequel.connect(url).tables
        exception = -> { migrator.create }.must_raise Hanami::Model::MigrationError

        exception.message.must_include 'createdb: database creation failed. If the database exists,'
        exception.message.must_include 'then its console may be open. See this issue for more details:'
        exception.message.must_include 'https://github.com/hanami/model/issues/250'
      end

      describe "when a command isn't available" do
        before do
          # We accomplish having a command not be available by setting PATH
          # to an empty string, which means *no commands* are available.
          @original_path = ENV['PATH']
          ENV['PATH']    = ''
        end

        after do
          ENV['PATH'] = @original_path
        end

        it 'raises MigrationError on create' do
          message = Platform.match do
{'fraction_non_alphanumeric': 0.12955812955812956, 'fraction_numerical': 0.006435006435006435, 'mean_word_length': 2.7818329278183294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5899245', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1413, 'n_words': 339}