db_supplier 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 +15 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +2 -0
- data/db_hiroba.gemspec +26 -0
- data/lib/db_supplier/migrator.rb +99 -0
- data/lib/db_supplier/rails/railtie.rb +9 -0
- data/lib/db_supplier/rails/tasks.rake +23 -0
- data/lib/db_supplier.rb +6 -0
- data/sample.rb +22 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04d68783b5719f14b5fd32af6f25479dcb8456f6
|
4
|
+
data.tar.gz: 2db6429073fc3871c42a52c9fd157bb725f61d9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4cb203dec17257a2014aecff29b96b805109ac7f0240c8d1f8bb470166df3abca02f5102e1205f72da92ad2041e6505cd6675f33eb68498e2f520196eff28d2b
|
7
|
+
data.tar.gz: 7fcb99cb8735a0f8edcfa8a0f37ca2cf555aa71769ddd4ab50ec8c6baeb6eec7d353e08ec313db70f959fdf33a85f4d96871115ae712a15b5084e62d2f561be9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryoichi SEKIGUCHI
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# DBSupplier
|
2
|
+
Migration tool from external database's DDL.
|
3
|
+
Fetch sql from GitHub repository and migrate local database.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'db_supplier'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Add gem to your Gemfile then defined some 'rake tasks'.
|
20
|
+
|
21
|
+
$ rake -T
|
22
|
+
rake db:supplier:defined # show migration target databases
|
23
|
+
rake db:supplier:migrate # Migrate database from DDL files of unmanaged in the Rails App
|
24
|
+
rake db:supplier:migrate:sql # Show DDL files of unmanaged in the Rails App
|
25
|
+
|
26
|
+
### Configurations
|
27
|
+
Add your Rails Application's config/environments/*.rb
|
28
|
+
|
29
|
+
example: RAILS_ENV=development
|
30
|
+
|
31
|
+
config/environments/development.rb
|
32
|
+
```ruby
|
33
|
+
Rails.application.configure do
|
34
|
+
DBSupplier::Migrator.configurations = {
|
35
|
+
schema_repository: 'username/reponame',
|
36
|
+
access_token: 'your github access token',
|
37
|
+
schema_files: {
|
38
|
+
databasename: ['path/to/ddl.sql']
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
#### params
|
45
|
+
schema_repository: DDL Repository
|
46
|
+
|
47
|
+
access_token: Your github access token
|
48
|
+
|
49
|
+
schema_files: Some pair of database name and ddl file(s)
|
50
|
+
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it ( https://github.com/ryopeko/db_supplier/fork )
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/db_hiroba.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "db_supplier"
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ["ryopeko"]
|
9
|
+
spec.email = ["ryopeko@gmail.com"]
|
10
|
+
spec.summary = %q{Migration tool from external database's DDL}
|
11
|
+
spec.homepage = "https://github.com/ryopeko/db_supplier"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "rails", ">= 4.0.0"
|
20
|
+
spec.add_dependency "octokit", "3.3.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "sqlite3"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'octokit'
|
3
|
+
require 'active_support/core_ext/hash'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
module DBSupplier
|
7
|
+
class Migrator
|
8
|
+
class << self
|
9
|
+
def configurations
|
10
|
+
@configurations
|
11
|
+
end
|
12
|
+
|
13
|
+
def configurations=(config={})
|
14
|
+
@configurations = config
|
15
|
+
|
16
|
+
@schema_repository = config[:schema_repository]
|
17
|
+
@schema_files = config[:schema_files].symbolize_keys
|
18
|
+
@access_token = config[:access_token]
|
19
|
+
|
20
|
+
@github_api_endpoint = config[:github_api_endpoint]
|
21
|
+
|
22
|
+
@logger = config[:logger] || Logger.new(STDOUT)
|
23
|
+
end
|
24
|
+
|
25
|
+
def migrate
|
26
|
+
@logger.info "----- migrate start -----"
|
27
|
+
|
28
|
+
databases.each do |database|
|
29
|
+
@logger.info "----- #{database} migrate start -----"
|
30
|
+
|
31
|
+
if (ActiveRecord.const_defined?(:Import))
|
32
|
+
connection = ActiveRecord::Base.establish_connection_without_activerecord_import(database).connection
|
33
|
+
else
|
34
|
+
connection = ActiveRecord::Base.establish_connection(database).connection
|
35
|
+
end
|
36
|
+
|
37
|
+
@logger.debug "----- connected -----"
|
38
|
+
|
39
|
+
sqls = fetch_sql(database)
|
40
|
+
sqls.each do |sql|
|
41
|
+
statements = sql.split(/;/)
|
42
|
+
|
43
|
+
statements.each do |query|
|
44
|
+
next if query == "\n\n"
|
45
|
+
|
46
|
+
@logger.debug "----- query execute -----"
|
47
|
+
connection.execute(query)
|
48
|
+
@logger.debug query
|
49
|
+
@logger.debug "----- query success -----"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
@logger.info "----- #{database} migrate finished -----"
|
54
|
+
end
|
55
|
+
|
56
|
+
@logger.info "----- migrate finished -----"
|
57
|
+
end
|
58
|
+
|
59
|
+
def fetch_sql(db_name)
|
60
|
+
migration_file_paths = Array(@schema_files.fetch(db_name.to_sym))
|
61
|
+
repository = @schema_repository || (raise RuntimeError, 'undefined schema repository')
|
62
|
+
|
63
|
+
migration_file_paths.map do |path|
|
64
|
+
client.contents(
|
65
|
+
repository,
|
66
|
+
path: path,
|
67
|
+
headers: {
|
68
|
+
accept: 'application/vnd.github.VERSION.raw'
|
69
|
+
}
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def show_sqls(db_name=nil)
|
75
|
+
return fetch_sql(db_name) if db_name
|
76
|
+
|
77
|
+
sqls = databases.map do |db_name|
|
78
|
+
fetch_sql(db_name)
|
79
|
+
end
|
80
|
+
|
81
|
+
return sqls.join("\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
def client
|
85
|
+
ac = @access_token || ENV['GITHUB_ACCESS_TOKEN'] || (raise RuntimeError, 'undefined access_token')
|
86
|
+
|
87
|
+
@client ||= begin
|
88
|
+
Octokit.api_endpoint = @github_api_endpoint if @github_api_endpoint
|
89
|
+
Octokit::Client.new(access_token: ac)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def databases
|
94
|
+
@schema_files.try(:keys) || []
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :supplier do
|
3
|
+
desc 'show migration target databases'
|
4
|
+
task defined: :environment do
|
5
|
+
raise "This task can be performed in non production." if Rails.env == 'production'
|
6
|
+
puts DBSupplier::Migrator.databases.join("\n")
|
7
|
+
end
|
8
|
+
|
9
|
+
desc 'Migrate database from DDL files of unmanaged in the Rails App'
|
10
|
+
task migrate: :environment do
|
11
|
+
raise "This task can be performed in non production." if Rails.env == 'production'
|
12
|
+
DBSupplier::Migrator.migrate
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :migrate do
|
16
|
+
desc 'Show DDL files of unmanaged in the Rails App'
|
17
|
+
task sql: :environment do
|
18
|
+
raise "This task can be performed in non production." if Rails.env == 'production'
|
19
|
+
puts DBSupplier::Migrator.show_sqls
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/db_supplier.rb
ADDED
data/sample.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'db_supplier'
|
2
|
+
require 'active_record'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
DBSupplier::Migrator.configurations = {
|
6
|
+
schema_repository: 'ryopeko/demo_schema',
|
7
|
+
access_token: ENV['TOKEN'],
|
8
|
+
schema_files: {
|
9
|
+
external_database: ['DEMO/APPS/DEFAULT/latest.sql']
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
ActiveRecord::Base.configurations = {
|
14
|
+
'external_database' => {
|
15
|
+
adapter: 'sqlite3',
|
16
|
+
database: 'external_db_name.sqlite3'
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
ActiveRecord::Base.establish_connection(:external_database)
|
21
|
+
|
22
|
+
binding.pry
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_supplier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ryopeko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-21 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.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: octokit
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.3.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.3.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- ryopeko@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- db_hiroba.gemspec
|
110
|
+
- lib/db_supplier.rb
|
111
|
+
- lib/db_supplier/migrator.rb
|
112
|
+
- lib/db_supplier/rails/railtie.rb
|
113
|
+
- lib/db_supplier/rails/tasks.rake
|
114
|
+
- sample.rb
|
115
|
+
homepage: https://github.com/ryopeko/db_supplier
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.2.2
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Migration tool from external database's DDL
|
139
|
+
test_files: []
|
140
|
+
has_rdoc:
|