kjeldahl-mysql_schema_bulk_change 0.2.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/History.txt +4 -0
- data/Manifest.txt +16 -0
- data/README.rdoc +62 -0
- data/Rakefile +28 -0
- data/features/development.feature +13 -0
- data/features/step_definitions/common_steps.rb +194 -0
- data/features/support/env.rb +6 -0
- data/lib/mysql_schema_bulk_change.rb +8 -0
- data/lib/mysql_schema_bulk_change/mysql_adapter.rb +142 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/mysql_schema_bulk_change_spec.rb +41 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +33 -0
- data/tasks/rspec.rake +21 -0
- metadata +101 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
features/development.feature
|
6
|
+
features/step_definitions/common_steps.rb
|
7
|
+
features/support/env.rb
|
8
|
+
lib/mysql_schema_bulk_change.rb
|
9
|
+
lib/mysql_schema_bulk_change/mysql_adapter.rb
|
10
|
+
script/console
|
11
|
+
script/destroy
|
12
|
+
script/generate
|
13
|
+
spec/mysql_schema_bulk_change_spec.rb
|
14
|
+
spec/spec.opts
|
15
|
+
spec/spec_helper.rb
|
16
|
+
tasks/rspec.rake
|
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= mysql_schema_bulk_change
|
2
|
+
|
3
|
+
* http://github.com/jacob_kjeldahl/mysql_schema_bulk_change
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This extension to the MysqlAdapter in ActiveRecord enables bulk updates to schema definitions.
|
8
|
+
|
9
|
+
Pr. default when calling connection#add_column the change will be executed a once, but Mysql
|
10
|
+
allows for multiple changes to be executed in one SQL statement (http://dev.mysql.com/doc/refman/5.1/en/alter-table.html). The advantage of this is that it takes
|
11
|
+
a lot less time, especially if the table is large.
|
12
|
+
|
13
|
+
== FEATURES/PROBLEMS:
|
14
|
+
|
15
|
+
* Bulk updates to schema definitions when using connection.change_table do |table|
|
16
|
+
* Use the old approch using change_table_direct
|
17
|
+
|
18
|
+
== SYNOPSIS:
|
19
|
+
|
20
|
+
In a migration:
|
21
|
+
|
22
|
+
change_table :large_table do |t|
|
23
|
+
t.string :name
|
24
|
+
t.column :name, :string
|
25
|
+
t.index :name => 'index_on_name'
|
26
|
+
t.remove_index :name => 'index_on_author'
|
27
|
+
t.remove :auhor_id, :editor_id
|
28
|
+
end
|
29
|
+
|
30
|
+
== REQUIREMENTS:
|
31
|
+
|
32
|
+
* ActiveRecord 2.3.2
|
33
|
+
* Mysql
|
34
|
+
|
35
|
+
== INSTALL:
|
36
|
+
|
37
|
+
* sudo gem install kjeldahl-mysql_schema_bulk_change --source http://gems.github.com
|
38
|
+
|
39
|
+
== LICENSE:
|
40
|
+
|
41
|
+
(The MIT License)
|
42
|
+
|
43
|
+
Copyright (c) 2009 Jacob Kjeldahl
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
'Software'), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
59
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
60
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
61
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
62
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/mysql_schema_bulk_change'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('mysql_schema_bulk_change', MysqlSchemaBulkChange::VERSION) do |p|
|
7
|
+
p.developer('Jacob Kjeldahl', 'jkj@lenio.dk')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
#p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
p.extra_deps = [
|
12
|
+
['activerecord','>= 2.3.2'],
|
13
|
+
]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
16
|
+
]
|
17
|
+
|
18
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
19
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
20
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
21
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
25
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
26
|
+
|
27
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
28
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Feature: Development processes of newgem itself (rake tasks)
|
2
|
+
|
3
|
+
As a Newgem maintainer or contributor
|
4
|
+
I want rake tasks to maintain and release the gem
|
5
|
+
So that I can spend time on the tests and code, and not excessive time on maintenance processes
|
6
|
+
|
7
|
+
Scenario: Generate RubyGem
|
8
|
+
Given this project is active project folder
|
9
|
+
And 'pkg' folder is deleted
|
10
|
+
When task 'rake gem' is invoked
|
11
|
+
Then folder 'pkg' is created
|
12
|
+
And file with name matching 'pkg/*.gem' is created else you should run "rake manifest" to fix this
|
13
|
+
And gem spec key 'rdoc_options' contains /--mainREADME.rdoc/
|
@@ -0,0 +1,194 @@
|
|
1
|
+
def in_project_folder(&block)
|
2
|
+
project_folder = @active_project_folder || @tmp_root
|
3
|
+
FileUtils.chdir(project_folder, &block)
|
4
|
+
end
|
5
|
+
|
6
|
+
def in_home_folder(&block)
|
7
|
+
FileUtils.chdir(@home_path, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
Given %r{^a safe folder} do
|
11
|
+
FileUtils.rm_rf @tmp_root = File.dirname(__FILE__) + "/../../tmp"
|
12
|
+
FileUtils.mkdir_p @tmp_root
|
13
|
+
FileUtils.mkdir_p @home_path = File.expand_path(File.join(@tmp_root, "home"))
|
14
|
+
@lib_path = File.expand_path(File.dirname(__FILE__) + '/../../lib')
|
15
|
+
Given "env variable $HOME set to '#{@home_path}'"
|
16
|
+
end
|
17
|
+
|
18
|
+
Given %r{^this project is active project folder} do
|
19
|
+
Given "a safe folder"
|
20
|
+
@active_project_folder = File.expand_path(File.dirname(__FILE__) + "/../..")
|
21
|
+
end
|
22
|
+
|
23
|
+
Given %r{^env variable \$([\w_]+) set to '(.*)'} do |env_var, value|
|
24
|
+
ENV[env_var] = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def force_local_lib_override(project_name = @project_name)
|
28
|
+
rakefile = File.read(File.join(project_name, 'Rakefile'))
|
29
|
+
File.open(File.join(project_name, 'Rakefile'), "w+") do |f|
|
30
|
+
f << "$:.unshift('#{@lib_path}')\n"
|
31
|
+
f << rakefile
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def setup_active_project_folder project_name
|
36
|
+
@active_project_folder = File.join(@tmp_root, project_name)
|
37
|
+
@project_name = project_name
|
38
|
+
end
|
39
|
+
|
40
|
+
Given %r{'(.*)' folder is deleted} do |folder|
|
41
|
+
in_project_folder do
|
42
|
+
FileUtils.rm_rf folder
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
When %r{^'(.*)' generator is invoked with arguments '(.*)'$} do |generator, arguments|
|
47
|
+
@stdout = StringIO.new
|
48
|
+
FileUtils.chdir(@active_project_folder) do
|
49
|
+
if Object.const_defined?("APP_ROOT")
|
50
|
+
APP_ROOT.replace(FileUtils.pwd)
|
51
|
+
else
|
52
|
+
APP_ROOT = FileUtils.pwd
|
53
|
+
end
|
54
|
+
run_generator(generator, arguments.split(' '), SOURCES, :stdout => @stdout)
|
55
|
+
end
|
56
|
+
File.open(File.join(@tmp_root, "generator.out"), "w") do |f|
|
57
|
+
@stdout.rewind
|
58
|
+
f << @stdout.read
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
When %r{run executable '(.*)' with arguments '(.*)'} do |executable, arguments|
|
63
|
+
@stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
|
64
|
+
in_project_folder do
|
65
|
+
system "#{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
When %r{run project executable '(.*)' with arguments '(.*)'} do |executable, arguments|
|
70
|
+
@stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
|
71
|
+
in_project_folder do
|
72
|
+
system "ruby #{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
When %r{run local executable '(.*)' with arguments '(.*)'} do |executable, arguments|
|
77
|
+
@stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
|
78
|
+
executable = File.expand_path(File.join(File.dirname(__FILE__), "/../../bin", executable))
|
79
|
+
in_project_folder do
|
80
|
+
system "ruby #{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
When %r{^task 'rake (.*)' is invoked$} do |task|
|
85
|
+
@stdout = File.expand_path(File.join(@tmp_root, "tests.out"))
|
86
|
+
FileUtils.chdir(@active_project_folder) do
|
87
|
+
system "rake #{task} --trace > #{@stdout} 2> #{@stdout}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
Then %r{^folder '(.*)' (is|is not) created} do |folder, is|
|
92
|
+
in_project_folder do
|
93
|
+
File.exists?(folder).should(is == 'is' ? be_true : be_false)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
Then %r{^file '(.*)' (is|is not) created} do |file, is|
|
98
|
+
in_project_folder do
|
99
|
+
File.exists?(file).should(is == 'is' ? be_true : be_false)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
Then %r{^file with name matching '(.*)' is created} do |pattern|
|
104
|
+
in_project_folder do
|
105
|
+
Dir[pattern].should_not be_empty
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
Then %r{gem file '(.*)' and generated file '(.*)' should be the same} do |gem_file, project_file|
|
110
|
+
File.exists?(gem_file).should be_true
|
111
|
+
File.exists?(project_file).should be_true
|
112
|
+
gem_file_contents = File.read(File.dirname(__FILE__) + "/../../#{gem_file}")
|
113
|
+
project_file_contents = File.read(File.join(@active_project_folder, project_file))
|
114
|
+
project_file_contents.should == gem_file_contents
|
115
|
+
end
|
116
|
+
|
117
|
+
Then %r{^output same as contents of '(.*)'$} do |file|
|
118
|
+
expected_output = File.read(File.join(File.dirname(__FILE__) + "/../expected_outputs", file))
|
119
|
+
actual_output = File.read(@stdout)
|
120
|
+
actual_output.should == expected_output
|
121
|
+
end
|
122
|
+
|
123
|
+
Then %r{^(does|does not) invoke generator '(.*)'$} do |does_invoke, generator|
|
124
|
+
actual_output = File.read(@stdout)
|
125
|
+
does_invoke == "does" ?
|
126
|
+
actual_output.should(match(/dependency\s+#{generator}/)) :
|
127
|
+
actual_output.should_not(match(/dependency\s+#{generator}/))
|
128
|
+
end
|
129
|
+
|
130
|
+
Then %r{help options '(.*)' and '(.*)' are displayed} do |opt1, opt2|
|
131
|
+
actual_output = File.read(@stdout)
|
132
|
+
actual_output.should match(/#{opt1}/)
|
133
|
+
actual_output.should match(/#{opt2}/)
|
134
|
+
end
|
135
|
+
|
136
|
+
Then %r{^output (does|does not) match \/(.*)\/} do |does, regex|
|
137
|
+
actual_output = File.read(@stdout)
|
138
|
+
(does == 'does') ?
|
139
|
+
actual_output.should(match(/#{regex}/)) :
|
140
|
+
actual_output.should_not(match(/#{regex}/))
|
141
|
+
end
|
142
|
+
|
143
|
+
Then %r{^contents of file '(.*)' (does|does not) match \/(.*)\/} do |file, does, regex|
|
144
|
+
in_project_folder do
|
145
|
+
actual_output = File.read(file)
|
146
|
+
(does == 'does') ?
|
147
|
+
actual_output.should(match(/#{regex}/)) :
|
148
|
+
actual_output.should_not(match(/#{regex}/))
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
Then %r{^all (\d+) tests pass} do |expected_test_count|
|
153
|
+
expected = %r{^#{expected_test_count} tests, \d+ assertions, 0 failures, 0 errors}
|
154
|
+
actual_output = File.read(@stdout)
|
155
|
+
actual_output.should match(expected)
|
156
|
+
end
|
157
|
+
|
158
|
+
Then %r{^all (\d+) examples pass} do |expected_test_count|
|
159
|
+
expected = %r{^#{expected_test_count} examples?, 0 failures}
|
160
|
+
actual_output = File.read(@stdout)
|
161
|
+
actual_output.should match(expected)
|
162
|
+
end
|
163
|
+
|
164
|
+
Then %r{^yaml file '(.*)' contains (\{.*\})} do |file, yaml|
|
165
|
+
in_project_folder do
|
166
|
+
yaml = eval yaml
|
167
|
+
YAML.load(File.read(file)).should == yaml
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
Then %r{^Rakefile can display tasks successfully} do
|
172
|
+
@stdout = File.expand_path(File.join(@tmp_root, "rakefile.out"))
|
173
|
+
FileUtils.chdir(@active_project_folder) do
|
174
|
+
system "rake -T > #{@stdout} 2> #{@stdout}"
|
175
|
+
end
|
176
|
+
actual_output = File.read(@stdout)
|
177
|
+
actual_output.should match(/^rake\s+\w+\s+#\s.*/)
|
178
|
+
end
|
179
|
+
|
180
|
+
Then %r{^task 'rake (.*)' is executed successfully} do |task|
|
181
|
+
@stdout.should_not be_nil
|
182
|
+
actual_output = File.read(@stdout)
|
183
|
+
actual_output.should_not match(/^Don't know how to build task '#{task}'/)
|
184
|
+
actual_output.should_not match(/Error/i)
|
185
|
+
end
|
186
|
+
|
187
|
+
Then %r{^gem spec key '(.*)' contains \/(.*)\/} do |key, regex|
|
188
|
+
in_project_folder do
|
189
|
+
gem_file = Dir["pkg/*.gem"].first
|
190
|
+
gem_spec = Gem::Specification.from_yaml(`gem spec #{gem_file}`)
|
191
|
+
spec_value = gem_spec.send(key.to_sym)
|
192
|
+
spec_value.to_s.should match(/#{regex}/)
|
193
|
+
end
|
194
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
3
|
+
require 'active_record/connection_adapters/mysql_adapter'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module ConnectionAdapters
|
7
|
+
# The MysqlBulkProxy will collect changes to a Mysql table definition and execute them all in one
|
8
|
+
# SQL statement
|
9
|
+
class MysqlBulkProxy
|
10
|
+
def initialize(table_name, base)
|
11
|
+
@table_name = table_name
|
12
|
+
@base = base
|
13
|
+
@statements = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_column(table_name, column_name, type, options = {})
|
17
|
+
check_table_name(table_name)
|
18
|
+
alter_specification = "ADD #{@base.quote_column_name(column_name)} #{@base.type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
|
19
|
+
@base.add_column_options!(alter_specification, options)
|
20
|
+
add_stmt(alter_specification)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_index(table_name, column_name, options = {})
|
24
|
+
check_table_name(table_name)
|
25
|
+
column_names = Array(column_name)
|
26
|
+
index_name = @base.index_name(@table_name, :column => column_names)
|
27
|
+
|
28
|
+
if Hash === options # legacy support, since this param was a string
|
29
|
+
index_type = options[:unique] ? "UNIQUE" : ""
|
30
|
+
index_name = options[:name] || index_name
|
31
|
+
else
|
32
|
+
index_type = options
|
33
|
+
end
|
34
|
+
quoted_column_names = column_names.map { |e| @base.quote_column_name(e) }.join(", ")
|
35
|
+
add_stmt("ADD #{index_type} INDEX #{@base.quote_column_name(index_name)}(#{quoted_column_names})")
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_timestamps(table_name)
|
39
|
+
check_table_name(table_name)
|
40
|
+
add_column table_name, :created_at, :datetime
|
41
|
+
add_column table_name, :updated_at, :datetime
|
42
|
+
end
|
43
|
+
|
44
|
+
def remove_timestamps(table_name)
|
45
|
+
remove_column table_name, :updated_at
|
46
|
+
remove_column table_name, :created_at
|
47
|
+
end
|
48
|
+
|
49
|
+
def change_column(table_name, column_name, type, options = {})
|
50
|
+
check_table_name(table_name)
|
51
|
+
column = @base.column_for(@table_name, column_name)
|
52
|
+
|
53
|
+
unless @base.options_include_default?(options)
|
54
|
+
options[:default] = column.default
|
55
|
+
end
|
56
|
+
|
57
|
+
unless options.has_key?(:null)
|
58
|
+
options[:null] = column.null
|
59
|
+
end
|
60
|
+
|
61
|
+
change_column_sql = "CHANGE #{@base.quote_column_name(column_name)} #{@base.quote_column_name(column_name)} #{@base.type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
|
62
|
+
@base.add_column_options!(change_column_sql, options)
|
63
|
+
add_stmt(change_column_sql)
|
64
|
+
end
|
65
|
+
|
66
|
+
def change_column_default(table_name, column_name, default) #:nodoc:
|
67
|
+
check_table_name(table_name)
|
68
|
+
column = @base.column_for(@table_name, column_name)
|
69
|
+
change_column(@table_name, column_name, column.sql_type, :default => default)
|
70
|
+
end
|
71
|
+
|
72
|
+
def rename_column(table_name, column_name, new_column_name) #:nodoc:
|
73
|
+
check_table_name(table_name)
|
74
|
+
options = {}
|
75
|
+
if column = @base.columns(table_name).find { |c| c.name == column_name.to_s }
|
76
|
+
options[:default] = column.default
|
77
|
+
options[:null] = column.null
|
78
|
+
else
|
79
|
+
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
|
80
|
+
end
|
81
|
+
current_type = @base.select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
|
82
|
+
rename_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
|
83
|
+
@base.add_column_options!(rename_column_sql, options)
|
84
|
+
add_stmt(rename_column_sql)
|
85
|
+
end
|
86
|
+
|
87
|
+
def remove_column(table_name, *column_names)
|
88
|
+
add_stmts(column_names.flatten.map { |column_name| "DROP COLUMN #{@base.quote_column_name(column_name)}"})
|
89
|
+
end
|
90
|
+
alias_method :remove_columns, :remove_column
|
91
|
+
|
92
|
+
def remove_index(table_name, options = {})
|
93
|
+
check_table_name(table_name)
|
94
|
+
add_stmt("DROP INDEX #{@base.quote_column_name(@base.index_name(@table_name, options))}")
|
95
|
+
end
|
96
|
+
|
97
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
|
98
|
+
@base.type_to_sql(type, limit, precision, scale)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Commits the updates to the database and clears the cached statements
|
102
|
+
def update_database
|
103
|
+
unless @statements.empty?
|
104
|
+
alter_table_sql = "ALTER TABLE #{@base.quote_table_name(@table_name)}"
|
105
|
+
result = @base.execute(alter_table_sql + " " + @statements.join(', '))
|
106
|
+
@statements.clear
|
107
|
+
result
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def native
|
112
|
+
@base.native_database_types
|
113
|
+
end
|
114
|
+
alias_method :native_database_types, :native
|
115
|
+
|
116
|
+
private
|
117
|
+
def check_table_name(table_name)
|
118
|
+
raise "Tablename mismatch expected '#{@table_name}', but got '#{table_name}'" unless @table_name == table_name
|
119
|
+
end
|
120
|
+
|
121
|
+
def add_stmt(*stmt)
|
122
|
+
@statements += stmt.flatten
|
123
|
+
end
|
124
|
+
alias :add_stmts :add_stmt
|
125
|
+
|
126
|
+
def method_missing(symbol, *args)
|
127
|
+
@base.send symbol, *args
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
class MysqlAdapter < AbstractAdapter
|
133
|
+
# Changed to return a proxy for collecting the changes and do bulk updates
|
134
|
+
alias_method :change_table_direct, :change_table
|
135
|
+
def change_table(table_name)
|
136
|
+
bulk_updater = MysqlBulkProxy.new(table_name, self)
|
137
|
+
yield Table.new(table_name, bulk_updater)
|
138
|
+
bulk_updater.update_database
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/mysql_schema_bulk_change.rb'}"
|
9
|
+
puts "Loading mysql_schema_bulk_change gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe 'mysql_schema_bulk_change' do
|
4
|
+
def connection
|
5
|
+
ActiveRecord::Base.connection
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
ActiveRecord::Migration.verbose = true
|
10
|
+
begin
|
11
|
+
connection.tables.each do |table|
|
12
|
+
connection.drop_table(table)
|
13
|
+
end
|
14
|
+
rescue
|
15
|
+
end
|
16
|
+
connection.create_table :test_table do |t|
|
17
|
+
t.string :name
|
18
|
+
end
|
19
|
+
connection.add_index :test_table, :name
|
20
|
+
end
|
21
|
+
after(:each) do
|
22
|
+
begin
|
23
|
+
connection.tables.each do |table|
|
24
|
+
connection.drop_table(table)
|
25
|
+
end
|
26
|
+
rescue
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not call the connection adapter in the block" do
|
31
|
+
connection.should_not_receive(:add_column)
|
32
|
+
connection.should_not_receive(:add_index)
|
33
|
+
connection.should_receive(:execute).with("ALTER TABLE `test_table` ADD `author` varchar(255), ADD INDEX `index_test_table_on_author`(`author`), DROP INDEX `index_test_table_on_name`")
|
34
|
+
connection.change_table :test_table do |t|
|
35
|
+
t.string :author
|
36
|
+
t.index :author
|
37
|
+
t.remove_index :name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
gem 'rspec'
|
6
|
+
require 'spec'
|
7
|
+
end
|
8
|
+
require 'rubygems'
|
9
|
+
gem 'rails', '= 2.3.2'
|
10
|
+
require 'active_record'
|
11
|
+
|
12
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
13
|
+
|
14
|
+
# Setup database connection piggy back on rails' unit test database
|
15
|
+
|
16
|
+
require 'logger'
|
17
|
+
|
18
|
+
ActiveRecord::Base.logger = Logger.new("debug.log")
|
19
|
+
|
20
|
+
# GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
|
21
|
+
# GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
|
22
|
+
|
23
|
+
ActiveRecord::Base.configurations = {
|
24
|
+
'arunit' => {
|
25
|
+
:adapter => 'mysql',
|
26
|
+
:username => 'rails',
|
27
|
+
:encoding => 'utf8',
|
28
|
+
:database => 'activerecord_unittest',
|
29
|
+
}}
|
30
|
+
|
31
|
+
ActiveRecord::Base.establish_connection 'arunit'
|
32
|
+
|
33
|
+
require 'mysql_schema_bulk_change'
|
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kjeldahl-mysql_schema_bulk_change
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Kjeldahl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: newgem
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.3.0
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.8.0
|
44
|
+
version:
|
45
|
+
description: This extension to the MysqlAdapter in ActiveRecord enables bulk updates to schema definitions. Pr. default when calling connection#add_column the change will be executed a once, but Mysql allows for multiple changes to be executed in one SQL statement (http://dev.mysql.com/doc/refman/5.1/en/alter-table.html). The advantage of this is that it takes a lot less time, especially if the table is large.
|
46
|
+
email:
|
47
|
+
- jkj@lenio.dk
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- README.rdoc
|
56
|
+
files:
|
57
|
+
- History.txt
|
58
|
+
- Manifest.txt
|
59
|
+
- README.rdoc
|
60
|
+
- Rakefile
|
61
|
+
- features/development.feature
|
62
|
+
- features/step_definitions/common_steps.rb
|
63
|
+
- features/support/env.rb
|
64
|
+
- lib/mysql_schema_bulk_change.rb
|
65
|
+
- lib/mysql_schema_bulk_change/mysql_adapter.rb
|
66
|
+
- script/console
|
67
|
+
- script/destroy
|
68
|
+
- script/generate
|
69
|
+
- spec/mysql_schema_bulk_change_spec.rb
|
70
|
+
- spec/spec.opts
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- tasks/rspec.rake
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: http://github.com/jacob_kjeldahl/mysql_schema_bulk_change
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --main
|
78
|
+
- README.rdoc
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "0"
|
86
|
+
version:
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
version:
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: mysql_schema_bulk_change
|
96
|
+
rubygems_version: 1.2.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 2
|
99
|
+
summary: This extension to the MysqlAdapter in ActiveRecord enables bulk updates to schema definitions
|
100
|
+
test_files: []
|
101
|
+
|