activerecord-tidb-adapter 6.1.0 → 6.1.1

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: 5f82e1eb06fb93971e146fafade1c45e2e3c5909e3968f32a719c9581efb55f3
4
- data.tar.gz: 5a0bc0100489a41230115924cbff3f52e1ef2206441724116135ed01ccdd28da
3
+ metadata.gz: 841479ce689b846211133017869602520491030764a7844b5888946c5b71f606
4
+ data.tar.gz: 141316e2fb3464a8eee551b1d27a156ac1d17d4fb85efedce8f5bf190cb71a13
5
5
  SHA512:
6
- metadata.gz: 0b059eabd253b3e7e0ead32528ac380d874dd9ab42ff942ec86ff3ddec86257b00aa5aea3c44912f95d3c0c8ba2dc7e8ba0a33abff668f6100f929fc2ed45f61
7
- data.tar.gz: 216b0b9917670ca40cd30d3c243493e88aaf2279d946b68eaa927b0a65d78226e075279f12c8839f32a4d9be8b3edb7c348ba2038cd25bfa2845dc62aa9d4944
6
+ metadata.gz: 54d1ce5aba761df255809464ba50cc5ceaba25b2f68fb79a5f1e012e934bda42bc63f7946d74081c27d01972d90290a5736bc312bb41ef856a4d35f6dda338ec
7
+ data.tar.gz: 9737ed2ead8e72a7ade7deeea3afeae186761f3a29cc3eaa6d01d3b9366eacb1a97167cdc31f71e16aeec216b3613687599f375d8bcfcf83b64f43b3a40c91bc
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+
3
+ # Place this in /etc/buildkite-agent/hooks/pre-command
4
+ #
5
+ # Needs to be `chown buildkite-agent` and `chmod +x`
6
+
7
+ # RVM uses unbound variables and will fail without this
8
+ set +u
9
+
10
+ source /var/lib/buildkite-agent/.rvm/scripts/rvm
@@ -0,0 +1,7 @@
1
+ env:
2
+ MYSQL_HOST: 127.0.0.1
3
+ MYSQL_USER: root
4
+
5
+ steps:
6
+ - command: "testing.sh"
7
+ label: "Run TiDB ActiveRecord Adapter testing 6-1-stable"
@@ -0,0 +1,16 @@
1
+ #!/bin/bash
2
+ set -eu
3
+ service supervisor start
4
+
5
+ while sleep 60; do
6
+ ps aux |grep tidb |grep -q -v grep
7
+ TIDB_STATUS=$?
8
+ ps aux |grep tikv |grep -q -v grep
9
+ TIKV_STATUS=$?
10
+ # If the greps above find anything, they exit with 0 status
11
+ # If they are not both 0, then something is wrong
12
+ if [ $TIDB_STATUS -ne 0 -o $TIKV_STATUS -ne 0 ]; then
13
+ echo "One of the processes has already exited."
14
+ exit 1
15
+ fi
16
+ done
@@ -0,0 +1,35 @@
1
+ name: activerecord-tidb-adapter ci
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ paths-ignore:
8
+ - 'README*.md'
9
+ - 'docs/**'
10
+ pull_request:
11
+ branches:
12
+ - main
13
+ paths-ignore:
14
+ - 'README*.md'
15
+ - 'docs/**'
16
+
17
+ jobs:
18
+ build:
19
+ if: ${{ !contains(github.event.commits[0].message, '[skip ci]') }}
20
+ runs-on: ubuntu-latest
21
+ services:
22
+ tidb:
23
+ image: hawkingrei/tind:v5.1.0
24
+ options: --entrypoint /entrypoint.sh
25
+ env:
26
+ TIDB_VERSION: v5.1.0
27
+ ports: ["4000:4000"]
28
+ steps:
29
+ - uses: actions/checkout@v2
30
+ - uses: ruby/setup-ruby@v1
31
+ with:
32
+ ruby-version: 2.7
33
+ bundler-cache: true
34
+ - run: MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake db:tidb:build
35
+ - run: MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:tidb
data/Gemfile.lock CHANGED
@@ -87,7 +87,7 @@ GIT
87
87
  PATH
88
88
  remote: .
89
89
  specs:
90
- activerecord-tidb-adapter (6.1.0)
90
+ activerecord-tidb-adapter (6.1.1)
91
91
  activerecord (~> 6.1)
92
92
  mysql2
93
93
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # ActiveRecord TiDB Adapter
2
+ [![Build status](https://badge.buildkite.com/2cc593ab1a36b95eeb063ebc5cbecb40492177c03800ef33a4.svg)](https://buildkite.com/hooopo/tidb-dot-5-1-dot-0-ar-tidb-adapter-dot-6-1-stable)
2
3
 
3
4
  TiDB adapter for ActiveRecord 5 and 6. This is a lightweight extension of the mysql2 adapter that establishes compatibility with [TiDB](https://github.com/pingcap/tidb).
4
5
 
@@ -11,6 +12,10 @@ Add this line to your application's Gemfile:
11
12
  gem 'activerecord-tidb-adapter', "~> 6.1.0"
12
13
  ```
13
14
 
15
+ If you're using Rails 5.2, use the 5.2.x versions of this gem.
16
+
17
+ If you're using Rails 6.1, use the 6.1.x versions of this gem.
18
+
14
19
  And then execute:
15
20
 
16
21
  $ bundle install
data/config.toml CHANGED
@@ -1 +1,2 @@
1
1
  new_collations_enabled_on_first_bootstrap = true
2
+ allow-expression-index = true
@@ -0,0 +1,18 @@
1
+ require 'active_record/connection_adapters/abstract/database_statements'
2
+
3
+ ActiveRecord::ConnectionAdapters::DatabaseStatements.class_eval do
4
+ def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
5
+ sql, binds = to_sql_and_binds(arel, binds)
6
+ value = exec_insert(sql, name, binds, pk, sequence_name)
7
+ return id_value if id_value.present?
8
+ return last_inserted_id(value) if arel.is_a?(String)
9
+ model = arel.ast.relation.instance_variable_get(:@klass)
10
+ pk_def = schema_cache.columns_hash(model.table_name)[pk]
11
+ if pk_def&.default_function && pk_def.default_function =~ /nextval/
12
+ query_value("SELECT #{pk_def.default_function.sub('nextval', 'lastval')}")
13
+ else
14
+ last_inserted_id(value)
15
+ end
16
+ end
17
+ alias create insert
18
+ end
@@ -0,0 +1,71 @@
1
+ require 'active_record/connection_adapters/mysql/schema_statements'
2
+
3
+ ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements.class_eval do
4
+ def indexes(table_name)
5
+ indexes = []
6
+ current_index = nil
7
+ execute_and_free("SHOW KEYS FROM #{quote_table_name(table_name)}", "SCHEMA") do |result|
8
+ each_hash(result) do |row|
9
+ if current_index != row[:Key_name]
10
+ next if row[:Key_name] == "PRIMARY" # skip the primary key
11
+ current_index = row[:Key_name]
12
+
13
+ mysql_index_type = row[:Index_type].downcase.to_sym
14
+ case mysql_index_type
15
+ when :fulltext, :spatial
16
+ index_type = mysql_index_type
17
+ when :btree, :hash
18
+ index_using = mysql_index_type
19
+ end
20
+
21
+ indexes << [
22
+ row[:Table],
23
+ row[:Key_name],
24
+ row[:Non_unique].to_i == 0,
25
+ [],
26
+ lengths: {},
27
+ orders: {},
28
+ type: index_type,
29
+ using: index_using,
30
+ comment: row[:Index_comment].presence
31
+ ]
32
+ end
33
+
34
+ # FIX https://github.com/pingcap/tidb/issues/26110 for older version of TiDB
35
+ row[:Expression] = nil if row[:Expression] == 'NULL'
36
+
37
+ if row[:Expression]
38
+ expression = row[:Expression]
39
+ expression = +"(#{expression})" unless expression.start_with?("(")
40
+ indexes.last[-2] << expression
41
+ indexes.last[-1][:expressions] ||= {}
42
+ indexes.last[-1][:expressions][expression] = expression
43
+ indexes.last[-1][:orders][expression] = :desc if row[:Collation] == "D"
44
+ else
45
+ indexes.last[-2] << row[:Column_name]
46
+ indexes.last[-1][:lengths][row[:Column_name]] = row[:Sub_part].to_i if row[:Sub_part]
47
+ indexes.last[-1][:orders][row[:Column_name]] = :desc if row[:Collation] == "D"
48
+ end
49
+ end
50
+ end
51
+
52
+ indexes.map do |index|
53
+ options = index.pop
54
+
55
+ if expressions = options.delete(:expressions)
56
+ orders = options.delete(:orders)
57
+ lengths = options.delete(:lengths)
58
+
59
+ columns = index[-1].map { |name|
60
+ [ name.to_sym, expressions[name] || +quote_column_name(name) ]
61
+ }.to_h
62
+
63
+ index[-1] = add_options_for_index_columns(
64
+ columns, order: orders, length: lengths
65
+ ).values.join(", ")
66
+ end
67
+
68
+ ActiveRecord::ConnectionAdapters::IndexDefinition.new(*index, **options)
69
+ end
70
+ end
71
+ end
@@ -5,6 +5,8 @@ require 'active_record/connection_adapters/mysql2_adapter'
5
5
  require 'active_record/connection_adapters/tidb/setup'
6
6
  require_relative '../../version'
7
7
  require_relative '../sequence'
8
+ require_relative 'tidb/schema_statements'
9
+ require_relative 'tidb/database_statements'
8
10
 
9
11
  ActiveRecord::ConnectionAdapters::Tidb.initial_setup
10
12
 
@@ -65,7 +67,7 @@ module ActiveRecord
65
67
  end
66
68
 
67
69
  def supports_expression_index?
68
- true
70
+ false
69
71
  end
70
72
 
71
73
  def supports_common_table_expressions?
@@ -100,23 +102,6 @@ module ActiveRecord
100
102
  false
101
103
  end
102
104
 
103
- def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
104
- sql, binds = to_sql_and_binds(arel, binds)
105
- value = exec_insert(sql, name, binds, pk, sequence_name)
106
- return id_value if id_value.present?
107
-
108
- model = arel.ast.relation.instance_variable_get(:@klass)
109
- pk_def = schema_cache.columns_hash(model.table_name)[pk]
110
- if pk_def&.default_function && pk_def.default_function =~ /nextval/
111
- query_value("SELECT #{pk_def.default_function.sub('nextval', 'lastval')}")
112
- elsif model.sequence_name
113
- ActiveRecord::Base.lastval(model.sequence_name)
114
- else
115
- last_inserted_id(value)
116
- end
117
- end
118
- alias create insert
119
-
120
105
  def new_column_from_field(_table_name, field)
121
106
  type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
122
107
  default = field[:Default]
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
- TIDB_ADAPTER_VERSION = '6.1.0'
4
+ TIDB_ADAPTER_VERSION = '6.1.1'
5
5
  end
data/testing.sh ADDED
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+
3
+ set -eo pipefail
4
+
5
+ bundle config set --local path '/tmp/buildkite-cache'
6
+
7
+ echo "Setup gem mirror"
8
+ bundle config mirror.https://rubygems.org https://gems.ruby-china.com
9
+
10
+ echo "Bundle install"
11
+ bundle install
12
+
13
+ echo "Setup database for testing"
14
+ tidb=1 ARCONN=tidb bundle exec rake db:tidb:rebuild && tidb=1 ARCONN=tidb bundle exec rake test:tidb
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-tidb-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 6.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo Wang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-19 00:00:00.000000000 Z
11
+ date: 2021-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -45,6 +45,10 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - ".buildkite/hooks/pre-command"
49
+ - ".buildkite/pipeline.yml"
50
+ - ".github/entrypoint.sh"
51
+ - ".github/workflows/ci.yml"
48
52
  - ".gitignore"
49
53
  - Gemfile
50
54
  - Gemfile.lock
@@ -55,7 +59,9 @@ files:
55
59
  - bin/console
56
60
  - bin/setup
57
61
  - config.toml
62
+ - lib/active_record/connection_adapters/tidb/database_statements.rb
58
63
  - lib/active_record/connection_adapters/tidb/database_tasks.rb
64
+ - lib/active_record/connection_adapters/tidb/schema_statements.rb
59
65
  - lib/active_record/connection_adapters/tidb/setup.rb
60
66
  - lib/active_record/connection_adapters/tidb/type.rb
61
67
  - lib/active_record/connection_adapters/tidb_adapter.rb
@@ -66,6 +72,7 @@ files:
66
72
  - lib/active_record/sequence/schema_dumper.rb
67
73
  - lib/activerecord-tidb-adapter.rb
68
74
  - lib/version.rb
75
+ - testing.sh
69
76
  homepage: https://github.com/pingcap/activerecord-tidb-adapter
70
77
  licenses:
71
78
  - Apache-2.0