tetragnatha 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/MIT-LICENSE +20 -0
- data/Rakefile +34 -0
- data/lib/tetragnatha.rb +15 -0
- data/lib/tetragnatha/config.rb +35 -0
- data/lib/tetragnatha/connection_adapters/abstract_mysql_adapter.rb +71 -0
- data/lib/tetragnatha/model.rb +25 -0
- data/lib/tetragnatha/railtie.rb +13 -0
- data/lib/tetragnatha/version.rb +3 -0
- data/spec/lib/tetragnatha/connection_adapters/abstract_mysql_adapter_spec.rb +52 -0
- data/spec/spec_helper.rb +14 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e872ba693d0467ca407aa6ee5cd6a121de6dbc2e
|
4
|
+
data.tar.gz: aaa64db1295e4a43b51df265c56898e5ff0f0a6e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5389187f724e54106f3281029b8457db3a4453b984bceed49f4949269198ab91194cc1488390c1e366b3f4b55e98103ad875755c38b1d46830b1f0e4490fa6b4
|
7
|
+
data.tar.gz: 697cbc1a4a5f5532cfecf4566e2fd3edb36ac54b3b497e48ffdfecf0f97917c1fcc230bcddca9817802848da5ec4d1325c90334bb642824127a4c87e17fddaa7
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Masashi AKISUE
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
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 = 'Tetragnatha'
|
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/lib/tetragnatha.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
if defined? Rails
|
5
|
+
require "tetragnatha/railtie"
|
6
|
+
else
|
7
|
+
#TODO DRY
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
require "tetragnatha/connection_adapters/abstract_mysql_adapter"
|
10
|
+
require "tetragnatha/config"
|
11
|
+
require "tetragnatha/model"
|
12
|
+
|
13
|
+
ActiveRecord::Base.send(:include, Tetragnatha::Model)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Tetragnatha
|
2
|
+
class Config
|
3
|
+
DEFAULT_HOST = "localhost"
|
4
|
+
DEFAULT_USER = "root"
|
5
|
+
DEFAULT_PORT = 3306
|
6
|
+
DEFAULT_PASSWORD = ""
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
# TODO delegate
|
13
|
+
def host
|
14
|
+
database_configurations[:host] || DEFAULT_HOST
|
15
|
+
end
|
16
|
+
|
17
|
+
def port
|
18
|
+
database_configurations[:port] || DEFAULT_PORT
|
19
|
+
end
|
20
|
+
|
21
|
+
def user
|
22
|
+
database_configurations[:username] || DEFAULT_USER
|
23
|
+
end
|
24
|
+
|
25
|
+
def password
|
26
|
+
database_configurations[:password] || DEFAULT_PASSWORD
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def database_configurations
|
32
|
+
ActiveRecord::Base.configurations.with_indifferent_access[@name] || {}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_mysql_adapter'
|
2
|
+
|
3
|
+
module Tetragnatha
|
4
|
+
class ModelClassNotFound < StandardError; end
|
5
|
+
|
6
|
+
module ConnectionAdapters
|
7
|
+
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
|
8
|
+
def initialize(types, name, temporary, options, as = nil)
|
9
|
+
builder = CommentBuilder.new(name, options)
|
10
|
+
super(types, name, temporary, builder.build, as)
|
11
|
+
end
|
12
|
+
|
13
|
+
class CommentBuilder
|
14
|
+
attr_reader :name, :options
|
15
|
+
|
16
|
+
def initialize(name, options)
|
17
|
+
@name = name
|
18
|
+
@options = options
|
19
|
+
end
|
20
|
+
|
21
|
+
def build
|
22
|
+
!skip_table? && blank_option? ? table_comment : @options
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def blank_option?
|
28
|
+
@options.blank? || @options == "ENGINE=InnoDB"
|
29
|
+
end
|
30
|
+
|
31
|
+
def table_comment
|
32
|
+
comment_sql = "ENGINE=SPIDER "
|
33
|
+
comment_sql << "COMMENT '"
|
34
|
+
comment_sql << "host \"#{spider_config.host}\", "
|
35
|
+
comment_sql << "user \"#{spider_config.user}\", "
|
36
|
+
comment_sql << "password \"#{spider_config.password}\", "
|
37
|
+
comment_sql << "port \"#{spider_config.port}\""
|
38
|
+
comment_sql << "'"
|
39
|
+
comment_sql
|
40
|
+
end
|
41
|
+
|
42
|
+
def spider_config
|
43
|
+
return @spider_config if @spider_config
|
44
|
+
@spider_config = get_model.spider_config
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_model
|
48
|
+
name.classify.constantize
|
49
|
+
rescue
|
50
|
+
fail(Tetragnatha::ModelClassNotFound, name)
|
51
|
+
end
|
52
|
+
|
53
|
+
def skip_table?
|
54
|
+
name == "schema_migrations"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_table_definition(name, temporary = false, options = nil, as = nil) # :nodoc:
|
60
|
+
TableDefinition.new(native_database_types, name, temporary, options, as)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
module ActiveRecord
|
66
|
+
module ConnectionAdapters
|
67
|
+
class AbstractMysqlAdapter < AbstractAdapter
|
68
|
+
prepend Tetragnatha::ConnectionAdapters
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Tetragnatha
|
2
|
+
module Model
|
3
|
+
def self.included(model)
|
4
|
+
model.singleton_class.class_eval do
|
5
|
+
include ClassMethods
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def spider_at(name)
|
11
|
+
@spider_at = name
|
12
|
+
end
|
13
|
+
|
14
|
+
def spider_config
|
15
|
+
if @spider_at
|
16
|
+
Config.new(@spider_at)
|
17
|
+
elsif self == ActiveRecord::Base
|
18
|
+
Config.new("default")
|
19
|
+
else
|
20
|
+
superclass.spider_config
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Tetragnatha
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "tetragnatha" do
|
4
|
+
ActiveSupport.on_load(:active_record) do
|
5
|
+
require "tetragnatha/connection_adapters/abstract_mysql_adapter"
|
6
|
+
require "tetragnatha/config"
|
7
|
+
require "tetragnatha/model"
|
8
|
+
|
9
|
+
ActiveRecord::Base.send(:include, Tetragnatha::Model)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Hoge < ActiveRecord::Base
|
4
|
+
spider_at :default
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Tetragnatha::ConnectionAdapters::TableDefinition do
|
8
|
+
describe "#initialize" do
|
9
|
+
let(:table_definition) do
|
10
|
+
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::TableDefinition.new("hoges", nil, nil, nil)
|
11
|
+
end
|
12
|
+
let(:schema_creation) do
|
13
|
+
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::SchemaCreation.new(nil)
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
allow(schema_creation).to receive(:quote_table_name).and_return("hoges")
|
18
|
+
allow_any_instance_of(Tetragnatha::ConnectionAdapters::TableDefinition::CommentBuilder).to receive(:get_model).and_return(Hoge)
|
19
|
+
|
20
|
+
ActiveRecord::Base.configurations = {
|
21
|
+
"default" => {
|
22
|
+
"adapter" => "mysql2",
|
23
|
+
"encoding" => "utf8",
|
24
|
+
"reconnect"=> false,
|
25
|
+
"host" => "localhost",
|
26
|
+
"username" => "usr",
|
27
|
+
"password" => "passwd",
|
28
|
+
"port" => 3306,
|
29
|
+
"database" => "default_database"
|
30
|
+
},
|
31
|
+
"spider_endpoint1" => {
|
32
|
+
"adapter" => "mysql2",
|
33
|
+
"encoding" => "utf8",
|
34
|
+
"reconnect"=> false,
|
35
|
+
"host" => "spider.db1.example",
|
36
|
+
"username" => "sp1",
|
37
|
+
"password" => "spsp",
|
38
|
+
"port" => 3306,
|
39
|
+
"database" => "spider_database"
|
40
|
+
}
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
subject do
|
45
|
+
schema_creation.__send__(:visit_TableDefinition, table_definition)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns create table sql with comment for default endpoint" do
|
49
|
+
expect(subject).to eq("CREATE TABLE hoges ENGINE=SPIDER COMMENT 'host \"localhost\", user \"usr\", password \"passwd\", port \"3306\"'")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_support"
|
3
|
+
require "tetragnatha"
|
4
|
+
|
5
|
+
ActiveSupport.on_load(:active_record) do
|
6
|
+
require "tetragnatha/connection_adapters/abstract_mysql_adapter"
|
7
|
+
require "tetragnatha/config"
|
8
|
+
require "tetragnatha/model"
|
9
|
+
|
10
|
+
ActiveRecord::Base.send(:include, Tetragnatha::Model)
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tetragnatha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masashi AKISUE
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-04 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.2.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.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mysql2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Table migration plugin for MySQL Spider storage engine
|
56
|
+
email:
|
57
|
+
- masashi.akisue@aktsk.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- Rakefile
|
64
|
+
- lib/tetragnatha.rb
|
65
|
+
- lib/tetragnatha/config.rb
|
66
|
+
- lib/tetragnatha/connection_adapters/abstract_mysql_adapter.rb
|
67
|
+
- lib/tetragnatha/model.rb
|
68
|
+
- lib/tetragnatha/railtie.rb
|
69
|
+
- lib/tetragnatha/version.rb
|
70
|
+
- spec/lib/tetragnatha/connection_adapters/abstract_mysql_adapter_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/aktsk/tetragnatha
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.5
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Table migration plugin for MySQL Spider storage engine
|
96
|
+
test_files:
|
97
|
+
- spec/lib/tetragnatha/connection_adapters/abstract_mysql_adapter_spec.rb
|
98
|
+
- spec/spec_helper.rb
|