multi_schema 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_schema.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vlad Bokov
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.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # MultiSchema
2
+
3
+
4
+ Packed into a gem from here:
5
+ http://timnew.github.com/blog/2012/07/17/use-postgres-multiple-schema-database-in-rails/
6
+
7
+ Thanks. @TimNew!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'multi_schema'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install multi_schema
22
+
23
+ ## Usage
24
+
25
+ You can use it as the utility class in the old-fashioned way:
26
+
27
+ ```ruby
28
+ MultiSchema.with_in_schemas :except => :public do
29
+ # Play around the data in one schema
30
+ end
31
+ ```
32
+ Or you can use it in a DSL-like way:
33
+ ```ruby
34
+ class SomeMigration < ActiveRecord::Migration
35
+ include MultiSchema
36
+
37
+ def change
38
+ with_in_schemas :except => :public do
39
+ # Play around the data in one schema
40
+ end
41
+ end
42
+ end
43
+ ```
44
+
45
+ * with_in_schemas yield all user schemas in the database
46
+ * with_in_schemas :only => %w(schema1 schema2) populates all given schemas.
47
+ * with_in_schemas :except => %w(schema1 schema2) populates all except given schemas.
48
+ * with_in_schemas :except => [:public] is equivalent to with_in_schemas :except => ['public']
49
+ * with_in_schemas :only => [:public] is equivalent to with_in_schemas :only => :public and equivalent to with_in_schemas :public
50
+ * with_in_schemas :except => [:public] is equivalent to with_in_schemas :except => :public
51
+
52
+ ## TODO
53
+
54
+ tests, ci...
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "multi_schema/behaviors"
2
+
3
+ module MultiSchema
4
+ include Behaviors
5
+ extend Behaviors
6
+ end
@@ -0,0 +1,84 @@
1
+ module MultiSchema
2
+ module Behaviors
3
+ @@disable_message = false
4
+
5
+ def disable_message=(val)
6
+ @@disable_message = val
7
+ end
8
+
9
+ def disable_message
10
+ @@disable_message
11
+ end
12
+
13
+ def with_in_schemas(options = nil)
14
+ options = unify_type(options, Hash) { |items| {:only => items} }
15
+ options[:only] = unify_type(options[:only], Array) { |item| item.nil? ? all_schemas : [item] }.map { |item| item.to_s }
16
+ options[:except] =unify_type(options[:except], Array) { |item| item.nil? ? [] : [item] }.map { |item| item.to_s }
17
+
18
+ options[:only] = unify_array_item_type(options[:only], String) { |symbol| symbol.to_s }
19
+ options[:except] = unify_array_item_type(options[:except], String) { |symbol| symbol.to_s }
20
+
21
+ schema_list = options[:only].select { |schema| options[:except].exclude? schema }
22
+
23
+ schema_list.each do |schema|
24
+ set_schema_path schema
25
+ yield schema
26
+ end
27
+ reset_schema_path
28
+ end
29
+
30
+ def all_schemas
31
+ ActiveRecord::Base.connection.select_values <<-END
32
+ SELECT *
33
+ FROM pg_namespace
34
+ WHERE
35
+ nspname NOT IN ('information_schema') AND
36
+ nspname NOT LIKE 'pg%'
37
+ END
38
+ end
39
+
40
+ def current_schema
41
+ ActiveRecord::Base.connection.current_schema
42
+ end
43
+
44
+ def reset_schema_path
45
+ puts "--- Restore Schema to public" unless disable_message
46
+ ActiveRecord::Base.connection.schema_search_path = 'public'
47
+ end
48
+
49
+ def schema_path
50
+ ActiveRecord::Base.connection.schema_search_path
51
+ end
52
+
53
+ def set_schema_path(schema)
54
+ puts "--- Select Schema: #{schema} " unless disable_message
55
+ ActiveRecord::Base.connection.schema_search_path = schema
56
+ end
57
+
58
+ def push_schema_to_path(schema)
59
+ new_path = "#{schema}, #{schema_path}"
60
+ set_schema_path(new_path)
61
+ end
62
+
63
+ def pop_schema_from_path
64
+ new_path = schema_path.sub(/\w,/, '')
65
+ set_schema_path(new_path)
66
+ end
67
+
68
+ private
69
+
70
+ def unify_type(input, type)
71
+ if input.is_a?(type)
72
+ input
73
+ else
74
+ yield input
75
+ end
76
+ end
77
+
78
+ def unify_array_item_type(input, type, &block)
79
+ input.map do |item|
80
+ unify_type item, type, &block
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module MultiSchema
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_schema/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "multi_schema"
8
+ gem.version = MultiSchema::VERSION
9
+ gem.authors = ["Vlad Bokov"]
10
+ gem.email = ["razum2um@mail.ru"]
11
+ gem.description = %q{Allows you to switch over the postgres schemas in runtime inside ruby/rails}
12
+ gem.summary = %q{Path switch for postgresql tables accross schemas}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("rake")
21
+ gem.add_development_dependency('rspec')
22
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+
3
+ $dir = File.dirname(File.expand_path(__FILE__))
4
+ $LOAD_PATH.unshift $dir + '/../lib'
5
+ require 'multi_schema'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_schema
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Vlad Bokov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ name: rake
30
+ - !ruby/object:Gem::Dependency
31
+ type: :development
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ name: rspec
46
+ description: Allows you to switch over the postgres schemas in runtime inside ruby/rails
47
+ email:
48
+ - razum2um@mail.ru
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/multi_schema.rb
60
+ - lib/multi_schema/behaviors.rb
61
+ - lib/multi_schema/version.rb
62
+ - multi_schema.gemspec
63
+ - spec/spec_helper.rb
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Path switch for postgresql tables accross schemas
88
+ test_files:
89
+ - spec/spec_helper.rb
90
+ has_rdoc: