baza_migrations 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +107 -0
- data/LICENSE.txt +20 -0
- data/README.md +23 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/baza_migrations.gemspec +81 -0
- data/lib/baza_migrations/commands/add_index.rb +45 -0
- data/lib/baza_migrations/commands/base.rb +3 -0
- data/lib/baza_migrations/commands/create_table.rb +25 -0
- data/lib/baza_migrations/commands/drop_table.rb +13 -0
- data/lib/baza_migrations/commands.rb +10 -0
- data/lib/baza_migrations/errors.rb +4 -0
- data/lib/baza_migrations/migration.rb +108 -0
- data/lib/baza_migrations.rb +7 -0
- data/lib/tasks/baza.rake +7 -0
- data/shippable.yml +10 -0
- data/spec/lib/baza_migrations/migration_spec.rb +62 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/test_classes/201503041644_test_change_migration.rb +10 -0
- data/spec/test_classes/201503041646_test_up_down_migration.rb +12 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4cb80ba9ce883bc9f98cd144768bd16c7449309f
|
4
|
+
data.tar.gz: b7658dd52e4fc68b73b2681df789db3cfbb09cfd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e776fec6e52acdda85c7be206c8d71fbf7638559830ae5a69634330dbfad9048c4ef84d746300dae61eb8e2a6a1e9a336d9a3c39f30e0b89e08161fe7c93b18
|
7
|
+
data.tar.gz: 3f2debcb6f02bd4ffde9446243f5acdcd0a76ca1bfd61cbc823453fb80eb5b48f403d8e04eb948e690a5ae1c790dab8fbd3dde9e08c559e98ad040cffa2cd928
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
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 "baza", "~> 0.0.14"
|
7
|
+
gem "string-cases", "~> 0.0.1"
|
8
|
+
|
9
|
+
# Add dependencies to develop your gem here.
|
10
|
+
# Include everything needed to run rake, tests, features, etc.
|
11
|
+
group :development do
|
12
|
+
gem "rspec", "~> 2.8.0"
|
13
|
+
gem "rdoc", "~> 3.12"
|
14
|
+
gem "bundler", "~> 1.0"
|
15
|
+
gem "jeweler", "~> 2.0.1"
|
16
|
+
gem "sqlite3"
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
gem "codeclimate-test-reporter", group: :test, require: nil
|
21
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.3.7)
|
5
|
+
array_enumerator (0.0.7)
|
6
|
+
baza (0.0.14)
|
7
|
+
array_enumerator
|
8
|
+
datet
|
9
|
+
knjrbfw
|
10
|
+
string-cases
|
11
|
+
wref
|
12
|
+
builder (3.2.2)
|
13
|
+
codeclimate-test-reporter (0.4.6)
|
14
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
15
|
+
datet (0.0.25)
|
16
|
+
descendants_tracker (0.0.4)
|
17
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
18
|
+
diff-lcs (1.1.3)
|
19
|
+
docile (1.1.5)
|
20
|
+
faraday (0.9.1)
|
21
|
+
multipart-post (>= 1.2, < 3)
|
22
|
+
git (1.2.9.1)
|
23
|
+
github_api (0.12.3)
|
24
|
+
addressable (~> 2.3)
|
25
|
+
descendants_tracker (~> 0.0.4)
|
26
|
+
faraday (~> 0.8, < 0.10)
|
27
|
+
hashie (>= 3.3)
|
28
|
+
multi_json (>= 1.7.5, < 2.0)
|
29
|
+
nokogiri (~> 1.6.3)
|
30
|
+
oauth2
|
31
|
+
hashie (3.4.0)
|
32
|
+
highline (1.7.1)
|
33
|
+
http2 (0.0.29)
|
34
|
+
string-cases
|
35
|
+
jeweler (2.0.1)
|
36
|
+
builder
|
37
|
+
bundler (>= 1.0)
|
38
|
+
git (>= 1.2.5)
|
39
|
+
github_api
|
40
|
+
highline (>= 1.6.15)
|
41
|
+
nokogiri (>= 1.5.10)
|
42
|
+
rake
|
43
|
+
rdoc
|
44
|
+
json (1.8.2)
|
45
|
+
jwt (1.3.0)
|
46
|
+
knjrbfw (0.0.110)
|
47
|
+
datet
|
48
|
+
http2
|
49
|
+
php4r
|
50
|
+
ruby_process
|
51
|
+
tsafe
|
52
|
+
wref
|
53
|
+
mini_portile (0.6.2)
|
54
|
+
multi_json (1.11.0)
|
55
|
+
multi_xml (0.5.5)
|
56
|
+
multipart-post (2.0.0)
|
57
|
+
nokogiri (1.6.6.2)
|
58
|
+
mini_portile (~> 0.6.0)
|
59
|
+
oauth2 (1.0.0)
|
60
|
+
faraday (>= 0.8, < 0.10)
|
61
|
+
jwt (~> 1.0)
|
62
|
+
multi_json (~> 1.3)
|
63
|
+
multi_xml (~> 0.5)
|
64
|
+
rack (~> 1.2)
|
65
|
+
php4r (0.0.4)
|
66
|
+
datet
|
67
|
+
http2
|
68
|
+
string-strtr
|
69
|
+
rack (1.6.0)
|
70
|
+
rake (10.4.2)
|
71
|
+
rdoc (3.12.2)
|
72
|
+
json (~> 1.4)
|
73
|
+
rspec (2.8.0)
|
74
|
+
rspec-core (~> 2.8.0)
|
75
|
+
rspec-expectations (~> 2.8.0)
|
76
|
+
rspec-mocks (~> 2.8.0)
|
77
|
+
rspec-core (2.8.0)
|
78
|
+
rspec-expectations (2.8.0)
|
79
|
+
diff-lcs (~> 1.1.2)
|
80
|
+
rspec-mocks (2.8.0)
|
81
|
+
ruby_process (0.0.9)
|
82
|
+
tsafe
|
83
|
+
wref
|
84
|
+
simplecov (0.9.2)
|
85
|
+
docile (~> 1.1.0)
|
86
|
+
multi_json (~> 1.0)
|
87
|
+
simplecov-html (~> 0.9.0)
|
88
|
+
simplecov-html (0.9.0)
|
89
|
+
sqlite3 (1.3.10)
|
90
|
+
string-cases (0.0.1)
|
91
|
+
string-strtr (0.0.3)
|
92
|
+
thread_safe (0.3.4)
|
93
|
+
tsafe (0.0.11)
|
94
|
+
wref (0.0.6)
|
95
|
+
|
96
|
+
PLATFORMS
|
97
|
+
ruby
|
98
|
+
|
99
|
+
DEPENDENCIES
|
100
|
+
baza (~> 0.0.14)
|
101
|
+
bundler (~> 1.0)
|
102
|
+
codeclimate-test-reporter
|
103
|
+
jeweler (~> 2.0.1)
|
104
|
+
rdoc (~> 3.12)
|
105
|
+
rspec (~> 2.8.0)
|
106
|
+
sqlite3
|
107
|
+
string-cases (~> 0.0.1)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 kaspernj
|
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.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
[![Build Status](https://api.shippable.com/projects/54f837035ab6cc135292f855/badge?branchName=master)](https://app.shippable.com/projects/54f837035ab6cc135292f855/builds/latest)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/kaspernj/baza_migrations/badges/gpa.svg)](https://codeclimate.com/github/kaspernj/baza_migrations)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/kaspernj/baza_migrations/badges/coverage.svg)](https://codeclimate.com/github/kaspernj/baza_migrations)
|
4
|
+
|
5
|
+
# baza_migrations
|
6
|
+
|
7
|
+
Migrations support for the Baza database framework in Ruby.
|
8
|
+
|
9
|
+
## Contributing to baza_migrations
|
10
|
+
|
11
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
12
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
13
|
+
* Fork the project.
|
14
|
+
* Start a feature/bugfix branch.
|
15
|
+
* Commit and push until you are happy with your contribution.
|
16
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
17
|
+
* 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.
|
18
|
+
|
19
|
+
## Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2015 kaspernj. See LICENSE.txt for
|
22
|
+
further details.
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
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://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "baza_migrations"
|
18
|
+
gem.homepage = "http://github.com/kaspernj/baza_migrations"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Migrations support for the Baza database framework in Ruby.}
|
21
|
+
gem.description = %Q{Migrations support for the Baza database framework in Ruby.}
|
22
|
+
gem.email = "k@spernj.org"
|
23
|
+
gem.authors = ["kaspernj"]
|
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
|
+
desc "Code coverage detail"
|
35
|
+
task :simplecov do
|
36
|
+
ENV['COVERAGE'] = "true"
|
37
|
+
Rake::Task['spec'].execute
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rdoc/task'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "baza_migrations #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,81 @@
|
|
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
|
+
# stub: baza_migrations 0.0.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "baza_migrations"
|
9
|
+
s.version = "0.0.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["kaspernj"]
|
14
|
+
s.date = "2015-04-18"
|
15
|
+
s.description = "Migrations support for the Baza database framework in Ruby."
|
16
|
+
s.email = "k@spernj.org"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"baza_migrations.gemspec",
|
31
|
+
"lib/baza_migrations.rb",
|
32
|
+
"lib/baza_migrations/commands.rb",
|
33
|
+
"lib/baza_migrations/commands/add_index.rb",
|
34
|
+
"lib/baza_migrations/commands/base.rb",
|
35
|
+
"lib/baza_migrations/commands/create_table.rb",
|
36
|
+
"lib/baza_migrations/commands/drop_table.rb",
|
37
|
+
"lib/baza_migrations/errors.rb",
|
38
|
+
"lib/baza_migrations/migration.rb",
|
39
|
+
"lib/tasks/baza.rake",
|
40
|
+
"shippable.yml",
|
41
|
+
"spec/lib/baza_migrations/migration_spec.rb",
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/test_classes/201503041644_test_change_migration.rb",
|
44
|
+
"spec/test_classes/201503041646_test_up_down_migration.rb"
|
45
|
+
]
|
46
|
+
s.homepage = "http://github.com/kaspernj/baza_migrations"
|
47
|
+
s.licenses = ["MIT"]
|
48
|
+
s.rubygems_version = "2.4.0"
|
49
|
+
s.summary = "Migrations support for the Baza database framework in Ruby."
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
s.specification_version = 4
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<baza>, ["~> 0.0.14"])
|
56
|
+
s.add_runtime_dependency(%q<string-cases>, ["~> 0.0.1"])
|
57
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
58
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
59
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
60
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
61
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<baza>, ["~> 0.0.14"])
|
64
|
+
s.add_dependency(%q<string-cases>, ["~> 0.0.1"])
|
65
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
66
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
67
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
68
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
69
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<baza>, ["~> 0.0.14"])
|
73
|
+
s.add_dependency(%q<string-cases>, ["~> 0.0.1"])
|
74
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
75
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
76
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
77
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
78
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class BazaMigrations::Commands::AddIndex < BazaMigrations::Commands::Base
|
2
|
+
def initialize(table_name, columns)
|
3
|
+
@table_name = table_name
|
4
|
+
@columns = columns
|
5
|
+
end
|
6
|
+
|
7
|
+
def sql
|
8
|
+
sql = "CREATE INDEX `#{@db.esc_col(index_name)}` ON `#{@db.esc_table(@table_name)}` ("
|
9
|
+
|
10
|
+
first = true
|
11
|
+
columns_as_array.each do |column|
|
12
|
+
sql << ", " unless first
|
13
|
+
first = false if first
|
14
|
+
sql << "`#{column}`"
|
15
|
+
end
|
16
|
+
|
17
|
+
sql << ")"
|
18
|
+
|
19
|
+
[sql]
|
20
|
+
end
|
21
|
+
|
22
|
+
def changed_rollback_sql
|
23
|
+
["DROP INDEX IF EXISTS `#{@db.esc_col(index_name)}`"]
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def columns_as_array
|
29
|
+
return @columns if @columns.is_a?(Array)
|
30
|
+
return [@columns]
|
31
|
+
end
|
32
|
+
|
33
|
+
def index_name
|
34
|
+
name = "index_#{@table_name}_on_"
|
35
|
+
|
36
|
+
first = true
|
37
|
+
columns_as_array.each do |column|
|
38
|
+
name << "_and_" unless first
|
39
|
+
first = false if first
|
40
|
+
name << column.to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
return name
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class BazaMigrations::Commands::CreateTable < BazaMigrations::Commands::Base
|
2
|
+
def initialize(name)
|
3
|
+
@name = name
|
4
|
+
|
5
|
+
@columns = []
|
6
|
+
@columns << {name: :id, type: :int, autoincr: true, primarykey: true}
|
7
|
+
end
|
8
|
+
|
9
|
+
def string(name)
|
10
|
+
@columns << {name: name, type: :string, null: true}
|
11
|
+
end
|
12
|
+
|
13
|
+
def timestamps
|
14
|
+
@columns << {name: :created_at, type: :datetime, null: true}
|
15
|
+
@columns << {name: :updated_at, type: :datetime, null: true}
|
16
|
+
end
|
17
|
+
|
18
|
+
def sql
|
19
|
+
db.tables.create(@name, {columns: @columns}, {return_sql: true})
|
20
|
+
end
|
21
|
+
|
22
|
+
def changed_rollback_sql
|
23
|
+
["DROP TABLE `#{@name}`"]
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class BazaMigrations::Commands::DropTable < BazaMigrations::Commands::Base
|
2
|
+
def initialize(name)
|
3
|
+
@name = name
|
4
|
+
end
|
5
|
+
|
6
|
+
def sql
|
7
|
+
["DROP TABLE `#{@name}`"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def changed_rollback_sql
|
11
|
+
raise BazaMigrations::Errors::IrreversibleMigration
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class BazaMigrations::Commands
|
2
|
+
path = "#{File.dirname(__FILE__)}/commands"
|
3
|
+
|
4
|
+
autoload :AddIndex, "#{path}/add_index"
|
5
|
+
autoload :Base, "#{path}/base"
|
6
|
+
autoload :CreateTable, "#{path}/create_table"
|
7
|
+
autoload :DropTable, "#{path}/drop_table"
|
8
|
+
autoload :AddColumn, "#{path}/add_column"
|
9
|
+
autoload :RemoveColumn, "#{path}/remove_column"
|
10
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
class BazaMigrations::Migration
|
2
|
+
def initialize(args = {})
|
3
|
+
@db = args[:db]
|
4
|
+
raise "No database was given" unless @db
|
5
|
+
|
6
|
+
@changes = []
|
7
|
+
check_schema_migrations_table
|
8
|
+
end
|
9
|
+
|
10
|
+
def change
|
11
|
+
raise BazaMigrations::Errors::NotImplementedError
|
12
|
+
end
|
13
|
+
|
14
|
+
def up
|
15
|
+
raise BazaMigrations::Errors::NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
def down
|
19
|
+
raise BazaMigrations::Errors::NotImplementedError
|
20
|
+
end
|
21
|
+
|
22
|
+
def migrate(direction)
|
23
|
+
if direction == :up
|
24
|
+
begin
|
25
|
+
up
|
26
|
+
execute_changes
|
27
|
+
rescue BazaMigrations::Errors::NotImplementedError
|
28
|
+
change
|
29
|
+
execute_changes
|
30
|
+
end
|
31
|
+
elsif direction == :down
|
32
|
+
begin
|
33
|
+
down
|
34
|
+
execute_changes
|
35
|
+
rescue BazaMigrations::Errors::NotImplementedError
|
36
|
+
change
|
37
|
+
rollback_changed_changes
|
38
|
+
end
|
39
|
+
else
|
40
|
+
raise "Invalid direction: #{direction}"
|
41
|
+
end
|
42
|
+
|
43
|
+
@changes = []
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def create_table(name)
|
49
|
+
command = new_command(:CreateTable, name)
|
50
|
+
@changes << command
|
51
|
+
|
52
|
+
yield command
|
53
|
+
end
|
54
|
+
|
55
|
+
def drop_table(name)
|
56
|
+
@changes << new_command(:DropTable, name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_index(name, columns)
|
60
|
+
@changes << new_command(:AddIndex, name, columns)
|
61
|
+
end
|
62
|
+
|
63
|
+
def remove_column(table_name, column_name)
|
64
|
+
@changes << new_command(:RemoveColumn, table_name, column_name)
|
65
|
+
end
|
66
|
+
|
67
|
+
def add_column(table_name, column_name, type, args = {})
|
68
|
+
@changes << new_command(:AddColumn, table_name, column_name, type, args)
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def execute_changes
|
74
|
+
@changes.each do |change|
|
75
|
+
change.sql.each do |sql|
|
76
|
+
@db.q(sql)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def rollback_changed_changes
|
82
|
+
@changes.each do |change|
|
83
|
+
change.changed_rollback_sql.each do |sql|
|
84
|
+
@db.q(sql)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def new_command(type, *args)
|
90
|
+
command = BazaMigrations::Commands.const_get(type, args).new(*args)
|
91
|
+
command.db = @db
|
92
|
+
command.table = @schema_migrations_table
|
93
|
+
|
94
|
+
return command
|
95
|
+
end
|
96
|
+
|
97
|
+
def check_schema_migrations_table
|
98
|
+
begin
|
99
|
+
@schema_migrations_table = @db.tables[:schema_migrations]
|
100
|
+
rescue Errno::ENOENT
|
101
|
+
@db.tables.create(:schema_migrations,
|
102
|
+
columns: [{name: :version, type: :varchar}],
|
103
|
+
indexes: [:version]
|
104
|
+
)
|
105
|
+
@schema_migrations_table = @db.tables[:schema_migrations]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require "baza"
|
2
|
+
|
3
|
+
class BazaMigrations
|
4
|
+
autoload :Commands, "#{File.dirname(__FILE__)}/baza_migrations/commands"
|
5
|
+
autoload :Errors, "#{File.dirname(__FILE__)}/baza_migrations/errors"
|
6
|
+
autoload :Migration, "#{File.dirname(__FILE__)}/baza_migrations/migration"
|
7
|
+
end
|
data/lib/tasks/baza.rake
ADDED
data/shippable.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe BazaMigrations::Migration do
|
4
|
+
let(:db) do
|
5
|
+
file_path = "#{Dir.tmpdir}/baza_migrations.sqlite3"
|
6
|
+
File.unlink(file_path) if File.exists?(file_path)
|
7
|
+
Baza::Db.new(type: :sqlite3, path: file_path)
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:change_migration) do
|
11
|
+
require "#{File.dirname(__FILE__)}/../../test_classes/201503041644_test_change_migration.rb"
|
12
|
+
TestChangeMigration.new(db: db)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:up_down_migration) do
|
16
|
+
require "#{File.dirname(__FILE__)}/../../test_classes/201503041646_test_up_down_migration.rb"
|
17
|
+
TestUpDownMigration.new(db: db)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "#check_schema_migrations_table" do
|
21
|
+
change_migration
|
22
|
+
db.tables[:schema_migrations].name.should eq :schema_migrations
|
23
|
+
end
|
24
|
+
|
25
|
+
it "#up" do
|
26
|
+
up_down_migration.migrate(:up)
|
27
|
+
|
28
|
+
table = db.tables[:test_table]
|
29
|
+
table.name.should eq :test_table
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#down" do
|
33
|
+
up_down_migration.migrate(:up)
|
34
|
+
|
35
|
+
table = db.tables[:test_table]
|
36
|
+
table.name.should eq :test_table
|
37
|
+
|
38
|
+
up_down_migration.migrate(:down)
|
39
|
+
db.tables.instance_variable_set(:@list, Wref_map.new) # Resets cache
|
40
|
+
|
41
|
+
expect { db.tables[:test_table] }.to raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#change" do
|
45
|
+
it "migrates" do
|
46
|
+
change_migration.migrate(:up)
|
47
|
+
table = db.tables[:table]
|
48
|
+
table.name.should eq :table
|
49
|
+
end
|
50
|
+
|
51
|
+
it "rolls back" do
|
52
|
+
change_migration.migrate(:up)
|
53
|
+
table = db.tables[:table]
|
54
|
+
table.name.should eq :table
|
55
|
+
|
56
|
+
change_migration.migrate(:down)
|
57
|
+
db.tables.instance_variable_set(:@list, Wref_map.new) # Resets cache
|
58
|
+
|
59
|
+
expect { db.tables[:table] }.to raise_error
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require "rspec"
|
8
|
+
require "baza_migrations"
|
9
|
+
require "tmpdir"
|
10
|
+
require "sqlite3"
|
11
|
+
|
12
|
+
# Requires supporting files with custom matchers and macros, etc,
|
13
|
+
# in ./support/ and its subdirectories.
|
14
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baza_migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kaspernj
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: baza
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.14
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.14
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: string-cases
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.8.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rdoc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jeweler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.0.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.0.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Migrations support for the Baza database framework in Ruby.
|
112
|
+
email: k@spernj.org
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- LICENSE.txt
|
117
|
+
- README.md
|
118
|
+
files:
|
119
|
+
- ".document"
|
120
|
+
- ".rspec"
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- VERSION
|
127
|
+
- baza_migrations.gemspec
|
128
|
+
- lib/baza_migrations.rb
|
129
|
+
- lib/baza_migrations/commands.rb
|
130
|
+
- lib/baza_migrations/commands/add_index.rb
|
131
|
+
- lib/baza_migrations/commands/base.rb
|
132
|
+
- lib/baza_migrations/commands/create_table.rb
|
133
|
+
- lib/baza_migrations/commands/drop_table.rb
|
134
|
+
- lib/baza_migrations/errors.rb
|
135
|
+
- lib/baza_migrations/migration.rb
|
136
|
+
- lib/tasks/baza.rake
|
137
|
+
- shippable.yml
|
138
|
+
- spec/lib/baza_migrations/migration_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/test_classes/201503041644_test_change_migration.rb
|
141
|
+
- spec/test_classes/201503041646_test_up_down_migration.rb
|
142
|
+
homepage: http://github.com/kaspernj/baza_migrations
|
143
|
+
licenses:
|
144
|
+
- MIT
|
145
|
+
metadata: {}
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 2.4.0
|
163
|
+
signing_key:
|
164
|
+
specification_version: 4
|
165
|
+
summary: Migrations support for the Baza database framework in Ruby.
|
166
|
+
test_files: []
|