partitioner_pg 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/partitioner_pg/separation_type/month.rb +80 -0
- data/lib/partitioner_pg/version.rb +3 -0
- data/lib/partitioner_pg.rb +28 -0
- data/partitioner_pg.gemspec +34 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b4fb71f05532ad74b92aed33d82c8ba773818fb6
|
4
|
+
data.tar.gz: bac1502e2523fc7a3819feb15f427883d02cba79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ae342aa32bef1f79728cf5e4e8d700362f1b0f576e7b4bd9e82cbbee679f4d582cecee4a535c16ada96cbab5fa03a78c4cb066b7e33355dee4e4ac523819d5d5
|
7
|
+
data.tar.gz: b0b9b211ae6db60edda1cb7cebf2f379fb3158dfdde81cc1d0f8c784330b817df80c386539cceaed43761a2a6680e5a150780fce6e08dbf68e3df17ac75537c6
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nikulinaleksandr@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Partitioner
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/partitioner`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'partitioner'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install partitioner
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/partitioner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "partitioner"
|
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,80 @@
|
|
1
|
+
module PartitionerPg
|
2
|
+
module SeparationType
|
3
|
+
module Month
|
4
|
+
def create_next_month_table
|
5
|
+
create_month_table(Date.today.next_month)
|
6
|
+
end
|
7
|
+
|
8
|
+
def drop_old_month_table
|
9
|
+
drop_month_table(Date.today.prev_month.prev_month)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_month_table(date=Date.today)
|
13
|
+
date_start = date.at_beginning_of_month
|
14
|
+
date_end = date.at_beginning_of_month.next_month
|
15
|
+
sql = "CREATE TABLE IF NOT EXISTS #{name_of_partition_table(date)} (
|
16
|
+
CHECK ( created_at >= DATE('#{date_start}') AND created_at < DATE('#{date_end}') )
|
17
|
+
) INHERITS (#{table_name});"
|
18
|
+
execute_sql(sql)
|
19
|
+
sql = "ALTER TABLE #{name_of_partition_table(date)} ADD PRIMARY KEY (id);"
|
20
|
+
execute_sql(sql)
|
21
|
+
end
|
22
|
+
|
23
|
+
def drop_month_table(date=Date.today)
|
24
|
+
sql = "DROP TABLE IF EXISTS #{name_of_partition_table(date)};"
|
25
|
+
execute_sql(sql)
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_partitioning_by_month_trigger_sql
|
29
|
+
"CREATE OR REPLACE FUNCTION #{table_name}_insert_trigger() RETURNS trigger AS
|
30
|
+
$$
|
31
|
+
DECLARE
|
32
|
+
curY varchar(4);
|
33
|
+
curM varchar(2);
|
34
|
+
tbl varchar(30);
|
35
|
+
BEGIN
|
36
|
+
select cast(DATE_PART('year', new.created_at) as varchar) into curY;
|
37
|
+
select lpad(cast(DATE_PART('month', new.created_at) as varchar), 2, '0') into curM;
|
38
|
+
tbl := '#{table_name}_y' || curY || 'm' || curM;
|
39
|
+
EXECUTE format('INSERT into %I values ($1.*);', tbl) USING NEW;
|
40
|
+
return NEW;
|
41
|
+
END;
|
42
|
+
$$
|
43
|
+
LANGUAGE plpgsql;
|
44
|
+
|
45
|
+
CREATE TRIGGER #{table_name}_insert
|
46
|
+
BEFORE INSERT ON #{table_name}
|
47
|
+
FOR EACH ROW
|
48
|
+
EXECUTE PROCEDURE #{table_name}_insert_trigger();
|
49
|
+
|
50
|
+
-- Trigger function to delete from the master table after the insert
|
51
|
+
CREATE OR REPLACE FUNCTION #{table_name}_delete_trigger() RETURNS trigger
|
52
|
+
AS $$
|
53
|
+
DECLARE
|
54
|
+
r #{table_name}%rowtype;
|
55
|
+
BEGIN
|
56
|
+
DELETE FROM ONLY #{table_name} where id = new.id returning * into r;
|
57
|
+
RETURN r;
|
58
|
+
end;
|
59
|
+
$$
|
60
|
+
LANGUAGE plpgsql;
|
61
|
+
|
62
|
+
CREATE TRIGGER #{table_name}_after_insert
|
63
|
+
AFTER INSERT ON #{table_name}
|
64
|
+
FOR EACH ROW
|
65
|
+
EXECUTE PROCEDURE #{table_name}_delete_trigger();"
|
66
|
+
end
|
67
|
+
|
68
|
+
def drop_partitioning_by_month_trigger_sql
|
69
|
+
"DROP TRIGGER #{table_name}_insert ON #{table_name};
|
70
|
+
DROP FUNCTION #{table_name}_insert_trigger();
|
71
|
+
DROP TRIGGER #{table_name}_after_insert ON #{table_name};
|
72
|
+
DROP FUNCTION #{table_name}_delete_trigger();"
|
73
|
+
end
|
74
|
+
|
75
|
+
def name_of_partition_table(date=Date.today)
|
76
|
+
date.strftime("#{table_name}_y%Ym%m")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "partitioner_pg/version"
|
2
|
+
require "partitioner_pg/separation_type/month"
|
3
|
+
|
4
|
+
module PartitionerPg
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
#available types - :month
|
8
|
+
def initialize(type)
|
9
|
+
@type = type
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
include SeparationType::Month
|
14
|
+
|
15
|
+
def partitioner_pg(type)
|
16
|
+
new(type.to_sym)
|
17
|
+
puts TEST0
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def execute_sql(sql)
|
23
|
+
connection.execute(sql)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'partitioner_pg/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "partitioner_pg"
|
8
|
+
spec.version = PartitionerPg::VERSION
|
9
|
+
spec.authors = ["Aleksander Nikulin"]
|
10
|
+
spec.email = ["nikulinaleksandr@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Gem for a partitoning PG tables"
|
13
|
+
spec.description = "Gem for a partitoning PG tables"
|
14
|
+
spec.homepage = "http://appodeal.com"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: partitioner_pg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksander Nikulin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-24 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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
|
+
description: Gem for a partitoning PG tables
|
42
|
+
email:
|
43
|
+
- nikulinaleksandr@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CODE_OF_CONDUCT.md
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/partitioner_pg.rb
|
56
|
+
- lib/partitioner_pg/separation_type/month.rb
|
57
|
+
- lib/partitioner_pg/version.rb
|
58
|
+
- partitioner_pg.gemspec
|
59
|
+
homepage: http://appodeal.com
|
60
|
+
licenses: []
|
61
|
+
metadata:
|
62
|
+
allowed_push_host: https://rubygems.org
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.5.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Gem for a partitoning PG tables
|
83
|
+
test_files: []
|