knjdbrevision 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +20 -0
- data/README +1 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/bin/run_with_schema.rb +46 -0
- data/knjdbrevision.gemspec +67 -0
- data/lib/knjdbrevision.rb +215 -0
- data/spec/knjdbrevision_spec.rb +59 -0
- data/spec/spec_helper.rb +12 -0
- metadata +133 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "knjrbfw", ">= 0.0.3"
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "rspec", "~> 2.3.0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.6.3"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
gem "sqlite3"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.3)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
knjrbfw (0.0.3)
|
11
|
+
rake (0.9.2)
|
12
|
+
rcov (0.9.9)
|
13
|
+
rspec (2.3.0)
|
14
|
+
rspec-core (~> 2.3.0)
|
15
|
+
rspec-expectations (~> 2.3.0)
|
16
|
+
rspec-mocks (~> 2.3.0)
|
17
|
+
rspec-core (2.3.1)
|
18
|
+
rspec-expectations (2.3.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.3.0)
|
21
|
+
sqlite3 (1.3.3)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (~> 1.0.0)
|
28
|
+
jeweler (~> 1.6.3)
|
29
|
+
knjrbfw (>= 0.0.3)
|
30
|
+
rcov
|
31
|
+
rspec (~> 2.3.0)
|
32
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Kasper Johansen
|
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
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This is an application for automatically managing database on multiple machines
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= knjdbrevision
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to knjdbrevision
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Kasper Johansen. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "knjdbrevision"
|
18
|
+
gem.homepage = "http://github.com/kaspernj/knjdbrevision"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A framework that can do database revision.}
|
21
|
+
gem.description = %Q{Can create/alter tables, columns, indexes and even table rows. Supports callbacks when certain events are triggered like an on_created.}
|
22
|
+
gem.email = "k@spernj.org"
|
23
|
+
gem.authors = ["Kasper Johansen"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "knjdbrevision #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "knjdbrevision"
|
5
|
+
require "knj/autoload"
|
6
|
+
Knj::Os.chdir_file(__FILE__)
|
7
|
+
|
8
|
+
begin
|
9
|
+
options = {
|
10
|
+
:dbargs => {
|
11
|
+
:return_keys => "symbols"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: example.rb [options]"
|
17
|
+
|
18
|
+
opts.on("-t DBTYPE", "--type DBTYPE", "This database type.") do |t|
|
19
|
+
options[:type] = t
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on("-d DBKEY=DBVALUE", "--dbarg DBKEY=DKVALUE", "Sets another database config key and value.") do |val|
|
23
|
+
split_data = val.split("=")
|
24
|
+
options[:dbargs][split_data.first.to_sym] = split_data.last
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-s FILEPATH", "A file with JSON content which contains the schema.") do |s|
|
28
|
+
options[:schema_file] = s
|
29
|
+
options[:schema] = JSON.parse(File.read(s))
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on("-r FILEPATH", "A Ruby file which sets the global variable $tables which contains the schema.") do |val|
|
33
|
+
options[:rb_schema_file] = val
|
34
|
+
require val
|
35
|
+
options[:schema] = $tables
|
36
|
+
end
|
37
|
+
end.parse!
|
38
|
+
rescue OptionParser::InvalidOption => e
|
39
|
+
print "#{e.message}\n"
|
40
|
+
exit
|
41
|
+
end
|
42
|
+
|
43
|
+
dbrev.init_db(
|
44
|
+
options[:schema],
|
45
|
+
Knjdbrevision.new
|
46
|
+
)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{knjdbrevision}
|
8
|
+
s.version = ""
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kasper Johansen"]
|
12
|
+
s.date = %q{2011-07-02}
|
13
|
+
s.default_executable = %q{run_with_schema.rb}
|
14
|
+
s.description = %q{Can create/alter tables, columns, indexes and even table rows. Supports callbacks when certain events are triggered like an on_created.}
|
15
|
+
s.email = %q{k@spernj.org}
|
16
|
+
s.executables = ["run_with_schema.rb"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README",
|
20
|
+
"README.rdoc"
|
21
|
+
]
|
22
|
+
s.files = [
|
23
|
+
".document",
|
24
|
+
".rspec",
|
25
|
+
"Gemfile",
|
26
|
+
"Gemfile.lock",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README",
|
29
|
+
"README.rdoc",
|
30
|
+
"Rakefile",
|
31
|
+
"bin/run_with_schema.rb",
|
32
|
+
"knjdbrevision.rb",
|
33
|
+
"lib/knjdbrevision.rb",
|
34
|
+
"spec/knjdbrevision_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/kaspernj/knjdbrevision}
|
38
|
+
s.licenses = ["MIT"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.6.2}
|
41
|
+
s.summary = %q{A framework that can do database revision.}
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
48
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
49
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.3"])
|
50
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.3"])
|
56
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
57
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.3"])
|
63
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
64
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,215 @@
|
|
1
|
+
class Knjdbrevision
|
2
|
+
def initialize(args = {})
|
3
|
+
@args = args
|
4
|
+
end
|
5
|
+
|
6
|
+
def init_db(schema, db)
|
7
|
+
schema["tables"].each do |table_name, table_data|
|
8
|
+
begin
|
9
|
+
begin
|
10
|
+
table_obj = db.tables[table_name.to_sym]
|
11
|
+
|
12
|
+
if table_data["columns"]
|
13
|
+
table_data["columns"].each do |col_data|
|
14
|
+
begin
|
15
|
+
col_obj = table_obj.column(col_data["name"])
|
16
|
+
col_str = "#{table_name}.#{col_obj.name}"
|
17
|
+
type = col_data["type"].to_s
|
18
|
+
|
19
|
+
#BUGFIX: When using SQLite3 the primary-column or a autoincr-column may never change type from int... This will break it!
|
20
|
+
if db.opts[:type] == "sqlite3" and col_obj.type.to_s == "int" and (col_data["primarykey"] or col_data["autoincr"]) and db.int_types.index(col_data["type"].to_s)
|
21
|
+
type = "int"
|
22
|
+
end
|
23
|
+
|
24
|
+
dochange = false
|
25
|
+
|
26
|
+
if type and col_obj.type.to_s != type
|
27
|
+
print "Type mismatch on #{col_str}: #{col_data["type"]}, #{col_obj.type}\n"
|
28
|
+
dochange = true
|
29
|
+
end
|
30
|
+
|
31
|
+
if col_data.has_key?("maxlength") and col_obj.maxlength.to_s != col_data["maxlength"].to_s
|
32
|
+
print "Maxlength mismatch on #{col_str}: #{col_data["maxlength"]}, #{col_obj.maxlength}\n"
|
33
|
+
dochange = true
|
34
|
+
end
|
35
|
+
|
36
|
+
if col_data.has_key?("null") and col_obj.null?.to_s != col_data["null"].to_s
|
37
|
+
print "Null mismatch on #{col_str}: #{col_data["null"]}, #{col_obj.null?}\n"
|
38
|
+
dochange = true
|
39
|
+
end
|
40
|
+
|
41
|
+
if col_data.has_key?("default") and col_obj.default.to_s != col_data["default"].to_s
|
42
|
+
print "Default mismatch on #{col_str}: #{col_data["default"]}, #{col_obj.default}\n"
|
43
|
+
dochange = true
|
44
|
+
end
|
45
|
+
|
46
|
+
if col_data.has_key?("comment") and col_obj.comment.to_s != col_data["comment"].to_s
|
47
|
+
print "Comment mismatch on #{col_str}: #{col_data["comment"]}, #{col_obj.comment}\n"
|
48
|
+
dochange = true
|
49
|
+
end
|
50
|
+
|
51
|
+
if col_data.is_a?(Hash) and col_data["on_before_alter"]
|
52
|
+
callback_data = col_data["on_before_alter"].call("db" => db, "table" => table_obj, "col" => col_obj, "col_data" => col_data)
|
53
|
+
if callback_data and callback_data["action"]
|
54
|
+
if callback_data["action"] == "retry"
|
55
|
+
raise Knj::Errors::Retry
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
col_obj.change(col_data) if dochange
|
61
|
+
rescue Knj::Errors::NotFound => e
|
62
|
+
print "Column not found: #{table_obj.name}.#{col_data["name"]}.\n"
|
63
|
+
|
64
|
+
if col_data.has_key?("renames")
|
65
|
+
rename_found = false
|
66
|
+
col_data["renames"].each do |col_name|
|
67
|
+
begin
|
68
|
+
col_rename = table_obj.column(col_name)
|
69
|
+
rescue Knj::Errors::NotFound => e
|
70
|
+
next
|
71
|
+
end
|
72
|
+
|
73
|
+
print "Rename #{table_obj.name}.#{col_name} to #{table_obj.name}.#{col_data["name"]}\n"
|
74
|
+
if col_data.is_a?(Hash) and col_data["on_before_rename"]
|
75
|
+
col_data["on_before_rename"].call("db" => db, "table" => table_obj, "col" => col_rename, "col_data" => col_data)
|
76
|
+
end
|
77
|
+
|
78
|
+
col_rename.change(col_data)
|
79
|
+
|
80
|
+
if col_data.is_a?(Hash) and col_data["on_after_rename"]
|
81
|
+
col_data["on_after_rename"].call("db" => db, "table" => table_obj, "col" => col_rename, "col_data" => col_data)
|
82
|
+
end
|
83
|
+
|
84
|
+
rename_found = true
|
85
|
+
break
|
86
|
+
end
|
87
|
+
|
88
|
+
retry if rename_found
|
89
|
+
end
|
90
|
+
|
91
|
+
#Try to find out the previous column - if so we can set "after" which makes the column being created in the right order as defined.
|
92
|
+
if !col_data.has_key?("after")
|
93
|
+
prev_no = table_data["columns"].index(col_data)
|
94
|
+
if prev_no != nil and prev_no != 0
|
95
|
+
prev_no = prev_no - 1
|
96
|
+
prev_col_data = table_data["columns"][prev_no]
|
97
|
+
col_data["after"] = prev_col_data["name"]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
oncreated = col_data["on_created"]
|
102
|
+
col_data.delete("on_created") if col_data["oncreated"]
|
103
|
+
col_obj = table_obj.create_columns([col_data])
|
104
|
+
oncreated.call("db" => db, "table" => table_obj) if oncreated
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
if table_data["columns_remove"]
|
110
|
+
table_data["columns_remove"].each do |column_name, column_data|
|
111
|
+
begin
|
112
|
+
col_obj = table_obj.column(column_name)
|
113
|
+
rescue Knj::Errors::NotFound => e
|
114
|
+
next
|
115
|
+
end
|
116
|
+
|
117
|
+
column_data["callback"].call if column_data.is_a?(Hash) and column_data["callback"]
|
118
|
+
col_obj.drop
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
if table_data["indexes"]
|
123
|
+
table_data["indexes"].each do |index_data|
|
124
|
+
begin
|
125
|
+
index_obj = table_obj.index(index_data["name"])
|
126
|
+
rescue Knj::Errors::NotFound => e
|
127
|
+
table_obj.create_indexes([index_data])
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
if table_data["indexes_remove"]
|
133
|
+
table_data["indexes_remove"].each do |index_name, index_data|
|
134
|
+
begin
|
135
|
+
index_obj = table_obj.index(index_name)
|
136
|
+
rescue Knj::Errors::NotFound => e
|
137
|
+
next
|
138
|
+
end
|
139
|
+
|
140
|
+
if index_data.is_a?(Hash) and index_data["callback"]
|
141
|
+
index_data["callback"].call if index_data["callback"]
|
142
|
+
end
|
143
|
+
|
144
|
+
index_obj.drop
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
if table_data["rows"]
|
149
|
+
table_data["rows"].each do |row_data|
|
150
|
+
if row_data["find_by"]
|
151
|
+
find_by = row_data["find_by"]
|
152
|
+
elsif row_data["data"]
|
153
|
+
find_by = row_data["data"]
|
154
|
+
else
|
155
|
+
raise "Could not figure out the find-by."
|
156
|
+
end
|
157
|
+
|
158
|
+
rows_found = 0
|
159
|
+
q_rows = db.select(table_name, find_by)
|
160
|
+
while d_rows = q_rows.fetch
|
161
|
+
rows_found += 1
|
162
|
+
|
163
|
+
if Knj::ArrayExt.hash_diff?(Knj::ArrayExt.hash_sym(row_data["data"]), Knj::ArrayExt.hash_sym(d_rows), {"h2_to_h1" => false})
|
164
|
+
print "Data was not right - updating row: #{JSON.generate(row_data["data"])}\n"
|
165
|
+
db.update(table_name, row_data["data"], d_rows)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
if rows_found == 0
|
170
|
+
print "Inserting row: #{JSON.generate(row_data["data"])}\n"
|
171
|
+
db.insert(table_name, row_data["data"])
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
rescue Knj::Errors::NotFound => e
|
176
|
+
if table_data["renames"]
|
177
|
+
table_data["renames"].each do |table_name_rename|
|
178
|
+
begin
|
179
|
+
table_rename = db.tables[table_name_rename]
|
180
|
+
table_rename.rename(table_name)
|
181
|
+
raise Knj::Errors::Retry
|
182
|
+
rescue Knj::Errors::NotFound
|
183
|
+
next
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
if table_data["on_create"]
|
189
|
+
table_data["on_create"].call("db" => db, "table_name" => table_name, "table_data" => table_data)
|
190
|
+
end
|
191
|
+
|
192
|
+
table_obj = db.tables.create(table_name, table_data)
|
193
|
+
|
194
|
+
if table_data["on_create_after"]
|
195
|
+
table_data["on_create_after"].call("db" => db, "table_name" => table_name, "table_data" => table_data)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
rescue Knj::Errors::Retry
|
199
|
+
retry
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
if schema["tables_remove"]
|
204
|
+
schema["tables_remove"].each do |table_name, table_data|
|
205
|
+
begin
|
206
|
+
table_obj = db.tables[table_name.to_sym]
|
207
|
+
table_data["callback"].call if table_data.is_a?(Hash) and table_data["callback"]
|
208
|
+
table_obj.drop
|
209
|
+
rescue Knj::Errors::NotFound => e
|
210
|
+
next
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Knjdbrevision" do
|
4
|
+
it "should create a SQLite3 database with a table who is a bigint with primarykey and autoincr. It should then convert it to a normal integer and not try to change it afterwards." do
|
5
|
+
require "knjdbrevision"
|
6
|
+
require "knjrbfw"
|
7
|
+
require "knj/autoload"
|
8
|
+
require "tmpdir"
|
9
|
+
|
10
|
+
db_path = "#{Dir.tmpdir}/knjdbrevision_test.sqlite3"
|
11
|
+
|
12
|
+
begin
|
13
|
+
db = Knj::Db.new(
|
14
|
+
:type => "sqlite3",
|
15
|
+
:return_keys => "symbols",
|
16
|
+
:index_append_table_name => true,
|
17
|
+
:path => db_path
|
18
|
+
)
|
19
|
+
|
20
|
+
schema = {
|
21
|
+
"tables" => {
|
22
|
+
"test_table" => {
|
23
|
+
"columns" => [
|
24
|
+
{"name" => "id", "type" => "bigint", "autoincr" => true, "primarykey" => true},
|
25
|
+
{"name" => "name", "type" => "varchar"}
|
26
|
+
]
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
dbrev = Knjdbrevision.new
|
32
|
+
dbrev.init_db(schema, db)
|
33
|
+
dbrev.init_db(schema, db)
|
34
|
+
|
35
|
+
db.close
|
36
|
+
db = Knj::Db.new(
|
37
|
+
:type => "sqlite3",
|
38
|
+
:return_keys => "symbols",
|
39
|
+
:index_append_table_name => true,
|
40
|
+
:path => db_path
|
41
|
+
)
|
42
|
+
|
43
|
+
#Test autoincr is still working (if primarykey 'id' was converted to a bigint then it will not... knjdbrevision should not convert the primarykey to bigint on SQLite3... Specific bugfix..
|
44
|
+
if db.tables["test_table"].column("id").type != "int"
|
45
|
+
raise "The primary ID was not of the 'int'-type."
|
46
|
+
end
|
47
|
+
|
48
|
+
db.insert("test_table", {"name" => "Kasper"})
|
49
|
+
db.q("SELECT * FROM test_table") do |d|
|
50
|
+
if d[:id].to_i != 1
|
51
|
+
Knj::Php.print_r(d)
|
52
|
+
raise "The ID was not 1 on the inserted row: '#{d[:id]}'."
|
53
|
+
end
|
54
|
+
end
|
55
|
+
ensure
|
56
|
+
File.unlink(db_path) if File.exists?(db_path)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'knjdbrevision'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knjdbrevision
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kasper Johansen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-07-28 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: knjrbfw
|
16
|
+
requirement: &21972080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *21972080
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &21948600 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.3.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *21948600
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &21946720 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *21946720
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &21945280 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.3
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *21945280
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &21942920 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *21942920
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: &21919620 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *21919620
|
80
|
+
description: Can create/alter tables, columns, indexes and even table rows. Supports
|
81
|
+
callbacks when certain events are triggered like an on_created.
|
82
|
+
email: k@spernj.org
|
83
|
+
executables:
|
84
|
+
- run_with_schema.rb
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files:
|
87
|
+
- LICENSE.txt
|
88
|
+
- README
|
89
|
+
- README.rdoc
|
90
|
+
files:
|
91
|
+
- .document
|
92
|
+
- .rspec
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README
|
97
|
+
- README.rdoc
|
98
|
+
- Rakefile
|
99
|
+
- VERSION
|
100
|
+
- bin/run_with_schema.rb
|
101
|
+
- knjdbrevision.gemspec
|
102
|
+
- lib/knjdbrevision.rb
|
103
|
+
- spec/knjdbrevision_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: http://github.com/kaspernj/knjdbrevision
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: 3882259143430684128
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 1.8.6
|
130
|
+
signing_key:
|
131
|
+
specification_version: 3
|
132
|
+
summary: A framework that can do database revision.
|
133
|
+
test_files: []
|