Skip to content

Commit

Permalink
[Rails 7.1] Initial changes required to run tests (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored Aug 3, 2023
1 parent d9efae7 commit 4143a27
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ WORKDIR $WORKDIR

COPY . $WORKDIR

RUN bundle install --jobs `expr $(cat /proc/cpuinfo | grep -c "cpu cores") - 1` --retry 3
RUN RAILS_MAIN=1 bundle install --jobs `expr $(cat /proc/cpuinfo | grep -c "cpu cores") - 1` --retry 3

CMD ["sh"]
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ gem "sqlite3", "~> 1.4"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "benchmark-ips"
gem "minitest", ">= 5.15.0", "< 5.16"
gem "msgpack", ">= 1.7.0"

if ENV["RAILS_SOURCE"]
gemspec path: ENV["RAILS_SOURCE"]
elsif ENV["RAILS_MAIN"]
gem "rails", github: "rails/rails", branch: 'main'
else
# Need to get rails source because the gem doesn't include tests
version = ENV["RAILS_VERSION"] || begin
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ task test: ["test:dblib"]
task default: [:test]

namespace :test do
ENV["ARCONN"] = "sqlserver"

%w(dblib).each do |mode|
Rake::TestTask.new(mode) do |t|
t.libs = ARTest::SQLServer.test_load_paths
Expand All @@ -19,7 +21,7 @@ namespace :test do
end

task "dblib:env" do
ENV["ARCONN"] = "dblib"
ENV["ARCONN_MODE"] = "dblib"
end
end

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.3.0
7.1.0.alpha
2 changes: 1 addition & 1 deletion activerecord-sqlserver-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "activerecord", "~> 7.0.0"
spec.add_dependency "activerecord", "~> 7.1.0.alpha"
spec.add_dependency "tiny_tds"
end
1 change: 1 addition & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
ci:
environment:
- ACTIVERECORD_UNITTEST_HOST=sqlserver
- RAILS_MAIN=1
build:
context: .
dockerfile: Dockerfile.ci
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def execute(sql, name = nil)
end
end

def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
sql = transform_query(sql)
if preventing_writes? && write_query?(sql)
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
Expand All @@ -41,7 +41,7 @@ def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
sp_executesql(sql, name, binds, prepare: prepare, async: async)
end

def exec_insert(sql, name = nil, binds = [], pk = nil, _sequence_name = nil)
def exec_insert(sql, name = nil, binds = [], pk = nil, _sequence_name = nil, returning: nil)
if id_insert_table_name = exec_insert_requires_identity?(sql, pk, binds)
with_identity_insert_enabled(id_insert_table_name) { super(sql, name, binds, pk) }
else
Expand Down Expand Up @@ -263,7 +263,7 @@ def newsequentialid_function

protected

def sql_for_insert(sql, pk, binds)
def sql_for_insert(sql, pk, binds, _returning)
if pk.nil?
table_name = query_requires_identity_insert?(sql)
pk = primary_key(table_name)
Expand Down Expand Up @@ -301,6 +301,8 @@ def set_identity_insert(table_name, enable = true)
# === SQLServer Specific (Executing) ============================ #

def do_execute(sql, name = "SQL")
connect if @connection.nil?

materialize_transactions
mark_transaction_written_if_write(sql)

Expand Down Expand Up @@ -328,6 +330,7 @@ def sp_executesql_types_and_parameters(binds)
end

def sp_executesql_sql_type(attr)
return "nvarchar(max)".freeze if attr.is_a?(Symbol)
return attr.type.sqlserver_type if attr.type.respond_to?(:sqlserver_type)

case value = attr.value_for_database
Expand All @@ -339,6 +342,8 @@ def sp_executesql_sql_type(attr)
end

def sp_executesql_sql_param(attr)
return quote(attr) if attr.is_a?(Symbol)

case value = attr.value_for_database
when Type::Binary::Data,
ActiveRecord::Type::SQLServer::Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def drop_table(table_name, **options)
do_execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
end
end
if options[:if_exists] && @version_year < 2016
if options[:if_exists] && version_year < 2016
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}", "SCHEMA"
else
super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def new_column_definition(name, type, **options)

super
end

private

def valid_column_definition_options
super + [:is_identity]
end
end

class Table < ActiveRecord::ConnectionAdapters::Table
Expand Down
33 changes: 20 additions & 13 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ def config_encoding(config)
end
end

def initialize(connection, logger, _connection_options, config)
super(connection, logger, config)
@connection_options = config
perform_connection_configuration
def initialize(...)
super

@config = @config.symbolize_keys
@config.reverse_merge!(mode: :dblib)
@config[:mode] = @config[:mode].to_s.downcase.underscore.to_sym

@connection_options ||= @config
end

# === Abstract Adapter ========================================== #
Expand Down Expand Up @@ -229,7 +233,7 @@ def supports_datetime_with_precision?
end

def supports_json?
@version_year >= 2016
version_year >= 2016
end

def supports_comments?
Expand All @@ -253,7 +257,7 @@ def supports_lazy_transactions?
end

def supports_in_memory_oltp?
@version_year >= 2014
version_year >= 2014
end

def supports_insert_returning?
Expand Down Expand Up @@ -308,7 +312,7 @@ def disconnect!
@collation = nil
end

def clear_cache!
def clear_cache!(...)
@view_information = nil
super
end
Expand Down Expand Up @@ -534,11 +538,15 @@ def initialize_dateformatter
end

def version_year
return 2016 if sqlserver_version =~ /vNext/

/SQL Server (\d+)/.match(sqlserver_version).to_a.last.to_s.to_i
rescue StandardError
2016
@version_year ||= begin
if sqlserver_version =~ /vNext/
2016
else
/SQL Server (\d+)/.match(sqlserver_version).to_a.last.to_s.to_i
end
rescue StandardError
2016
end
end

def sqlserver_version
Expand All @@ -559,7 +567,6 @@ def perform_connection_configuration

def configure_connection_defaults
@spid = _raw_select("SELECT @@SPID", fetch: :rows).first.first
@version_year = version_year

initialize_dateformatter
use_database
Expand Down
11 changes: 1 addition & 10 deletions lib/active_record/sqlserver_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ def sqlserver_adapter_class
end

def sqlserver_connection(config) #:nodoc:
config = config.symbolize_keys
config.reverse_merge!(mode: :dblib)
config[:mode] = config[:mode].to_s.downcase.underscore.to_sym

sqlserver_adapter_class.new(
sqlserver_adapter_class.new_client(config),
logger,
nil,
config
)
sqlserver_adapter_class.new(config)
end
end
end
4 changes: 2 additions & 2 deletions test/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

default_connection_info: &default_connection_info
adapter: sqlserver
mode: <%= ENV['ARCONN'] || 'dblib' %>
mode: <%= ENV['ARCONN_MODE'] || 'dblib' %>
host: <%= ENV['ACTIVERECORD_UNITTEST_HOST'] || 'localhost' %>
port: <%= ENV['ACTIVERECORD_UNITTEST_PORT'] %>
database: activerecord_unittest
Expand All @@ -12,7 +12,7 @@ default_connection_info: &default_connection_info

connections:

dblib:
sqlserver:
arunit:
<<: *default_connection_info
appname: SQLServerAdptrUnit
Expand Down

0 comments on commit 4143a27

Please sign in to comment.