sequel-postgres-schemata 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d37a44aec26c81236ec3faed93d070e979470d24
4
- data.tar.gz: 8f45729d8dfb844871fe82fb87fe2b5887675c56
3
+ metadata.gz: 82a2bd1a15e792cf64681981c3be64c1e11d71cb
4
+ data.tar.gz: cedd9abd2407e342c4b5f1e1e8f99dbe20086004
5
5
  SHA512:
6
- metadata.gz: 97eae6ecccd8c8244812da30778488380b8f8feb947fea5f544e521dc3726af14d53f8f8e74f4026d347cb6d570ea8cf8e4824e27d6ef2f80b8551b68d0653cf
7
- data.tar.gz: 2845aa0d56e81c4f164836d51210f1c10cf0661f5a6ed1ac59e33f4fc2af39d0dacef095d1258a89e55e90f14fc394ecd638452ca258b67e256ef1a6e0cefc43
6
+ metadata.gz: a0774f6ba877d48a5c9c81cb9254768eb7717529fb30362c209e208f054e8fc720a4d8cec06495c7fa76cd9681cad5fb7e3511c21f7e0a4843541c12137f6ad0
7
+ data.tar.gz: d273ce571db7b5f2e73214a99b3406b01afa3f20e7ac4e1014182766595d411f0c017c403eaa92c3e4e705b0817aeca1fea4ac78b8412e9b04d69c587d753726
data/README.md CHANGED
@@ -24,12 +24,22 @@ db = Sequel.connect adapter: 'postgres', search_path: %w(foo public)
24
24
  db.create_schema :bar
25
25
 
26
26
  db.search_path # => [:foo, :public]
27
+
28
+ db.search_path :baz do
29
+ db.search_path # => [:baz]
30
+ end
31
+
32
+ db.search_path :baz, prepend: true do
33
+ db.search_path # => [:baz, :foo, :public]
34
+ end
35
+
27
36
  db.schemata # => [:pg_toast, :pg_temp_1, :pg_toast_temp_1, :pg_catalog, :public, :information_schema, :bar]
28
37
  db.current_schemata # => [:public]
29
38
  db.search_path = [:bar, :foo, :public]
30
39
  db.current_schemata # => [:bar, :public]
31
40
  db.rename_schema :bar, :foo
32
41
  db.current_schemata # => [:foo, :public]
42
+
33
43
  ```
34
44
 
35
45
  ## Contributing
@@ -15,16 +15,24 @@ module Sequel
15
15
 
16
16
  # Returns a symbol list containing the current search path.
17
17
  # Note that the search path can contain non-existent schematas.
18
- def search_path
19
- metadata_dataset.with_sql(SHOW_SEARCH_PATH).
20
- single_value.scan(SCHEMA_SCAN_RE).flatten.
21
- map{|s|s.strip.sub(SCHEMA_SUB_RE, '\1').gsub('""', '"').to_sym}
18
+ # If given a block and an argument, instead temporarily changes
19
+ # the search path inside the block. It also accepts several arguments,
20
+ # in which case it treats them as an array of schemata to put in search path.
21
+ # If you use prepend: true, it prepends any given schemata to the current search path.
22
+ def search_path *a, prepend: false, &block
23
+ if block_given?
24
+ a = a.flatten
25
+ a += search_path if prepend
26
+ run_with_search_path a, &block
27
+ else
28
+ get_search_path
29
+ end
22
30
  end
23
31
 
24
32
  # Sets the search path. Starting with Postgres 9.2 it can contain
25
33
  # non-existent schematas.
26
- # Accepted formats include a single symbol, a single string (passed
27
- # to the server verbatim) and lists of symbols or strings.
34
+ # Accepted formats include a single symbol, a single string (split on ,)
35
+ # and lists of symbols or strings.
28
36
  def search_path= search_path
29
37
  case search_path
30
38
  when String
@@ -52,7 +60,20 @@ module Sequel
52
60
  end
53
61
 
54
62
  private
55
-
63
+
64
+ def get_search_path
65
+ metadata_dataset.with_sql(SHOW_SEARCH_PATH).
66
+ single_value.scan(SCHEMA_SCAN_RE).flatten.
67
+ map{|s|s.strip.sub(SCHEMA_SUB_RE, '\1').gsub('""', '"').to_sym}
68
+ end
69
+
70
+ def run_with_search_path path, &block
71
+ old_path = search_path
72
+ self.search_path = path
73
+ yield
74
+ self.search_path = old_path
75
+ end
76
+
56
77
  SHOW_SEARCH_PATH = "SHOW search_path".freeze
57
78
  SCHEMA_SCAN_RE = /(?<=\A|,)(".*?"|.*?)(?=,|\z)/.freeze
58
79
  SCHEMA_SUB_RE = /\A"(.*)"\z/.freeze
@@ -1,7 +1,7 @@
1
1
  module Sequel
2
2
  module Postgres
3
3
  module Schemata
4
- VERSION = "0.0.4"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -21,6 +21,29 @@ describe Sequel::Postgres::Schemata do
21
21
  it "correctly handles the default list" do
22
22
  expect(plain_db.search_path).to eq(%i($user public))
23
23
  end
24
+
25
+ describe "with a block" do
26
+ it "changes the search path temporarily" do
27
+ db.search_path :bar do
28
+ db.search_path.should == %i(bar)
29
+ end
30
+ db.search_path.should == %i(foo public)
31
+ end
32
+
33
+ it "accepts symbols as arglist" do
34
+ db.search_path :bar, :baz do
35
+ db.search_path.should == %i(bar baz)
36
+ end
37
+ db.search_path.should == %i(foo public)
38
+ end
39
+
40
+ it "allows prepending with prepend: true" do
41
+ db.search_path :bar, prepend: true do
42
+ db.search_path.should == %i(bar foo public)
43
+ end
44
+ db.search_path.should == %i(foo public)
45
+ end
46
+ end
24
47
  end
25
48
 
26
49
  describe "#search_path=" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-postgres-schemata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafał Rzepecki