multitenancy_tools 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/multitenancy_tools.rb +29 -2
- data/lib/multitenancy_tools/functions_dumper.rb +40 -0
- data/lib/multitenancy_tools/version.rb +1 -1
- data/multitenancy_tools.gemspec +4 -2
- metadata +32 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38a295e4d0fb3dc90035639d066137927c576551
|
4
|
+
data.tar.gz: f0ee6b1bd47bf9d4f357b599fd88aab97c7a2bfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c354ac59d1a9bdd44ebcd9832f69b16e794d086104b9d8fa1ba9361c86b82715f48220479ba5a31091c3f90bc7af3e84e7028f58a78cc3ef1c9747fb1930720
|
7
|
+
data.tar.gz: acdf161ac4b73e1c57c9631c5d836d3fb892312a487d1ff9f9266d92fac985f37ca58b96e2d7a613e970954c60d48a8e0e500198627bf97c0b22f5d7f61aafd0
|
data/README.md
CHANGED
@@ -5,8 +5,11 @@
|
|
5
5
|
[![Test Coverage][cc-cov-badge]][cc-cov-details]
|
6
6
|
|
7
7
|
This Ruby gem is a collection of tools that can be used to handle multitenant
|
8
|
-
Ruby/Rails apps.
|
9
|
-
|
8
|
+
Ruby/Rails apps. Like the name says, it is just a collection of tools and not
|
9
|
+
an opinionated implementation and does not require a specific architecture.
|
10
|
+
|
11
|
+
The currently only supported database is PostgreSQL and there is no plan to
|
12
|
+
support other databases.
|
10
13
|
|
11
14
|
The documentation is [available on RubyDoc.info][docs].
|
12
15
|
|
data/lib/multitenancy_tools.rb
CHANGED
@@ -6,10 +6,14 @@ require 'multitenancy_tools/schema_creator'
|
|
6
6
|
require 'multitenancy_tools/table_dumper'
|
7
7
|
require 'multitenancy_tools/schema_switcher'
|
8
8
|
require 'multitenancy_tools/schema_destroyer'
|
9
|
+
require 'multitenancy_tools/functions_dumper'
|
9
10
|
|
10
11
|
module MultitenancyTools
|
11
|
-
# Creates a new schema using the SQL file as template.
|
12
|
+
# Creates a new schema using the SQL file as template. This SQL file can be
|
13
|
+
# generated by {SchemaDumper}.
|
12
14
|
#
|
15
|
+
# @see SchemaCreator
|
16
|
+
# @see SchemaDumper
|
13
17
|
# @param name [String] schema name
|
14
18
|
# @param sql_file [String] absolute path to the SQL template
|
15
19
|
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
@@ -17,8 +21,9 @@ module MultitenancyTools
|
|
17
21
|
SchemaCreator.new(name, connection).create_from_file(sql_file)
|
18
22
|
end
|
19
23
|
|
20
|
-
#
|
24
|
+
# Drops the schema from the database.
|
21
25
|
#
|
26
|
+
# @see SchemaDestroyer
|
22
27
|
# @param name [String] schema name
|
23
28
|
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
24
29
|
def self.destroy(name, connection = ActiveRecord::Base.connection)
|
@@ -27,10 +32,32 @@ module MultitenancyTools
|
|
27
32
|
|
28
33
|
# Uses the passed schema as the scope for all queries triggered by the block.
|
29
34
|
#
|
35
|
+
# @see SchemaSwitcher
|
30
36
|
# @param schema [String] schema name
|
31
37
|
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
32
38
|
# @yield The block that must be executed using the schema as scope
|
33
39
|
def self.using(schema, connection = ActiveRecord::Base.connection, &block)
|
34
40
|
SchemaSwitcher.new(schema, connection).run(&block)
|
35
41
|
end
|
42
|
+
|
43
|
+
# Generates a SQL dump of the schema. Requires pg_dump.
|
44
|
+
#
|
45
|
+
# @see SchemaDumper
|
46
|
+
# @param database [String]
|
47
|
+
# @param schema [String]
|
48
|
+
# @param file [String]
|
49
|
+
def self.dump_schema(database, schema, file, **args)
|
50
|
+
SchemaDumper.new(database, schema).dump_to(file, **args)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Generates a SQL dump of the table. Requires pg_dump.
|
54
|
+
#
|
55
|
+
# @see TableDumper
|
56
|
+
# @param database [String]
|
57
|
+
# @param schema [String]
|
58
|
+
# @param table [String]
|
59
|
+
# @param file [String]
|
60
|
+
def self.dump_table(database, schema, table, file, **args)
|
61
|
+
TableDumper.new(database, schema, table).dump_to(file, **args)
|
62
|
+
end
|
36
63
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module MultitenancyTools
|
2
|
+
# {FunctionsDumper} can be used to generate a SQL dump of all functions that
|
3
|
+
# are present on a PostgreSQL schema.
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# dumper = MultitenancyTools::FunctionsDumper.new('schema name')
|
7
|
+
# dumper.dump_to('path/to/file.sql')
|
8
|
+
class FunctionsDumper
|
9
|
+
# @param schema [String] schema name
|
10
|
+
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
11
|
+
def initialize(schema, connection = ActiveRecord::Base.connection)
|
12
|
+
@connection = connection
|
13
|
+
@schema = @connection.quote(schema)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Generates a dump and writes it into a file. Please see {IO.new} for open
|
17
|
+
# modes.
|
18
|
+
#
|
19
|
+
# @see http://ruby-doc.org/core-2.2.2/IO.html#method-c-new-label-Open+Mode
|
20
|
+
# IO Open Modes
|
21
|
+
# @param file [String] file path
|
22
|
+
# @param mode [String] IO open mode
|
23
|
+
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
|
32
|
+
|
33
|
+
File.open(file, mode) do |f|
|
34
|
+
results.each do |result|
|
35
|
+
f.write result['definition']
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/multitenancy_tools.gemspec
CHANGED
@@ -20,10 +20,12 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
+
spec.add_dependency 'activerecord', '~> 4.2.0'
|
24
|
+
spec.add_dependency 'pg'
|
25
|
+
|
23
26
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
24
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
28
|
spec.add_development_dependency 'rspec', '~> 3.3.0'
|
26
|
-
spec.add_development_dependency 'activerecord', '~> 4.2.0'
|
27
|
-
spec.add_development_dependency 'pg'
|
28
29
|
spec.add_development_dependency 'yard'
|
30
|
+
spec.add_development_dependency 'pry'
|
29
31
|
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.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lenon Marcel
|
@@ -11,66 +11,80 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: exe
|
13
13
|
cert_chain: []
|
14
|
-
date: 2015-08-
|
14
|
+
date: 2015-08-18 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: activerecord
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
20
|
- - "~>"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
23
|
-
type: :
|
22
|
+
version: 4.2.0
|
23
|
+
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 4.2.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
31
|
+
name: pg
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: bundler
|
32
46
|
requirement: !ruby/object:Gem::Requirement
|
33
47
|
requirements:
|
34
48
|
- - "~>"
|
35
49
|
- !ruby/object:Gem::Version
|
36
|
-
version: '10
|
50
|
+
version: '1.10'
|
37
51
|
type: :development
|
38
52
|
prerelease: false
|
39
53
|
version_requirements: !ruby/object:Gem::Requirement
|
40
54
|
requirements:
|
41
55
|
- - "~>"
|
42
56
|
- !ruby/object:Gem::Version
|
43
|
-
version: '10
|
57
|
+
version: '1.10'
|
44
58
|
- !ruby/object:Gem::Dependency
|
45
|
-
name:
|
59
|
+
name: rake
|
46
60
|
requirement: !ruby/object:Gem::Requirement
|
47
61
|
requirements:
|
48
62
|
- - "~>"
|
49
63
|
- !ruby/object:Gem::Version
|
50
|
-
version:
|
64
|
+
version: '10.0'
|
51
65
|
type: :development
|
52
66
|
prerelease: false
|
53
67
|
version_requirements: !ruby/object:Gem::Requirement
|
54
68
|
requirements:
|
55
69
|
- - "~>"
|
56
70
|
- !ruby/object:Gem::Version
|
57
|
-
version:
|
71
|
+
version: '10.0'
|
58
72
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
73
|
+
name: rspec
|
60
74
|
requirement: !ruby/object:Gem::Requirement
|
61
75
|
requirements:
|
62
76
|
- - "~>"
|
63
77
|
- !ruby/object:Gem::Version
|
64
|
-
version:
|
78
|
+
version: 3.3.0
|
65
79
|
type: :development
|
66
80
|
prerelease: false
|
67
81
|
version_requirements: !ruby/object:Gem::Requirement
|
68
82
|
requirements:
|
69
83
|
- - "~>"
|
70
84
|
- !ruby/object:Gem::Version
|
71
|
-
version:
|
85
|
+
version: 3.3.0
|
72
86
|
- !ruby/object:Gem::Dependency
|
73
|
-
name:
|
87
|
+
name: yard
|
74
88
|
requirement: !ruby/object:Gem::Requirement
|
75
89
|
requirements:
|
76
90
|
- - ">="
|
@@ -84,7 +98,7 @@ dependencies:
|
|
84
98
|
- !ruby/object:Gem::Version
|
85
99
|
version: '0'
|
86
100
|
- !ruby/object:Gem::Dependency
|
87
|
-
name:
|
101
|
+
name: pry
|
88
102
|
requirement: !ruby/object:Gem::Requirement
|
89
103
|
requirements:
|
90
104
|
- - ">="
|
@@ -119,6 +133,7 @@ files:
|
|
119
133
|
- lib/multitenancy_tools.rb
|
120
134
|
- lib/multitenancy_tools/dump_cleaner.rb
|
121
135
|
- lib/multitenancy_tools/errors.rb
|
136
|
+
- lib/multitenancy_tools/functions_dumper.rb
|
122
137
|
- lib/multitenancy_tools/schema_creator.rb
|
123
138
|
- lib/multitenancy_tools/schema_destroyer.rb
|
124
139
|
- lib/multitenancy_tools/schema_dumper.rb
|