automatic_foreign_key 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ MIT-LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 1.0.0
2
+ * renamed from foreign_key_migration to automatic_foreign_key
3
+ * added gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 RedHill Consulting, Pty. Ltd.
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/README.rdoc ADDED
@@ -0,0 +1,92 @@
1
+ = Automatic Foreign Key
2
+
3
+ Automatic Foreign Key is an ActiveRecord extension that automatically generates foreign-key
4
+ constraints when creating tables. It uses SQL-92 syntax and as such should be
5
+ compatible with most databases that support foreign-key constraints.
6
+
7
+ In the simplest case, the plugin assumes that if you have a column named
8
+ +customer_id+ that you want a foreign-key constraint generated that references
9
+ the +id+ column in the +customers+ table:
10
+
11
+ create_table :orders do |t|
12
+ t.column :customer_id, :integer, :null => false
13
+ ...
14
+ end
15
+
16
+ If you have multiple columns referencing a table or for whatever reason, your
17
+ column name isn't the same as the referenced table name, you can use the
18
+ <code>:references</code> option:
19
+
20
+ create_table :orders do |t|
21
+ t.column :ordered_by_id, :integer, :null => false, :references => :customers
22
+ ...
23
+ end
24
+
25
+ If you have a column with a name ending in +_id+ for which you do not wish a
26
+ foreign-key to be generated, you can use <code>:references => nil</code>:
27
+
28
+ create_table :orders do |t|
29
+ t.column :external_id, :integer, :null => false, :references => nil
30
+ ...
31
+ end
32
+
33
+ Sometimes you may (for legacy reasons) need to reference a primary key column that is
34
+ named something other than +id+. In this case you can specify the name of the column:
35
+
36
+ create_table :orders do |t|
37
+ t.column :ordered_by_pk, :integer, :null => false, :references => [:customers, :pk]
38
+ ...
39
+ end
40
+
41
+ You also have the option of specifying what to do on delete/update using
42
+ <code>:on_delete</code>/<code>:on_update</code>, respectively to one of:
43
+ <code>:cascade</code>; <code>:restrict</code>; and <code>:set_null</code>:
44
+
45
+ create_table :orders do |t|
46
+ t.column :customer_id, :integer, :on_delete => :set_null, :on_update => :cascade
47
+ ...
48
+ end
49
+
50
+ If your database supports it (for example PostgreSQL) you can also mark the constraint as deferrable:
51
+
52
+ create_table :orders do |t|
53
+ t.column :customer_id, :integer, :deferrable => true
54
+ ...
55
+ end
56
+
57
+ By convention, if a column is named +parent_id+ it will be treated as a circular reference to
58
+ the table in which it is defined.
59
+
60
+ Sometimes you may (for legacy reasons) need to name your primary key column such that it
61
+ would be misinterpreted as a foreign-key (say for example if you named the primary key
62
+ +order_id+). In this case you can manually create the primary key as follows:
63
+
64
+ create_table :orders, :id => false do |t|
65
+ ...
66
+ t.primary_key :order_id, :references => nil
67
+ end
68
+
69
+ There is also a generator for creating foreign keys on a database that currently has none:
70
+
71
+ ruby script/generate foreign_key_migration
72
+
73
+ The plugin fully supports and understands the following active-record
74
+ configuration properties:
75
+
76
+ * <code>config.active_record.pluralize_table_names</code>
77
+ * <code>config.active_record.table_name_prefix</code>
78
+ * <code>config.active_record.table_name_suffix</code>
79
+
80
+ === Dependencies
81
+
82
+ * RedHill on Rails Core (redhillonrails_core).
83
+
84
+ === NOTE
85
+
86
+ * Code was created by harukizaemon(http://github.com/harukizaemon) but is not supported currently by him.
87
+ * Former name was foreign_key_migrations
88
+
89
+ === License
90
+
91
+ This plugin is copyright 2010 by Michał Łomnicki and is released
92
+ under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "automatic_foreign_key"
8
+ gem.summary = %Q{Automatically generate foreign-key constraints when creating tables}
9
+ gem.description = %Q{Automatic Key Migrations is a gem that automatically generates foreign-key
10
+ constraints when creating tables. It uses SQL-92 syntax and as such should be compatible with most databases that support foreign-key constraints.}
11
+ gem.email = "michal.lomnicki@gmail.com"
12
+ gem.homepage = "http://github.com/mlomnicki/automatic_foreign_key"
13
+ gem.authors = ["Michał Łomnicki"]
14
+ gem.add_dependency "redhillonrails_core", ">= 1.0.0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "automatic_foreign_key #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{automatic_foreign_key}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Micha\305\202 \305\201omnicki"]
12
+ s.date = %q{2010-03-13}
13
+ s.description = %q{Automatic Key Migrations is a gem that automatically generates foreign-key
14
+ constraints when creating tables. It uses SQL-92 syntax and as such should be compatible with most databases that support foreign-key constraints.}
15
+ s.email = %q{michal.lomnicki@gmail.com}
16
+ s.extra_rdoc_files = [
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGELOG",
23
+ "MIT-LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "automatic_foreign_key.gemspec",
28
+ "generators/foreign_key_migration/foreign_key_migration_generator.rb",
29
+ "generators/foreign_key_migration/templates/migration.rb",
30
+ "init.rb",
31
+ "install.rb",
32
+ "lib/automatic_foreign_key.rb",
33
+ "lib/red_hill_consulting/automatic_foreign_key/active_record/base.rb",
34
+ "lib/red_hill_consulting/automatic_foreign_key/active_record/connection_adapters/table_definition.rb",
35
+ "lib/red_hill_consulting/automatic_foreign_key/active_record/migration.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/mlomnicki/automatic_foreign_key}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{Automatically generate foreign-key constraints when creating tables}
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<redhillonrails_core>, [">= 1.0.0"])
49
+ else
50
+ s.add_dependency(%q<redhillonrails_core>, [">= 1.0.0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<redhillonrails_core>, [">= 1.0.0"])
54
+ end
55
+ end
56
+
@@ -0,0 +1,24 @@
1
+ class ForeignKeyMigrationGenerator < Rails::Generator::NamedBase
2
+ def initialize(runtime_args, runtime_options = {})
3
+ runtime_args << 'create_foreign_keys' if runtime_args.empty?
4
+ super
5
+ end
6
+
7
+ def manifest
8
+ foreign_keys = []
9
+
10
+ connection = ActiveRecord::Base.connection
11
+ connection.tables.each do |table_name|
12
+ connection.columns(table_name).each do |column|
13
+ references = ActiveRecord::Base.references(table_name, column.name)
14
+ foreign_keys << RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(nil, table_name, column.name, references.first, references.last) if references
15
+ end
16
+ end
17
+
18
+ record do |m|
19
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
20
+ :migration_name => class_name, :foreign_keys => foreign_keys
21
+ }, :migration_file_name => file_name
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ <% foreign_keys.each do |foreign_key| -%>
4
+ <%= foreign_key.to_dump %>
5
+ <% end -%>
6
+ end
7
+
8
+ def self.down
9
+ end
10
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'automatic_foreign_key' unless defined?(RedHillConsulting::AutomaticForeignKey)
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts "WARNING: You also need to install redhillonrails_core" unless File.exists?(File.join(File.dirname(__FILE__), "..", "redhillonrails_core"))
@@ -0,0 +1,4 @@
1
+ require 'redhillonrails_core'
2
+ ActiveRecord::Base.send(:include, RedHillConsulting::AutomaticForeignKey::ActiveRecord::Base)
3
+ ActiveRecord::Migration.send(:include, RedHillConsulting::AutomaticForeignKey::ActiveRecord::Migration)
4
+ ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, RedHillConsulting::AutomaticForeignKey::ActiveRecord::ConnectionAdapters::TableDefinition)
@@ -0,0 +1,22 @@
1
+ module RedHillConsulting::AutomaticForeignKey::ActiveRecord
2
+ module Base
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def references(table_name, column_name, options = {})
9
+ column_name = column_name.to_s
10
+ if options.has_key?(:references)
11
+ references = options[:references]
12
+ references = [references, :id] unless references.nil? || references.is_a?(Array)
13
+ references
14
+ elsif column_name == 'parent_id'
15
+ [table_name, :id]
16
+ elsif column_name =~ /^(.*)_id$/
17
+ [pluralized_table_name($1), :id]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module RedHillConsulting::AutomaticForeignKey::ActiveRecord::ConnectionAdapters
2
+ module TableDefinition
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :column, :automatic_foreign_key
6
+ alias_method_chain :primary_key, :automatic_foreign_key
7
+ end
8
+ end
9
+
10
+ def primary_key_with_automatic_foreign_key(name, options = {})
11
+ column(name, :primary_key, options)
12
+ end
13
+
14
+ def column_with_automatic_foreign_key(name, type, options = {})
15
+ column_without_automatic_foreign_key(name, type, options)
16
+ references = ActiveRecord::Base.references(self.name, name, options)
17
+ foreign_key(name, references.first, references.last, options) if references
18
+ self
19
+ end
20
+
21
+ # Some people liked this; personally I've decided against using it but I'll keep it nonetheless
22
+ def belongs_to(table, options = {})
23
+ options = options.merge(:references => table)
24
+ options[:on_delete] = options.delete(:dependent) if options.has_key?(:dependent)
25
+ column("#{table.to_s.singularize}_id".to_sym, :integer, options)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ module RedHillConsulting::AutomaticForeignKey::ActiveRecord
2
+ module Migration
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def add_column(table_name, column_name, type, options = {})
9
+ super
10
+ references = ActiveRecord::Base.references(table_name, column_name, options)
11
+ add_foreign_key(table_name, column_name, references.first, references.last, options) if references
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: automatic_foreign_key
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - "Micha\xC5\x82 \xC5\x81omnicki"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: redhillonrails_core
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ description: |-
26
+ Automatic Key Migrations is a gem that automatically generates foreign-key
27
+ constraints when creating tables. It uses SQL-92 syntax and as such should be compatible with most databases that support foreign-key constraints.
28
+ email: michal.lomnicki@gmail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - CHANGELOG
39
+ - MIT-LICENSE
40
+ - README.rdoc
41
+ - Rakefile
42
+ - VERSION
43
+ - automatic_foreign_key.gemspec
44
+ - generators/foreign_key_migration/foreign_key_migration_generator.rb
45
+ - generators/foreign_key_migration/templates/migration.rb
46
+ - init.rb
47
+ - install.rb
48
+ - lib/automatic_foreign_key.rb
49
+ - lib/red_hill_consulting/automatic_foreign_key/active_record/base.rb
50
+ - lib/red_hill_consulting/automatic_foreign_key/active_record/connection_adapters/table_definition.rb
51
+ - lib/red_hill_consulting/automatic_foreign_key/active_record/migration.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/mlomnicki/automatic_foreign_key
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Automatically generate foreign-key constraints when creating tables
80
+ test_files: []
81
+