pg_saurus 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/README.markdown +341 -0
  3. data/lib/colorized_text.rb +33 -0
  4. data/lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb +155 -0
  5. data/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb +191 -0
  6. data/lib/core_ext/active_record/errors.rb +6 -0
  7. data/lib/core_ext/active_record/schema_dumper.rb +42 -0
  8. data/lib/pg_saurus/connection_adapters/abstract_adapter/comment_methods.rb +80 -0
  9. data/lib/pg_saurus/connection_adapters/abstract_adapter/foreigner_methods.rb +67 -0
  10. data/lib/pg_saurus/connection_adapters/abstract_adapter/index_methods.rb +6 -0
  11. data/lib/pg_saurus/connection_adapters/abstract_adapter/schema_methods.rb +20 -0
  12. data/lib/pg_saurus/connection_adapters/abstract_adapter.rb +20 -0
  13. data/lib/pg_saurus/connection_adapters/foreign_key_definition.rb +5 -0
  14. data/lib/pg_saurus/connection_adapters/index_definition.rb +8 -0
  15. data/lib/pg_saurus/connection_adapters/postgresql_adapter/comment_methods.rb +114 -0
  16. data/lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb +124 -0
  17. data/lib/pg_saurus/connection_adapters/postgresql_adapter/foreigner_methods.rb +221 -0
  18. data/lib/pg_saurus/connection_adapters/postgresql_adapter/index_methods.rb +42 -0
  19. data/lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb +58 -0
  20. data/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb +20 -0
  21. data/lib/pg_saurus/connection_adapters/postgresql_adapter/view_methods.rb +17 -0
  22. data/lib/pg_saurus/connection_adapters/postgresql_adapter.rb +28 -0
  23. data/lib/pg_saurus/connection_adapters/table/comment_methods.rb +58 -0
  24. data/lib/pg_saurus/connection_adapters/table/foreigner_methods.rb +51 -0
  25. data/lib/pg_saurus/connection_adapters/table.rb +17 -0
  26. data/lib/pg_saurus/connection_adapters.rb +9 -0
  27. data/lib/pg_saurus/create_index_concurrently.rb +227 -0
  28. data/lib/pg_saurus/engine.rb +57 -0
  29. data/lib/pg_saurus/errors.rb +6 -0
  30. data/lib/pg_saurus/migration/command_recorder/comment_methods.rb +68 -0
  31. data/lib/pg_saurus/migration/command_recorder/extension_methods.rb +25 -0
  32. data/lib/pg_saurus/migration/command_recorder/foreigner_methods.rb +31 -0
  33. data/lib/pg_saurus/migration/command_recorder/schema_methods.rb +59 -0
  34. data/lib/pg_saurus/migration/command_recorder/view_methods.rb +31 -0
  35. data/lib/pg_saurus/migration/command_recorder.rb +17 -0
  36. data/lib/pg_saurus/migration.rb +4 -0
  37. data/lib/pg_saurus/schema_dumper/comment_methods.rb +51 -0
  38. data/lib/pg_saurus/schema_dumper/extension_methods.rb +29 -0
  39. data/lib/pg_saurus/schema_dumper/foreigner_methods.rb +63 -0
  40. data/lib/pg_saurus/schema_dumper/schema_methods.rb +27 -0
  41. data/lib/pg_saurus/schema_dumper/view_methods.rb +32 -0
  42. data/lib/pg_saurus/schema_dumper.rb +28 -0
  43. data/lib/pg_saurus/tools.rb +104 -0
  44. data/lib/pg_saurus/version.rb +4 -0
  45. data/lib/pg_saurus.rb +18 -0
  46. data/lib/tasks/pg_saurus_tasks.rake +4 -0
  47. metadata +226 -0
@@ -0,0 +1,27 @@
1
+ # Extends ActiveRecord::SchemaDumper class to dump schemas other than "public"
2
+ # and tables from those schemas.
3
+ module PgSaurus::SchemaDumper::SchemaMethods
4
+ # Dump create schema statements
5
+ def header_with_schemas(stream)
6
+ header_without_schemas(stream)
7
+ schemas(stream)
8
+ stream
9
+ end
10
+
11
+ # Generates code to create schemas.
12
+ def schemas(stream)
13
+ # Don't create "public" schema since it exists by default.
14
+ schema_names = PgSaurus::Tools.schemas - ["public", "information_schema"]
15
+ schema_names.each do |schema_name|
16
+ schema(schema_name, stream)
17
+ end
18
+ stream << "\n"
19
+ end
20
+ private :schemas
21
+
22
+ # Generates code to create schema.
23
+ def schema(schema_name, stream)
24
+ stream << " create_schema \"#{schema_name}\"\n"
25
+ end
26
+ private :schema
27
+ end
@@ -0,0 +1,32 @@
1
+ # Extends ActiveRecord::SchemaDumper class to dump views
2
+ module PgSaurus::SchemaDumper::ViewMethods
3
+ # Dump create view statements
4
+ def tables_with_views(stream)
5
+ tables_without_views(stream)
6
+ views(stream)
7
+ stream
8
+ end
9
+
10
+ # Generates code to create views.
11
+ def views(stream)
12
+ # Don't create "system" views.
13
+ view_names = PgSaurus::Tools.views
14
+ view_names.each do |options|
15
+ write_view_definition(stream,
16
+ options["table_schema"],
17
+ options["table_name"],
18
+ options["view_definition"])
19
+ end
20
+ stream << "\n"
21
+ end
22
+ private :views
23
+
24
+ # Generates code to create view.
25
+ def write_view_definition(stream, table_schema, table_name, view_definition)
26
+ stream << " create_view \"#{table_schema}.#{table_name}\", <<-SQL\n" \
27
+ " #{view_definition}\n" \
28
+ " SQL\n"
29
+ end
30
+ private :write_view_definition
31
+
32
+ end
@@ -0,0 +1,28 @@
1
+ # Provides methods to extend {ActiveRecord::SchemaDumper} to appropriately
2
+ # build schema.rb file with schemas, foreign keys and comments on columns
3
+ # and tables.
4
+ module PgSaurus::SchemaDumper
5
+ extend ActiveSupport::Autoload
6
+ extend ActiveSupport::Concern
7
+
8
+ autoload :ExtensionMethods
9
+ autoload :CommentMethods
10
+ autoload :SchemaMethods
11
+ autoload :ForeignerMethods
12
+ autoload :ViewMethods
13
+
14
+ include ExtensionMethods
15
+ include CommentMethods
16
+ include SchemaMethods
17
+ include ForeignerMethods
18
+ include ViewMethods
19
+
20
+ included do
21
+ alias_method_chain :header, :schemas
22
+ alias_method_chain :header, :extensions
23
+
24
+ alias_method_chain :tables, :views
25
+ alias_method_chain :tables, :comments
26
+ alias_method_chain :tables, :foreign_keys
27
+ end
28
+ end
@@ -0,0 +1,104 @@
1
+ module PgSaurus
2
+ # Provides utility methods to work with PostgreSQL databases.
3
+ # Usage:
4
+ # PgSaurus::Tools.create_schema "services" # => create new PG schema "services"
5
+ # PgSaurus::Tools.create_schema "nets"
6
+ # PgSaurus::Tools.drop_schema "services" # => remove the schema
7
+ # PgSaurus::Tools.schemas # => ["public", "information_schema", "nets"]
8
+ # PgSaurus::Tools.move_table_to_schema :computers, :nets
9
+ # PgSaurus::Tools.create_view view_name, view_definition # => creates new DB view
10
+ # PgSaurus::Tools.drop_view view_name # => removes the view
11
+ # PgSaurus::Tools.views # => ["x_view", "y_view", "z_view"]
12
+ module Tools
13
+ extend self
14
+
15
+ # Creates PostgreSQL schema
16
+ def create_schema(schema_name)
17
+ sql = %{CREATE SCHEMA "#{schema_name}"}
18
+ connection.execute sql
19
+ end
20
+
21
+ # Create a schema if it does not exist yet.
22
+ #
23
+ # @note
24
+ # CREATE SCHEMA IF EXISTS appeared only in PostgreSQL 9.3
25
+ # It is not used here for backward compatibility.
26
+ #
27
+ # @return [void]
28
+ def create_schema_if_not_exists(schema_name)
29
+ unless schemas.include?(schema_name.to_s)
30
+ create_schema(schema_name)
31
+ end
32
+ end
33
+
34
+ # Ensure schema does not exists.
35
+ #
36
+ # @return [void]
37
+ def drop_schema_if_exists(schema_name)
38
+ if schemas.include?(schema_name.to_s)
39
+ drop_schema(schema_name)
40
+ end
41
+ end
42
+
43
+ # Drops PostgreSQL schema
44
+ def drop_schema(schema_name)
45
+ sql = %{DROP SCHEMA "#{schema_name}"}
46
+ connection.execute sql
47
+ end
48
+
49
+ # Returns an array of existing schemas.
50
+ def schemas
51
+ sql = "SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' order by nspname"
52
+ connection.query(sql).flatten
53
+ end
54
+
55
+ # Move table to another schema without loosing data, indexes or constraints.
56
+ # @param [String] table table name (schema prefix is allowed)
57
+ # @param [String] new_schema schema where table should be moved to
58
+ def move_table_to_schema(table, new_schema)
59
+ schema, table = to_schema_and_table(table)
60
+ sql = %{ALTER TABLE "#{schema}"."#{table}" SET SCHEMA "#{new_schema}"}
61
+ connection.execute sql
62
+ end
63
+
64
+ # Creates PostgreSQL view
65
+ # @param [String, Symbol] view_name
66
+ # @param [String] view_definition
67
+ def create_view(view_name, view_definition)
68
+ sql = "CREATE VIEW #{view_name} AS #{view_definition}"
69
+ connection.execute sql
70
+ end
71
+
72
+ # Drops PostgreSQL view
73
+ # @param [String, Symbol] view_name
74
+ def drop_view(view_name)
75
+ sql = "DROP VIEW #{view_name}"
76
+ connection.execute sql
77
+ end
78
+
79
+ # Returns an array of existing, non system views.
80
+ def views
81
+ sql = <<-SQL
82
+ SELECT table_schema, table_name, view_definition
83
+ FROM INFORMATION_SCHEMA.views
84
+ WHERE table_schema NOT IN ('pg_catalog','information_schema')
85
+ SQL
86
+ connection.execute sql
87
+ end
88
+
89
+ # Return database connections
90
+ def connection
91
+ ActiveRecord::Base.connection
92
+ end
93
+ private :connection
94
+
95
+ # Extract schema name and table name from qualified table name
96
+ # @param [String, Symbol] table_name table name
97
+ # @return [Array[String, String]] schema and table
98
+ def to_schema_and_table(table_name)
99
+ table, schema = table_name.to_s.split(".", 2).reverse
100
+ schema ||= "public"
101
+ [schema, table]
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,4 @@
1
+ module PgSaurus
2
+ # Version of pg_saurus gem.
3
+ VERSION = '2.1.1'
4
+ end
data/lib/pg_saurus.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "pg_saurus/engine"
2
+ require "pg_saurus/errors"
3
+
4
+ # Rails engine which allows to use some PostgreSQL features:
5
+ # * Schemas.
6
+ # * Comments on columns and tables.
7
+ # * Foreign keys.
8
+ # * Partial indexes.
9
+ module PgSaurus
10
+ extend ActiveSupport::Autoload
11
+
12
+ autoload :Adapter
13
+ autoload :SchemaDumper
14
+ autoload :Tools
15
+ autoload :Migration
16
+ autoload :ConnectionAdapters
17
+ autoload :CreateIndexConcurrently
18
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :pg_saurus do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_saurus
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Potapov Sergey
8
+ - Arthur Shagall
9
+ - Artem Ignatyev
10
+ - Matt Dressel
11
+ - Bruce Burdick
12
+ - HornsAndHooves
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2014-09-11 00:00:00.000000000 Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: pg
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rails
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '4.0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '4.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: 3.1.0
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 3.1.0
60
+ - !ruby/object:Gem::Dependency
61
+ name: yard
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: metric_fu
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ - !ruby/object:Gem::Dependency
89
+ name: jeweler
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ type: :development
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ - !ruby/object:Gem::Dependency
103
+ name: byebug
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ - !ruby/object:Gem::Dependency
117
+ name: pry
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ - !ruby/object:Gem::Dependency
131
+ name: pry-byebug
132
+ requirement: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ description: ActiveRecord extensions for PostgreSQL. Provides useful tools for schema,
145
+ foreign_key, index, comment and extension manipulations in migrations.
146
+ email:
147
+ - blake131313@gmail.com
148
+ - arthur.shagall@gmail.com
149
+ - cryo28@gmail.com
150
+ - matt.dressel@gmail.com
151
+ - rubygems.org@bruceburdick.com
152
+ executables: []
153
+ extensions: []
154
+ extra_rdoc_files:
155
+ - README.markdown
156
+ files:
157
+ - README.markdown
158
+ - lib/colorized_text.rb
159
+ - lib/core_ext/active_record/connection_adapters/abstract/schema_statements.rb
160
+ - lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb
161
+ - lib/core_ext/active_record/errors.rb
162
+ - lib/core_ext/active_record/schema_dumper.rb
163
+ - lib/pg_saurus.rb
164
+ - lib/pg_saurus/connection_adapters.rb
165
+ - lib/pg_saurus/connection_adapters/abstract_adapter.rb
166
+ - lib/pg_saurus/connection_adapters/abstract_adapter/comment_methods.rb
167
+ - lib/pg_saurus/connection_adapters/abstract_adapter/foreigner_methods.rb
168
+ - lib/pg_saurus/connection_adapters/abstract_adapter/index_methods.rb
169
+ - lib/pg_saurus/connection_adapters/abstract_adapter/schema_methods.rb
170
+ - lib/pg_saurus/connection_adapters/foreign_key_definition.rb
171
+ - lib/pg_saurus/connection_adapters/index_definition.rb
172
+ - lib/pg_saurus/connection_adapters/postgresql_adapter.rb
173
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/comment_methods.rb
174
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/extension_methods.rb
175
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/foreigner_methods.rb
176
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/index_methods.rb
177
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb
178
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb
179
+ - lib/pg_saurus/connection_adapters/postgresql_adapter/view_methods.rb
180
+ - lib/pg_saurus/connection_adapters/table.rb
181
+ - lib/pg_saurus/connection_adapters/table/comment_methods.rb
182
+ - lib/pg_saurus/connection_adapters/table/foreigner_methods.rb
183
+ - lib/pg_saurus/create_index_concurrently.rb
184
+ - lib/pg_saurus/engine.rb
185
+ - lib/pg_saurus/errors.rb
186
+ - lib/pg_saurus/migration.rb
187
+ - lib/pg_saurus/migration/command_recorder.rb
188
+ - lib/pg_saurus/migration/command_recorder/comment_methods.rb
189
+ - lib/pg_saurus/migration/command_recorder/extension_methods.rb
190
+ - lib/pg_saurus/migration/command_recorder/foreigner_methods.rb
191
+ - lib/pg_saurus/migration/command_recorder/schema_methods.rb
192
+ - lib/pg_saurus/migration/command_recorder/view_methods.rb
193
+ - lib/pg_saurus/schema_dumper.rb
194
+ - lib/pg_saurus/schema_dumper/comment_methods.rb
195
+ - lib/pg_saurus/schema_dumper/extension_methods.rb
196
+ - lib/pg_saurus/schema_dumper/foreigner_methods.rb
197
+ - lib/pg_saurus/schema_dumper/schema_methods.rb
198
+ - lib/pg_saurus/schema_dumper/view_methods.rb
199
+ - lib/pg_saurus/tools.rb
200
+ - lib/pg_saurus/version.rb
201
+ - lib/tasks/pg_saurus_tasks.rake
202
+ homepage: https://github.com/HornsAndHooves/pg_saurus
203
+ licenses:
204
+ - MIT
205
+ metadata: {}
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 2.2.2
223
+ signing_key:
224
+ specification_version: 4
225
+ summary: ActiveRecord extensions for PostgreSQL.
226
+ test_files: []