pg_saurus 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +41 -1
- data/lib/generators/pg_saurus/install/install_generator.rb +22 -0
- data/lib/generators/pg_saurus/install/templates/config/initializers/pg_saurus.rb +6 -0
- data/lib/pg_saurus.rb +9 -0
- data/lib/pg_saurus/config.rb +12 -0
- data/lib/pg_saurus/engine.rb +4 -0
- data/lib/pg_saurus/errors.rb +6 -2
- data/lib/pg_saurus/migration.rb +2 -0
- data/lib/pg_saurus/migration/set_role_method.rb +76 -0
- data/lib/pg_saurus/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18d59e56fbebaf661db0f4282517d39ed626e929
|
4
|
+
data.tar.gz: 486ae857a907186f330793f3ab94fc26e1491100
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df7e2e4c78ef8aedf0e9d26394a3decdefae7761a5e26e6a4e2e9039c3f1c679348539aed360d7e340f653572f07f31d75cb56900d364f0be72569b8f5fdcc5b
|
7
|
+
data.tar.gz: 25fabf172159dd396218b7833a98f4561bea292b58614beabaff3713d1edb660d8803134f4309a098fba9e500439fd84eb5210c9b3cca685ca17a5cc97c05501
|
data/README.markdown
CHANGED
@@ -270,9 +270,49 @@ with the understanding that it is preliminary 'alpha' at best.
|
|
270
270
|
create_view "demography.citizens_view", "select * from demography.citizens"
|
271
271
|
```
|
272
272
|
|
273
|
+
## Roles
|
274
|
+
|
275
|
+
If you want to execute a migration as a specific PostgreSQL role you can use the `set_role` method:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
class CreateRockBands < ActiveRecord::Migration
|
279
|
+
set_role "rocker"
|
280
|
+
|
281
|
+
def change
|
282
|
+
create_table :rock_bands do |t|
|
283
|
+
# create columns
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
```
|
288
|
+
|
289
|
+
Technically it is equivalent to the following:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
class CreateRockBands < ActiveRecord::Migration
|
293
|
+
def change
|
294
|
+
execute "SET ROLE rocker"
|
295
|
+
create_table :rock_bands do |t|
|
296
|
+
# create columns
|
297
|
+
end
|
298
|
+
ensure
|
299
|
+
execute "RESET ROLE"
|
300
|
+
end
|
301
|
+
end
|
302
|
+
```
|
303
|
+
|
304
|
+
You may force all migrations to have `set_role`, for this, configure PgSaurus with
|
305
|
+
`ensure_role_set=true`:
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
PgSaurus.configre do |config|
|
309
|
+
config.ensure_role_set = true
|
310
|
+
end
|
311
|
+
```
|
312
|
+
|
273
313
|
## Tools
|
274
314
|
|
275
|
-
PgSaurus::Tools provides number of useful methods:
|
315
|
+
PgSaurus::Tools provides a number of useful methods:
|
276
316
|
|
277
317
|
```ruby
|
278
318
|
PgSaurus::Tools.create_schema "services" # => create new PG schema "services"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module PgSaurus
|
2
|
+
# :nodoc:
|
3
|
+
module Generators
|
4
|
+
# Generates config/initializers/pg_saurus.rb with default settings.
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
# :nodoc:
|
8
|
+
desc <<-DESC
|
9
|
+
Description:
|
10
|
+
Create default PgSaurus configuration
|
11
|
+
DESC
|
12
|
+
|
13
|
+
source_root File.expand_path('../templates', __FILE__)
|
14
|
+
|
15
|
+
# :nodoc:
|
16
|
+
def copy_rails_files
|
17
|
+
template "config/initializers/pg_saurus.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/pg_saurus.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "pg_saurus/engine"
|
2
2
|
require "pg_saurus/errors"
|
3
|
+
require "pg_saurus/config"
|
3
4
|
|
4
5
|
# Rails engine which allows to use some PostgreSQL features:
|
5
6
|
# * Schemas.
|
@@ -15,4 +16,12 @@ module PgSaurus
|
|
15
16
|
autoload :Migration
|
16
17
|
autoload :ConnectionAdapters
|
17
18
|
autoload :CreateIndexConcurrently
|
19
|
+
|
20
|
+
mattr_accessor :config
|
21
|
+
self.config = PgSaurus::Config.new
|
22
|
+
|
23
|
+
# Configure the engine.
|
24
|
+
def self.configure
|
25
|
+
yield(config)
|
26
|
+
end
|
18
27
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module PgSaurus
|
2
|
+
# Configuration for PgSaurus behaviour.
|
3
|
+
class Config
|
4
|
+
# When true, raise exception if migration is executed without a role.
|
5
|
+
attr_accessor :ensure_role_set
|
6
|
+
|
7
|
+
# Instantiate and set default config settings.
|
8
|
+
def initialize
|
9
|
+
@ensure_role_set = true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/pg_saurus/engine.rb
CHANGED
@@ -14,6 +14,10 @@ module PgSaurus
|
|
14
14
|
|
15
15
|
ActiveRecord::SchemaDumper.class_eval { include ::PgSaurus::SchemaDumper }
|
16
16
|
|
17
|
+
ActiveRecord::Migration.class_eval do
|
18
|
+
include ::PgSaurus::Migration::SetRoleMethod
|
19
|
+
end
|
20
|
+
|
17
21
|
if defined?(ActiveRecord::Migration::CommandRecorder)
|
18
22
|
ActiveRecord::Migration::CommandRecorder.class_eval do
|
19
23
|
include ::PgSaurus::Migration::CommandRecorder
|
data/lib/pg_saurus/errors.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
module PgSaurus
|
1
|
+
module PgSaurus
|
2
|
+
# Base error for PgSaurus errors.
|
3
|
+
class Error < StandardError; end
|
2
4
|
|
3
5
|
# Raised when an unexpected index exists
|
4
|
-
class IndexExistsError <
|
6
|
+
class IndexExistsError < Error; end
|
5
7
|
|
8
|
+
# Raised if config.ensure_role_set = true, but migration have no role set.
|
9
|
+
class RoleNotSetError < Error; end
|
6
10
|
end
|
data/lib/pg_saurus/migration.rb
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
module PgSaurus
|
2
|
+
# Wrap original `exec_migration` to run migration with set postgresql role.
|
3
|
+
# If config.ensure_role_set=true but no role is set for the migration, then an
|
4
|
+
# exception is raised.
|
5
|
+
module Migration::SetRoleMethod
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
class << self
|
10
|
+
attr_reader :role
|
11
|
+
|
12
|
+
# Set role
|
13
|
+
#
|
14
|
+
# @param role [String]
|
15
|
+
def set_role(role)
|
16
|
+
@role = role
|
17
|
+
end
|
18
|
+
|
19
|
+
# Prevents raising exception when ensure_role_set=true and no role is set.
|
20
|
+
def keep_default_role
|
21
|
+
@keep_default_role = true
|
22
|
+
end
|
23
|
+
|
24
|
+
# Was +keep_default_role+ called for the migration?
|
25
|
+
#
|
26
|
+
# @return [Boolean]
|
27
|
+
def keep_default_role?
|
28
|
+
@keep_default_role
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_method_chain :exec_migration, :role
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get role
|
36
|
+
def role
|
37
|
+
self.class.role
|
38
|
+
end
|
39
|
+
|
40
|
+
# :nodoc:
|
41
|
+
def keep_default_role?
|
42
|
+
self.class.keep_default_role?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Wrap original `exec_migration` to run migration with set role.
|
46
|
+
#
|
47
|
+
# @param conn [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter]
|
48
|
+
# @param direction [Symbole] :up or :down
|
49
|
+
#
|
50
|
+
# @return [void]
|
51
|
+
def exec_migration_with_role(conn, direction)
|
52
|
+
if role
|
53
|
+
begin
|
54
|
+
conn.execute "SET ROLE #{role}"
|
55
|
+
exec_migration_without_role(conn, direction)
|
56
|
+
ensure
|
57
|
+
conn.execute "RESET ROLE"
|
58
|
+
end
|
59
|
+
elsif PgSaurus.config.ensure_role_set && !keep_default_role?
|
60
|
+
msg =
|
61
|
+
"Role for migration #{self.class} is not set\n\n" \
|
62
|
+
"You've configured PgSaurus with ensure_role_set=true. \n" \
|
63
|
+
"That means that every migration must explicitly set role with set_role method.\n\n" \
|
64
|
+
"Example:\n" \
|
65
|
+
" class CreateNewTable < ActiveRecord::Migration\n" \
|
66
|
+
" set_role \"superhero\"\n" \
|
67
|
+
" end\n\n" \
|
68
|
+
"If you want to set ensure_role_set=false, take a look at config/initializers/pg_saurus.rb\n\n"
|
69
|
+
raise PgSaurus::RoleNotSetError, msg
|
70
|
+
else
|
71
|
+
exec_migration_without_role(conn, direction)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
data/lib/pg_saurus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_saurus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Potapov Sergey
|
@@ -160,7 +160,10 @@ files:
|
|
160
160
|
- lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb
|
161
161
|
- lib/core_ext/active_record/errors.rb
|
162
162
|
- lib/core_ext/active_record/schema_dumper.rb
|
163
|
+
- lib/generators/pg_saurus/install/install_generator.rb
|
164
|
+
- lib/generators/pg_saurus/install/templates/config/initializers/pg_saurus.rb
|
163
165
|
- lib/pg_saurus.rb
|
166
|
+
- lib/pg_saurus/config.rb
|
164
167
|
- lib/pg_saurus/connection_adapters.rb
|
165
168
|
- lib/pg_saurus/connection_adapters/abstract_adapter.rb
|
166
169
|
- lib/pg_saurus/connection_adapters/abstract_adapter/comment_methods.rb
|
@@ -190,6 +193,7 @@ files:
|
|
190
193
|
- lib/pg_saurus/migration/command_recorder/foreigner_methods.rb
|
191
194
|
- lib/pg_saurus/migration/command_recorder/schema_methods.rb
|
192
195
|
- lib/pg_saurus/migration/command_recorder/view_methods.rb
|
196
|
+
- lib/pg_saurus/migration/set_role_method.rb
|
193
197
|
- lib/pg_saurus/schema_dumper.rb
|
194
198
|
- lib/pg_saurus/schema_dumper/comment_methods.rb
|
195
199
|
- lib/pg_saurus/schema_dumper/extension_methods.rb
|