activerecord-sqlserver-adapter 7.1.1 → 7.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 670622c4f84954789b5050d84bbe47ce3235a614bcd12a083716d9c2c8b775c7
4
- data.tar.gz: 07720ea291eabf91fbb2def06c493a423795533a08394a2837faff4825ef660a
3
+ metadata.gz: b304aee82f2ca78fe1db797b127b31a73b95df1dfb670a7232a68d57b6d9846e
4
+ data.tar.gz: bb52aecdc34253336fdeafb2fad5d26a3cb48ea7dfd2a453c3b87f07a423c0a6
5
5
  SHA512:
6
- metadata.gz: 0144e10d2f2670cf7ab24fc9e5ca50351474b09e82a8d2322a4a402ce5d3365e697f487db5741b9b8199afbe68319093d7c14cd3fbfecce49f041d8b3ee295d7
7
- data.tar.gz: c4d3666c0f01d14e7985651cbd7fd40da9e5bfb03e509e53a5709fe0fcd956efff3d01bdb7a97c12037c80a1588c8afa34e1eaad11623341ba748cff9195e112
6
+ metadata.gz: 34a017ac93ad7e077f41e4897185ccf20820774e1b5ee3c49988fc58d3b0febf422e6989155100b06eab3b2bcfcd79fab35ec83a31d4c9b0a266fc54f06da652
7
+ data.tar.gz: e9fa8fc3869bc0cc20eac834eef6737421d0ff4cfc27a1064843c89a3fd4447f02c5b35338661740c55cbd7caf687151aa469fc2092c01a048ac4967612c88ad
@@ -0,0 +1,30 @@
1
+ # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile
2
+
3
+ # [Choice] Ruby version: 3, 3.0, 2, 2.7, 2.6
4
+ ARG VARIANT="3"
5
+ FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}
6
+
7
+ # TinyTDS
8
+ RUN apt-get -y install libc6-dev \
9
+ && wget http://www.freetds.org/files/stable/freetds-1.1.32.tar.gz \
10
+ && tar -xzf freetds-1.1.32.tar.gz \
11
+ && cd freetds-1.1.32 \
12
+ && ./configure --prefix=/usr/local --with-tdsver=7.3 \
13
+ && make \
14
+ && make install
15
+
16
+ # Install the SQL Server command-line tools
17
+ RUN curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc \
18
+ && curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list \
19
+ && apt-get update \
20
+ && ACCEPT_EULA=Y apt-get install -y mssql-tools18 unixodbc-dev \
21
+ && echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc \
22
+ && echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> /root/.bashrc
23
+
24
+ # Add the SQL Server main Gemfile and install the gems.
25
+ RUN mkdir -p /tmp/activerecord-sqlserver-adapter
26
+ COPY Gemfile VERSION activerecord-sqlserver-adapter.gemspec /tmp/activerecord-sqlserver-adapter/
27
+ RUN cd /tmp/activerecord-sqlserver-adapter \
28
+ && bundle install \
29
+ && rm -rf /tmp/activerecord-sqlserver-adapter
30
+ RUN chown -R vscode:vscode /usr/local/rvm
@@ -0,0 +1,22 @@
1
+ sudo chown -R vscode:vscode /usr/local/bundle
2
+
3
+ # Wait for 10 seconds to make sure SQL Server came up.
4
+ sleep 10
5
+
6
+ # Setup test databases and users.
7
+ /opt/mssql-tools18/bin/sqlcmd -C -S sqlserver -U sa -P "MSSQLadmin!" <<SQL
8
+ CREATE DATABASE [activerecord_unittest];
9
+ CREATE DATABASE [activerecord_unittest2];
10
+ GO
11
+ CREATE LOGIN [rails] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [activerecord_unittest];
12
+ GO
13
+ USE [activerecord_unittest];
14
+ CREATE USER [rails] FOR LOGIN [rails];
15
+ GO
16
+ EXEC sp_addrolemember N'db_owner', N'rails';
17
+ EXEC master..sp_addsrvrolemember @loginame = N'rails', @rolename = N'sysadmin';
18
+ GO
19
+ SQL
20
+
21
+ # Mark directory as safe in Git so that commands run without warnings.
22
+ git config --global --add safe.directory /workspaces/activerecord-sqlserver-adapter
@@ -0,0 +1,38 @@
1
+ // For format details, see https://aka.ms/devcontainer.json.
2
+ {
3
+ "name": "ActiveRecord SQL Server Adapter project development",
4
+ "dockerComposeFile": "docker-compose.yml",
5
+ "service": "activerecord-sqlserver-adapter",
6
+ "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
7
+
8
+ // Features to add to the dev container. More info: https://containers.dev/features.
9
+ "features": {
10
+ "ghcr.io/devcontainers/features/github-cli:1": {
11
+ "version": "latest"
12
+ }
13
+ },
14
+
15
+ "containerEnv": {
16
+ "ACTIVERECORD_UNITTEST_HOST": "sqlserver"
17
+ },
18
+
19
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
20
+ // This can be used to network with other containers or the host.
21
+ // "forwardPorts": [3000, 5432],
22
+
23
+ // Use 'postCreateCommand' to run commands after the container is created.
24
+ "postCreateCommand": ".devcontainer/boot.sh",
25
+
26
+ // Configure tool-specific properties.
27
+ "customizations": {
28
+ "vscode": {
29
+ // Add the IDs of extensions you want installed when the container is created.
30
+ "extensions": [
31
+ "Shopify.ruby-lsp"
32
+ ]
33
+ }
34
+ },
35
+
36
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
37
+ // "remoteUser": "root"
38
+ }
@@ -0,0 +1,42 @@
1
+ version: '3'
2
+
3
+ services:
4
+ activerecord-sqlserver-adapter:
5
+ build:
6
+ context: ..
7
+ dockerfile: .devcontainer/Dockerfile
8
+
9
+ volumes:
10
+ - ../..:/workspaces:cached
11
+
12
+ # Overrides default command so things don't shut down after the process ends.
13
+ command: sleep infinity
14
+
15
+ # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
16
+ networks:
17
+ - default
18
+
19
+ depends_on:
20
+ - sqlserver
21
+
22
+ # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
23
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
24
+
25
+ sqlserver:
26
+ image: mcr.microsoft.com/mssql/server:2022-latest
27
+ restart: unless-stopped
28
+ networks:
29
+ - default
30
+ volumes:
31
+ - sqlserver-data:/var/opt/mssql
32
+ ports:
33
+ - "1433:1433"
34
+ environment:
35
+ MSSQL_SA_PASSWORD: MSSQLadmin!
36
+ ACCEPT_EULA: Y
37
+
38
+ networks:
39
+ default:
40
+
41
+ volumes:
42
+ sqlserver-data:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## v7.1.3
2
+
3
+ #### Fixed
4
+
5
+ - [#1152](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1152) Fix Composite Key Inserts with Triggers
6
+
7
+ ## v7.1.2
8
+
9
+ #### Fixed
10
+
11
+ - [#1151](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1151) FROM subquery should work if order provided
12
+
1
13
  ## v7.1.1
2
14
 
3
15
  #### Fixed
data/README.md CHANGED
@@ -13,7 +13,7 @@ Interested in older versions? We follow a rational versioning policy that tracks
13
13
 
14
14
  | Adapter Version | Rails Version | Support | Branch |
15
15
  |-----------------|---------------|---------|--------------------------------------------------------------------------------------------------|
16
- | `7.1.1` | `7.1.x` | Active | [main](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
16
+ | `7.1.3` | `7.1.x` | Active | [main](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
17
17
  | `7.0.5.1` | `7.0.x` | Active | [7-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/7-0-stable) |
18
18
  | `6.1.3.0` | `6.1.x` | Active | [6-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-1-stable) |
19
19
  | `6.0.3` | `6.0.x` | Ended | [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-0-stable) |
@@ -183,11 +183,49 @@ gem 'activerecord-sqlserver-adapter'
183
183
 
184
184
  ## Contributing
185
185
 
186
- If you would like to contribute a feature or bugfix, thanks! To make sure your fix/feature has a high chance of being added, please read the following guidelines. First, ask on the Gitter, or post a ticket on github issues. Second, make sure there are tests! We will not accept any patch that is not tested. Please read the [`RUNNING_UNIT_TESTS`](RUNNING_UNIT_TESTS.md) file for the details of how to run the unit tests.
186
+ Please contribute to the project by submitting bug fixes and features. To make sure your fix/feature has
187
+ a high chance of being added, please include tests in your pull request. To run the tests you will need to
188
+ setup your development environment.
187
189
 
188
- * Github: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
189
- * Gitter: https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter
190
+ ## Setting Up Your Development Environment
190
191
 
192
+ To run the test suite you can use any of the following methods below. See [RUNNING_UNIT_TESTS](RUNNING_UNIT_TESTS.md) for
193
+ more detailed information on running unit tests.
194
+
195
+ ### Dev Container CLI
196
+
197
+ With [Docker](https://www.docker.com) and [npm](https://github.com/npm/cli) installed, you can run [Dev Container CLI](https://github.com/devcontainers/cli) to
198
+ utilize the [`.devcontainer`](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main/.devcontainer) configuration from the command line.
199
+
200
+ ```bash
201
+ $ npm install -g @devcontainers/cli
202
+ $ cd activerecord-sqlserver-adapter
203
+ $ devcontainer up --workspace-folder .
204
+ $ devcontainer exec --workspace-folder . bash
205
+ ```
206
+
207
+ From within the container, you can run the tests using the following command:
208
+
209
+ ```bash
210
+ $ bundle install
211
+ $ bundle exec rake test
212
+ ```
213
+
214
+ _Note: The setup we use is based on the [Rails Dev Container setup.](https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#using-dev-container-cli)_
215
+
216
+ ### VirtualBox & Vagrant
217
+
218
+ The [activerecord-sqlserver-adapter-dev-box](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter-dev-box)
219
+ is a Vagrant/VirtualBox virtual machine that has MS SQL Server installed. However, the
220
+ activerecord-sqlserver-adapter-dev-box uses Vagrant and Virtual Box which will not work on Macs with Apple silicon.
221
+
222
+ ### Local Development
223
+
224
+ See the [RUNNING_UNIT_TESTS](RUNNING_UNIT_TESTS.md) file for the details of how to run the unit tests locally.
225
+
226
+ ## Community
227
+
228
+ There is a [Gitter channel](https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter) for the project where you are free to ask questions about the project.
191
229
 
192
230
  ## Credits & Contributions
193
231
 
@@ -1,9 +1,8 @@
1
+ # How To Run The Tests Locally
1
2
 
2
- # How To Run The Test!
3
+ The following is a description of how to run the tests for the SQL Server adapter on a local environment.
3
4
 
4
- This process is much easier than it has been before!
5
-
6
- ## MS SQL SERVER
5
+ ## MS SQL Server instance
7
6
 
8
7
  If you don't have easy access to MS SQL Server, you can set up a Vagrant/VirtualBox virtual machine with MS SQL Server. [Here's how](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter-dev-box).
9
8
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 7.1.1
1
+ 7.1.3
@@ -10,6 +10,8 @@ module ActiveRecord
10
10
  def load_records_for_keys(keys, &block)
11
11
  return super unless scope.connection.sqlserver?
12
12
 
13
+ return [] if keys.empty?
14
+
13
15
  if association_key_name.is_a?(Array)
14
16
  query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }
15
17
 
@@ -278,13 +278,13 @@ module ActiveRecord
278
278
  exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
279
279
 
280
280
  if exclude_output_inserted
281
- quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
281
+ quoted_pk = Array(pk).map { |subkey| SQLServer::Utils.extract_identifiers(subkey).quoted }
282
282
 
283
283
  id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
284
284
  <<~SQL.squish
285
- DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
286
- #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
287
- SELECT CAST(#{quoted_pk} AS #{id_sql_type}) FROM @ssaIdInsertTable
285
+ DECLARE @ssaIdInsertTable table (#{quoted_pk.map { |subkey| "#{subkey} #{id_sql_type}"}.join(", ") });
286
+ #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT #{ quoted_pk.map { |subkey| "INSERTED.#{subkey}" }.join(", ") } INTO @ssaIdInsertTable"}
287
+ SELECT #{quoted_pk.map {|subkey| "CAST(#{subkey} AS #{id_sql_type}) #{subkey}"}.join(", ")} FROM @ssaIdInsertTable
288
288
  SQL
289
289
  else
290
290
  returning_columns = returning || Array(pk)
@@ -213,7 +213,7 @@ module Arel
213
213
 
214
214
  def visit_Orders_And_Let_Fetch_Happen(o, collector)
215
215
  make_Fetch_Possible_And_Deterministic o
216
- unless o.orders.empty?
216
+ if o.orders.any?
217
217
  collector << " ORDER BY "
218
218
  len = o.orders.length - 1
219
219
  o.orders.each_with_index { |x, i|
@@ -261,15 +261,14 @@ module Arel
261
261
 
262
262
  def make_Fetch_Possible_And_Deterministic(o)
263
263
  return if o.limit.nil? && o.offset.nil?
264
+ return if o.orders.any?
264
265
 
265
266
  t = table_From_Statement o
266
267
  pk = primary_Key_From_Table t
267
268
  return unless pk
268
269
 
269
- if o.orders.empty?
270
- # Prefer deterministic vs a simple `(SELECT NULL)` expr.
271
- o.orders = [pk.asc]
272
- end
270
+ # Prefer deterministic vs a simple `(SELECT NULL)` expr.
271
+ o.orders = [pk.asc]
273
272
  end
274
273
 
275
274
  def distinct_One_As_One_Is_So_Not_Fetch(o)
@@ -42,6 +42,25 @@ class FetchTestSqlserver < ActiveRecord::TestCase
42
42
  end
43
43
  end
44
44
 
45
+ describe "FROM subquery" do
46
+ let(:from_sql) { "(SELECT [books].* FROM [books]) [books]" }
47
+
48
+ it "SQL generated correctly for FROM subquery if order provided" do
49
+ query = Book.from(from_sql).order(:id).limit(5)
50
+
51
+ assert_equal query.to_sql, "SELECT [books].* FROM (SELECT [books].* FROM [books]) [books] ORDER BY [books].[id] ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY"
52
+ assert_equal query.to_a.count, 5
53
+ end
54
+
55
+ it "exception thrown if FROM subquery is provided without an order" do
56
+ query = Book.from(from_sql).limit(5)
57
+
58
+ assert_raise(ActiveRecord::StatementInvalid) do
59
+ query.to_sql
60
+ end
61
+ end
62
+ end
63
+
45
64
  protected
46
65
 
47
66
  def create_10_books
@@ -28,4 +28,14 @@ class SQLServerTriggerTest < ActiveRecord::TestCase
28
28
  _(obj.id).must_be :present?
29
29
  _(obj.id.to_s).must_equal SSTestTriggerHistory.first.id_source
30
30
  end
31
+
32
+ it "can insert into a table with composite pk with output inserted - with a true setting for table name" do
33
+ exclude_output_inserted_table_names["sst_table_with_composite_pk_trigger"] = true
34
+ assert SSTestTriggerHistory.all.empty?
35
+ obj = SSTestTriggerCompositePk.create! pk_col_one: 123, pk_col_two: 42, event_name: "test trigger"
36
+ _(obj.event_name).must_equal "test trigger"
37
+ _(obj.pk_col_one).must_equal 123
38
+ _(obj.pk_col_two).must_equal 42
39
+ _(obj.pk_col_one.to_s).must_equal SSTestTriggerHistory.first.id_source
40
+ end
31
41
  end
@@ -7,3 +7,7 @@ end
7
7
  class SSTestTriggerUuid < ActiveRecord::Base
8
8
  self.table_name = "sst_table_with_uuid_trigger"
9
9
  end
10
+
11
+ class SSTestTriggerCompositePk < ActiveRecord::Base
12
+ self.table_name = "sst_table_with_composite_pk_trigger"
13
+ end
@@ -232,6 +232,23 @@ ActiveRecord::Schema.define do
232
232
  SELECT id AS id_source, event_name FROM INSERTED
233
233
  SQL
234
234
 
235
+ execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_table_with_composite_pk_trigger') DROP TABLE sst_table_with_composite_pk_trigger"
236
+ execute <<-SQL
237
+ CREATE TABLE sst_table_with_composite_pk_trigger(
238
+ pk_col_one int NOT NULL,
239
+ pk_col_two int NOT NULL,
240
+ event_name nvarchar(255),
241
+ CONSTRAINT PK_sst_table_with_composite_pk_trigger PRIMARY KEY (pk_col_one, pk_col_two)
242
+ )
243
+ SQL
244
+ execute <<-SQL
245
+ CREATE TRIGGER sst_table_with_composite_pk_trigger_t ON sst_table_with_composite_pk_trigger
246
+ FOR INSERT
247
+ AS
248
+ INSERT INTO sst_table_with_trigger_history (id_source, event_name)
249
+ SELECT pk_col_one AS id_source, event_name FROM INSERTED
250
+ SQL
251
+
235
252
  # Another schema.
236
253
 
237
254
  create_table :sst_schema_columns, force: true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sqlserver-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.1
4
+ version: 7.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2024-01-08 00:00:00.000000000 Z
18
+ date: 2024-02-15 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activerecord
@@ -53,6 +53,10 @@ executables: []
53
53
  extensions: []
54
54
  extra_rdoc_files: []
55
55
  files:
56
+ - ".devcontainer/Dockerfile"
57
+ - ".devcontainer/boot.sh"
58
+ - ".devcontainer/devcontainer.json"
59
+ - ".devcontainer/docker-compose.yml"
56
60
  - ".editorconfig"
57
61
  - ".github/issue_template.md"
58
62
  - ".github/workflows/ci.yml"
@@ -235,8 +239,8 @@ licenses:
235
239
  - MIT
236
240
  metadata:
237
241
  bug_tracker_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues
238
- changelog_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/v7.1.1/CHANGELOG.md
239
- source_code_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v7.1.1
242
+ changelog_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/v7.1.3/CHANGELOG.md
243
+ source_code_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v7.1.3
240
244
  post_install_message:
241
245
  rdoc_options: []
242
246
  require_paths:
@@ -252,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
256
  - !ruby/object:Gem::Version
253
257
  version: '0'
254
258
  requirements: []
255
- rubygems_version: 3.4.7
259
+ rubygems_version: 3.4.22
256
260
  signing_key:
257
261
  specification_version: 4
258
262
  summary: ActiveRecord SQL Server Adapter.