mysql_partition 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 329d3a6196fd828a8804f20c07341ef7314bcae1
4
+ data.tar.gz: d77353e95f03dee0a78e471b9f004e39be4f4ab2
5
+ SHA512:
6
+ metadata.gz: e186ad65391e956aca95c421ad930dfbae7913d528a8155ff786254ff20aa37f4125cdeaa527e83f6303efceaa99cb3433e8e4324ae3a433a7598de461619b8f
7
+ data.tar.gz: 6e54a49841ececbfc90dd724114d4ab00aa036c550f71ca5858c3637493c9fc062ed78812957febceee1b57fb85c959c7ef3a751acea3e07c37c98a6e2fc6f8d
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_partition.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 hisaichi5518
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # MysqlPartition
2
+
3
+ Ruby port of Perl's MySQL::Partition
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require 'mysql_partition'
9
+
10
+ # List Partition
11
+ partition = MysqlPartition::SqlMaker.new(type: :list, table: "test", expression: "event_id")
12
+
13
+ partition.create_partitions("p1" => 1)
14
+ #=> ALTER TABLE test PARTITION BY LIST (event_id) (PARTITION p1 VALUES IN (1))
15
+
16
+ partition.add_partitions("p2" => "2, 3")
17
+ #=> ALTER TABLE test ADD PARTITION (PARTITION p2 VALUES IN (2, 3))
18
+
19
+ partition.drop_partitions("p1")
20
+ #=> ALTER TABLE test DROP PARTITION p1
21
+
22
+ # Range Partition
23
+ partition = MysqlPartition::SqlMaker.new(type: :range, table: "test2", expression: "created_at")
24
+
25
+ partition.create_partitions('p20100101' => '2010-01-01')
26
+ #=> ALTER TABLE test2 PARTITION BY RANGE (created_at) (PARTITION p20100101 VALUES LESS THAN ('2010-01-01'))
27
+
28
+ partition.add_partitions(
29
+ 'p20110101' => '2011-01-01',
30
+ 'p20120101' => '2012-01-01',
31
+ )
32
+ #=> ALTER TABLE test2 ADD PARTITION (PARTITION p20110101 VALUES LESS THAN ('2011-01-01'), PARTITION p20120101 VALUES LESS THAN ('2012-01-01'))
33
+
34
+ partition.drop_partitions('p20100101')
35
+ #=> ALTER TABLE test2 DROP PARTITION p20100101
36
+ ```
37
+
38
+ ## Installation
39
+
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'mysql_partition'
44
+ ```
45
+
46
+ And then execute:
47
+
48
+ $ bundle
49
+
50
+ Or install it yourself as:
51
+
52
+ $ gem install mysql_partition
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/hisaichi5518/mysql_partition/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,5 @@
1
+ require "mysql_partition/version"
2
+ require "mysql_partition/sql_maker"
3
+
4
+ module MysqlPartition
5
+ end
@@ -0,0 +1,46 @@
1
+ require "mysql_partition/type"
2
+
3
+ module MysqlPartition
4
+ class SqlMaker
5
+
6
+ attr_accessor :type, :table, :expression, :args
7
+ def initialize(hash)
8
+ if !(hash[:type] and hash[:table] and hash[:expression])
9
+ raise ArgumentError, "need type, table and expression"
10
+ end
11
+ @type = hash[:type].to_s.upcase
12
+ @table = hash[:table]
13
+ @expression = hash[:expression]
14
+ @args = hash
15
+
16
+ klass = Object.const_get "::MysqlPartition::Type::#{type.capitalize}"
17
+ self.class.prepend klass
18
+ end
19
+
20
+ def create_partitions(hash)
21
+ sprintf 'ALTER TABLE %s PARTITION BY %s (%s) (%s)',
22
+ table, type, expression, build_partition_parts(hash)
23
+ end
24
+
25
+ def add_partitions(hash)
26
+ sprintf 'ALTER TABLE %s ADD PARTITION (%s)',
27
+ table, build_partition_parts(hash)
28
+ end
29
+
30
+ def drop_partitions(*partition_names)
31
+ sprintf 'ALTER TABLE %s DROP PARTITION %s',
32
+ table, partition_names.join(', ')
33
+ end
34
+
35
+ private
36
+ # build_partition_parts("p1" => 1, ...)
37
+ def build_partition_parts(hash)
38
+ parts = []
39
+ hash.each do |partition_name, partition_description|
40
+ part = build_partition_part(partition_name, partition_description)
41
+ parts.push(part)
42
+ end
43
+ parts.join(", ")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ require "mysql_partition/type/list"
2
+ require "mysql_partition/type/range"
3
+
4
+ module MysqlPartition
5
+ module Type
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module MysqlPartition
2
+ module Type
3
+ module List
4
+
5
+ private
6
+ def build_partition_part(partition_name, partition_description)
7
+ # TODO: support comment, description
8
+ sprintf 'PARTITION %s VALUES IN (%s)',
9
+ partition_name, partition_description;
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ module MysqlPartition
2
+ module Type
3
+ module Range
4
+
5
+ def create_partitions(hash)
6
+ if catch_all_partition_name
7
+ hash.merge!(catch_all_partition_name => "MAXVALUE")
8
+ end
9
+ super(hash)
10
+ end
11
+
12
+ def add_catch_all_partition
13
+ unless catch_all_partition_name
14
+ raise "catch_all_partition_name isn't specified"
15
+ end
16
+
17
+ sprintf 'ALTER TABLE %s ADD PARTITION (%s)',
18
+ table, build_partition_part(catch_all_partition_name, 'MAXVALUE')
19
+ end
20
+
21
+ def reorganize_catch_all_partition(hash)
22
+ unless catch_all_partition_name
23
+ raise "catch_all_partition_name isn't specified"
24
+ end
25
+
26
+ sprintf 'ALTER TABLE %s REORGANIZE PARTITION %s INTO (%s, PARTITION %s VALUES LESS THAN (MAXVALUE))',
27
+ table, catch_all_partition_name, build_partition_parts(hash), catch_all_partition_name
28
+ end
29
+
30
+ private
31
+ def catch_all_partition_name
32
+ args[:catch_all_partition_name]
33
+ end
34
+
35
+ def build_partition_part(partition_name, partition_description)
36
+ # TODO: support comment, description
37
+ if string?(partition_description)
38
+ partition_description = "'#{partition_description}'"
39
+ end
40
+
41
+ sprintf 'PARTITION %s VALUES LESS THAN (%s)',
42
+ partition_name, partition_description;
43
+ end
44
+
45
+ def string?(v)
46
+ !(integer?(v) || maxvalue?(v) || function?(v))
47
+ end
48
+
49
+ def integer?(v)
50
+ v =~ /^[0-9]+$/
51
+ end
52
+
53
+ def maxvalue?(v)
54
+ v == 'MAXVALUE'
55
+ end
56
+
57
+ def function?(v)
58
+ v =~ /\(/
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module MysqlPartition
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_partition/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysql_partition"
8
+ spec.version = MysqlPartition::VERSION
9
+ spec.authors = ["hisaichi5518"]
10
+ spec.email = ["hisaichi5518@gmail.com"]
11
+ spec.summary = %q{Utility for MySQL partitioning}
12
+ spec.description = %q{Utility for MySQL partitioning}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe MysqlPartition::SqlMaker do
4
+ context "when type is :list" do
5
+ let :sql_maker do
6
+ MysqlPartition::SqlMaker.new(
7
+ type: :list,
8
+ table: "test",
9
+ expression: "event_id"
10
+ )
11
+ end
12
+
13
+ describe "#create_partitions" do
14
+ subject do
15
+ sql_maker.create_partitions("p1" => 1)
16
+ end
17
+
18
+ it "returns ALTER TABLE" do
19
+ is_expected.to eq "ALTER TABLE test PARTITION BY LIST (event_id) (PARTITION p1 VALUES IN (1))"
20
+ end
21
+ end
22
+
23
+ describe "#add_partitions" do
24
+ subject do
25
+ sql_maker.add_partitions("p2" => "2, 3")
26
+ end
27
+
28
+ it "returns ALTER TABLE" do
29
+ is_expected.to eq "ALTER TABLE test ADD PARTITION (PARTITION p2 VALUES IN (2, 3))"
30
+ end
31
+ end
32
+
33
+ describe "#drop_partitions" do
34
+ subject do
35
+ sql_maker.drop_partitions("p1")
36
+ end
37
+
38
+ it "returns ALTER TABLE" do
39
+ is_expected.to eq "ALTER TABLE test DROP PARTITION p1"
40
+ end
41
+ end
42
+ end
43
+
44
+
45
+ context "when type is :range" do
46
+ let :sql_maker do
47
+ MysqlPartition::SqlMaker.new(
48
+ type: :range,
49
+ table: "test2",
50
+ expression: "created_at"
51
+ )
52
+ end
53
+
54
+ describe "#create_partitions" do
55
+ subject do
56
+ sql_maker.create_partitions('p20100101' => '2010-01-01')
57
+ end
58
+
59
+ it "returns ALTER TABLE" do
60
+ is_expected.to eq "ALTER TABLE test2 PARTITION BY RANGE (created_at) (PARTITION p20100101 VALUES LESS THAN ('2010-01-01'))"
61
+ end
62
+ end
63
+
64
+ describe "#add_partitions" do
65
+ subject do
66
+ sql_maker.add_partitions(
67
+ 'p20110101' => '2011-01-01',
68
+ 'p20120101' => '2012-01-01',
69
+ )
70
+ end
71
+
72
+ it "returns ALTER TABLE" do
73
+ is_expected.to eq "ALTER TABLE test2 ADD PARTITION (PARTITION p20110101 VALUES LESS THAN ('2011-01-01'), PARTITION p20120101 VALUES LESS THAN ('2012-01-01'))"
74
+ end
75
+ end
76
+
77
+ describe "#drop_partitions" do
78
+ subject do
79
+ sql_maker.drop_partitions("p20100101")
80
+ end
81
+
82
+ it "returns ALTER TABLE" do
83
+ is_expected.to eq "ALTER TABLE test2 DROP PARTITION p20100101"
84
+ end
85
+ end
86
+ end
87
+
88
+
89
+ context "when type is :range and catch-all" do
90
+ let :sql_maker do
91
+ MysqlPartition::SqlMaker.new(
92
+ type: :range,
93
+ table: "test3",
94
+ expression: "TO_DAYS(created_at)",
95
+ catch_all_partition_name: "pmax",
96
+ )
97
+ end
98
+
99
+ describe "#create_partitions" do
100
+ subject do
101
+ sql_maker.create_partitions('p20100101' => "TO_DAYS('2010-01-01')")
102
+ end
103
+
104
+ it "returns ALTER TABLE" do
105
+ is_expected.to eq "ALTER TABLE test3 PARTITION BY RANGE (TO_DAYS(created_at)) (PARTITION p20100101 VALUES LESS THAN (TO_DAYS('2010-01-01')), PARTITION pmax VALUES LESS THAN (MAXVALUE))"
106
+ end
107
+ end
108
+
109
+ describe "#add_catch_all_partition" do
110
+ subject do
111
+ sql_maker.add_catch_all_partition
112
+ end
113
+
114
+ it "returns ALTER TABLE" do
115
+ is_expected.to eq "ALTER TABLE test3 ADD PARTITION (PARTITION pmax VALUES LESS THAN (MAXVALUE))"
116
+ end
117
+ end
118
+
119
+ describe "#reorganize_catch_all_partition" do
120
+ subject do
121
+ sql_maker.reorganize_catch_all_partition('p20110101' => "TO_DAYS('2011-01-01')")
122
+ end
123
+
124
+ it "returns ALTER TABLE" do
125
+ is_expected.to eq "ALTER TABLE test3 REORGANIZE PARTITION pmax INTO (PARTITION p20110101 VALUES LESS THAN (TO_DAYS('2011-01-01')), PARTITION pmax VALUES LESS THAN (MAXVALUE))"
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe MysqlPartition do
4
+ it 'has a version number' do
5
+ expect(MysqlPartition::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'mysql_partition'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_partition
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hisaichi5518
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Utility for MySQL partitioning
56
+ email:
57
+ - hisaichi5518@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/mysql_partition.rb
70
+ - lib/mysql_partition/sql_maker.rb
71
+ - lib/mysql_partition/type.rb
72
+ - lib/mysql_partition/type/list.rb
73
+ - lib/mysql_partition/type/range.rb
74
+ - lib/mysql_partition/version.rb
75
+ - mysql_partition.gemspec
76
+ - spec/mysql_partition/sql_maker_spec.rb
77
+ - spec/mysql_partition_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '2.0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Utility for MySQL partitioning
103
+ test_files:
104
+ - spec/mysql_partition/sql_maker_spec.rb
105
+ - spec/mysql_partition_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: