easy_partition 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40a1a8add96c1d6a6bdd2f6b10a5dc38db160826
4
+ data.tar.gz: 993cf42f0e62499803837d7f545494146e402321
5
+ SHA512:
6
+ metadata.gz: 1e7b627e65d4983c8dcb3c40a41ed94fe56acf6f5bac1c47b023483053e4756791c9c72061bed245df49c09741667c8323b4c1e77c9e883b57b1f85c69ecf9f3
7
+ data.tar.gz: 5abedc6cf9d0e751e0b78263fc76cbc0f57706dc6890ade079d4f59811aaa80b11a53fbf3ff5df697d0aaa6d11eef0cd7efad25706f18ad6ec2eb1145b6f4a09
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_partition.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Daniel Carneiro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # EasyPartition
2
+
3
+ First of all there's a way more mature gem on https://github.com/fiksu/partitioned that probably suits your need.
4
+
5
+ If you're still here, I decided to make this gem because the others solutions seem to have a lot of functionalities that I don't need.
6
+ This gem will allow you to create postgres partitions directly on your rails migrations.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'easy_partition'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install easy_partition
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/dcarneiro/easy_partition/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
35
+
36
+ ## LICENSE:
37
+
38
+ (The MIT License)
39
+
40
+ Copyright (c) 2015 Daniel Carneiro
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ end
7
+
8
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy_partition"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_partition/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "easy_partition"
8
+ s.version = EasyPartition::VERSION
9
+ s.authors = ["Daniel Carneiro"]
10
+ s.email = ["daniel.carneiro@nmusic.pt"]
11
+
12
+ s.summary = %q{A gem to manage postgres partitions.}
13
+ s.description = %q{A gem to manage postgres partitions only using the rails migration files to do it.}
14
+ s.homepage = "https://github.com/dcarneiro/easy_partition"
15
+
16
+ # # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # # delete this section to allow pushing this gem to any host.
18
+ # if s.respond_to?(:metadata)
19
+ # s.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ s.platform = Gem::Platform::RUBY
26
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
27
+ s.require_paths = ["lib"]
28
+
29
+ s.add_dependency 'activerecord', '>= 4.2.0', '< 4.3'
30
+ s.add_dependency 'activemodel', '>= 4.2.0', '< 4.3'
31
+
32
+ s.add_development_dependency "bundler", "~> 1.9"
33
+ s.add_development_dependency "minitest"
34
+ s.add_development_dependency 'minitest-reporters'
35
+
36
+ s.add_development_dependency "rake", "~> 10.0"
37
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require 'easy_partition/version'
3
+
4
+ module EasyPartition
5
+ autoload :ActiveRecord, 'easy_partition/active_record'
6
+ end
7
+
8
+ ActiveRecord::Base.extend(EasyPartition::ActiveRecord::Migration)
@@ -0,0 +1,5 @@
1
+ module EasyPartition
2
+ module ActiveRecord
3
+ autoload :Migration, 'easy_partition/active_record/migration'
4
+ end
5
+ end
@@ -0,0 +1,119 @@
1
+ module EasyPartition
2
+ module ActiveRecord
3
+ module Migration
4
+ def create_partitions_tables!(column_name, *partitions)
5
+ create_child_tables(column_name, partitions)
6
+ define_trigger(column_name, partitions)
7
+ enable_trigger
8
+ end
9
+
10
+ def drop_partitions_tables!(column_name, *partitions)
11
+ drop_trigger
12
+ drop_trigger_definition
13
+ drop_child_tables(column_name, partitions)
14
+ end
15
+
16
+ def create_child_tables(column_name, partitions)
17
+ partitions.each do |partition|
18
+ create_child_table(column_name, partition)
19
+ create_child_table_partition_index(column_name, partition)
20
+ end
21
+ end
22
+
23
+ def drop_child_tables(column_name, partitions)
24
+ partitions.each do |partition|
25
+ drop_child_table_partition_index(column_name, partition)
26
+ drop_child_table(partition)
27
+ end
28
+ end
29
+
30
+ def create_child_table(column_name, partition)
31
+ connection.execute <<-SQL
32
+ CREATE TABLE #{child_table_name(partition)} (
33
+ CHECK ( #{column_name} = '#{partition}' )
34
+ ) INHERITS (#{master_table});
35
+ SQL
36
+ end
37
+
38
+ def drop_child_table(partition)
39
+ connection.execute <<-SQL
40
+ DROP TABLE #{child_table_name(partition)};
41
+ SQL
42
+ end
43
+
44
+ def create_child_table_partition_index(column_name, partition)
45
+ connection.execute <<-SQL
46
+ CREATE INDEX #{child_table_index_name(column_name, partition)} ON #{child_table_name(partition)} (#{column_name});
47
+ SQL
48
+ end
49
+
50
+ def drop_child_table_partition_index(column_name, partition)
51
+ connection.execute <<-SQL
52
+ DROP INDEX #{child_table_index_name(column_name, partition)};
53
+ SQL
54
+ end
55
+
56
+ def define_trigger(column_name, partitions)
57
+ query = trigger_header
58
+ partitions.each do |partition|
59
+ query += %[IF (NEW.#{column_name} = '#{partition}') THEN
60
+ INSERT INTO #{child_table_name(partition)} VALUES (NEW.*);
61
+ ELS]
62
+ end
63
+ query += trigger_footer
64
+ connection.execute query
65
+ end
66
+
67
+ def drop_trigger_definition
68
+ connection.execute <<-SQL
69
+ DROP FUNCTION IF EXISTS #{master_table}_insert_trigger();
70
+ SQL
71
+ end
72
+
73
+ def enable_trigger
74
+ connection.execute <<-SQL
75
+ CREATE TRIGGER insert_#{master_table}_trigger
76
+ BEFORE INSERT ON #{master_table}
77
+ FOR EACH ROW EXECUTE PROCEDURE #{master_table}_insert_trigger();
78
+ SQL
79
+ end
80
+
81
+ def drop_trigger
82
+ connection.execute <<-SQL
83
+ DROP TRIGGER IF EXISTS insert_#{master_table}_trigger ON #{master_table};
84
+ SQL
85
+ end
86
+
87
+ def trigger_header
88
+ <<-SQL
89
+ CREATE OR REPLACE FUNCTION #{master_table}_insert_trigger()
90
+ RETURNS TRIGGER AS $$
91
+ BEGIN
92
+ SQL
93
+ end
94
+
95
+ def trigger_footer
96
+ %(E
97
+ RAISE EXCEPTION 'partition out of range';
98
+ END IF;
99
+ RETURN NULL;
100
+ END;
101
+ $$
102
+ LANGUAGE plpgsql;
103
+ )
104
+ end
105
+
106
+ def master_table
107
+ table_name
108
+ end
109
+
110
+ def child_table_name(partition)
111
+ [master_table, partition].join('_')
112
+ end
113
+
114
+ def child_table_index_name(column_name, partition)
115
+ [child_table_name(partition), column_name].join('_')
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module EasyPartition
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_partition
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Carneiro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 4.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activemodel
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 4.2.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '4.3'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 4.2.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '4.3'
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.9'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.9'
67
+ - !ruby/object:Gem::Dependency
68
+ name: minitest
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: minitest-reporters
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rake
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '10.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '10.0'
109
+ description: A gem to manage postgres partitions only using the rails migration files
110
+ to do it.
111
+ email:
112
+ - daniel.carneiro@nmusic.pt
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - easy_partition.gemspec
127
+ - lib/easy_partition.rb
128
+ - lib/easy_partition/active_record.rb
129
+ - lib/easy_partition/active_record/migration.rb
130
+ - lib/easy_partition/version.rb
131
+ homepage: https://github.com/dcarneiro/easy_partition
132
+ licenses: []
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.2.2
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A gem to manage postgres partitions.
154
+ test_files: []