mongoid_schema_dump 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/mongoid_schema_dump.rb +121 -0
- data/lib/mongoid_schema_dump/engine.rb +5 -0
- data/lib/mongoid_schema_dump/version.rb +3 -0
- data/lib/rails/generators/mongoid_schema_dump/task_generator.rb +16 -0
- data/lib/rails/generators/mongoid_schema_dump/templates/mongoid_schema_dump.rake +10 -0
- data/mongoid_schema_dump.gemspec +27 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 005aae594a16407adeddafc34cabefce3a262366
|
4
|
+
data.tar.gz: 9b70d0fa0b87c436a1f3e7c239c90c8d4eb0340f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82ce1e21d99c4f23089bbf7fe93f0817dd0becd374c93446778f90ac2e291eda5e8e8848b682b5affd8124cb5a456a66d99bb9004b2ba5feb4aa620c1e3d0e3d
|
7
|
+
data.tar.gz: a1ee07445eb9c7480cdb4cbcf8b44e84d298a0af0b67444de52c9d9978a0fe3eb6cd3a94f0c890893541e42b5b6f4c4df15b2a1c844140e8db2c5f02e6229c9c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Applidget SAS
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Mongoid Schema Dump
|
2
|
+
|
3
|
+
This gem allows you to dump your Mongoid schema based on a top level object. If you have an `Organization` model or equivalent, this tool can help you get the shortest path (by following `belongs_to` relationships) from any other object from your data model to your `Organization` model.
|
4
|
+
|
5
|
+
You can then use the output of this gem in combination with https://github.com/applidget/mongo-partial-dump, a MongoDB extractor reading from a database a writing to another one based on the filters you define with this gem.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mongoid_schema_dump'
|
13
|
+
```
|
14
|
+
Then run `bundle install`
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install mongoid_schema_dump
|
19
|
+
|
20
|
+
## Usage in Rails
|
21
|
+
|
22
|
+
You can install a rake task in your app allowing you to dump your schema. This rake task will have to be customized in order to take into account what is specific about your data model, in particular the reference class, which is Organization in the default generated rake task :
|
23
|
+
|
24
|
+
$ bundle exec rails generate mongoid_schema_dump:task
|
25
|
+
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/applidget/mongoid_schema_dump. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mongoid_schema_dump"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require "mongoid_schema_dump/version"
|
2
|
+
|
3
|
+
module MongoidSchemaDump
|
4
|
+
require 'mongoid_schema_dump/engine' if defined?(Rails)
|
5
|
+
module_function
|
6
|
+
|
7
|
+
#Computes all paths to reference class
|
8
|
+
def paths_to_klass(klass, reference_klass)
|
9
|
+
displayed_for_klass = false
|
10
|
+
paths = []
|
11
|
+
to_visit = [[klass, []]]
|
12
|
+
current_klass = klass
|
13
|
+
|
14
|
+
loop do
|
15
|
+
current_klass, path = to_visit.pop #FIFO
|
16
|
+
break unless current_klass
|
17
|
+
path << current_klass
|
18
|
+
|
19
|
+
if current_klass.to_s == reference_klass
|
20
|
+
paths << path
|
21
|
+
next
|
22
|
+
end
|
23
|
+
break if current_klass.nil?
|
24
|
+
next_klasses = all_belongs_to(current_klass)
|
25
|
+
next_klasses.each do |next_klass|
|
26
|
+
next if next_klass == current_klass.to_s
|
27
|
+
to_visit << [next_klass.constantize, path.clone]
|
28
|
+
end
|
29
|
+
break if to_visit.empty?
|
30
|
+
end
|
31
|
+
return paths
|
32
|
+
end
|
33
|
+
|
34
|
+
#returns all has_many relation class names
|
35
|
+
def has_many(from_klass, to_klass)
|
36
|
+
all_has_many = from_klass.relations.map(&:second).select do |r|
|
37
|
+
r.macro == :has_many && r.class_name == to_klass.to_s
|
38
|
+
end.map(&:class_name)
|
39
|
+
end
|
40
|
+
|
41
|
+
#returns all belongs_to class names
|
42
|
+
def all_belongs_to(from_klass)
|
43
|
+
monomorphic_relations = from_klass.relations.map(&:second).select do |r|
|
44
|
+
r.macro == :belongs_to && !r[:polymorphic]
|
45
|
+
end.map do |relation|
|
46
|
+
relation.class_name
|
47
|
+
end
|
48
|
+
|
49
|
+
polymorphic_relations = from_klass.relations.map(&:second).reduce([]) do |memo, r|
|
50
|
+
if r.macro == :belongs_to && r[:polymorphic] == true
|
51
|
+
models.each do |model_klass_name|
|
52
|
+
next unless has_many(model_klass_name.constantize, from_klass).any?
|
53
|
+
next if monomorphic_relations.include? model_klass_name
|
54
|
+
memo << model_klass_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
memo
|
58
|
+
end
|
59
|
+
monomorphic_relations + polymorphic_relations
|
60
|
+
end
|
61
|
+
|
62
|
+
def models
|
63
|
+
Mongoid.models.map &:to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
def shortest_array(arrays)
|
67
|
+
result = arrays.reduce({path: [], size: 1000000}) do |memo, current|
|
68
|
+
new_size = [current.count, memo[:size]].min
|
69
|
+
memo = {path: current, size: new_size} if new_size < memo[:size]
|
70
|
+
memo
|
71
|
+
end
|
72
|
+
result[:path]
|
73
|
+
end
|
74
|
+
|
75
|
+
def export_options(klass_name, ref)
|
76
|
+
path = shortest_array(paths_to_klass(klass_name.constantize, ref))
|
77
|
+
return {} if path.count <= 1
|
78
|
+
dependency_klass = path.second
|
79
|
+
|
80
|
+
#1. Standard belongs_to
|
81
|
+
relation = klass_name.constantize.relations.map(&:second).select do |r|
|
82
|
+
r.macro == :belongs_to && !r[:polymorphic] && r.class_name == dependency_klass.to_s
|
83
|
+
end.first
|
84
|
+
if relation.present?
|
85
|
+
return {
|
86
|
+
"collection" => klass_name.constantize.collection.name,
|
87
|
+
"dependency" => dependency_klass.collection.name,
|
88
|
+
"foreign_key" => relation.key
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
#2. Handle polymorphic relations
|
93
|
+
hm_names = dependency_klass.relations.map(&:second).select do |r|
|
94
|
+
r.macro == :has_many && r.class_name == klass_name
|
95
|
+
end.map(&:as)
|
96
|
+
|
97
|
+
relation = klass_name.constantize.relations.map(&:second).select do |r|
|
98
|
+
r.macro == :belongs_to && r[:polymorphic] == true && hm_names.include?(r.name)
|
99
|
+
end.first
|
100
|
+
return nil if relation.blank?
|
101
|
+
|
102
|
+
return {
|
103
|
+
"collection" => klass_name.constantize.collection.name,
|
104
|
+
"dependency" => dependency_klass.collection.name,
|
105
|
+
"foreign_key" => relation.key,
|
106
|
+
"filters" => {
|
107
|
+
"#{relation.name}_type" => dependency_klass.to_s
|
108
|
+
}
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def dump(reference_klass_name)
|
113
|
+
#path_to_reference_klass(model_name.constantize, "Guest")
|
114
|
+
schema = []
|
115
|
+
models.each do |model_name|
|
116
|
+
res = export_options(model_name, reference_klass_name)
|
117
|
+
schema << res if res.present?
|
118
|
+
end
|
119
|
+
return schema
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module MongoidSchemaDump
|
2
|
+
module Generators
|
3
|
+
class TaskGenerator < Rails::Generators::Base
|
4
|
+
desc "Creates a Rake task that you can customize for your own schema"
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@_source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_rake_task
|
11
|
+
template 'mongoid_schema_dump.rake', File.join('lib', "tasks", "mongoid_schema_dump.rake")
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "mongoid_schema_dump"
|
2
|
+
namespace :mongoid do
|
3
|
+
task :schema_dump => :environment do
|
4
|
+
#This is used to make sure all your models are loaded
|
5
|
+
Rails.application.eager_load!
|
6
|
+
|
7
|
+
#Customize your base class here )
|
8
|
+
MongoidSchemaDump.dump("Organization")
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_schema_dump/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_schema_dump"
|
8
|
+
spec.version = MongoidSchemaDump::VERSION
|
9
|
+
spec.authors = ["rpechayr"]
|
10
|
+
spec.email = ["romain.pechayre@applidget.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extract your Mongoid schema based on a top level class}
|
13
|
+
spec.description = %q{Extract your Mongoid schema based on a top level class}
|
14
|
+
spec.homepage = "https://github.com/applidget/mongoid_schema_dump"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "mongoid", "~> 5"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_schema_dump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rpechayr
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Extract your Mongoid schema based on a top level class
|
70
|
+
email:
|
71
|
+
- romain.pechayre@applidget.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/mongoid_schema_dump.rb
|
87
|
+
- lib/mongoid_schema_dump/engine.rb
|
88
|
+
- lib/mongoid_schema_dump/version.rb
|
89
|
+
- lib/rails/generators/mongoid_schema_dump/task_generator.rb
|
90
|
+
- lib/rails/generators/mongoid_schema_dump/templates/mongoid_schema_dump.rake
|
91
|
+
- mongoid_schema_dump.gemspec
|
92
|
+
homepage: https://github.com/applidget/mongoid_schema_dump
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.5
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Extract your Mongoid schema based on a top level class
|
116
|
+
test_files: []
|