Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Feb 15, 2017
0 parents commit 6a71819
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--format documentation
--color
73 changes: 73 additions & 0 deletions .rubocop.yml
@@ -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
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.3.1
before_install: gem install bundler -v 1.12.5
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in tmpfk.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
@@ -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.
37 changes: 37 additions & 0 deletions README.md
@@ -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).

6 changes: 6 additions & 0 deletions Rakefile
@@ -0,0 +1,6 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
6 changes: 6 additions & 0 deletions bin/console
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'tmpfk'
require 'pry'
Pry.start
8 changes: 8 additions & 0 deletions bin/setup
@@ -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
5 changes: 5 additions & 0 deletions exe/tmpfk
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require 'tmpfk'

Tmpfk::CLI.start
13 changes: 13 additions & 0 deletions lib/tmpfk.rb
@@ -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'
71 changes: 71 additions & 0 deletions lib/tmpfk/cli.rb
@@ -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
10 changes: 10 additions & 0 deletions lib/tmpfk/ext/column.rb
@@ -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
3 changes: 3 additions & 0 deletions lib/tmpfk/version.rb
@@ -0,0 +1,3 @@
module Tmpfk
VERSION = '0.1.0'
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'tmpfk'
11 changes: 11 additions & 0 deletions spec/tmpfk_spec.rb
@@ -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
33 changes: 33 additions & 0 deletions tmpfk.gemspec
@@ -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

0 comments on commit 6a71819

Please sign in to comment.