multitenancy_tools 0.1.7 → 0.1.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 607a5090d739ade79846b7d136b1b9b08fa28980
4
- data.tar.gz: 8672d9af120642bcfc42f0b2431553210bd62ac7
3
+ metadata.gz: b17c6615883612747959c317412a274e079dc2c7
4
+ data.tar.gz: 9159b347030e2aea1985ffa2cea028b4d489d89f
5
5
  SHA512:
6
- metadata.gz: 142763f57d6f6f7b5356dc2d5160423ec312e54f59058036510d2267d741ec50431003733c49db5ac2af75be9d8c91c118a7e0bfe043b4c5ceb7070cadf06454
7
- data.tar.gz: 8c699c5a0e5c89a793234c0fed79985ace9de9b67c465d40a176a04515a14a1df41ed36c3ee17937be7eb3c6426637fa6e349bc8ff878ddc7e23413de3eb7ae5
6
+ metadata.gz: 3eec95eda8db2c65de64ed630dd17b05c3a53b7b249bcaf2e4693f470a616ea2e25ef442e6daaac4038d61a231fb45f15ecd658fd0ba034845a2eb558c7c65f9
7
+ data.tar.gz: aafbd8959cac1ca983bb5800788453bdcfb45120e89b40f78f73b83206bb8a445f614e57cc4c79b76ce8266ee7a76180c2df41cc83338f754a906ace0f4c7325
@@ -1,4 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.3
3
+ - 2.1.6
4
+ - 2.2.2
5
+ addons:
6
+ postgresql: "9.3"
4
7
  before_install: gem install bundler -v 1.10.5
8
+ before_script:
9
+ - cp spec/database.yml.travis spec/database.yml
10
+ script:
11
+ - bundle exec rake spec
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in multitenancy_tools.gemspec
4
4
  gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # MultitenancyTools
2
2
 
3
- This gem is a collection of tools that can be used to handle multitenant
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
- #### Dumping the structure of a PostgreSQL schema to a SQL file
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
- #### Dumping the content of a table to a SQL file
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
- #### Creating a new PostgreSQL schema using a SQL file as template
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, run `bin/setup` to install dependencies. Then,
56
- edit `spec/database.yml` with your PostgreSQL database connection details (take
57
- a look at the example in `spec/database.yml.example`). **IMPORTANT:** this
58
- database will *be destroyed and recreated* on test execution.
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 `rake spec` to run the tests. You can also run `bin/console` for an
61
- interactive prompt that will allow you to experiment.
69
+ You can use `bin/console` to get an interactive prompt that will allow you to
70
+ experiment.
62
71
 
63
- To install this gem onto your local machine, run `bundle exec rake install`.
64
- To release a new version, update the version number in `version.rb`, and then
65
- run `bundle exec rake release`, which will create a git tag for the version,
66
- push git commits and tags, and push the `.gem` file to
67
- [rubygems.org](https://rubygems.org).
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module MultitenancyTools
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
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.7
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-07-20 00:00:00.000000000 Z
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