activerecord-tidb-adapter 6.1.0 → 6.1.1
Sign up to get free protection for your applications and to get access to all the features.
- 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.lock +1 -1
- data/README.md +5 -0
- 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 +3 -18
- 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.lock
CHANGED
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
@@ -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
|
-
|
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
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.
|
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
|