sequel-postgres-schemata 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/lib/sequel/extensions/postgres_schemata.rb +1 -0
- data/lib/sequel/postgres/schemata.rb +64 -0
- data/lib/sequel/postgres/schemata/version.rb +7 -0
- data/sequel-postgres-schemata.gemspec +27 -0
- data/spec/schemata_spec.rb +71 -0
- data/spec/spec_helper.rb +3 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35ac3d183de7028ab777f9456e5a6e0b23808473
|
4
|
+
data.tar.gz: 8ac4fdafa29976a867510da067edb9e385abcb66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4e50d2605da25f2e7cec6b2587f5b00965c7c5386afe575b6f1bf51581d45bebc43fe374d29108dd4e071b815797b7baa55d140e6c5456ff3e312244f35bd65
|
7
|
+
data.tar.gz: c5a8258ba6a92a1e6e28b989e212d96ca270f0eb7017a408de0bab11770f3535ceeba44fd0de4c71a1cfa7a4451a3a460995bc280d998d67ae62a49375ee6a8f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rafał Rzepecki
|
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,41 @@
|
|
1
|
+
# Sequel::Postgres::Schemata
|
2
|
+
|
3
|
+
Easily manipulate Postgres schemas in Sequel.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sequel-postgres-schemata'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sequel-postgres-schemata
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
```ruby
|
21
|
+
Sequel.extension :postgres_schemata
|
22
|
+
|
23
|
+
db = Sequel.connect adapter: 'postgres', search_path: %w(foo public)
|
24
|
+
db.create_schema :bar
|
25
|
+
|
26
|
+
db.search_path # => [:foo, :public]
|
27
|
+
db.schemata # => [:pg_toast, :pg_temp_1, :pg_toast_temp_1, :pg_catalog, :public, :information_schema, :bar]
|
28
|
+
db.current_schemata # => [:public]
|
29
|
+
db.search_path = [:bar, :foo, :public]
|
30
|
+
db.current_schemata # => [:bar, :public]
|
31
|
+
db.rename_schema :bar, :foo
|
32
|
+
db.current_schemata # => [:foo, :public]
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sequel/postgres/schemata'
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "sequel/postgres/schemata/version"
|
2
|
+
|
3
|
+
Sequel.extension :pg_array
|
4
|
+
|
5
|
+
module Sequel
|
6
|
+
module Postgres
|
7
|
+
module Schemata
|
8
|
+
module DatabaseMethods
|
9
|
+
|
10
|
+
# List all existing schematas (including the system ones).
|
11
|
+
# Returns a symbol list.
|
12
|
+
def schemata
|
13
|
+
metadata_dataset.select(:nspname).from(:pg_namespace).map(:nspname).map(&:to_sym)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a symbol list containing the current search path.
|
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.sub(SCHEMA_SUB_RE, '\1').gsub('""', '"').to_sym}
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sets the search path. Starting with Postgres 9.2 it can contain
|
25
|
+
# 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.
|
28
|
+
def search_path= search_path
|
29
|
+
case search_path
|
30
|
+
when String
|
31
|
+
search_path = search_path.split(",").map{|s| s.strip}
|
32
|
+
when Array
|
33
|
+
# nil
|
34
|
+
else
|
35
|
+
raise Error, "unrecognized value for search_path: #{search_path.inspect}"
|
36
|
+
end
|
37
|
+
self << "SET search_path = #{search_path.map{|s| "\"#{s.to_s.gsub('"', '""')}\""}.join(',')}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the current schemata, as return by current_schemas(false).
|
41
|
+
def current_schemata
|
42
|
+
metadata_dataset.select(Sequel::function(:current_schemas, false).
|
43
|
+
cast('varchar[]')).single_value.map(&:to_sym)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Renames a schema
|
47
|
+
def rename_schema from, to
|
48
|
+
self << RENAME_SCHEMA_SQL % [from.to_s.gsub('"', '""'), to.to_s.gsub('"', '""')]
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
SHOW_SEARCH_PATH = "SHOW search_path".freeze
|
54
|
+
SCHEMA_SCAN_RE = /(?<=\A|, )(".*?"|.*?)(?=, |\z)/.freeze
|
55
|
+
SCHEMA_SUB_RE = /\A"(.*)"\z/.freeze
|
56
|
+
RENAME_SCHEMA_SQL = 'ALTER SCHEMA "%s" RENAME TO "%s"'.freeze
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
module DatabaseMethods
|
61
|
+
include ::Sequel::Postgres::Schemata::DatabaseMethods
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/postgres/schemata/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-postgres-schemata"
|
8
|
+
spec.version = Sequel::Postgres::Schemata::VERSION
|
9
|
+
spec.authors = ["Rafał Rzepecki"]
|
10
|
+
spec.email = ["divided.mind@gmail.com"]
|
11
|
+
spec.description = %q{Allows easy manipulation of Postgres schemas from Ruby with Sequel}
|
12
|
+
spec.summary = %q{Postgres schema manipulation}
|
13
|
+
spec.homepage = "https://github.com/dividedmind/sequel-postgres-schemata"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "sequel", "~> 4.3"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.1"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
26
|
+
spec.add_development_dependency "pg", "~> 0.16"
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sequel::Postgres::Schemata do
|
4
|
+
|
5
|
+
let(:db) { Sequel::connect adapter: 'postgres', search_path: %w(foo public) }
|
6
|
+
|
7
|
+
describe "#schemata" do
|
8
|
+
it "lists all existing schematas" do
|
9
|
+
schemata = db.schemata
|
10
|
+
schemata.should include(:public)
|
11
|
+
schemata.should_not include(:foo)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#search_path" do
|
16
|
+
it "returns the search path" do
|
17
|
+
db.search_path.should == %i(foo public)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#search_path=" do
|
22
|
+
it "accepts a single symbol" do
|
23
|
+
db.search_path = %i(bar)
|
24
|
+
db.search_path.should == %i(bar)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "accepts a single string" do
|
28
|
+
db.search_path = 'bar'
|
29
|
+
db.search_path.should == %i(bar)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "accepts a formatted string" do
|
33
|
+
db.search_path = 'bar, baz'
|
34
|
+
db.search_path.should == %i(bar baz)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "accepts a symbol list" do
|
38
|
+
db.search_path = %i(bar baz)
|
39
|
+
db.search_path.should == %i(bar baz)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "accepts a string list" do
|
43
|
+
db.search_path = %w(bar baz)
|
44
|
+
db.search_path.should == %i(bar baz)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "quotes the string list" do
|
48
|
+
db.search_path = %w(bar, baz)
|
49
|
+
db.search_path.should == %i(bar, baz)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#current_schemata" do
|
54
|
+
it "returns the current schemata" do
|
55
|
+
db.current_schemata.should == %i(public)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#rename_schema" do
|
60
|
+
it "renames a schema" do
|
61
|
+
db.transaction rollback: :always do
|
62
|
+
db.create_schema :test_schema
|
63
|
+
db.schemata.should include(:test_schema)
|
64
|
+
db.current_schemata.should == %i(public)
|
65
|
+
db.rename_schema :test_schema, :foo
|
66
|
+
db.current_schemata.should == %i(foo public)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-postgres-schemata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafał Rzepecki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.16'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.16'
|
83
|
+
description: Allows easy manipulation of Postgres schemas from Ruby with Sequel
|
84
|
+
email:
|
85
|
+
- divided.mind@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/sequel/extensions/postgres_schemata.rb
|
96
|
+
- lib/sequel/postgres/schemata.rb
|
97
|
+
- lib/sequel/postgres/schemata/version.rb
|
98
|
+
- sequel-postgres-schemata.gemspec
|
99
|
+
- spec/schemata_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: https://github.com/dividedmind/sequel-postgres-schemata
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.1.4
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Postgres schema manipulation
|
125
|
+
test_files:
|
126
|
+
- spec/schemata_spec.rb
|
127
|
+
- spec/spec_helper.rb
|