multitenancy_tools 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 492c6182c4299f4b2895b9b4709a481c202727a7
4
- data.tar.gz: ffa962fe311628d2d51d8e84238d980ee0905b79
3
+ metadata.gz: 4856c5976dee593214b6b38ae73a1f0c2c0d3c2e
4
+ data.tar.gz: 9a37b22e130a50d8fc5dc472635091e1febc3a87
5
5
  SHA512:
6
- metadata.gz: 346b3a9769bc400f4dcc11704a0a1734bba480a165ce8bfd4e03148f9e25ce96d05ed2ad39a9d02b1b9824cb40c8b2786ccc64ba121a6a3ce3e72c91ec44962e
7
- data.tar.gz: fd44ff73caf4949a6d91a659b08439a4b1f105920747b289ab6ae7fb8c68541acd3c511802c0419102bad5617a5e641cf94a6146ef5c70a9fbd4c1ead4ed782e
6
+ metadata.gz: 6e78755759a2c2852385ca94aefd8f376daf8b86f46bd3439c1562dda8dca7e647896c1dcdb3af735b5905a79e1b9be5656914afbfd8ce94d034647ed776a772
7
+ data.tar.gz: 595e5b7f7ac6860bb41aa05b17534b3c1c312b06161ecb46f4feed3164ce5a182be68a34a46ad2fcb2b248d90b28a9575a0df035f1278ead35ac256007709b83
@@ -8,6 +8,7 @@ require 'multitenancy_tools/schema_switcher'
8
8
  require 'multitenancy_tools/schema_destroyer'
9
9
  require 'multitenancy_tools/functions_dumper'
10
10
  require 'multitenancy_tools/schema_migrator'
11
+ require 'multitenancy_tools/extensions_dumper'
11
12
 
12
13
  module MultitenancyTools
13
14
  # Creates a new schema using the SQL file as template. This SQL file can be
@@ -61,4 +62,13 @@ module MultitenancyTools
61
62
  def self.dump_table(database, schema, table, file, **args)
62
63
  TableDumper.new(database, schema, table).dump_to(file, **args)
63
64
  end
65
+
66
+ # Generates a SQL dump of all extensions enabled on the database.
67
+ #
68
+ # @see ExtensionsDumper
69
+ # @param file [String]
70
+ # @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
71
+ def self.dump_extensions(file, connection = ActiveRecord::Base.connection, **args)
72
+ ExtensionsDumper.new(connection).dump_to(file, **args)
73
+ end
64
74
  end
@@ -0,0 +1,41 @@
1
+ module MultitenancyTools
2
+ # {ExtensionsDumper} can be used to generate a SQL dump of all extensions that
3
+ # are enabled on a PostgreSQL database.
4
+ #
5
+ # @example
6
+ # dumper = MultitenancyTools::ExtensionsDumper.new
7
+ # dumper.dump_to('path/to/file.sql')
8
+ class ExtensionsDumper
9
+ EXTENSION_SQL = <<-'SQL'.freeze
10
+ SELECT extname, nspname FROM pg_catalog.pg_extension
11
+ JOIN pg_catalog.pg_namespace n ON (extnamespace = n.oid)
12
+ SQL
13
+
14
+ CREATE_EXTENSION_SQL = 'CREATE EXTENSION IF NOT EXISTS "%s" WITH SCHEMA %s;'.freeze
15
+
16
+ # @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
17
+ def initialize(connection = ActiveRecord::Base.connection)
18
+ @connection = connection
19
+ end
20
+
21
+ # Generates a dump and writes it into a file. Please see {IO.new} for open
22
+ # modes.
23
+ #
24
+ # @see http://ruby-doc.org/core-2.2.2/IO.html#method-c-new-label-Open+Mode
25
+ # IO Open Modes
26
+ # @param file [String] file path
27
+ # @param mode [String] IO open mode
28
+ def dump_to(file, mode: 'w')
29
+ results = @connection.execute(EXTENSION_SQL)
30
+
31
+ File.open(file, mode) do |f|
32
+ results.each do |result|
33
+ name = result.fetch('extname')
34
+ schema = result.fetch('nspname')
35
+
36
+ f.puts(format(CREATE_EXTENSION_SQL, name, schema))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,10 +2,23 @@ module MultitenancyTools
2
2
  # {FunctionsDumper} can be used to generate a SQL dump of all functions that
3
3
  # are present on a PostgreSQL schema.
4
4
  #
5
+ # Please note that C functions are not included in the dump.
6
+ #
5
7
  # @example
6
8
  # dumper = MultitenancyTools::FunctionsDumper.new('schema name')
7
9
  # dumper.dump_to('path/to/file.sql')
8
10
  class FunctionsDumper
11
+ FUNCTIONS_SQL = <<-SQL.freeze
12
+ SELECT
13
+ trim(trailing e' \n' from pg_get_functiondef(f.oid)) || ';'
14
+ AS definition
15
+ FROM pg_catalog.pg_proc f
16
+ JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
17
+ JOIN pg_catalog.pg_language l ON (f.prolang = l.oid)
18
+ WHERE n.nspname = %s
19
+ AND l.lanname != 'c';
20
+ SQL
21
+
9
22
  # @param schema [String] schema name
10
23
  # @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
11
24
  def initialize(schema, connection = ActiveRecord::Base.connection)
@@ -21,18 +34,11 @@ module MultitenancyTools
21
34
  # @param file [String] file path
22
35
  # @param mode [String] IO open mode
23
36
  def dump_to(file, mode: 'w')
24
- results = @connection.execute(<<-SQL)
25
- SELECT
26
- trim(trailing e' \n' from pg_get_functiondef(f.oid)) || ';\n'
27
- AS definition
28
- FROM pg_catalog.pg_proc f
29
- INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
30
- WHERE n.nspname = #{@schema};
31
- SQL
37
+ results = @connection.execute(format(FUNCTIONS_SQL, @schema))
32
38
 
33
39
  File.open(file, mode) do |f|
34
40
  results.each do |result|
35
- f.write result['definition']
41
+ f.puts result.fetch('definition')
36
42
  end
37
43
  end
38
44
  end
@@ -9,6 +9,7 @@ module MultitenancyTools
9
9
  # * tablespace assigments
10
10
  # * ownership information
11
11
  # * any table data
12
+ # * extensions (CREATE EXTENSION)
12
13
  #
13
14
  # {SchemaDumper} is suitable to create SQL templates for {SchemaCreator}.
14
15
  #
@@ -1,3 +1,3 @@
1
1
  module MultitenancyTools
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multitenancy_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lenon Marcel
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2015-09-02 00:00:00.000000000 Z
14
+ date: 2015-12-02 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -147,6 +147,7 @@ files:
147
147
  - lib/multitenancy_tools.rb
148
148
  - lib/multitenancy_tools/dump_cleaner.rb
149
149
  - lib/multitenancy_tools/errors.rb
150
+ - lib/multitenancy_tools/extensions_dumper.rb
150
151
  - lib/multitenancy_tools/functions_dumper.rb
151
152
  - lib/multitenancy_tools/schema_creator.rb
152
153
  - lib/multitenancy_tools/schema_destroyer.rb
@@ -176,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
177
  version: '0'
177
178
  requirements: []
178
179
  rubyforge_project:
179
- rubygems_version: 2.4.6
180
+ rubygems_version: 2.4.5.1
180
181
  signing_key:
181
182
  specification_version: 4
182
183
  summary: A collection of tools for multitenant Ruby/Rails apps