pg_partitioning 0.0.1
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/MIT-LICENSE +20 -0
- data/README.rdoc +97 -0
- data/Rakefile +23 -0
- data/config/locales/en.yml +28 -0
- data/lib/generators/partitioning_generator.rb +18 -0
- data/lib/pg_partitioning/input_master.rb +83 -0
- data/lib/pg_partitioning/partitioning_master.rb +78 -0
- data/lib/pg_partitioning/printer.rb +28 -0
- data/lib/pg_partitioning/strategies/base.rb +72 -0
- data/lib/pg_partitioning/strategies/date.rb +73 -0
- data/lib/pg_partitioning/strategies/equal.rb +43 -0
- data/lib/pg_partitioning/strategies/step.rb +57 -0
- data/lib/pg_partitioning/version.rb +3 -0
- data/lib/pg_partitioning.rb +4 -0
- data/lib/tasks/pg_partitioning_tasks.rake +4 -0
- data/spec/db_helpers.rb +36 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/bandit.rb +4 -0
- data/spec/dummy/app/models/crime.rb +3 -0
- data/spec/dummy/app/models/gang.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +32 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +28 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20160306173540_create_gangs.rb +9 -0
- data/spec/dummy/db/migrate/20160306174017_create_bandits.rb +12 -0
- data/spec/dummy/db/migrate/20160306174042_create_crimes.rb +10 -0
- data/spec/dummy/db/schema.rb +47 -0
- data/spec/dummy/log/development.log +3421 -0
- data/spec/dummy/log/production.log +0 -0
- data/spec/dummy/log/test.log +154834 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/bandits.rb +23 -0
- data/spec/factories/crimes.rb +6 -0
- data/spec/factories/gangs.rb +5 -0
- data/spec/input_master_spec.rb +74 -0
- data/spec/models/bandit_spec.rb +13 -0
- data/spec/models/crime_spec.rb +10 -0
- data/spec/models/gang_spec.rb +10 -0
- data/spec/partitioning_master_spec.rb +156 -0
- data/spec/rails_helper.rb +67 -0
- data/spec/shared_examples.rb +28 -0
- data/spec/spec_helper.rb +92 -0
- metadata +213 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'pg_partitioning/strategies/base'
|
2
|
+
|
3
|
+
module PgPartitioning
|
4
|
+
module Strategies
|
5
|
+
class Date < Base
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
if super
|
11
|
+
@error_message = if !(@data_type.include?('timestamp') || @data_type.include?('date'))
|
12
|
+
I18n.t "pg_partitioning.failure.column_type"
|
13
|
+
elsif (@cond.blank? || !cond_valid?)
|
14
|
+
I18n.t "pg_partitioning.failure.blank_cond"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@error_message.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
def cond_valid?
|
21
|
+
res = []
|
22
|
+
patterns = { year: /(?<year>Y{1,4})/,
|
23
|
+
month: /(?<month>M{2})/,
|
24
|
+
day: /(?<day>D{2})/,
|
25
|
+
week: /(?<week>W{1,2})/,
|
26
|
+
hour: /(?<hour>HH24{1})/ }
|
27
|
+
|
28
|
+
patterns.each do |key, value|
|
29
|
+
match = @cond.match value
|
30
|
+
res << match[key] if match
|
31
|
+
end
|
32
|
+
@cond = res.join '_'
|
33
|
+
!@cond.blank?
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_insert_master_function
|
37
|
+
@sql.execute "CREATE OR REPLACE FUNCTION #{@table_name}_insert_master() RETURNS TRIGGER AS $$
|
38
|
+
DECLARE
|
39
|
+
colname text := '#{@column_name.to_sym}';
|
40
|
+
colval timestamp := NEW.#{@column_name.to_sym};
|
41
|
+
pattern text := '#{@cond.to_sym}';
|
42
|
+
sample text := TO_CHAR(colval, pattern);
|
43
|
+
tblname text := '#{@table_name}_' || sample;
|
44
|
+
BEGIN
|
45
|
+
IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=tblname) THEN
|
46
|
+
EXECUTE 'CREATE TABLE '
|
47
|
+
|| tblname
|
48
|
+
|| '(check (TO_CHAR('
|
49
|
+
|| quote_ident(colname)
|
50
|
+
|| ','
|
51
|
+
|| quote_literal(pattern)
|
52
|
+
|| ')='
|
53
|
+
|| quote_literal(sample)
|
54
|
+
|| ')) INHERITS ('
|
55
|
+
|| TG_RELNAME
|
56
|
+
|| ');';
|
57
|
+
EXECUTE 'CREATE INDEX '
|
58
|
+
|| quote_ident('index_' || tblname)
|
59
|
+
|| ' ON '
|
60
|
+
|| quote_ident(tblname)
|
61
|
+
|| ' (id, '
|
62
|
+
|| quote_ident(colname)
|
63
|
+
|| ');';
|
64
|
+
END IF;
|
65
|
+
EXECUTE 'INSERT INTO ' || tblname || ' SELECT ($1).*' USING NEW;
|
66
|
+
RETURN NEW;
|
67
|
+
END;
|
68
|
+
$$ LANGUAGE plpgsql;"
|
69
|
+
info I18n.t("pg_partitioning.progress.insert_master", state: "OK")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'pg_partitioning/strategies/base'
|
2
|
+
|
3
|
+
module PgPartitioning
|
4
|
+
module Strategies
|
5
|
+
class Equal < Base
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def create_insert_master_function
|
10
|
+
@sql.execute "CREATE OR REPLACE FUNCTION #{@table_name}_insert_master() RETURNS TRIGGER AS $$
|
11
|
+
DECLARE
|
12
|
+
colname text := '#{@column_name.to_sym}';
|
13
|
+
colval text := NEW.#{@column_name.to_sym};
|
14
|
+
tblname text := '#{@table_name}_' || colval;
|
15
|
+
BEGIN
|
16
|
+
IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=tblname) THEN
|
17
|
+
EXECUTE 'CREATE TABLE '
|
18
|
+
|| tblname
|
19
|
+
|| '(check ('
|
20
|
+
|| quote_ident(colname)
|
21
|
+
|| '='
|
22
|
+
|| quote_literal(colval)
|
23
|
+
|| ')) INHERITS ('
|
24
|
+
|| TG_RELNAME
|
25
|
+
|| ');';
|
26
|
+
EXECUTE 'CREATE INDEX '
|
27
|
+
|| quote_ident('index_' || tblname)
|
28
|
+
|| ' ON '
|
29
|
+
|| quote_ident(tblname)
|
30
|
+
|| ' (id, '
|
31
|
+
|| quote_ident(colname)
|
32
|
+
|| ');';
|
33
|
+
END IF;
|
34
|
+
EXECUTE 'INSERT INTO ' || tblname || ' SELECT ($1).*' USING NEW;
|
35
|
+
RETURN NEW;
|
36
|
+
END;
|
37
|
+
$$ LANGUAGE plpgsql;"
|
38
|
+
info I18n.t("pg_partitioning.progress.insert_master", state: "OK")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'pg_partitioning/strategies/base'
|
2
|
+
|
3
|
+
module PgPartitioning
|
4
|
+
module Strategies
|
5
|
+
class Step < Base
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
if super
|
9
|
+
@error_message = if !@data_type.include? 'integer'
|
10
|
+
I18n.t "pg_partitioning.failure.column_type"
|
11
|
+
elsif @cond.blank?
|
12
|
+
I18n.t "pg_partitioning.failure.blank_cond"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
@error_message.blank?
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def create_insert_master_function
|
21
|
+
@sql.execute "CREATE OR REPLACE FUNCTION #{@table_name}_insert_master() RETURNS TRIGGER AS $$
|
22
|
+
DECLARE
|
23
|
+
colname text := '#{@column_name.to_sym}';
|
24
|
+
colval integer := NEW.#{@column_name.to_sym};
|
25
|
+
threshold integer := '#{@cond}';
|
26
|
+
step integer := ROUND(colval / threshold);
|
27
|
+
tblname text := '#{@table_name}_' || step;
|
28
|
+
BEGIN
|
29
|
+
IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=tblname) THEN
|
30
|
+
EXECUTE 'CREATE TABLE '
|
31
|
+
|| tblname
|
32
|
+
|| '(check (ROUND('
|
33
|
+
|| quote_ident(colname)
|
34
|
+
|| '/'
|
35
|
+
|| threshold
|
36
|
+
|| ')='
|
37
|
+
|| step
|
38
|
+
|| ')) INHERITS ('
|
39
|
+
|| TG_RELNAME
|
40
|
+
|| ');';
|
41
|
+
EXECUTE 'CREATE INDEX '
|
42
|
+
|| quote_ident('index_' || tblname)
|
43
|
+
|| ' ON '
|
44
|
+
|| quote_ident(tblname)
|
45
|
+
|| ' (id, '
|
46
|
+
|| quote_ident(colname)
|
47
|
+
|| ');';
|
48
|
+
END IF;
|
49
|
+
EXECUTE 'INSERT INTO ' || tblname || ' SELECT ($1).*' USING NEW;
|
50
|
+
RETURN NEW;
|
51
|
+
END;
|
52
|
+
$$ LANGUAGE plpgsql;"
|
53
|
+
info I18n.t("pg_partitioning.progress.insert_master", state: "OK")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/db_helpers.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module DbHelpers
|
2
|
+
def clear_triggers(table)
|
3
|
+
sql = <<-SQL
|
4
|
+
DROP TRIGGER IF EXISTS #{table}_before_insert_trigger ON #{table};
|
5
|
+
DROP TRIGGER IF EXISTS #{table}_after_insert_trigger ON #{table};
|
6
|
+
SQL
|
7
|
+
ActiveRecord::Base.connection.execute(sql)
|
8
|
+
end
|
9
|
+
|
10
|
+
def insert_master_function_exists?(table)
|
11
|
+
sql = <<-SQL
|
12
|
+
SELECT 1 FROM pg_trigger, pg_proc
|
13
|
+
WHERE pg_proc.oid=pg_trigger.tgfoid
|
14
|
+
AND pg_trigger.tgname = '#{table}_before_insert_trigger';
|
15
|
+
SQL
|
16
|
+
result = nil
|
17
|
+
ActiveRecord::Base.connection.execute(sql).each do |r|
|
18
|
+
result = r["?column?"]
|
19
|
+
end
|
20
|
+
result.to_i == 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def after_insert_trigger_exists?(table)
|
24
|
+
sql = <<-SQL
|
25
|
+
SELECT 1 FROM pg_trigger, pg_proc
|
26
|
+
WHERE pg_proc.oid=pg_trigger.tgfoid
|
27
|
+
AND pg_trigger.tgname = '#{table}_after_insert_trigger';
|
28
|
+
SQL
|
29
|
+
result = nil
|
30
|
+
ActiveRecord::Base.connection.execute(sql).each do |r|
|
31
|
+
result = r["?column?"]
|
32
|
+
end
|
33
|
+
result.to_i == 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
# Pick the frameworks you want:
|
4
|
+
require "active_record/railtie"
|
5
|
+
require "action_controller/railtie"
|
6
|
+
require "action_mailer/railtie"
|
7
|
+
require "action_view/railtie"
|
8
|
+
require "sprockets/railtie"
|
9
|
+
# require "rails/test_unit/railtie"
|
10
|
+
|
11
|
+
Bundler.require(*Rails.groups)
|
12
|
+
require "pg_partitioning"
|
13
|
+
|
14
|
+
module Dummy
|
15
|
+
class Application < Rails::Application
|
16
|
+
# Settings in config/environments/* take precedence over those specified here.
|
17
|
+
# Application configuration should go into files in config/initializers
|
18
|
+
# -- all .rb files in that directory are automatically loaded.
|
19
|
+
|
20
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
21
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
22
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
23
|
+
|
24
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
25
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
26
|
+
# config.i18n.default_locale = :de
|
27
|
+
|
28
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
29
|
+
config.active_record.raise_in_transactional_callbacks = true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: postgresql
|
9
|
+
uncoding: unicode
|
10
|
+
pool: 5
|
11
|
+
timeout: 5000
|
12
|
+
username: victor
|
13
|
+
password: mAGArLAm0v
|
14
|
+
|
15
|
+
development:
|
16
|
+
<<: *default
|
17
|
+
database: dev_pg
|
18
|
+
|
19
|
+
# Warning: The database defined as "test" will be erased and
|
20
|
+
# re-generated from your development database when you run "rake".
|
21
|
+
# Do not set this db to the same as development or production.
|
22
|
+
test:
|
23
|
+
<<: *default
|
24
|
+
database: test_pg
|
25
|
+
|
26
|
+
production:
|
27
|
+
<<: *default
|
28
|
+
database: prod_pg
|