activerecord-tidb-adapter 0.4.0 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85ed982e6735cfcb567e2d863975ba8f56e98dd6243578394e2ca01dc07ca91a
4
- data.tar.gz: c7aefb6293698e59ce8ff6812b156070622c6ed7c58689e39c4eca5946309959
3
+ metadata.gz: 841479ce689b846211133017869602520491030764a7844b5888946c5b71f606
4
+ data.tar.gz: 141316e2fb3464a8eee551b1d27a156ac1d17d4fb85efedce8f5bf190cb71a13
5
5
  SHA512:
6
- metadata.gz: af4ea51bd264d25cd621e3c95438fd9fd3c7df8b2dea533172cbeb0e07110e661dc68b0d294c23a1420e2b9c0a74334b14c46213d59425764a37336702d17034
7
- data.tar.gz: d07ec9fa2f944d6832c7f70287d4583073ea633b7c8364936801e555ac5592a4c5d21b18e9fff5b12a2599b4b8bfc637c5163cbb0b6604e0af1c5b388bc7f70f
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 CHANGED
@@ -14,7 +14,7 @@ gem 'pry'
14
14
 
15
15
  gem 'rubocop', '~> 1.18'
16
16
 
17
- gem 'rails', git: 'https://github.com/tidb-incubator/rails.git', branch: '6-1-stable'
17
+ gem 'rails', git: 'https://github.com/pingcap/rails.git', branch: '6-1-stable'
18
18
 
19
19
  gem 'byebug', '~> 11.1'
20
20
 
data/Gemfile.lock CHANGED
@@ -1,6 +1,6 @@
1
1
  GIT
2
- remote: https://github.com/tidb-incubator/rails.git
3
- revision: e377e0dc16e9841a048ec7eab2666c06155b6a0c
2
+ remote: https://github.com/pingcap/rails.git
3
+ revision: e2a3d3fb2ee24c709b9f32221fbcb66cfda5acfc
4
4
  branch: 6-1-stable
5
5
  specs:
6
6
  actioncable (6.1.4)
@@ -87,7 +87,7 @@ GIT
87
87
  PATH
88
88
  remote: .
89
89
  specs:
90
- activerecord-tidb-adapter (0.4.0)
90
+ activerecord-tidb-adapter (6.1.1)
91
91
  activerecord (~> 6.1)
92
92
  mysql2
93
93
 
@@ -105,7 +105,7 @@ GEM
105
105
  activesupport (>= 5.0)
106
106
  i18n (1.8.10)
107
107
  concurrent-ruby (~> 1.0)
108
- loofah (2.11.0)
108
+ loofah (2.12.0)
109
109
  crass (~> 1.0.2)
110
110
  nokogiri (>= 1.5.9)
111
111
  mail (2.7.1)
@@ -118,6 +118,8 @@ GEM
118
118
  minitest (~> 5.0)
119
119
  mysql2 (0.5.3)
120
120
  nio4r (2.5.8)
121
+ nokogiri (1.12.3-x86_64-darwin)
122
+ racc (~> 1.4)
121
123
  nokogiri (1.12.3-x86_64-linux)
122
124
  racc (~> 1.4)
123
125
  parallel (1.20.1)
@@ -134,22 +136,22 @@ GEM
134
136
  rails-dom-testing (2.0.3)
135
137
  activesupport (>= 4.2.0)
136
138
  nokogiri (>= 1.6)
137
- rails-html-sanitizer (1.3.0)
139
+ rails-html-sanitizer (1.4.1)
138
140
  loofah (~> 2.3)
139
141
  rainbow (3.0.0)
140
142
  rake (13.0.6)
141
143
  regexp_parser (2.1.1)
142
144
  rexml (3.2.5)
143
- rubocop (1.18.4)
145
+ rubocop (1.19.0)
144
146
  parallel (~> 1.10)
145
147
  parser (>= 3.0.0.0)
146
148
  rainbow (>= 2.2.2, < 4.0)
147
149
  regexp_parser (>= 1.8, < 3.0)
148
150
  rexml
149
- rubocop-ast (>= 1.8.0, < 2.0)
151
+ rubocop-ast (>= 1.9.1, < 2.0)
150
152
  ruby-progressbar (~> 1.7)
151
153
  unicode-display_width (>= 1.4.0, < 3.0)
152
- rubocop-ast (1.8.0)
154
+ rubocop-ast (1.10.0)
153
155
  parser (>= 3.0.1.1)
154
156
  ruby-progressbar (1.11.0)
155
157
  sprockets (4.0.2)
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
 
@@ -8,9 +9,13 @@ TiDB adapter for ActiveRecord 5 and 6. This is a lightweight extension of the my
8
9
  Add this line to your application's Gemfile:
9
10
 
10
11
  ```ruby
11
- gem 'activerecord-tidb-adapter'
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
@@ -140,11 +145,17 @@ MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle e
140
145
 
141
146
  ```
142
147
 
143
- run tidb adapter tests
148
+ run tidb adapter tests and activerecord buildin tests
149
+
150
+ ```
151
+ MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:tidb
144
152
 
145
153
  ```
146
- MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:idb
147
154
 
155
+ run **ONLY** tidb adapter tests using `ONLY_TEST_TIDB` env:
156
+
157
+ ```
158
+ ONLY_TEST_TIDB=1 MYSQL_USER=root MYSQL_HOST=127.0.0.1 MYSQL_PORT=4000 tidb=1 ARCONN=tidb bundle exec rake test:tidb
148
159
  ```
149
160
 
150
161
  ## Contributing
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,13 +5,15 @@ 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
 
11
13
  module ActiveRecord
12
- module ConnectionHandling #:nodoc:
14
+ module ConnectionHandling # :nodoc:
13
15
  # Establishes a connection to the database that's used by all Active Record objects.
14
- def tidb_connection(config) #:nodoc:
16
+ def tidb_connection(config) # :nodoc:
15
17
  config = config.symbolize_keys
16
18
  config[:flags] ||= 0
17
19
 
@@ -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?
@@ -37,9 +37,14 @@ module ActiveRecord
37
37
 
38
38
  def drop_sequence(name)
39
39
  name = quote_column_name(name)
40
- sql = "DROP SEQUENCE #{name}"
40
+ sql = "DROP SEQUENCE IF EXISTS #{name}"
41
41
  execute(sql)
42
42
  end
43
+
44
+ def recreate_sequence(name, options = {})
45
+ drop_sequence(name)
46
+ create_sequence(name, options)
47
+ end
43
48
  end
44
49
  end
45
50
  end
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 = '0.4.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: 0.4.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-15 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