artemk-migration_fu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = MigrationFu
2
+
3
+ Rails gem / plugin for generating mysql foreign key constraints.
4
+
5
+ == Install
6
+
7
+ === as gem
8
+
9
+ sudo gem install artemk-migration_fu
10
+
11
+ === or plugin
12
+
13
+ script/plugin install git://github.com/sleistner/migration_fu.git
14
+
15
+ == Usage
16
+
17
+ ----------------- -----------------
18
+ | users | | addresses |
19
+ ----------------- -----------------
20
+ | id | | id |
21
+ | username | <---- | user_id |
22
+ | password | | street |
23
+ ----------------- -----------------
24
+
25
+ [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
26
+ [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
27
+
28
+ see http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
29
+
30
+ arguments: from_table, to_table, options => { :name, :on_delete, :on_update }
31
+
32
+ add_foreign_key(:addresses, :users, :name => 'fk_add_user')
33
+ add_foreign_key(:addresses, :users, :on_delete => :cascade)
34
+ add_foreign_key(:addresses, :users, :on_delete => :cascade, :on_update => :cascade)
35
+
36
+ class CreateUsers < ActiveRecord::Migration
37
+
38
+ def self.up
39
+ create_table :users, :force => true do |t|
40
+ t.string :username, :null => false
41
+ t.string :password, :null => false
42
+ end
43
+
44
+ create_table :addresses, :force => true do |t|
45
+ t.references :user
46
+ t.string :street, :null => false
47
+ end
48
+
49
+ add_foreign_key(:addresses, :users, :on_delete => :cascade)
50
+ end
51
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'migration_fu'
@@ -0,0 +1,58 @@
1
+ module ActiveRecord
2
+
3
+ class Migration
4
+
5
+ MAX_KEY_LENGTH = 64
6
+ OPTION_KEYS = [:restrict, :set_null, :cascade, :no_action]
7
+ OPTION_VALUES = [:on_update, :on_delete]
8
+
9
+ class << self
10
+
11
+ def add_foreign_key(from_table, to_table, options = {})
12
+ process(from_table, to_table, options) do |ft, tt, id, fk|
13
+ execute "ALTER TABLE #{ft} ADD CONSTRAINT #{id} FOREIGN KEY(#{fk}) REFERENCES #{tt}(id)" << conditions(options)
14
+ end
15
+ end
16
+
17
+ def remove_foreign_key(from_table, to_table, options = {})
18
+ process(from_table, to_table, options) do |ft, tt, id|
19
+ execute "ALTER TABLE #{ft} DROP FOREIGN KEY #{id}, DROP KEY #{id}"
20
+ end
21
+ end
22
+
23
+ def entirely_reset_column_information
24
+ ActiveRecord::Base.send(:subclasses).each(&:reset_column_information)
25
+ end
26
+
27
+ private
28
+
29
+ def conditions(options)
30
+ conditions = ''
31
+ options.each_pair do |key, value|
32
+ conditions << " #{key.to_s.gsub(/_/, ' ')} #{value.to_s.gsub(/_/, ' ')}".upcase if condition_valid?(key, value)
33
+ end
34
+ conditions
35
+ end
36
+
37
+ def condition_valid?(key, value)
38
+ OPTION_VALUES.include?(key.to_sym) && OPTION_KEYS.include?(value.to_sym)
39
+ end
40
+
41
+ def process(from_table, to_table, options)
42
+ id = options[:name] || "fk_#{from_table}_#{to_table}"
43
+ if options[:fk_field]
44
+ fk = options[:fk_field]
45
+ id = "#{id}_#{options[:fk_field]}"
46
+ else
47
+ fk = "#{to_table.to_s.singularize}_id"
48
+ end
49
+
50
+ if id.size > MAX_KEY_LENGTH
51
+ puts "*** foreign key id has more than #{MAX_KEY_LENGTH} characters - sliced to '#{id}'"
52
+ end
53
+ yield(from_table.to_s, to_table, id[0...MAX_KEY_LENGTH], fk)
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,85 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ begin require 'redgreen'; rescue LoadError; end
4
+ require File.dirname(__FILE__) + '/../lib/migration_fu'
5
+
6
+ class String
7
+ def singularize; self[0...-1] end
8
+ end
9
+
10
+ class ActiveRecord::Migration
11
+ def self.execute command; command end
12
+ end
13
+
14
+ class MigrationFuTest < Test::Unit::TestCase
15
+
16
+ ID = 'fk_users_files'
17
+ CUSTOM_ID = 'fk_my_name'
18
+ CUSTOM_FK = 'person_id'
19
+
20
+ def setup
21
+ @foo = ActiveRecord::Migration
22
+ end
23
+
24
+ def test_should_add_foreign_key_without_options
25
+ assert_equal add_command, add
26
+ end
27
+
28
+ def test_should_add_foreign_key_with_invalid_options_but_ignore_them
29
+ assert_equal add_command, add(:on_del => :ca)
30
+ end
31
+
32
+ def test_should_add_foreign_key_with_valid_options
33
+ assert_equal "#{add_command} ON DELETE CASCADE", add(:on_delete => :cascade)
34
+ assert_match(/ON DELETE CASCADE/, add(:on_update => :set_null, :on_delete => :cascade))
35
+ assert_match(/ON UPDATE SET NULL/, add(:on_update => :set_null, :on_delete => :cascade))
36
+ end
37
+
38
+ def test_should_add_foreign_key_with_optional_name
39
+ assert_equal add_command(CUSTOM_ID), add(:name => CUSTOM_ID)
40
+ end
41
+
42
+ #
43
+ def test_should_add_foreign_key_with_optional_fk_field
44
+ assert_equal add_command(CUSTOM_ID, :fk_field => CUSTOM_FK), add(:name => CUSTOM_ID, :fk_field => CUSTOM_FK)
45
+ end
46
+
47
+ def test_should_add_foreign_key_and_truncate_id
48
+ to = 'x' * 70
49
+ assert_equal add_command('fk_users_' << 'x' * 55, :to => to), @foo.add_foreign_key(:users, to.to_sym)
50
+ end
51
+
52
+ def test_should_remove_foreign_key
53
+ assert_equal remove_command, remove
54
+ end
55
+
56
+ def test_should_remove_foreign_key_with_optional_name
57
+ assert_equal remove_command(CUSTOM_ID), remove(:name => CUSTOM_ID)
58
+ end
59
+
60
+ private
61
+
62
+ def add(options = {})
63
+ @foo.add_foreign_key :users, :files, options
64
+ end
65
+
66
+ def remove(options = {})
67
+ @foo.remove_foreign_key :users, :files, options
68
+ end
69
+
70
+ def add_command(id = ID, opts = {})
71
+ opts = {:to => 'files'}.merge!(opts)
72
+ if opts[:fk_field]
73
+ fk = opts[:fk_field]
74
+ id = "#{id}_#{opts[:fk_field]}"
75
+ else
76
+ fk = "#{opts[:to].singularize}_id"
77
+ end
78
+ "ALTER TABLE users ADD CONSTRAINT #{id} FOREIGN KEY(#{fk}) REFERENCES #{opts[:to]}(id)"
79
+ end
80
+
81
+ def remove_command(id = ID)
82
+ "ALTER TABLE users DROP FOREIGN KEY #{id}, DROP KEY #{id}"
83
+ end
84
+
85
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artemk-migration_fu
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors: []
13
+
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-21 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Add and remove FK in MYSQL
23
+ email: artemk@svitla.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - init.rb
32
+ - lib/migration_fu.rb
33
+ - README.rdoc
34
+ - test/migration_fu_test.rb
35
+ has_rdoc: true
36
+ homepage:
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Add and remove FK in MYSQL
69
+ test_files:
70
+ - test/migration_fu_test.rb