pg_power 1.0.0
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.
- data/README.markdown +212 -0
- data/lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb +139 -0
- data/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb +135 -0
- data/lib/core_ext/active_record/schema_dumper.rb +40 -0
- data/lib/pg_power.rb +16 -0
- data/lib/pg_power/connection_adapters.rb +9 -0
- data/lib/pg_power/connection_adapters/abstract_adapter.rb +20 -0
- data/lib/pg_power/connection_adapters/abstract_adapter/comment_methods.rb +62 -0
- data/lib/pg_power/connection_adapters/abstract_adapter/foreigner_methods.rb +67 -0
- data/lib/pg_power/connection_adapters/abstract_adapter/index_methods.rb +6 -0
- data/lib/pg_power/connection_adapters/abstract_adapter/schema_methods.rb +18 -0
- data/lib/pg_power/connection_adapters/foreign_key_definition.rb +5 -0
- data/lib/pg_power/connection_adapters/index_definition.rb +6 -0
- data/lib/pg_power/connection_adapters/postgresql_adapter.rb +16 -0
- data/lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb +79 -0
- data/lib/pg_power/connection_adapters/postgresql_adapter/foreigner_methods.rb +190 -0
- data/lib/pg_power/connection_adapters/postgresql_adapter/index_methods.rb +42 -0
- data/lib/pg_power/connection_adapters/postgresql_adapter/schema_methods.rb +22 -0
- data/lib/pg_power/connection_adapters/table.rb +17 -0
- data/lib/pg_power/connection_adapters/table/comment_methods.rb +58 -0
- data/lib/pg_power/connection_adapters/table/foreigner_methods.rb +51 -0
- data/lib/pg_power/engine.rb +46 -0
- data/lib/pg_power/migration.rb +4 -0
- data/lib/pg_power/migration/command_recorder.rb +13 -0
- data/lib/pg_power/migration/command_recorder/comment_methods.rb +52 -0
- data/lib/pg_power/migration/command_recorder/foreigner_methods.rb +29 -0
- data/lib/pg_power/migration/command_recorder/schema_methods.rb +39 -0
- data/lib/pg_power/schema_dumper.rb +21 -0
- data/lib/pg_power/schema_dumper/comment_methods.rb +36 -0
- data/lib/pg_power/schema_dumper/foreigner_methods.rb +58 -0
- data/lib/pg_power/schema_dumper/schema_methods.rb +51 -0
- data/lib/pg_power/tools.rb +56 -0
- data/lib/pg_power/version.rb +4 -0
- data/lib/tasks/pg_power_tasks.rake +4 -0
- metadata +213 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
# Provides methods to extend {ActiveRecord::SchemaDumper} to dump
|
2
|
+
# foreign keys.
|
3
|
+
module PgPower::SchemaDumper::ForeignerMethods
|
4
|
+
# Hooks {ActiveRecord::SchemaDumper#table} method to dump foreign keys.
|
5
|
+
def tables_with_foreign_keys(stream)
|
6
|
+
tables_without_foreign_keys(stream)
|
7
|
+
|
8
|
+
table_names = @connection.tables.sort
|
9
|
+
table_names += get_non_public_schema_table_names.sort
|
10
|
+
|
11
|
+
table_names.sort.each do |table|
|
12
|
+
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
|
13
|
+
case ignored
|
14
|
+
when String; table == ignored
|
15
|
+
when Regexp; table =~ ignored
|
16
|
+
else
|
17
|
+
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
foreign_keys(table, stream)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# Finds all foreign keys on passed table and writes appropriated
|
26
|
+
# statements to stream.
|
27
|
+
def foreign_keys(table_name, stream)
|
28
|
+
if (foreign_keys = @connection.foreign_keys(table_name)).any?
|
29
|
+
add_foreign_key_statements = foreign_keys.map do |foreign_key|
|
30
|
+
statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
|
31
|
+
statement_parts << foreign_key.to_table.inspect
|
32
|
+
statement_parts << (':name => ' + foreign_key.options[:name].inspect)
|
33
|
+
|
34
|
+
if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
|
35
|
+
statement_parts << (':column => ' + foreign_key.options[:column].inspect)
|
36
|
+
end
|
37
|
+
if foreign_key.options[:primary_key] != 'id'
|
38
|
+
statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
|
39
|
+
end
|
40
|
+
if foreign_key.options[:dependent].present?
|
41
|
+
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Always exclude the index
|
45
|
+
# If an index was created in a migration, it will get dumped to the schema
|
46
|
+
# separately from the foreign key. This will raise an exception if
|
47
|
+
# add_foreign_key is run without :exclude_index => true.
|
48
|
+
statement_parts << (':exclude_index => true')
|
49
|
+
|
50
|
+
' ' + statement_parts.join(', ')
|
51
|
+
end
|
52
|
+
|
53
|
+
stream.puts add_foreign_key_statements.sort.join("\n")
|
54
|
+
stream.puts
|
55
|
+
end
|
56
|
+
end
|
57
|
+
private :foreign_keys
|
58
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Extends ActiveRecord::SchemaDumper class to dump schemas other than "public"
|
2
|
+
# and tables from those schemas.
|
3
|
+
module PgPower::SchemaDumper::SchemaMethods
|
4
|
+
# * Dumps schemas.
|
5
|
+
# * Dumps tables from public schema using native #tables method.
|
6
|
+
# * Dumps tables from schemas other than public.
|
7
|
+
def tables_with_schemas(stream)
|
8
|
+
schemas(stream)
|
9
|
+
tables_without_schemas(stream)
|
10
|
+
non_public_schema_tables(stream)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Generates code to create schemas.
|
14
|
+
def schemas(stream)
|
15
|
+
# Don't create "public" schema since it exists by default.
|
16
|
+
schema_names = PgPower::Tools.schemas - ["public", "information_schema"]
|
17
|
+
schema_names.each do |schema_name|
|
18
|
+
schema(schema_name, stream)
|
19
|
+
end
|
20
|
+
stream << "\n"
|
21
|
+
end
|
22
|
+
private :schemas
|
23
|
+
|
24
|
+
# Generates code to create schema.
|
25
|
+
def schema(schema_name, stream)
|
26
|
+
stream << " create_schema \"#{schema_name}\"\n"
|
27
|
+
end
|
28
|
+
private :schema
|
29
|
+
|
30
|
+
# Dumps tables from schemas other than public
|
31
|
+
def non_public_schema_tables(stream)
|
32
|
+
get_non_public_schema_table_names.each do |name|
|
33
|
+
table(name, stream)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
private :non_public_schema_tables
|
37
|
+
|
38
|
+
# Returns a sorted list of non-public schema tables
|
39
|
+
# Usage:
|
40
|
+
# get_non_public_schema_table_names # => ['demography.cities','demography.countries','politics.members']
|
41
|
+
def get_non_public_schema_table_names
|
42
|
+
result = @connection.query(<<-SQL, 'SCHEMA')
|
43
|
+
SELECT schemaname || '.' || tablename
|
44
|
+
FROM pg_tables
|
45
|
+
WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'public')
|
46
|
+
ORDER BY schemaname, tablename
|
47
|
+
SQL
|
48
|
+
result.flatten
|
49
|
+
end
|
50
|
+
private :get_non_public_schema_table_names
|
51
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module PgPower
|
2
|
+
# Provides utility methods to work with PostgreSQL databases.
|
3
|
+
# Usage:
|
4
|
+
# PgPower::Tools.create_schema "services" # => create new PG schema "services"
|
5
|
+
# PgPower::Tools.create_schema "nets"
|
6
|
+
# PgPower::Tools.drop_schema "services" # => remove the schema
|
7
|
+
# PgPower::Tools.schemas # => ["public", "information_schema", "nets"]
|
8
|
+
# PgPower::Tools.move_table_to_schema :computers, :nets
|
9
|
+
module Tools
|
10
|
+
extend self
|
11
|
+
|
12
|
+
# Creates PostgreSQL schema
|
13
|
+
def create_schema(schema_name)
|
14
|
+
sql = %{CREATE SCHEMA "#{schema_name}"}
|
15
|
+
connection.execute sql
|
16
|
+
end
|
17
|
+
|
18
|
+
# Drops PostgreSQL schema
|
19
|
+
def drop_schema(schema_name)
|
20
|
+
sql = %{DROP SCHEMA "#{schema_name}"}
|
21
|
+
connection.execute sql
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns an array of existing schemas.
|
25
|
+
def schemas
|
26
|
+
sql = "SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' order by nspname"
|
27
|
+
connection.query(sql).flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
# Move table to another schema without loosing data, indexes or constraints.
|
31
|
+
# @param [String] table table name (schema prefix is allowed)
|
32
|
+
# @param [String] new_schema schema where table should be moved to
|
33
|
+
def move_table_to_schema(table, new_schema)
|
34
|
+
schema, table = to_schema_and_table(table)
|
35
|
+
sql = %{ALTER TABLE "#{schema}"."#{table}" SET SCHEMA "#{new_schema}"}
|
36
|
+
connection.execute sql
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
# Return databas connections
|
42
|
+
def connection
|
43
|
+
ActiveRecord::Base.connection
|
44
|
+
end
|
45
|
+
private :connection
|
46
|
+
|
47
|
+
# Extract schema name and table name from qualified table name
|
48
|
+
# @param [String, Symbol] table_name table name
|
49
|
+
# @return [Array[String, String]] schema and table
|
50
|
+
def to_schema_and_table(table_name)
|
51
|
+
table, schema = table_name.to_s.split(".", 2).reverse
|
52
|
+
schema ||= "public"
|
53
|
+
[schema, table]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_power
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Potapov Sergey
|
14
|
+
- Arthur Shagall
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-09-05 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
version_requirements: *id001
|
33
|
+
name: pg
|
34
|
+
prerelease: false
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :runtime
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
version_requirements: *id002
|
47
|
+
name: rspec-rails
|
48
|
+
prerelease: false
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
type: :runtime
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 5
|
57
|
+
segments:
|
58
|
+
- 3
|
59
|
+
- 1
|
60
|
+
version: "3.1"
|
61
|
+
version_requirements: *id003
|
62
|
+
name: rails
|
63
|
+
prerelease: false
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
type: :development
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
version_requirements: *id004
|
76
|
+
name: rcov
|
77
|
+
prerelease: false
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
type: :development
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
version_requirements: *id005
|
90
|
+
name: yard
|
91
|
+
prerelease: false
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
type: :development
|
94
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
version_requirements: *id006
|
104
|
+
name: metrical
|
105
|
+
prerelease: false
|
106
|
+
- !ruby/object:Gem::Dependency
|
107
|
+
type: :development
|
108
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
hash: 3
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
version_requirements: *id007
|
118
|
+
name: jeweler
|
119
|
+
prerelease: false
|
120
|
+
- !ruby/object:Gem::Dependency
|
121
|
+
type: :development
|
122
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
version_requirements: *id008
|
132
|
+
name: ruby-debug
|
133
|
+
prerelease: false
|
134
|
+
description: ActiveRecord extensions for PostgreSQL. Provides useful tools and ability to create/drop schemas in migrations.
|
135
|
+
email:
|
136
|
+
- blake131313@gmail.com
|
137
|
+
- arthur.shagall@gmail.com
|
138
|
+
executables: []
|
139
|
+
|
140
|
+
extensions: []
|
141
|
+
|
142
|
+
extra_rdoc_files:
|
143
|
+
- README.markdown
|
144
|
+
files:
|
145
|
+
- README.markdown
|
146
|
+
- lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb
|
147
|
+
- lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb
|
148
|
+
- lib/core_ext/active_record/schema_dumper.rb
|
149
|
+
- lib/pg_power.rb
|
150
|
+
- lib/pg_power/connection_adapters.rb
|
151
|
+
- lib/pg_power/connection_adapters/abstract_adapter.rb
|
152
|
+
- lib/pg_power/connection_adapters/abstract_adapter/comment_methods.rb
|
153
|
+
- lib/pg_power/connection_adapters/abstract_adapter/foreigner_methods.rb
|
154
|
+
- lib/pg_power/connection_adapters/abstract_adapter/index_methods.rb
|
155
|
+
- lib/pg_power/connection_adapters/abstract_adapter/schema_methods.rb
|
156
|
+
- lib/pg_power/connection_adapters/foreign_key_definition.rb
|
157
|
+
- lib/pg_power/connection_adapters/index_definition.rb
|
158
|
+
- lib/pg_power/connection_adapters/postgresql_adapter.rb
|
159
|
+
- lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb
|
160
|
+
- lib/pg_power/connection_adapters/postgresql_adapter/foreigner_methods.rb
|
161
|
+
- lib/pg_power/connection_adapters/postgresql_adapter/index_methods.rb
|
162
|
+
- lib/pg_power/connection_adapters/postgresql_adapter/schema_methods.rb
|
163
|
+
- lib/pg_power/connection_adapters/table.rb
|
164
|
+
- lib/pg_power/connection_adapters/table/comment_methods.rb
|
165
|
+
- lib/pg_power/connection_adapters/table/foreigner_methods.rb
|
166
|
+
- lib/pg_power/engine.rb
|
167
|
+
- lib/pg_power/migration.rb
|
168
|
+
- lib/pg_power/migration/command_recorder.rb
|
169
|
+
- lib/pg_power/migration/command_recorder/comment_methods.rb
|
170
|
+
- lib/pg_power/migration/command_recorder/foreigner_methods.rb
|
171
|
+
- lib/pg_power/migration/command_recorder/schema_methods.rb
|
172
|
+
- lib/pg_power/schema_dumper.rb
|
173
|
+
- lib/pg_power/schema_dumper/comment_methods.rb
|
174
|
+
- lib/pg_power/schema_dumper/foreigner_methods.rb
|
175
|
+
- lib/pg_power/schema_dumper/schema_methods.rb
|
176
|
+
- lib/pg_power/tools.rb
|
177
|
+
- lib/pg_power/version.rb
|
178
|
+
- lib/tasks/pg_power_tasks.rake
|
179
|
+
homepage: https://github.com/TMXCredit/pg_power
|
180
|
+
licenses: []
|
181
|
+
|
182
|
+
post_install_message:
|
183
|
+
rdoc_options: []
|
184
|
+
|
185
|
+
require_paths:
|
186
|
+
- lib
|
187
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
+
none: false
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
hash: 3
|
193
|
+
segments:
|
194
|
+
- 0
|
195
|
+
version: "0"
|
196
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
|
+
none: false
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
hash: 3
|
202
|
+
segments:
|
203
|
+
- 0
|
204
|
+
version: "0"
|
205
|
+
requirements: []
|
206
|
+
|
207
|
+
rubyforge_project:
|
208
|
+
rubygems_version: 1.8.24
|
209
|
+
signing_key:
|
210
|
+
specification_version: 3
|
211
|
+
summary: ActiveRecord extensions for PostgreSQL.
|
212
|
+
test_files: []
|
213
|
+
|