Skip to content

Commit

Permalink
Merge branch 'master' into 20240101-eol-ruby-rails
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld authored Jan 2, 2024
2 parents 9eb96b3 + 6f7bd48 commit df2a27e
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 193 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
rails:
- 6.1.7.6
- 7.0.8
- 7.1.2
env:
PERCONA_DB_USER: root
PERCONA_DB_PASSWORD: root
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Please follow the format in [Keep a Changelog](http://keepachangelog.com/)

- Fix support for Rails 6.0 and ForAlter `remove_index` .
- Remove support for EOL Ruby (< 3.0) and Rails (< 6.1)
- Support Rails 7.1.2

## [6.5.0] - 2023-01-24

Expand Down
2 changes: 1 addition & 1 deletion departure.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require 'departure/version'

# This environment variable is set on CI to facilitate testing with multiple
# versions of Rails.
RAILS_DEPENDENCY_VERSION = ENV.fetch('RAILS_VERSION', ['>= 6.1.0', '!= 7.0.0', '< 7.1'])
RAILS_DEPENDENCY_VERSION = ENV.fetch('RAILS_VERSION', ['>= 6.1.0', '!= 7.0.0', '< 7.2.0'])

Gem::Specification.new do |spec|
spec.name = 'departure'
Expand Down
25 changes: 22 additions & 3 deletions lib/active_record/connection_adapters/percona_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def percona_connection(config)

module ConnectionAdapters
class DepartureAdapter < AbstractMysqlAdapter
TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) } if defined?(initialize_type_map)

class Column < ActiveRecord::ConnectionAdapters::MySQL::Column
def adapter
DepartureAdapter
Expand Down Expand Up @@ -86,19 +88,20 @@ def write_query?(sql) # :nodoc:

def exec_delete(sql, name, binds)
execute(to_sql(sql, binds), name)
@connection.affected_rows
mysql_adapter.raw_connection.affected_rows
end
alias exec_update exec_delete

def exec_insert(sql, name, binds, pk = nil, sequence_name = nil) # rubocop:disable Lint/UnusedMethodArgument, Metrics/LineLength
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil, returning: nil) # rubocop:disable Lint/UnusedMethodArgument, Metrics/LineLength, Metrics/ParameterLists
execute(to_sql(sql, binds), name)
end

def exec_query(sql, name = 'SQL', _binds = [], **_kwargs)
def internal_exec_query(sql, name = 'SQL', _binds = [], **_kwargs) # :nodoc:
result = execute(sql, name)
fields = result.fields if defined?(result.fields)
ActiveRecord::Result.new(fields, result.to_a)
end
alias exec_query internal_exec_query

# Executes a SELECT query and returns an array of rows. Each row is an
# array of field values.
Expand Down Expand Up @@ -196,6 +199,22 @@ def last_inserted_id(result)
private

attr_reader :mysql_adapter

if ActiveRecord.version >= Gem::Version.create('7.1.0')
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
log(sql, name, async: async) do
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
sync_timezone_changes(conn)
result = conn.query(sql)
verified!
handle_warnings(sql)
result
end
end
end
end

def reconnect; end
end
end
end
6 changes: 3 additions & 3 deletions lib/lhm/column_with_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def column
#
# @return [String, NilClass]
def default_value
match = if definition =~ /timestamp|datetime/i
match = if definition.match?(/timestamp|datetime/i)
/default '?(.+[^'])'?/i.match(definition)
else
/default '?(\w+)'?/i.match(definition)
Expand All @@ -87,10 +87,10 @@ def default_value
#
# @return [Boolean]
def null_value
match = /((\w*) NULL)/i.match(definition)
match = /((NOT)? NULL)/i.match(definition)
return true unless match

match[2].downcase == 'not' ? false : true
match[2]&.downcase == 'not' ? false : true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@

describe '#exec_delete' do
let(:sql) { 'DELETE FROM comments WHERE id = 1' }
let(:affected_rows) { 1 }
let(:name) { nil }
let(:binds) { nil }

before do
allow(runner).to receive(:query).with(sql)
allow(runner).to receive(:affected_rows).and_return(1)
allow(runner).to receive(:query).with(anything)
allow(mysql_client).to receive(:affected_rows).and_return(affected_rows)
end

it 'executes the sql' do
Expand All @@ -195,7 +196,7 @@
end

it 'returns the number of affected rows' do
expect(adapter.exec_delete(sql, name, binds)).to eq(1)
expect(adapter.exec_delete(sql, name, binds)).to eq(affected_rows)
end
end

Expand Down
11 changes: 5 additions & 6 deletions spec/integration/change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
describe Departure, integration: true do
class Comment < ActiveRecord::Base; end

let(:migration_fixtures) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration).migrations.select do |m|
m.version == version
end
let(:migration_context) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration)
end

let(:direction) { :up }

context 'change_table' do
Expand All @@ -19,7 +18,7 @@ def column_metadata(table, name)

context 'creating column' do
before(:each) do
ActiveRecord::Migrator.new(direction, migration_fixtures, ActiveRecord::SchemaMigration, version).migrate
migration_context.run(direction, version)
end

it 'adds the column in the DB table' do
Expand All @@ -38,7 +37,7 @@ def column_metadata(table, name)
end

it 'marks the migration as up' do
expect(ActiveRecord::Migrator.current_version).to eq(version)
expect(migration_context.current_version).to eq(version)
end

it 'changes column' do
Expand Down
69 changes: 24 additions & 45 deletions spec/integration/columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
describe Departure, integration: true do
class Comment < ActiveRecord::Base; end

let(:migration_fixtures) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration).migrations
let(:migration_context) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration)
end
let(:migration_paths) { [MIGRATION_FIXTURES] }

let(:direction) { :up }

Expand All @@ -17,55 +16,35 @@ class Comment < ActiveRecord::Base; end
let(:direction) { :up }

it 'adds the column in the DB table' do
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version
).migrate
migration_context.run(direction, version)

expect(:comments).to have_column('some_id_field')
end

it 'marks the migration as up' do
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version
).migrate

expect(ActiveRecord::Migrator.current_version).to eq(version)
migration_context.run(direction, version)

expect(migration_context.current_version).to eq(version)
end
end

context 'dropping column' do
let(:direction) { :down }

before do
ActiveRecord::Migrator.new(:up, migration_fixtures, ActiveRecord::SchemaMigration, version).migrate
migration_context.up(version)
end

it 'drops the column from the DB table' do
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version - 1
).migrate
migration_context.run(direction, version)

expect(:comments).not_to have_column('some_id_field')
end

it 'marks the migration as down' do
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version - 1
).migrate

expect(ActiveRecord::Migrator.current_version).to eq(version - 1)
migration_context.run(direction, version)

expect(migration_context.current_version).to eq(version - 1)
end
end

Expand All @@ -82,12 +61,12 @@ class Comment < ActiveRecord::Base; end
end

it 'changes the column name' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).to have_column('new_id_field')
end

it 'does not keep the old column' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).not_to have_column('some_id_field')
end
end
Expand All @@ -100,34 +79,34 @@ class Comment < ActiveRecord::Base; end
end

before do
ActiveRecord::Migrator.new(:up, migration_fixtures, ActiveRecord::SchemaMigration, 1).migrate
migration_context.up(1)
end

context 'when null is true' do
let(:version) { 14 }

it 'sets the column to allow nulls' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(column.null).to be_truthy
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(ActiveRecord::Migrator.current_version).to eq(version)
migration_context.run(direction, version)
expect(migration_context.current_version).to eq(version)
end
end

context 'when null is false' do
let(:version) { 15 }

it 'sets the column not to allow nulls' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(column.null).to be_falsey
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(ActiveRecord::Migrator.current_version).to eq(version)
migration_context.run(direction, version)
expect(migration_context.current_version).to eq(version)
end
end
end
Expand All @@ -136,12 +115,12 @@ class Comment < ActiveRecord::Base; end
let(:version) { 22 }

it 'adds a created_at column' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).to have_column('created_at')
end

it 'adds a updated_at column' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).to have_column('updated_at')
end
end
Expand All @@ -158,12 +137,12 @@ class Comment < ActiveRecord::Base; end
end

it 'removes the created_at column' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).not_to have_column('created_at')
end

it 'removes the updated_at column' do
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
migration_context.run(direction, version)
expect(:comments).not_to have_column('updated_at')
end
end
Expand Down
Loading

0 comments on commit df2a27e

Please sign in to comment.