Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6a71819
Showing
18 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/Gemfile.lock | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--format documentation | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
AllCops: | ||
TargetRubyVersion: 2.1 | ||
Exclude: | ||
- 'lib/sconb/ext/**/*' | ||
|
||
Lint/Eval: | ||
Enabled: false | ||
|
||
Lint/HandleExceptions: | ||
Enabled: false | ||
|
||
Lint/UselessAssignment: | ||
Enabled: false | ||
|
||
Metrics/AbcSize: | ||
Max: 50 | ||
|
||
Metrics/ClassLength: | ||
Max: 120 | ||
|
||
Metrics/CyclomaticComplexity: | ||
Max: 15 | ||
|
||
Metrics/LineLength: | ||
Max: 120 | ||
|
||
Metrics/MethodLength: | ||
Max: 30 | ||
|
||
Metrics/PerceivedComplexity: | ||
Max: 15 | ||
|
||
Performance/StringReplacement: | ||
Enabled: false | ||
|
||
Performance/Casecmp: | ||
Enabled: false | ||
|
||
Style/Alias: | ||
Enabled: false | ||
|
||
Style/BarePercentLiterals: | ||
Enabled: false | ||
|
||
Style/ClassAndModuleChildren: | ||
Enabled: false | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Style/MutableConstant: | ||
Enabled: false | ||
|
||
Style/MultilineOperationIndentation: | ||
Enabled: false | ||
|
||
Style/StabbyLambdaParentheses: | ||
Enabled: false | ||
|
||
Style/PercentLiteralDelimiters: | ||
Enabled: false | ||
|
||
Style/PredicateName: | ||
Enabled: false | ||
|
||
Style/RedundantSelf: | ||
Enabled: false | ||
|
||
Style/SymbolProc: | ||
Enabled: false | ||
|
||
Style/BracesAroundHashParameters: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
sudo: false | ||
language: ruby | ||
rvm: | ||
- 2.3.1 | ||
before_install: gem install bundler -v 1.12.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in tmpfk.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 k1LoW | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# tmpfk | ||
|
||
Temporary foreign key setting tool for using ER generator for "Keyless entry (SQL Antipatterns)" schema. | ||
|
||
## Installation | ||
|
||
install it yourself as: | ||
|
||
```sh | ||
$ gem install specific_install | ||
$ gem specific_install https://github.com/k1LoW/tmpfk.git | ||
``` | ||
|
||
## Usage | ||
|
||
Use the same config.yml as [Ridgepole](https://github.com/winebarrel/ridgepole) config.yml. | ||
|
||
### Add foreign keys | ||
|
||
```sh | ||
$ tmpfk add -c config.yml | ||
``` | ||
|
||
### Dop foreign keys | ||
|
||
```sh | ||
$ tmpfk drop -c config.yml | ||
``` | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/k1LoW/tmpfk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'bundler/setup' | ||
require 'tmpfk' | ||
require 'pry' | ||
Pry.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'tmpfk' | ||
|
||
Tmpfk::CLI.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'thor' | ||
require 'active_record' | ||
require 'active_support' | ||
require 'ridgepole' | ||
require 'ridgepole/cli/config' | ||
require 'term/ansicolor' | ||
require 'tmpfk/ext/column' | ||
|
||
module Tmpfk | ||
end | ||
|
||
require 'tmpfk/cli' | ||
require 'tmpfk/version' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Tmpfk | ||
class CLI < Thor | ||
class_option :config, aliases: '-c', type: :string, default: 'config.yml', desc: 'CONF_OR_FILE' | ||
class_option :env, aliases: '-E', type: :string, default: 'development', desc: 'ENVIRONMENT' | ||
class_option :prefix, type: :string, default: 'tmpfk_', desc: 'Tmp foreign key name prefix' | ||
|
||
desc 'add', 'ALTER TABLE [all tables] ADD [tmp]FOREIGN KEY' | ||
def add | ||
conn.data_sources.each do |table| | ||
next if conn.view_exists?(table) | ||
|
||
ActiveRecord::Base.table_name = table | ||
|
||
ActiveRecord::Base.columns.each do |column| | ||
fk_name = "#{options[:prefix]}#{table}_#{column.name}_fk" | ||
primary_key = 'id' | ||
next unless column.foreign_key? | ||
next if conn.foreign_key_exists?(table, name: fk_name) | ||
begin | ||
conn.add_foreign_key( | ||
table, | ||
column.foreign_table, | ||
column: column.name, primary_key: primary_key, name: fk_name | ||
) | ||
puts "ALTER TABLE #{table} ADD CONSTRAINT #{fk_name} FOREIGN KEY (#{column.name}) REFERENCES #{column.foreign_table} (#{primary_key})" | ||
rescue ActiveRecord::InvalidForeignKey => e | ||
c = Term::ANSIColor | ||
puts c.red(e.to_s) | ||
end | ||
end | ||
end | ||
puts 'Done.' | ||
end | ||
|
||
desc 'drop', 'ALTER TABLE [all tables] DROP [tmp]FOREIGN KEY' | ||
def drop | ||
conn.data_sources.each do |table| | ||
next if conn.view_exists?(table) | ||
|
||
ActiveRecord::Base.table_name = table | ||
|
||
ActiveRecord::Base.columns.each do |column| | ||
fk_name = "#{options[:prefix]}#{table}_#{column.name}_fk" | ||
next unless column.foreign_key? | ||
next unless conn.foreign_key_exists?(table, name: fk_name) | ||
begin | ||
conn.remove_foreign_key( | ||
table, | ||
name: fk_name | ||
) | ||
puts "ALTER TABLE #{table} DROP FOREIGN KEY #{fk_name}" | ||
rescue ActiveRecord::InvalidForeignKey => e | ||
c = Term::ANSIColor | ||
puts c.red(e.to_s) | ||
end | ||
end | ||
end | ||
puts 'Done.' | ||
end | ||
|
||
private | ||
|
||
def conn | ||
return ActiveRecord::Base.connection if ActiveRecord::Base.connected? | ||
conn_spec = Ridgepole::Config.load(options[:config], options[:env]) | ||
c = ActiveRecord::Base.establish_connection(conn_spec) | ||
puts "Connect to #{c.spec.config[:database]}..." | ||
ActiveRecord::Base.connection | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ActiveRecord::ConnectionAdapters::Column | ||
def foreign_key? | ||
return false unless name.end_with?('_id') | ||
ActiveRecord::Base.connection.data_source_exists?(name.humanize(capitalize: false).tableize) | ||
end | ||
|
||
def foreign_table | ||
name.humanize(capitalize: false).tableize | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Tmpfk | ||
VERSION = '0.1.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'tmpfk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'spec_helper' | ||
|
||
describe Tmpfk do | ||
it 'has a version number' do | ||
expect(Tmpfk::VERSION).not_to be nil | ||
end | ||
|
||
it 'does something useful' do | ||
expect(false).to eq(true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'tmpfk/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = 'tmpfk' | ||
spec.version = Tmpfk::VERSION | ||
spec.authors = ['k1LoW'] | ||
spec.email = ['k1lowxb@gmail.com'] | ||
|
||
spec.summary = 'Temporary foreign key setting tool for using ER generator for "Keyless entry (SQL Antipatterns)" schema.' | ||
spec.description = 'Temporary foreign key setting tool for using ER generator for "Keyless entry (SQL Antipatterns)" schema.' | ||
spec.homepage = 'https://github.com/k1LoW/tmpfk' | ||
spec.license = 'MIT' | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.bindir = 'exe' | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_dependency 'ridgepole' | ||
spec.add_runtime_dependency 'thor' | ||
spec.add_runtime_dependency 'term-ansicolor' | ||
spec.add_development_dependency 'mysql2' | ||
spec.add_development_dependency 'pg' | ||
spec.add_development_dependency 'bundler', '~> 1.12' | ||
spec.add_development_dependency 'rake', '~> 10.0' | ||
spec.add_development_dependency 'rspec', '~> 3.0' | ||
spec.add_development_dependency 'rubocop', '~> 0.40.0' | ||
spec.add_development_dependency 'octorelease' | ||
spec.add_development_dependency 'pry' | ||
end |