migreatest-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +21 -0
- data/README.md +72 -0
- data/Rakefile +34 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/config/initializers/extensions/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +38 -0
- data/config/initializers/extensions/rails/activerecord/lib/active_record/migration.rb +78 -0
- data/config/initializers/extensions/rails/activerecord/lib/active_record/railties/databases.rake +31 -0
- data/config/initializers/extensions/rails/activerecord/lib/active_record/tasks/database_tasks.rb +90 -0
- data/lib/migreatest-rails.rb +1 -0
- data/lib/migreatest/rails/connector.rb +129 -0
- data/lib/migreatest/rails/engine.rb +63 -0
- data/lib/migreatest/rails/version.rb +5 -0
- data/lib/tasks/active_record/railties/truncate_table.rake +34 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87a2175c45266ccf514549776c34d1854037fa7d
|
4
|
+
data.tar.gz: 3366da1de13a2375c2b57122384063245128bec2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ce3110cb762a0d38bc44a1f9133450ed1f30b8aaf053f5851d1c0d5561a9114e62e23ec1ecd817096a6e9c3ae8e4a32483debf35410235162827a864547d761
|
7
|
+
data.tar.gz: 48b0c365c99067def355daeaefcf4291921cae1327dc5e49c1d053fab38858d215447c42b336c13e656139d198212f52216ebf19fa22dc7dfcae585437198af3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Rafael Pereira Laurindo
|
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,72 @@
|
|
1
|
+
# Migreatest
|
2
|
+
|
3
|
+
This gem provides support to ActiveRecord gem of Ruby on Rails working with namespaces.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'migreatest-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install migreatest-rails
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
In your ```config/database.yml``` add a namespace to configurations. Like that.
|
24
|
+
|
25
|
+
```yaml
|
26
|
+
<namespace>:
|
27
|
+
<environment>:
|
28
|
+
database: ...
|
29
|
+
adapter: ...
|
30
|
+
username: ...
|
31
|
+
host: ...
|
32
|
+
```
|
33
|
+
|
34
|
+
Includes ```Migreatest::Rails::Connector``` in your model that are encapsulated in a module to automatically connect to database.
|
35
|
+
|
36
|
+
**Obs.**: The modules must have same name that the namespace defined in ```database.yml```
|
37
|
+
|
38
|
+
### Generating database
|
39
|
+
|
40
|
+
Run
|
41
|
+
```shell
|
42
|
+
$ rake db:create db:migrate MIGRATION_NAMESPACE=<namespace>
|
43
|
+
```
|
44
|
+
|
45
|
+
or
|
46
|
+
|
47
|
+
```shell
|
48
|
+
$ rake db:create db:migrate MIGRATION_NAMESPACES=<namespace1,namespace2>
|
49
|
+
```
|
50
|
+
|
51
|
+
### When installation engine migrations
|
52
|
+
|
53
|
+
After Run
|
54
|
+
|
55
|
+
```shell
|
56
|
+
$ rake <your engine>:install:migrations
|
57
|
+
```
|
58
|
+
The migrations will go to a folder with same name of migration. That folder can be renamed, but the namespace defined in ```database.yml``` must have the same name of the folder.
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
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).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rplaurindo/migreatest-rails.
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'DbUtilsRails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "migreatest"
|
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,38 @@
|
|
1
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
|
6
|
+
class PostgreSQLAdapter < AbstractAdapter
|
7
|
+
|
8
|
+
def truncate table_name, params = {}, name = nil
|
9
|
+
query = "TRUNCATE TABLE #{quote_table_name(table_name)}"
|
10
|
+
query = "#{query} RESTART IDENTITY" if params[:restart_identity]
|
11
|
+
query = "#{query} CASCADE" if params[:cascade]
|
12
|
+
|
13
|
+
exec_query query, name, []
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
class Base
|
21
|
+
|
22
|
+
class << self
|
23
|
+
|
24
|
+
def has_connection?
|
25
|
+
begin
|
26
|
+
ActiveRecord::Base.connection
|
27
|
+
rescue ActiveRecord::NoDatabaseError
|
28
|
+
false
|
29
|
+
else
|
30
|
+
true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'active_record/migration'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class Migrator
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def migrations(paths)
|
9
|
+
paths = Array(paths)
|
10
|
+
|
11
|
+
files = Dir[*paths.map { |p| "#{p}/[0-9]*_*.rb" }]
|
12
|
+
|
13
|
+
migrations = files.map do |file|
|
14
|
+
version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first
|
15
|
+
|
16
|
+
raise IllegalMigrationNameError.new(file) unless version
|
17
|
+
version = version.to_i
|
18
|
+
name = name.camelize
|
19
|
+
|
20
|
+
MigrationProxy.new(name, version, file, scope)
|
21
|
+
end
|
22
|
+
|
23
|
+
migrations.sort_by(&:version)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class Migration
|
31
|
+
def copy(destination, sources, options = {})
|
32
|
+
copied = []
|
33
|
+
|
34
|
+
FileUtils.mkdir_p(destination) unless File.exist?(destination)
|
35
|
+
|
36
|
+
destination_migrations = ActiveRecord::Migrator.migrations(destination)
|
37
|
+
last = destination_migrations.last
|
38
|
+
sources.each do |scope, path|
|
39
|
+
source_migrations = ActiveRecord::Migrator.migrations(path)
|
40
|
+
|
41
|
+
source_migrations.each do |migration|
|
42
|
+
source = File.binread(migration.filename)
|
43
|
+
inserted_comment = "# This migration comes from #{scope} (originally #{migration.version})\n"
|
44
|
+
if /\A#.*\b(?:en)?coding:\s*\S+/ =~ source
|
45
|
+
# If we have a magic comment in the original migration,
|
46
|
+
# insert our comment after the first newline(end of the magic comment line)
|
47
|
+
# so the magic keep working.
|
48
|
+
# Note that magic comments must be at the first line(except sh-bang).
|
49
|
+
source[/\n/] = "\n#{inserted_comment}"
|
50
|
+
else
|
51
|
+
source = "#{inserted_comment}#{source}"
|
52
|
+
end
|
53
|
+
|
54
|
+
if duplicate = destination_migrations.detect { |m| m.name == migration.name }
|
55
|
+
if options[:on_skip] && duplicate.scope != scope.to_s
|
56
|
+
options[:on_skip].call(scope, migration)
|
57
|
+
end
|
58
|
+
next
|
59
|
+
end
|
60
|
+
|
61
|
+
migration.version = next_migration_number(last ? last.version + 1 : 0).to_i
|
62
|
+
new_path = File.join(destination, "#{migration.version}_#{migration.name.underscore}.rb")
|
63
|
+
old_path, migration.filename = migration.filename, new_path
|
64
|
+
last = migration
|
65
|
+
|
66
|
+
File.binwrite(migration.filename, source)
|
67
|
+
copied << migration
|
68
|
+
options[:on_copy].call(scope, migration, old_path) if options[:on_copy]
|
69
|
+
destination_migrations << migration
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
copied
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
data/config/initializers/extensions/rails/activerecord/lib/active_record/railties/databases.rake
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Rake::Task["railties:install:migrations"].clear
|
2
|
+
|
3
|
+
namespace :railties do
|
4
|
+
namespace :install do
|
5
|
+
# desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
|
6
|
+
task :migrations => :'db:load_config' do
|
7
|
+
to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip }
|
8
|
+
railties = {}
|
9
|
+
namespace = ''
|
10
|
+
Rails.application.migration_railties.each do |railtie|
|
11
|
+
next unless to_load == :all || to_load.include?(railtie.railtie_name)
|
12
|
+
namespace = railtie.railtie_name
|
13
|
+
|
14
|
+
if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
|
15
|
+
railties[railtie.railtie_name] = path
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
on_skip = Proc.new do |name, migration|
|
20
|
+
puts "NOTE: Migration #{migration.basename} from #{name} has been skipped. Migration with the same name already exists."
|
21
|
+
end
|
22
|
+
|
23
|
+
on_copy = Proc.new do |name, migration|
|
24
|
+
puts "Copied migration #{migration.basename} from #{name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Migration.copy("#{ActiveRecord::Migrator.migrations_paths.first}/#{namespace}", railties,
|
28
|
+
:on_skip => on_skip, :on_copy => on_copy)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/config/initializers/extensions/rails/activerecord/lib/active_record/tasks/database_tasks.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'active_record/tasks/database_tasks'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Tasks
|
5
|
+
module DatabaseTasks
|
6
|
+
|
7
|
+
def migrate
|
8
|
+
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
9
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
10
|
+
scope = ENV['SCOPE']
|
11
|
+
|
12
|
+
migrations_paths.each do |migration_path|
|
13
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose
|
14
|
+
namespace = File.basename migration_path
|
15
|
+
db_configs = Base.configurations
|
16
|
+
Base.establish_connection db_configs[namespace][Rails.env] unless namespace === "migrate"
|
17
|
+
begin
|
18
|
+
Migrator.migrate(migration_path, version) do |migration|
|
19
|
+
scope.blank? || scope == migration.scope
|
20
|
+
end
|
21
|
+
ensure
|
22
|
+
Migration.verbose = verbose_was
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def migrations_paths
|
28
|
+
paths = Rails.application.paths['db/migrate'].to_a
|
29
|
+
root_path = paths.first
|
30
|
+
|
31
|
+
namespaces = ENV['MIGRATION_NAMESPACE'] ||
|
32
|
+
ENV['MIGRATION_NAMESPACES'] ?
|
33
|
+
(ENV['MIGRATION_NAMESPACE'] ||
|
34
|
+
ENV['MIGRATION_NAMESPACES']).split(",").flatten :
|
35
|
+
[]
|
36
|
+
|
37
|
+
namespaces.each do |namespace|
|
38
|
+
paths << "#{root_path}/#{namespace.strip}"
|
39
|
+
end
|
40
|
+
|
41
|
+
@migrations_paths ||= paths
|
42
|
+
end
|
43
|
+
|
44
|
+
def create(*arguments)
|
45
|
+
configuration = arguments.first
|
46
|
+
begin
|
47
|
+
class_for_adapter(configuration['adapter']).new(*arguments).create
|
48
|
+
rescue DatabaseAlreadyExists
|
49
|
+
$stderr.puts "#{configuration['database']} already exists"
|
50
|
+
rescue Exception => error
|
51
|
+
$stderr.puts error, *(error.backtrace)
|
52
|
+
$stderr.puts "Couldn't create database for #{configuration.inspect}"
|
53
|
+
else
|
54
|
+
$stderr.puts "Database #{configuration['database']} has been created"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def each_current_configuration(environment)
|
61
|
+
environments = [environment]
|
62
|
+
# add test environment only if no RAILS_ENV was specified.
|
63
|
+
environments << 'test' if environment == 'development' &&
|
64
|
+
ENV['RAILS_ENV'].nil?
|
65
|
+
|
66
|
+
configurations = []
|
67
|
+
|
68
|
+
db_configs = Base.configurations
|
69
|
+
namespaces = ENV['MIGRATION_NAMESPACE'] ||
|
70
|
+
ENV['MIGRATION_NAMESPACES'] ?
|
71
|
+
(ENV['MIGRATION_NAMESPACE'] ||
|
72
|
+
ENV['MIGRATION_NAMESPACES']).split(",").flatten :
|
73
|
+
[]
|
74
|
+
|
75
|
+
namespaces_configs = db_configs.values_at(*namespaces)
|
76
|
+
if namespaces_configs.any?
|
77
|
+
namespaces_configs.each do |namespace|
|
78
|
+
configurations << namespace.values_at(*environments)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
configurations << db_configs.values_at(*environments)
|
83
|
+
configurations.flatten.compact.each do |configuration|
|
84
|
+
yield configuration unless configuration['database'].blank?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "migreatest/rails/engine"
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module Migreatest
|
2
|
+
module Rails
|
3
|
+
module Connector
|
4
|
+
|
5
|
+
extend self
|
6
|
+
|
7
|
+
@full_constant = nil
|
8
|
+
@full_parent = nil
|
9
|
+
|
10
|
+
# to recognize who include (module and class) to connect automatically
|
11
|
+
def connect namespace = nil
|
12
|
+
|
13
|
+
# @full_constant.table_name = resolve_class_name
|
14
|
+
# o reload deve ser feito aqui?
|
15
|
+
# binding.pry
|
16
|
+
# namespace = namespace.to_s
|
17
|
+
# db_config = YAML::load_file('config/database.yml')
|
18
|
+
# # tentar fazer include aos modelos do projeto dinamicamente por aqui, para tal deve-se pegar todos os modelos, incluir um observador neles, e toda vez que forem chamados esse módulo deve ser incluído, ou o método deve ser chamado.
|
19
|
+
# # Um outra solução é incluir este mṕdulo a todos os modelos, sem a intervenção do usuário, neste caso um observador não seria necessário.
|
20
|
+
# # ou, talvez tenha que usar os dois, incluir em todas, e quando chamadas executar o método connect
|
21
|
+
# # binding.pry
|
22
|
+
# if namespace
|
23
|
+
# # try
|
24
|
+
# begin
|
25
|
+
# ActiveRecord::Base
|
26
|
+
# .establish_connection db_config[namespace][::Rails.env]
|
27
|
+
# # catch
|
28
|
+
# rescue => details
|
29
|
+
# $stderr.puts "Could not possible connect to database with namespace \"#{namespace}.\""
|
30
|
+
# $stderr.puts "Details: #{details}."
|
31
|
+
# # throw
|
32
|
+
# # raise
|
33
|
+
# end
|
34
|
+
# else
|
35
|
+
# ActiveRecord::Base.establish_connection db_config[::Rails.env]
|
36
|
+
# end
|
37
|
+
|
38
|
+
# reload_model!
|
39
|
+
end
|
40
|
+
|
41
|
+
class << self
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# called only once by module or class that include it
|
46
|
+
def included constant
|
47
|
+
@full_constant = constant.name.demodulize
|
48
|
+
@full_parent = has_parent? constant
|
49
|
+
if !@full_parent || @full_parent != ActiveRecord
|
50
|
+
connect
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def has_parent? full_constant
|
59
|
+
parent_name = full_constant.name.deconstantize
|
60
|
+
parent_name.constantize unless parent_name.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
def reload_model!
|
64
|
+
object = @full_parent ? Module.const_get(@full_parent.name.to_sym) : Class
|
65
|
+
object.send :remove_const, @full_constant.name.demodulize.to_sym
|
66
|
+
require_model_file
|
67
|
+
end
|
68
|
+
|
69
|
+
def model_paths
|
70
|
+
::Rails.application.config.paths["app/models"]
|
71
|
+
end
|
72
|
+
|
73
|
+
def require_model_file
|
74
|
+
require_model_files.each do |file|
|
75
|
+
require file
|
76
|
+
end
|
77
|
+
|
78
|
+
# constant_path = @full_constant.name.underscore
|
79
|
+
# # module_name = @module.name.underscore
|
80
|
+
# # files = model_paths.select do |path|
|
81
|
+
# model_paths.each do |path|
|
82
|
+
# # para ficar direito vai ter de pegar a path das gems e colocar no $LOAD_PATH deste conexto
|
83
|
+
# file = File.join(path, "#{constant_path}.rb")
|
84
|
+
# binding.pry
|
85
|
+
# # file if File.exists? file
|
86
|
+
# require file
|
87
|
+
# end
|
88
|
+
end
|
89
|
+
|
90
|
+
def is_gem? name
|
91
|
+
begin
|
92
|
+
gem_specification = GEM::Specification.find_by_name(name)
|
93
|
+
rescue
|
94
|
+
false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def require_model_files
|
99
|
+
constant_name = @full_constant.name.underscore
|
100
|
+
gem_name = @full_parent.name.parametrize
|
101
|
+
files = []
|
102
|
+
|
103
|
+
if gem_specification = is_gem?(gem_name)
|
104
|
+
files = model_paths.select do |path|
|
105
|
+
# ENV["GEM_HOME"]
|
106
|
+
binding.pry
|
107
|
+
# colocar com as especificações aqui
|
108
|
+
# file = File.join(::Rails.root.parent, gem_name, path, "#{constant_name}.rb")
|
109
|
+
# if File.exists? file
|
110
|
+
# file
|
111
|
+
# else
|
112
|
+
# file = File.join(::Rails.root.parent, gem_name, path, "#{constant_name}.rb")
|
113
|
+
# file if File.exists? file
|
114
|
+
# end
|
115
|
+
end
|
116
|
+
else
|
117
|
+
::Rails.application.config.paths["app/models"].select do |path|
|
118
|
+
# binding.pry
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def resolve_class_name
|
124
|
+
@full_constant.name.demodulize.underscore.pluralize
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "pry"
|
2
|
+
|
3
|
+
module Migreatest
|
4
|
+
module Rails
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# works only called in another module
|
9
|
+
protected
|
10
|
+
def root_path
|
11
|
+
File.expand_path "../../../../", __FILE__
|
12
|
+
end
|
13
|
+
|
14
|
+
def lib_path
|
15
|
+
File.join(root_path, "lib")
|
16
|
+
end
|
17
|
+
|
18
|
+
def extensions_path
|
19
|
+
File.join(root_path, "config/initializers/extensions")
|
20
|
+
end
|
21
|
+
|
22
|
+
class Engine < ::Rails::Engine
|
23
|
+
|
24
|
+
extend Rails
|
25
|
+
include Rails
|
26
|
+
|
27
|
+
isolate_namespace Rails
|
28
|
+
|
29
|
+
# with to_prepare will raise a error
|
30
|
+
config.before_initialize do
|
31
|
+
Dir[File.join(lib_path, "**/*.rb")].each do |file|
|
32
|
+
require file
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
config.to_prepare do
|
38
|
+
ActiveSupport.on_load :active_model do
|
39
|
+
include Migreatest::Rails::Connector
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
rake_tasks do
|
44
|
+
Dir[File.join(extensions_path, "**/*.rake")].each do |file|
|
45
|
+
load file
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class Railtie < ::Rails::Railtie
|
54
|
+
extend Rails
|
55
|
+
|
56
|
+
Dir[File.join(extensions_path, "**/*.rb")].each do |file|
|
57
|
+
require file
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
# p File.expand_path "../../../../", __FILE__
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
namespace :truncate do
|
5
|
+
|
6
|
+
desc "Truncate Table"
|
7
|
+
|
8
|
+
task :table, [:table_name, :restart_keys, :cascade] => [:environment] do |t, params|
|
9
|
+
|
10
|
+
active_record = ActiveRecord::Base
|
11
|
+
|
12
|
+
db_config = YAML::load_file('config/database.yml')
|
13
|
+
namespace = ENV['NAMESPACE']
|
14
|
+
|
15
|
+
env = Rails.env
|
16
|
+
|
17
|
+
connection_config = (namespace ? db_config[namespace][env] : db_config[env]).clone
|
18
|
+
database = db_config[namespace][env]["database"]
|
19
|
+
active_record.establish_connection connection_config
|
20
|
+
connection = active_record.connection
|
21
|
+
|
22
|
+
table_name = params[:table_name].strip
|
23
|
+
begin
|
24
|
+
connection.truncate table_name, restart_keys: params[:restart_keys], cascade: params[:cascade]
|
25
|
+
rescue => e
|
26
|
+
$stderr.puts e
|
27
|
+
else
|
28
|
+
$stderr.puts "Table \"#{table_name}\" has been truncated."
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: migreatest-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Laurindo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
description: This gem provides support for ActiveRecord gem of Ruby on Rails to work
|
28
|
+
with namespaces
|
29
|
+
email:
|
30
|
+
- rafaelplaurindo@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- bin/console
|
39
|
+
- bin/setup
|
40
|
+
- config/initializers/extensions/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
|
41
|
+
- config/initializers/extensions/rails/activerecord/lib/active_record/migration.rb
|
42
|
+
- config/initializers/extensions/rails/activerecord/lib/active_record/railties/databases.rake
|
43
|
+
- config/initializers/extensions/rails/activerecord/lib/active_record/tasks/database_tasks.rb
|
44
|
+
- lib/migreatest-rails.rb
|
45
|
+
- lib/migreatest/rails/connector.rb
|
46
|
+
- lib/migreatest/rails/engine.rb
|
47
|
+
- lib/migreatest/rails/version.rb
|
48
|
+
- lib/tasks/active_record/railties/truncate_table.rake
|
49
|
+
homepage: https://rubygems.org/gems/migreatest-rails
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- bin
|
57
|
+
- config
|
58
|
+
- lib
|
59
|
+
- vendor
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.5.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Summary of Migreatest
|
76
|
+
test_files: []
|