activerecord-tidb-adapter 0.4.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 +4 -4
- data/.buildkite/hooks/pre-command +10 -0
- data/.buildkite/pipeline.yml +7 -0
- data/.github/entrypoint.sh +16 -0
- data/.github/workflows/ci.yml +35 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +10 -8
- data/README.md +14 -3
- data/config.toml +1 -0
- data/lib/active_record/connection_adapters/tidb/database_statements.rb +18 -0
- data/lib/active_record/connection_adapters/tidb/schema_statements.rb +71 -0
- data/lib/active_record/connection_adapters/tidb_adapter.rb +5 -3
- data/lib/active_record/sequence/adapter.rb +6 -1
- data/lib/version.rb +1 -1
- data/testing.sh +14 -0
- metadata +9 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 841479ce689b846211133017869602520491030764a7844b5888946c5b71f606
|
|
4
|
+
data.tar.gz: 141316e2fb3464a8eee551b1d27a156ac1d17d4fb85efedce8f5bf190cb71a13
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54d1ce5aba761df255809464ba50cc5ceaba25b2f68fb79a5f1e012e934bda42bc63f7946d74081c27d01972d90290a5736bc312bb41ef856a4d35f6dda338ec
|
|
7
|
+
data.tar.gz: 9737ed2ead8e72a7ade7deeea3afeae186761f3a29cc3eaa6d01d3b9366eacb1a97167cdc31f71e16aeec216b3613687599f375d8bcfcf83b64f43b3a40c91bc
|
|
@@ -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
data/Gemfile.lock
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
GIT
|
|
2
|
-
remote: https://github.com/
|
|
3
|
-
revision:
|
|
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 (
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
+
[](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
|
@@ -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
|
|
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)
|
|
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
|
-
|
|
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
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:
|
|
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-
|
|
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
|