multitenancy_tools 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -1
- data/Gemfile +2 -0
- data/README.md +36 -15
- data/lib/multitenancy_tools.rb +26 -0
- data/lib/multitenancy_tools/schema_creator.rb +1 -1
- data/lib/multitenancy_tools/schema_destroyer.rb +20 -0
- data/lib/multitenancy_tools/schema_switcher.rb +1 -1
- data/lib/multitenancy_tools/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b17c6615883612747959c317412a274e079dc2c7
|
4
|
+
data.tar.gz: 9159b347030e2aea1985ffa2cea028b4d489d89f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3eec95eda8db2c65de64ed630dd17b05c3a53b7b249bcaf2e4693f470a616ea2e25ef442e6daaac4038d61a231fb45f15ecd658fd0ba034845a2eb558c7c65f9
|
7
|
+
data.tar.gz: aafbd8959cac1ca983bb5800788453bdcfb45120e89b40f78f73b83206bb8a445f614e57cc4c79b76ce8266ee7a76180c2df41cc83338f754a906ace0f4c7325
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
# MultitenancyTools
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status][travis-badge]][travis-build]
|
4
|
+
[![Code Climate][cc-badge]][cc-details]
|
5
|
+
[![Test Coverage][cc-cov-badge]][cc-cov-details]
|
6
|
+
|
7
|
+
This Ruby gem is a collection of tools that can be used to handle multitenant
|
4
8
|
Ruby/Rails apps. The currently only supported database is PostgreSQL and there
|
5
9
|
is no plan to support other databases.
|
6
10
|
|
11
|
+
The documentation is [available on RubyDoc.info][docs].
|
12
|
+
|
7
13
|
## Installation
|
8
14
|
|
9
15
|
Add this line to your application's Gemfile:
|
@@ -22,7 +28,7 @@ Or install it yourself as:
|
|
22
28
|
|
23
29
|
## Usage
|
24
30
|
|
25
|
-
|
31
|
+
### Dumping the structure of a PostgreSQL schema to a SQL file
|
26
32
|
|
27
33
|
Please note that `pg_dump` must be on your `PATH`:
|
28
34
|
|
@@ -31,7 +37,7 @@ dumper = MultitenancyTools::SchemaDumper.new('database name', 'schema name')
|
|
31
37
|
dumper.dump_to('path/to/file.sql')
|
32
38
|
```
|
33
39
|
|
34
|
-
|
40
|
+
### Dumping the content of a table to a SQL file
|
35
41
|
|
36
42
|
Like `SchemaDumper`, this tool also requires `pg_dump` to be on the `PATH`:
|
37
43
|
|
@@ -40,7 +46,7 @@ dumper = MultitenancyTools::TableDumper.new('database name', 'schema name', 'tab
|
|
40
46
|
dumper.dump_to('path/to/file.sql')
|
41
47
|
```
|
42
48
|
|
43
|
-
|
49
|
+
### Creating a new PostgreSQL schema using a SQL file as template
|
44
50
|
|
45
51
|
After using `SchemaDumper` to create the SQL template, you can use the following
|
46
52
|
class to create a new schema using this file as template:
|
@@ -52,19 +58,25 @@ creator.create_from_file('path/to/file.sql')
|
|
52
58
|
|
53
59
|
## Development
|
54
60
|
|
55
|
-
After checking out the repo
|
56
|
-
|
57
|
-
|
58
|
-
|
61
|
+
After checking out the repo:
|
62
|
+
|
63
|
+
1. Install dependencies using `bin/setup`.
|
64
|
+
2. Create the file `spec/database.yml` and configure it with your PostgreSQL
|
65
|
+
database. There is an example on `spec/database.yml.example`. **Important:**
|
66
|
+
this database *will be destroyed and recreated* on test execution.
|
67
|
+
3. Run specs using `bundle exec rake spec` to make sure that everything is fine.
|
59
68
|
|
60
|
-
You can use `
|
61
|
-
|
69
|
+
You can use `bin/console` to get an interactive prompt that will allow you to
|
70
|
+
experiment.
|
62
71
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
72
|
+
## Releasing a new version
|
73
|
+
|
74
|
+
If you are the maintainer of this project:
|
75
|
+
|
76
|
+
1. Update the version number in `lib/multitenancy_tools/version.rb`.
|
77
|
+
2. Make sure that all tests are green (run `bundle exec rake spec`).
|
78
|
+
3. Execute `bundle exec rake release` to create a git tag for the version, push
|
79
|
+
git commits and tags, and publish the gem on [RubyGems.org][rubygems].
|
68
80
|
|
69
81
|
## Contributing
|
70
82
|
|
@@ -75,3 +87,12 @@ https://github.com/locaweb/multitenancy_tools.
|
|
75
87
|
|
76
88
|
The gem is available as open source under the terms of the
|
77
89
|
[MIT License](http://opensource.org/licenses/MIT).
|
90
|
+
|
91
|
+
[travis-badge]: https://travis-ci.org/locaweb/multitenancy_tools.svg?branch=master
|
92
|
+
[travis-build]: https://travis-ci.org/locaweb/multitenancy_tools
|
93
|
+
[cc-badge]: https://codeclimate.com/github/locaweb/multitenancy_tools/badges/gpa.svg
|
94
|
+
[cc-details]: https://codeclimate.com/github/locaweb/multitenancy_tools
|
95
|
+
[cc-cov-badge]: https://codeclimate.com/github/locaweb/multitenancy_tools/badges/coverage.svg
|
96
|
+
[cc-cov-details]: https://codeclimate.com/github/locaweb/multitenancy_tools/coverage
|
97
|
+
[docs]: http://www.rubydoc.info/gems/multitenancy_tools
|
98
|
+
[rubygems]: https://rubygems.org/gems/multitenancy_tools
|
data/lib/multitenancy_tools.rb
CHANGED
@@ -5,6 +5,32 @@ require 'multitenancy_tools/schema_dumper'
|
|
5
5
|
require 'multitenancy_tools/schema_creator'
|
6
6
|
require 'multitenancy_tools/table_dumper'
|
7
7
|
require 'multitenancy_tools/schema_switcher'
|
8
|
+
require 'multitenancy_tools/schema_destroyer'
|
8
9
|
|
9
10
|
module MultitenancyTools
|
11
|
+
# Creates a new schema using the SQL file as template.
|
12
|
+
#
|
13
|
+
# @param name [String] schema name
|
14
|
+
# @param sql_file [String] absolute path to the SQL template
|
15
|
+
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
16
|
+
def self.create(name, sql_file, connection = ActiveRecord::Base.connection)
|
17
|
+
SchemaCreator.new(name, connection).create_from_file(sql_file)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Removes a schema from the database.
|
21
|
+
#
|
22
|
+
# @param name [String] schema name
|
23
|
+
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
24
|
+
def self.destroy(name, connection = ActiveRecord::Base.connection)
|
25
|
+
SchemaDestroyer.new(name, connection).destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
# Uses the passed schema as the scope for all queries triggered by the block.
|
29
|
+
#
|
30
|
+
# @param schema [String] schema name
|
31
|
+
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
32
|
+
# @yield The block that must be executed using the schema as scope
|
33
|
+
def self.using(schema, connection = ActiveRecord::Base.connection, &block)
|
34
|
+
SchemaSwitcher.new(schema, connection).run(&block)
|
35
|
+
end
|
10
36
|
end
|
@@ -9,7 +9,7 @@ module MultitenancyTools
|
|
9
9
|
class SchemaCreator
|
10
10
|
# @param schema [String] schema name
|
11
11
|
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
12
|
-
def initialize(schema, connection)
|
12
|
+
def initialize(schema, connection = ActiveRecord::Base.connection)
|
13
13
|
@schema = schema
|
14
14
|
@connection = connection
|
15
15
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MultitenancyTools
|
2
|
+
# {SchemaDestroyer} can be used to destroy a PostgreSQL schema.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
# destroyer = MultitenancyTools::SchemaDestroyer.new('schema name')
|
6
|
+
# destroyer.destroy
|
7
|
+
class SchemaDestroyer
|
8
|
+
# @param schema [String] schema name
|
9
|
+
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
10
|
+
def initialize(schema, connection = ActiveRecord::Base.connection)
|
11
|
+
@connection = connection
|
12
|
+
@schema = @connection.quote_table_name(schema)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Drops the schema.
|
16
|
+
def destroy
|
17
|
+
@connection.drop_schema(@schema)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -4,7 +4,7 @@ module MultitenancyTools
|
|
4
4
|
class SchemaSwitcher
|
5
5
|
# @param schema [String] schema name
|
6
6
|
# @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
|
7
|
-
def initialize(schema, connection)
|
7
|
+
def initialize(schema, connection = ActiveRecord::Base.connection)
|
8
8
|
@connection = connection
|
9
9
|
@schema = @connection.quote(schema)
|
10
10
|
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.8
|
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-
|
14
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/multitenancy_tools/dump_cleaner.rb
|
121
121
|
- lib/multitenancy_tools/errors.rb
|
122
122
|
- lib/multitenancy_tools/schema_creator.rb
|
123
|
+
- lib/multitenancy_tools/schema_destroyer.rb
|
123
124
|
- lib/multitenancy_tools/schema_dumper.rb
|
124
125
|
- lib/multitenancy_tools/schema_switcher.rb
|
125
126
|
- lib/multitenancy_tools/table_dumper.rb
|