pg_power 2.0.0 → 2.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 +4 -4
- data/lib/colorized_text.rb +33 -0
- data/lib/pg_power/connection_adapters/index_definition.rb +5 -3
- data/lib/pg_power/connection_adapters/postgresql_adapter/extension_methods.rb +6 -4
- data/lib/pg_power/connection_adapters/postgresql_adapter/foreigner_methods.rb +22 -13
- data/lib/pg_power/connection_adapters/postgresql_adapter/translate_exception.rb +10 -8
- data/lib/pg_power/create_index_concurrently.rb +5 -3
- data/lib/pg_power/migration/command_recorder/foreigner_methods.rb +6 -4
- data/lib/pg_power/schema_dumper/comment_methods.rb +5 -5
- data/lib/pg_power/schema_dumper/extension_methods.rb +3 -3
- data/lib/pg_power/schema_dumper/foreigner_methods.rb +14 -8
- data/lib/pg_power/schema_dumper/view_methods.rb +5 -2
- data/lib/pg_power/tools.rb +2 -2
- data/lib/pg_power/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9419cdd07d825d2debcae661e0dbc5774c9ff6f
|
4
|
+
data.tar.gz: 2e269d9cf4a7479f422cb91f4859b7c48c345f39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e75f5b1ef010822863304fab18252d16d9bc592a75b8f949e4d3b5f67e7d360b2bff2c88aecdee83df28783d365830d15cfd5a800dea528628e6e53163e5a23
|
7
|
+
data.tar.gz: 7bff956f8b498fc11c94bf91cb328eec069890b66bf60ceb143ffe99009e87240d03f4b5888f0084d5e81468fda691ceeeacdb508c19efc31442e8345d815788
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Colorizes text with ASCII colors.
|
2
|
+
# == Usage:
|
3
|
+
# include ColorizedText
|
4
|
+
#
|
5
|
+
# puts green "OK" # => green output
|
6
|
+
# puts bold "Running... # => bold output
|
7
|
+
# puts bold green "OK!!!" # => bold green output
|
8
|
+
module ColorizedText
|
9
|
+
# Colorize text using ASCII color code
|
10
|
+
def colorize(text, code)
|
11
|
+
"\033[#{code}m#{text}\033[0m"
|
12
|
+
end
|
13
|
+
|
14
|
+
# :nodoc:
|
15
|
+
def yellow(text)
|
16
|
+
colorize(text, 33)
|
17
|
+
end
|
18
|
+
|
19
|
+
# :nodoc:
|
20
|
+
def green(text)
|
21
|
+
colorize(text, 32)
|
22
|
+
end
|
23
|
+
|
24
|
+
# :nodoc:
|
25
|
+
def red(text)
|
26
|
+
colorize(text, 31)
|
27
|
+
end
|
28
|
+
|
29
|
+
# :nodoc:
|
30
|
+
def bold(text)
|
31
|
+
colorize(text, 1)
|
32
|
+
end
|
33
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module PgPower::ConnectionAdapters
|
2
2
|
# Structure to store index parameters
|
3
|
-
# Overrides ActiveRecord::ConnectionAdapters::IndexDefinition
|
4
|
-
|
3
|
+
# Overrides ActiveRecord::ConnectionAdapters::IndexDefinition
|
4
|
+
# with the additional :where parameter.
|
5
|
+
class IndexDefinition < Struct.new( :table, :name, :unique, :columns,
|
6
|
+
:lengths, :where, :access_method )
|
5
7
|
end
|
6
|
-
end
|
8
|
+
end
|
@@ -40,18 +40,18 @@ module PgPower::ConnectionAdapters::PostgreSQLAdapter::ExtensionMethods
|
|
40
40
|
# that absorbs the existing objects into the extension, instead of creating new objects
|
41
41
|
def create_extension(extension_name, options = {})
|
42
42
|
options = CREATE_EXTENSION_DEFAULTS.merge(options.symbolize_keys)
|
43
|
+
|
43
44
|
sql = ['CREATE EXTENSION']
|
44
|
-
sql << 'IF NOT EXISTS'
|
45
|
+
sql << 'IF NOT EXISTS' if options[:if_not_exists]
|
45
46
|
sql << %Q{"#{extension_name.to_s}"}
|
46
47
|
sql << "SCHEMA #{options[:schema_name]}" if options[:schema_name].present?
|
47
|
-
sql << "VERSION '#{options[:version]}'"
|
48
|
+
sql << "VERSION '#{options[:version]}'" if options[:version].present?
|
48
49
|
sql << "FROM #{options[:old_version]}" if options[:old_version].present?
|
49
50
|
|
50
51
|
sql = sql.join(' ')
|
51
52
|
execute(sql)
|
52
53
|
end
|
53
54
|
|
54
|
-
|
55
55
|
# Execute SQL to remove a postgresql extension module from the current database.
|
56
56
|
#
|
57
57
|
# @param [#to_s] extension_name Name of the extension module to unload
|
@@ -70,7 +70,9 @@ module PgPower::ConnectionAdapters::PostgreSQLAdapter::ExtensionMethods
|
|
70
70
|
mode = mode.to_sym
|
71
71
|
|
72
72
|
unless AVAILABLE_DROP_MODES.include?(mode)
|
73
|
-
raise ArgumentError,
|
73
|
+
raise ArgumentError,
|
74
|
+
"Expected one of #{AVAILABLE_DROP_MODES.keys.inspect} drop modes, " \
|
75
|
+
"but #{mode} received"
|
74
76
|
end
|
75
77
|
|
76
78
|
sql << AVAILABLE_DROP_MODES[mode]
|
@@ -33,13 +33,16 @@ module PgPower # :nodoc:
|
|
33
33
|
SQL
|
34
34
|
|
35
35
|
fk_info.map do |row|
|
36
|
-
options = {:column
|
36
|
+
options = { :column => row['column'],
|
37
|
+
:name => row['name'],
|
38
|
+
:primary_key => row['primary_key'] }
|
37
39
|
|
38
|
-
options[:dependent] =
|
40
|
+
options[:dependent] =
|
41
|
+
case row['dependency']
|
39
42
|
when 'c' then :delete
|
40
43
|
when 'n' then :nullify
|
41
44
|
when 'r' then :restrict
|
42
|
-
|
45
|
+
end
|
43
46
|
|
44
47
|
PgPower::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row['to_table'], options)
|
45
48
|
end
|
@@ -116,16 +119,18 @@ module PgPower # :nodoc:
|
|
116
119
|
|
117
120
|
# Return the SQL code fragment to add the foreign key based on the table names and options.
|
118
121
|
def add_foreign_key_sql(from_table, to_table, options = {})
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
+
column = options[:column]
|
123
|
+
options_options = options[:options]
|
124
|
+
foreign_key_name = foreign_key_name(from_table, column, options)
|
125
|
+
primary_key = options[:primary_key] || "id"
|
126
|
+
dependency = dependency_sql(options[:dependent])
|
122
127
|
|
123
128
|
sql =
|
124
129
|
"ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
|
125
|
-
"FOREIGN KEY (#{quote_column_name(
|
130
|
+
"FOREIGN KEY (#{quote_column_name(column)}) " +
|
126
131
|
"REFERENCES #{quote_table_name(ActiveRecord::Migrator.proper_table_name(to_table))}(#{primary_key})"
|
127
|
-
sql << " #{dependency}"
|
128
|
-
sql << " #{
|
132
|
+
sql << " #{dependency}" if dependency.present?
|
133
|
+
sql << " #{options_options}" if options_options
|
129
134
|
|
130
135
|
sql
|
131
136
|
end
|
@@ -160,7 +165,9 @@ module PgPower # :nodoc:
|
|
160
165
|
execute "ALTER TABLE #{quote_table_name(from_table)} #{remove_foreign_key_sql(foreign_key_name)}"
|
161
166
|
|
162
167
|
options[:exclude_index] ||= false
|
163
|
-
|
168
|
+
unless options[:exclude_index] || !index_exists?(from_table, column) then
|
169
|
+
remove_index(from_table, column)
|
170
|
+
end
|
164
171
|
end
|
165
172
|
|
166
173
|
# Return the SQL code fragment to remove foreign key based on table name and options.
|
@@ -177,7 +184,7 @@ module PgPower # :nodoc:
|
|
177
184
|
# @param [String, Symbol] from_table
|
178
185
|
# @param [String] foreign_key_name
|
179
186
|
def id_column_name_from_foreign_key_metadata(from_table, foreign_key_name)
|
180
|
-
keys
|
187
|
+
keys = foreign_keys(from_table)
|
181
188
|
this_key = keys.find {|key| key.options[:name] == foreign_key_name}
|
182
189
|
this_key.options[:column]
|
183
190
|
end
|
@@ -185,8 +192,10 @@ module PgPower # :nodoc:
|
|
185
192
|
|
186
193
|
# Build default name for constraint.
|
187
194
|
def foreign_key_name(table, column, options = {})
|
188
|
-
|
189
|
-
|
195
|
+
name = options[:name]
|
196
|
+
|
197
|
+
if !name.nil? then
|
198
|
+
name
|
190
199
|
else
|
191
200
|
prefix = table.gsub(".", "_")
|
192
201
|
"#{prefix}_#{column}_fk"
|
@@ -6,13 +6,15 @@ module PgPower::ConnectionAdapters::PostgreSQLAdapter::TranslateException
|
|
6
6
|
|
7
7
|
# Intercept insufficient privilege PGError and raise active_record wrapped database exception
|
8
8
|
def translate_exception(exception, message)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
exception_result = exception.result
|
10
|
+
|
11
|
+
case exception_result.try(:error_field, PGresult::PG_DIAG_SQLSTATE)
|
12
|
+
when INSUFFICIENT_PRIVILEGE
|
13
|
+
exc_message = exception_result.try(:error_field, PGresult::PG_DIAG_MESSAGE_PRIMARY)
|
14
|
+
exc_message ||= message
|
15
|
+
::ActiveRecord::InsufficientPrivilege.new(exc_message, exception)
|
16
|
+
else
|
17
|
+
super
|
16
18
|
end
|
17
19
|
end
|
18
|
-
end
|
20
|
+
end
|
@@ -108,14 +108,16 @@ module PgPower::CreateIndexConcurrently
|
|
108
108
|
#
|
109
109
|
# @see ::PgPower::ConnectionAdapters::PostgreSQLAdapter::ForeignerMethods.add_foreign_key
|
110
110
|
def add_foreign_key(from_table, to_table, options = {}, &block)
|
111
|
-
from_table
|
112
|
-
|
111
|
+
from_table = ::ActiveRecord::Migrator.proper_table_name(from_table)
|
112
|
+
concurrent_index = options[:concurrent_index]
|
113
|
+
|
114
|
+
if concurrent_index then
|
113
115
|
if options[:exclude_index]
|
114
116
|
raise ArgumentError, 'Conflicted options(exclude_index, concurrent_index) was found, both are set to true.'
|
115
117
|
end
|
116
118
|
|
117
119
|
options[:column] ||= connection.id_column_name_from_table_name(to_table)
|
118
|
-
options = options.merge(:concurrently =>
|
120
|
+
options = options.merge(:concurrently => concurrent_index)
|
119
121
|
|
120
122
|
index_options = { :concurrently => true }
|
121
123
|
enque(from_table, options[:column], index_options)
|
@@ -15,11 +15,13 @@ module PgPower::Migration::CommandRecorder::ForeignerMethods
|
|
15
15
|
def invert_add_foreign_key(args)
|
16
16
|
from_table, to_table, add_options = *args
|
17
17
|
add_options ||= {}
|
18
|
+
add_name_option = add_options[:name]
|
19
|
+
add_column_option = add_options[:column]
|
18
20
|
|
19
|
-
if
|
20
|
-
options = {:name =>
|
21
|
-
elsif
|
22
|
-
options = {:column =>
|
21
|
+
if add_name_option then
|
22
|
+
options = {:name => add_name_option}
|
23
|
+
elsif add_column_option
|
24
|
+
options = {:column => add_column_option}
|
23
25
|
else
|
24
26
|
options = to_table
|
25
27
|
end
|
@@ -15,10 +15,9 @@ module PgPower::SchemaDumper::CommentMethods
|
|
15
15
|
|
16
16
|
# Now dump index comments
|
17
17
|
unless (index_comments = @connection.index_comments).empty?
|
18
|
-
index_comments.each do |
|
19
|
-
|
20
|
-
|
21
|
-
comment = format_comment(row[2])
|
18
|
+
index_comments.each do |schema_name, table_name, raw_comment|
|
19
|
+
index_name = schema_name == 'public' ? "'#{table_name}'" : "'#{schema_name}.#{table_name}'"
|
20
|
+
comment = format_comment(raw_comment)
|
22
21
|
stream.puts " set_index_comment #{index_name}, '#{comment}'"
|
23
22
|
end
|
24
23
|
stream.puts
|
@@ -31,7 +30,8 @@ module PgPower::SchemaDumper::CommentMethods
|
|
31
30
|
unless (comments = @connection.comments(table_name)).empty?
|
32
31
|
comment_statements = comments.map do |row|
|
33
32
|
column_name = row[0]
|
34
|
-
comment
|
33
|
+
comment = format_comment(row[1])
|
34
|
+
|
35
35
|
if column_name
|
36
36
|
" set_column_comment '#{table_name}', '#{column_name}', '#{comment}'"
|
37
37
|
else
|
@@ -13,15 +13,15 @@ module PgPower::SchemaDumper::ExtensionMethods
|
|
13
13
|
# @param [#puts] stream Stream to write to
|
14
14
|
def dump_extensions(stream)
|
15
15
|
extensions = @connection.pg_extensions
|
16
|
-
commands
|
16
|
+
commands = extensions.map do |extension_name, options|
|
17
17
|
result = [%Q|create_extension "#{extension_name}"|]
|
18
18
|
result << %Q|:schema_name => "#{options[:schema_name]}"| unless options[:schema_name] == 'public'
|
19
19
|
result << %Q|:version => "#{options[:version]}"|
|
20
20
|
result.join(', ')
|
21
21
|
end
|
22
22
|
|
23
|
-
commands.each do |
|
24
|
-
stream.puts(" "
|
23
|
+
commands.each do |command|
|
24
|
+
stream.puts(" #{command}")
|
25
25
|
end
|
26
26
|
|
27
27
|
stream.puts
|
@@ -26,18 +26,24 @@ module PgPower::SchemaDumper::ForeignerMethods
|
|
26
26
|
def foreign_keys(table_name, stream)
|
27
27
|
if (foreign_keys = @connection.foreign_keys(table_name)).any?
|
28
28
|
add_foreign_key_statements = foreign_keys.map do |foreign_key|
|
29
|
+
options = foreign_key.options
|
30
|
+
table_from_key = foreign_key.to_table
|
29
31
|
statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
|
30
|
-
statement_parts <<
|
31
|
-
statement_parts << (':name => ' +
|
32
|
+
statement_parts << table_from_key.inspect
|
33
|
+
statement_parts << (':name => ' + options[:name].inspect)
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
+
column_from_options = options[:column]
|
36
|
+
primary_key_from_options = options[:primary_key]
|
37
|
+
dependent_from_options = options[:dependent]
|
38
|
+
|
39
|
+
if column_from_options != "#{table_from_key.singularize}_id"
|
40
|
+
statement_parts << (":column => #{column_from_options.inspect}")
|
35
41
|
end
|
36
|
-
if
|
37
|
-
statement_parts << (
|
42
|
+
if primary_key_from_options != 'id'
|
43
|
+
statement_parts << (":primary_key => #{primary_key_from_options.inspect}")
|
38
44
|
end
|
39
|
-
if
|
40
|
-
statement_parts << (
|
45
|
+
if dependent_from_options.present?
|
46
|
+
statement_parts << (":dependent => #{dependent_from_options.inspect}")
|
41
47
|
end
|
42
48
|
|
43
49
|
# Always exclude the index
|
@@ -6,13 +6,16 @@ module PgPower::SchemaDumper::ViewMethods
|
|
6
6
|
views(stream)
|
7
7
|
stream
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
# Generates code to create views.
|
11
11
|
def views(stream)
|
12
12
|
# Don't create "system" views.
|
13
13
|
view_names = PgPower::Tools.views
|
14
14
|
view_names.each do |options|
|
15
|
-
write_view_definition(stream,
|
15
|
+
write_view_definition(stream,
|
16
|
+
options["table_schema"],
|
17
|
+
options["table_name"],
|
18
|
+
options["view_definition"])
|
16
19
|
end
|
17
20
|
stream << "\n"
|
18
21
|
end
|
data/lib/pg_power/tools.rb
CHANGED
@@ -46,14 +46,14 @@ module PgPower
|
|
46
46
|
sql = "CREATE VIEW #{view_name} AS #{view_definition}"
|
47
47
|
connection.execute sql
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
# Drops PostgreSQL view
|
51
51
|
# @param [String, Symbol] view_name
|
52
52
|
def drop_view(view_name)
|
53
53
|
sql = "DROP VIEW #{view_name}"
|
54
54
|
connection.execute sql
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# Returns an array of existing, non system views.
|
58
58
|
def views
|
59
59
|
sql = <<-SQL
|
data/lib/pg_power/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_power
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Potapov Sergey
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2014-
|
15
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: pg
|
@@ -98,6 +98,20 @@ dependencies:
|
|
98
98
|
- - '>='
|
99
99
|
- !ruby/object:Gem::Version
|
100
100
|
version: '0'
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: debugger
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
101
115
|
- !ruby/object:Gem::Dependency
|
102
116
|
name: pry
|
103
117
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,6 +140,7 @@ extra_rdoc_files:
|
|
126
140
|
- README.markdown
|
127
141
|
files:
|
128
142
|
- README.markdown
|
143
|
+
- lib/colorized_text.rb
|
129
144
|
- lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb
|
130
145
|
- lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb
|
131
146
|
- lib/core_ext/active_record/errors.rb
|