fakey 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +12 -0
- data/fakey.gemspec +26 -0
- data/lib/fakey.rb +19 -0
- data/lib/fakey/mysql_adapter.rb +10 -0
- data/lib/fakey/postgresql_adapter.rb +27 -0
- data/lib/fakey/schema_definitions.rb +74 -0
- data/lib/fakey/version.rb +3 -0
- data/test/database.yml +8 -0
- data/test/fakey_test.rb +189 -0
- data/test/test_helper.rb +26 -0
- metadata +128 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009-2014 Tutuf Ltd
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Fakey - foreign keys support for ActiveRecord migrations
|
2
|
+
|
3
|
+
|
4
|
+
Before resorting to write yet another foreign key plugin I tried
|
5
|
+
[foreign_key_migrations](http://github.com/harukizaemon/foreign_key_migrations)
|
6
|
+
(no longer available) and
|
7
|
+
[foreigner](http://github.com/matthuhiggins/foreigner), but they have annoying
|
8
|
+
shortcomings. *foreign_key_migrations* assumes every column ending on `_id` to
|
9
|
+
be a foreign key and tries to infer the table name, which in my case was not
|
10
|
+
the right one. *foreigner* doesn't let you declare foreign key in
|
11
|
+
`create_table` block, instead you have to call explicitly
|
12
|
+
`add_foreign_key(:comments, :posts)` after it. Not quite helpful for tables
|
13
|
+
containing a lot of foreign keys.
|
14
|
+
|
15
|
+
Fakey is fully tested by directly inspecting schema in the database and
|
16
|
+
supports both MySQL and PostgreSQL.
|
17
|
+
|
18
|
+
##Example
|
19
|
+
Fakey hooks foreign keys to the standard `belongs_to` and `references` methods in migrations.
|
20
|
+
|
21
|
+
# Standard usage
|
22
|
+
create_table :books do |t|
|
23
|
+
t.belongs_to :author
|
24
|
+
end
|
25
|
+
# => CREATE TABLE "books" ("id" serial primary key, "author_id" integer REFERENCES "authors")
|
26
|
+
|
27
|
+
# Table name specified explicitly
|
28
|
+
create_table :poems do |t|
|
29
|
+
t.belongs_to :author, :references => :poets
|
30
|
+
end
|
31
|
+
# => CREATE TABLE "poems" ("id" serial primary key, "author_id" integer REFERENCES "poets")
|
32
|
+
|
33
|
+
# Column name specified explicitly (if you don't want _id suffix)
|
34
|
+
create_table :books do |t|
|
35
|
+
t.belongs_to :author, :column => :author
|
36
|
+
end
|
37
|
+
# => CREATE TABLE "books" ("id" serial primary key, "author" integer REFERENCES "books")
|
38
|
+
|
39
|
+
# Non-integer primary key (for those with legacy databases) - this only works in PostgreSQL now!
|
40
|
+
execute "CREATE TABLE authors( name VARCHAR(255) PRIMARY KEY)"
|
41
|
+
create_table :books do |t|
|
42
|
+
t.belongs_to :author_name, :column => :author_name, :references => :authors, :type => :string
|
43
|
+
end
|
44
|
+
# => CREATE TABLE "books" ("id" serial primary key, "author_name" VARCHAR(255) REFERENCES "authors")
|
45
|
+
|
46
|
+
# Non-primary key (for those with legacy databases)
|
47
|
+
create_table :authors do |t|
|
48
|
+
t.integer :ssid
|
49
|
+
end
|
50
|
+
execute("ALTER TABLE authors ADD UNIQUE(ssid)") # note that add_index doesn't create constraint!
|
51
|
+
create_table :books do |t|
|
52
|
+
t.belongs_to :author_ssid, :column => :author_ssid, :references => :authors, :referenced_column => :ssid
|
53
|
+
end
|
54
|
+
# => CREATE TABLE "books" ("id" serial primary key, "author_ssid" integer REFERENCES "authors"(ssid))
|
55
|
+
|
56
|
+
|
57
|
+
# Changing table too
|
58
|
+
change_table :atuhors do |t|
|
59
|
+
t.belongs_to :author
|
60
|
+
end
|
61
|
+
|
62
|
+
##Compatibility
|
63
|
+
|
64
|
+
Rails: 3.2+
|
65
|
+
|
66
|
+
PostgreSQL 7.0+
|
67
|
+
|
68
|
+
MySQL 5.0+
|
69
|
+
|
70
|
+
Copyright (c) 2009-2014 Tutuf Ltd, released under the MIT license. Thanks to Bryan Evans for database introspection query (taken from drysql gem).
|
data/Rakefile
ADDED
data/fakey.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fakey/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fakey"
|
8
|
+
spec.version = Fakey::VERSION
|
9
|
+
spec.authors = ["Sava Chankov"]
|
10
|
+
spec.email = ["sava@tutuf.com"]
|
11
|
+
spec.description = %q{Foreign keys support for ActiveRecord migrations}
|
12
|
+
spec.summary = %q{Foreign keys support for ActiveRecord migrations}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", "~>3.2.18"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rails", "~>3.2.18"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
data/lib/fakey.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'fakey/schema_definitions'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters
|
6
|
+
TableDefinition.class_eval { include Fakey::TableDefinition }
|
7
|
+
Table.class_eval { include Fakey::Table }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
12
|
+
require 'fakey/postgresql_adapter'
|
13
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval { include Fakey::PostgreSQLAdapter }
|
14
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
15
|
+
require 'fakey/mysql_adapter'
|
16
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval { include Fakey::MysqlAdapter }
|
17
|
+
else
|
18
|
+
raise "Only PostgreSQL and MySQL are currently supported by the fakey plugin."
|
19
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Fakey #:nodoc:
|
3
|
+
module MysqlAdapter
|
4
|
+
def add_foreign_key(to_table, column, options={})
|
5
|
+
to_table = options[:references] if options[:references]
|
6
|
+
referenced_column = options[:referenced_column] || primary_key(to_table)
|
7
|
+
"FOREIGN KEY(#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)} (#{referenced_column})"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Fakey #:nodoc:
|
3
|
+
module PostgreSQLAdapter
|
4
|
+
def add_foreign_key(to_table, column, options={})
|
5
|
+
to_table = options[:references] if options[:references]
|
6
|
+
res = "FOREIGN KEY(#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}"
|
7
|
+
referenced_column = options[:referenced_column] || options[:primary_key]
|
8
|
+
res += "(#{referenced_column})" if referenced_column
|
9
|
+
res
|
10
|
+
end
|
11
|
+
|
12
|
+
# Adds a new column to the named table.
|
13
|
+
# See TableDefinition#column for details of the options you can use.
|
14
|
+
def add_column_with_foreign_key(table_name, column_name, type, options = {})
|
15
|
+
default = options[:default]
|
16
|
+
notnull = options[:null] == false
|
17
|
+
foreign_key = "REFERENCES #{quote_table_name(options[:references])}"
|
18
|
+
foreign_key +="(#{quote_column_name(options[:referenced_column] || options[:primary_key])})" if options[:referenced_column] || options[:primary_key]
|
19
|
+
|
20
|
+
# Add the column.
|
21
|
+
execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])} #{foreign_key}")
|
22
|
+
|
23
|
+
change_column_default(table_name, column_name, default) if options_include_default?(options)
|
24
|
+
change_column_null(table_name, column_name, false, default) if notnull
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Fakey
|
3
|
+
module TableDefinition
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :to_sql, :foreign_keys
|
7
|
+
alias_method_chain :references, :foreign_keys
|
8
|
+
alias_method_chain :belongs_to, :foreign_keys
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def references_with_foreign_keys(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
polymorphic = options.delete(:polymorphic)
|
15
|
+
args.each do |col|
|
16
|
+
column_name = options[:column] || "#{col}_id"
|
17
|
+
column_database_type = options[:type] || :integer
|
18
|
+
column(column_name, column_database_type, options)
|
19
|
+
if polymorphic
|
20
|
+
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options)
|
21
|
+
else
|
22
|
+
table_name = options[:references] || col.to_s.pluralize
|
23
|
+
add_foreign_key(column_name, table_name, options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
alias :belongs_to_with_foreign_keys :references_with_foreign_keys
|
28
|
+
|
29
|
+
def add_foreign_key(column_name,table_name,options)
|
30
|
+
@foreign_keys ||= []
|
31
|
+
@foreign_keys << {:column_name => column_name, :to_table => table_name, :options => options}
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_sql_with_foreign_keys
|
35
|
+
sql = to_sql_without_foreign_keys
|
36
|
+
if @foreign_keys
|
37
|
+
sql << ',' << @foreign_keys.map{|fk| @base.add_foreign_key(fk[:to_table],fk[:column_name],fk[:options])}.join(",")
|
38
|
+
end
|
39
|
+
sql
|
40
|
+
end
|
41
|
+
|
42
|
+
def foreign_key(*args)
|
43
|
+
return unless Array === args.first
|
44
|
+
composite_foreign_key, options = args.first, args.last
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module Table
|
50
|
+
def self.included(base)
|
51
|
+
base.class_eval do
|
52
|
+
alias_method_chain :references, :foreign_keys
|
53
|
+
alias_method_chain :belongs_to, :foreign_keys
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def references_with_foreign_keys(*args)
|
58
|
+
options = args.extract_options!
|
59
|
+
polymorphic = options.delete(:polymorphic)
|
60
|
+
args.each do |col|
|
61
|
+
column_name = options[:column] || "#{col}_id"
|
62
|
+
column_database_type = options[:type] || :integer
|
63
|
+
if polymorphic
|
64
|
+
@base.add_column(@table_name, column_name, column_database_type, options)
|
65
|
+
@base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options)
|
66
|
+
else
|
67
|
+
options[:references] ||= col.to_s.pluralize
|
68
|
+
@base.add_column_with_foreign_key(@table_name, column_name, column_database_type, options)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
alias :belongs_to_with_foreign_keys :references_with_foreign_keys
|
73
|
+
end
|
74
|
+
end
|
data/test/database.yml
ADDED
data/test/fakey_test.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
class FakeyTest < ActiveSupport::TestCase
|
4
|
+
def inspect_foreign_keys(table)
|
5
|
+
connection = ActiveRecord::Base.connection
|
6
|
+
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
7
|
+
connection.select_all <<-SQL
|
8
|
+
SELECT tc.table_name,
|
9
|
+
kcu.column_name,
|
10
|
+
kcu2.table_name AS referenced_table_name,
|
11
|
+
kcu2.column_name AS referenced_column_name
|
12
|
+
FROM information_schema.table_constraints AS tc
|
13
|
+
JOIN information_schema.key_column_usage AS kcu ON (tc.constraint_name = kcu.constraint_name)
|
14
|
+
JOIN information_schema.referential_constraints AS rc ON (tc.constraint_name = rc.constraint_name)
|
15
|
+
JOIN information_schema.key_column_usage AS kcu2 ON (rc.unique_constraint_name = kcu2.constraint_name)
|
16
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
17
|
+
AND tc.table_name='#{table}'
|
18
|
+
ORDER BY table_name,
|
19
|
+
column_name
|
20
|
+
SQL
|
21
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
22
|
+
connection.select_all <<-SQL
|
23
|
+
SELECT kcu.table_name,
|
24
|
+
kcu.column_name,
|
25
|
+
kcu.referenced_table_name,
|
26
|
+
kcu.referenced_column_name
|
27
|
+
FROM information_schema.table_constraints tc
|
28
|
+
JOIN information_schema.key_column_usage kcu USING(constraint_name)
|
29
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
30
|
+
ORDER BY table_name,
|
31
|
+
column_name
|
32
|
+
SQL
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
self.class.const_get(@method_name.match(/test_(.+)/)[1].camelize + "Migration").down if defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
38
|
+
end
|
39
|
+
|
40
|
+
class BelongsToMigration < ActiveRecord::Migration
|
41
|
+
def self.up
|
42
|
+
create_table(:authors) {}
|
43
|
+
create_table :books do |t|
|
44
|
+
t.belongs_to :author
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.down
|
49
|
+
drop_table :books
|
50
|
+
drop_table :authors
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_belongs_to
|
55
|
+
BelongsToMigration.up
|
56
|
+
assert_equal( {'table_name' => 'books', 'column_name' => 'author_id', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'id'},
|
57
|
+
inspect_foreign_keys(:books).first)
|
58
|
+
end
|
59
|
+
|
60
|
+
class ReferencesMigration < ActiveRecord::Migration
|
61
|
+
def self.up
|
62
|
+
create_table(:authors) {}
|
63
|
+
create_table :articles do |t|
|
64
|
+
t.references :author
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.down
|
69
|
+
drop_table :articles
|
70
|
+
drop_table :authors
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_references
|
75
|
+
ReferencesMigration.up
|
76
|
+
assert_equal( {'table_name' => 'articles', 'column_name' => 'author_id', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'id'},
|
77
|
+
inspect_foreign_keys(:articles).first)
|
78
|
+
end
|
79
|
+
|
80
|
+
class ExplicitColumnNameMigration < ActiveRecord::Migration
|
81
|
+
def self.up
|
82
|
+
create_table(:poets) {}
|
83
|
+
create_table :poems do |t|
|
84
|
+
t.belongs_to :author, :column => :author, :references => :poets
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.down
|
89
|
+
drop_table :poems
|
90
|
+
drop_table :poets
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_explicit_column_name
|
95
|
+
ExplicitColumnNameMigration.up
|
96
|
+
assert_equal( {'table_name' => 'poems', 'column_name' => 'author', 'referenced_table_name' => 'poets', 'referenced_column_name' => 'id'},
|
97
|
+
inspect_foreign_keys(:poems).first)
|
98
|
+
end
|
99
|
+
|
100
|
+
class MoreThanOneForeignKeyMigration < ActiveRecord::Migration
|
101
|
+
def self.up
|
102
|
+
create_table(:authors) {}
|
103
|
+
create_table(:editors) {}
|
104
|
+
create_table :scientific_articles do |t|
|
105
|
+
t.belongs_to :author
|
106
|
+
t.belongs_to :editor
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.down
|
111
|
+
drop_table :scientific_articles
|
112
|
+
drop_table :editors
|
113
|
+
drop_table :authors
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_more_than_one_foreign_key
|
118
|
+
MoreThanOneForeignKeyMigration.up
|
119
|
+
assert_equal [{'table_name' => 'scientific_articles', 'column_name' => 'author_id', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'id'},
|
120
|
+
{'table_name' => 'scientific_articles', 'column_name' => 'editor_id', 'referenced_table_name' => 'editors', 'referenced_column_name' => 'id'}],
|
121
|
+
inspect_foreign_keys(:scientific_articles)
|
122
|
+
end
|
123
|
+
|
124
|
+
class StringForeignKeyMigration < ActiveRecord::Migration
|
125
|
+
class Author < ActiveRecord::Base; end
|
126
|
+
def self.up
|
127
|
+
execute "CREATE TABLE authors( name VARCHAR(255) PRIMARY KEY)"
|
128
|
+
create_table :books do |t|
|
129
|
+
t.belongs_to :author_name, :column => :author_name, :references => :authors, :type => :string
|
130
|
+
end
|
131
|
+
end
|
132
|
+
def self.down
|
133
|
+
drop_table :books if ActiveRecord::Base.connection.tables.include? "books"
|
134
|
+
drop_table :authors if ActiveRecord::Base.connection.tables.include? "authors"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_string_foreign_key
|
139
|
+
StringForeignKeyMigration.up
|
140
|
+
assert_equal :string, StringForeignKeyMigration::Author.columns.detect{|c| c.name =='name'}.type
|
141
|
+
assert_equal({'table_name' => 'books', 'column_name' => 'author_name', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'name'},
|
142
|
+
inspect_foreign_keys(:books).first)
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
class ReferenceToNonPrimaryKeyMigration < ActiveRecord::Migration
|
147
|
+
def self.up
|
148
|
+
create_table :authors do |t|
|
149
|
+
t.integer :ssid
|
150
|
+
end
|
151
|
+
execute("ALTER TABLE authors ADD UNIQUE(ssid)")
|
152
|
+
create_table :books do |t|
|
153
|
+
t.belongs_to :author_ssid, :column => :author_ssid, :references => :authors, :referenced_column => :ssid
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.down
|
158
|
+
drop_table :books
|
159
|
+
drop_table :authors
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_reference_to_non_primary_key
|
164
|
+
ReferenceToNonPrimaryKeyMigration.up
|
165
|
+
assert_equal({'table_name' => 'books', 'column_name' => 'author_ssid', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'ssid'},
|
166
|
+
inspect_foreign_keys(:books).first)
|
167
|
+
end
|
168
|
+
|
169
|
+
class ChangeTableMigration < ActiveRecord::Migration
|
170
|
+
def self.up
|
171
|
+
create_table(:authors) {}
|
172
|
+
create_table(:books) {}
|
173
|
+
change_table :books do |t|
|
174
|
+
t.belongs_to :author
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def self.down
|
179
|
+
drop_table :books
|
180
|
+
drop_table :authors
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_change_table
|
185
|
+
ChangeTableMigration.up
|
186
|
+
assert_equal( {'table_name' => 'books', 'column_name' => 'author_id', 'referenced_table_name' => 'authors', 'referenced_column_name' => 'id'},
|
187
|
+
inspect_foreign_keys(:books).first)
|
188
|
+
end
|
189
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
plugin_test_dir = File.dirname(__FILE__)
|
2
|
+
$:.unshift(plugin_test_dir + '/../lib')
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/fixtures'
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'ruby-debug'
|
10
|
+
rescue LoadError
|
11
|
+
# no debugger available
|
12
|
+
end
|
13
|
+
|
14
|
+
config = ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + '/database.yml'))
|
15
|
+
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
|
16
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB']] || config['postgresql'])
|
17
|
+
|
18
|
+
require 'fakey'
|
19
|
+
|
20
|
+
class ActiveSupport::TestCase #:nodoc:
|
21
|
+
# allow testing in Rails < 2.3
|
22
|
+
include ActiveRecord::TestFixtures rescue NameError
|
23
|
+
self.use_transactional_fixtures = true
|
24
|
+
self.use_instantiated_fixtures = false
|
25
|
+
ActiveRecord::Migration.verbose = false
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fakey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sava Chankov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.18
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.18
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rails
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.18
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.18
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Foreign keys support for ActiveRecord migrations
|
79
|
+
email:
|
80
|
+
- sava@tutuf.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- fakey.gemspec
|
91
|
+
- lib/fakey.rb
|
92
|
+
- lib/fakey/mysql_adapter.rb
|
93
|
+
- lib/fakey/postgresql_adapter.rb
|
94
|
+
- lib/fakey/schema_definitions.rb
|
95
|
+
- lib/fakey/version.rb
|
96
|
+
- test/database.yml
|
97
|
+
- test/fakey_test.rb
|
98
|
+
- test/test_helper.rb
|
99
|
+
homepage: ''
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.8.23
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: Foreign keys support for ActiveRecord migrations
|
124
|
+
test_files:
|
125
|
+
- test/database.yml
|
126
|
+
- test/fakey_test.rb
|
127
|
+
- test/test_helper.rb
|
128
|
+
has_rdoc:
|