simple_mysql_partitioning 0.1.2 → 0.2.0
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 +5 -5
- data/.travis.yml +1 -1
- data/README.md +31 -2
- data/lib/simple_mysql_partitioning.rb +9 -2
- data/lib/simple_mysql_partitioning/adapter.rb +22 -0
- data/lib/simple_mysql_partitioning/base_partitioning.rb +23 -0
- data/lib/simple_mysql_partitioning/range.rb +26 -0
- data/lib/simple_mysql_partitioning/sql.rb +6 -6
- data/lib/simple_mysql_partitioning/version.rb +1 -1
- data/simple_mysql_partitioning.gemspec +2 -2
- metadata +16 -8
- data/lib/simple_mysql_partitioning/executor.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 59e0a9007776bd60b91764a3dac868fdfcbf23c8e4abfa6e57b6ed29068e6ed7
|
4
|
+
data.tar.gz: afba71ed17a0d99e111987e08b7152d8571ae098dcd86043d3133f6467267cce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b751bb14ee88a45e12c461716625770ffdfd8db54f08e434fc9a01a5e0012de5a33753b1ae4f6897761e5220b8bab1461eb9411cc443ae449b30fe8f7b4c5e2
|
7
|
+
data.tar.gz: d3acbe4c209918294712fafe0fff86a6ced399bab903f7de6eb56881a729352f213c8b1b0a46076953d3cff36f105d5462623d1973641a519e506e3ab542c877
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,7 @@ TODO: Delete this and the text above, and describe your gem
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'simple_mysql_partitioning'
|
12
|
+
gem 'simple_mysql_partitioning', '~> 0.2.0'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -22,7 +22,36 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
``` ruby
|
26
|
+
class DailyReport < ActiveRecord::Base
|
27
|
+
include SimpleMySQLPartitioning
|
28
|
+
|
29
|
+
# arg1: column
|
30
|
+
# type: partitiong type
|
31
|
+
partitioning_by :day, type: :range
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# partition name, less than value
|
36
|
+
pairs_name_with_values = [
|
37
|
+
['p201808', '2018-09-01']
|
38
|
+
]
|
39
|
+
|
40
|
+
# add partition
|
41
|
+
DailyReport.partition.add(pairs_name_with_values)
|
42
|
+
|
43
|
+
# reorganize partition
|
44
|
+
# If you want to reorganize partition, use this method and set reorganize partition name to second arg.
|
45
|
+
DailyReport.partition.reorganize(pairs_name_with_values, 'p999999')
|
46
|
+
|
47
|
+
# drop
|
48
|
+
DailyReport.partition.drop('p201808')
|
49
|
+
|
50
|
+
# exists?
|
51
|
+
DailyReport.partition.exists?('p201808')
|
52
|
+
```
|
53
|
+
|
54
|
+
We support only range partitioning.
|
26
55
|
|
27
56
|
## Development
|
28
57
|
|
@@ -1,9 +1,16 @@
|
|
1
1
|
require 'simple_mysql_partitioning/version'
|
2
2
|
require 'active_record'
|
3
3
|
require 'mysql2'
|
4
|
+
require 'simple_mysql_partitioning/adapter'
|
4
5
|
require 'simple_mysql_partitioning/sql'
|
5
|
-
require 'simple_mysql_partitioning/
|
6
|
+
require 'simple_mysql_partitioning/range'
|
7
|
+
require 'simple_mysql_partitioning/base_partitioning'
|
6
8
|
|
7
|
-
module
|
9
|
+
module SimpleMySQLPartitioning
|
8
10
|
# Your code goes here...
|
11
|
+
class << self
|
12
|
+
def included(klass)
|
13
|
+
klass.send :include, SimpleMySQLPartitioning::Adapter
|
14
|
+
end
|
15
|
+
end
|
9
16
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module SimpleMySQLPartitioning
|
2
|
+
module Adapter
|
3
|
+
def self.included(model)
|
4
|
+
model.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def partitioning_by(column, type:)
|
9
|
+
@partition_config = { column: column, type: type }
|
10
|
+
@partition = "SimpleMySQLPartitioning::#{type.to_s.classify}".constantize.new(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def partition
|
14
|
+
@partition
|
15
|
+
end
|
16
|
+
|
17
|
+
def partition_config
|
18
|
+
@partition_config
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'simple_mysql_partitioning/sql'
|
3
|
+
|
4
|
+
module SimpleMySQLPartitioning
|
5
|
+
class BasePartitioning
|
6
|
+
attr_reader :klass, :table_name
|
7
|
+
|
8
|
+
def initialize(klass)
|
9
|
+
@klass = klass
|
10
|
+
@table_name = klass.table_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def exists?(partition_name)
|
14
|
+
klass.connection.select_all(
|
15
|
+
SQL.exists_sql(table_name, partition_name)
|
16
|
+
).to_hash.present?
|
17
|
+
end
|
18
|
+
|
19
|
+
def drop(partition_name)
|
20
|
+
klass.connection.execute(SQL.parge_sql(table_name, partition_name))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'simple_mysql_partitioning/sql'
|
3
|
+
require 'simple_mysql_partitioning/base_partitioning'
|
4
|
+
|
5
|
+
module SimpleMySQLPartitioning
|
6
|
+
class Range < BasePartitioning
|
7
|
+
def add(pairs_name_with_value)
|
8
|
+
pairs_name_with_value.map do |pair|
|
9
|
+
add_partition_sql = SQL.add_sql(table_name, pair.first, pair.last)
|
10
|
+
klass.connection.execute(add_partition_sql)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def reorganize(pairs_name_with_value, reorganize_partition_name)
|
15
|
+
pairs_name_with_value.map do |pair|
|
16
|
+
klass.connection.execute(
|
17
|
+
SQL.reorganize_sql(
|
18
|
+
table_name,
|
19
|
+
pair.first, pair.last,
|
20
|
+
reorganize_partition_name
|
21
|
+
)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module SimpleMySQLPartitioning
|
2
2
|
class SQL
|
3
3
|
class << self
|
4
|
-
def
|
4
|
+
def exists_sql(table_name, partition_name)
|
5
5
|
"SELECT
|
6
6
|
table_schema,
|
7
7
|
table_name,
|
@@ -16,15 +16,15 @@ module SimpleMySQLPartitioning
|
|
16
16
|
|
17
17
|
def add_sql(table_name, partition_name, value)
|
18
18
|
"ALTER TABLE #{table_name}
|
19
|
-
|
20
|
-
VALUES LESS THAN ('#{value}'));"
|
19
|
+
ADD PARTITION ( PARTITION #{partition_name} VALUES LESS THAN ('#{value}'));"
|
21
20
|
end
|
22
21
|
|
23
22
|
def reorganize_sql(table_name, partition_name, value, reorganize_partition_name, max_value = 'MAXVALUE')
|
24
23
|
"ALTER TABLE #{table_name}
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
REORGANIZE PARTITION #{reorganize_partition_name} INTO (
|
25
|
+
PARTITION #{partition_name} VALUES LESS THAN ('#{value}'),
|
26
|
+
PARTITION #{reorganize_partition_name} VALUES LESS THAN #{max_value}
|
27
|
+
);"
|
28
28
|
end
|
29
29
|
|
30
30
|
def parge_sql(table_name, partition_name)
|
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
|
-
spec.add_dependency 'activerecord', ['>
|
24
|
+
spec.add_dependency 'activerecord', ['> 4.2.5', '<= 5.2.0']
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
-
spec.add_development_dependency 'mysql2', '~> 0.
|
26
|
+
spec.add_development_dependency 'mysql2', '~> 0.5.0'
|
27
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_mysql_partitioning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shunsuke Andoh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.2.5
|
20
|
+
- - "<="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.2.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - ">"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
29
|
+
version: 4.2.5
|
30
|
+
- - "<="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.2.0
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: bundler
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +50,14 @@ dependencies:
|
|
44
50
|
requirements:
|
45
51
|
- - "~>"
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
53
|
+
version: 0.5.0
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
58
|
- - "~>"
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.
|
60
|
+
version: 0.5.0
|
55
61
|
- !ruby/object:Gem::Dependency
|
56
62
|
name: rake
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,7 +103,9 @@ files:
|
|
97
103
|
- bin/setup
|
98
104
|
- docker/docker-compose.yml
|
99
105
|
- lib/simple_mysql_partitioning.rb
|
100
|
-
- lib/simple_mysql_partitioning/
|
106
|
+
- lib/simple_mysql_partitioning/adapter.rb
|
107
|
+
- lib/simple_mysql_partitioning/base_partitioning.rb
|
108
|
+
- lib/simple_mysql_partitioning/range.rb
|
101
109
|
- lib/simple_mysql_partitioning/sql.rb
|
102
110
|
- lib/simple_mysql_partitioning/version.rb
|
103
111
|
- simple_mysql_partitioning.gemspec
|
@@ -120,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
128
|
version: '0'
|
121
129
|
requirements: []
|
122
130
|
rubyforge_project:
|
123
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.7.7
|
124
132
|
signing_key:
|
125
133
|
specification_version: 4
|
126
134
|
summary: Generate partitioning sql for mysql
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'simple_mysql_partitioning/sql'
|
3
|
-
|
4
|
-
module SimpleMySQLPartitioning
|
5
|
-
class Executor
|
6
|
-
def initialize(klass, partition_name)
|
7
|
-
@klass = klass
|
8
|
-
@table_name = @klass.table_name
|
9
|
-
@partition_name = partition_name
|
10
|
-
end
|
11
|
-
|
12
|
-
def add(less_than_value)
|
13
|
-
return if exist?
|
14
|
-
add_partition_sql = SQL.add_sql(@table_name, @partition_name, less_than_value)
|
15
|
-
@klass.connection.execute(add_partition_sql)
|
16
|
-
end
|
17
|
-
|
18
|
-
def reorganize(less_than_value, reorganize_partition_name)
|
19
|
-
return if exist?
|
20
|
-
@klass.connection.execute(
|
21
|
-
SQL.reorganize_sql(
|
22
|
-
@table_name,
|
23
|
-
@partition_name, less_than_value,
|
24
|
-
reorganize_partition_name
|
25
|
-
)
|
26
|
-
)
|
27
|
-
end
|
28
|
-
|
29
|
-
def exist?
|
30
|
-
@klass.connection.select_all(
|
31
|
-
SQL.exist_sql(@table_name, @partition_name)
|
32
|
-
).to_hash.present?
|
33
|
-
end
|
34
|
-
|
35
|
-
def drop
|
36
|
-
return unless exist?
|
37
|
-
@klass.connection.execute(SQL.parge_sql(@table_name, @partition_name))
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|