sequel-auto_migration 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTcwOGU5NDFiNjg1MjY3YTQwZGI5MzU2MzFkNzIzNjFjNTRmZjNiMw==
5
+ data.tar.gz: !binary |-
6
+ ZWJkMjgyMzZjNjQ1NzlkZDY3MjIyM2Y1ZjI1ZjU3OWI2NTZiNmQ4ZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDk5YmU0MTFmYjYxNWYxZjQ4MzBhOWIwZGM3ZjNkYjUwYmUxOGJiNjA5YzQ1
10
+ MWRmNDU0ZTc4ZjMzMTVmOGNkNGQ5M2M0ZDJhOWI3Y2FmMWFkODllNTVkMjM3
11
+ MjQ2NmM3MDIzMjBiMDAxYjNlMjlmNmJiZWQ5MDAxNDk5ZmZjMDY=
12
+ data.tar.gz: !binary |-
13
+ NWVjY2Y3ZTU4ZDUyOTVlMWEzNjhiMWZkMTg1M2Q3MjM1YjM2Njc1NDE2ODFh
14
+ MDc2ZDBhZjlkY2UzYzQ3YjliYmRhNTljNGRlYTIzNzU3ZDllZTMzZmFkMzg1
15
+ ZTY0Y2UzZDIwYjI2YjVmZDQyOTdlMzE0NmI3ZTc1YzZhZjk5OTI=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rbx
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ujifgc
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,6 @@
1
+ [![Build Status](https://travis-ci.org/ujifgc/sequel-auto_migration.png)](https://travis-ci.org/ujifgc/sequel-auto_migration)
2
+
3
+ This gem provides auto migration mechanism to Sequel database toolkit.
4
+
5
+ Note that the automatic migration is limited to only adding new columns to
6
+ existing database tables. It does not provide removing or changing type of the columns.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
6
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module Sequel
2
+ module AutoMigrate
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ # This extension is designed to automatically upgrade database schema to
2
+ # correspond to the model definition. It performs basic auto migration.
3
+ #
4
+ # Usage (models/account.rb):
5
+ #
6
+ # DB.upgrade_table? :accounts do
7
+ # primary_key :id
8
+ # column :title, String, :default => 'foobar'
9
+ # end
10
+ # class Account < Sequel::Model
11
+ # end
12
+ #
13
+ # The :accounts table is created if it does not exist.
14
+ # If it exists and lacks :id or :key column, the corresponding column
15
+ # is added with ALTER TABLE. If the table already has the column,
16
+ # no action is performed on it.
17
+
18
+ module Sequel
19
+ module AutoMigration
20
+ end
21
+
22
+ class Database
23
+
24
+ # Upgrades the table if it exists, or passes to create_table.
25
+ #
26
+ # DB.upgrade_table?(:accounts){column :title, String, :default => 'foobar'}
27
+ # # SELECT NULL AS `nil` FROM `accounts` LIMIT 1 -- check existence
28
+ # # DESCRIBE `accounts` -- or similar to get database schema
29
+ # # ALTER TABLE `accounts` ADD COLUMN `title` varchar(255) DEFAULT 'foobar'
30
+ #
31
+ # NOTE: this method ignores column definition if the column already is in the database
32
+ # schema. This behavior is to evade possible DESTRUCTIVE action.
33
+ def upgrade_table?( name, options={}, &block )
34
+ return create_table(name, options, &block) unless table_exists?(name)
35
+ current_schema = Hash[schema name]
36
+ create_table_generator(&block).columns.each do |column|
37
+ if current_schema[column[:name]]
38
+ next
39
+ else
40
+ add_column name, column.delete(:name), column.delete(:type), column
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ Database.register_extension(:auto_migration, AutoMigration)
47
+ end
@@ -0,0 +1,2 @@
1
+ require 'sequel'
2
+ require 'sequel/extensions/auto_migration'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
3
+ require 'sequel/auto_migration/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sequel-auto_migration'
7
+ spec.version = Sequel::AutoMigrate::VERSION
8
+ spec.description = 'Basic auto migration for Sequel'
9
+ spec.summary = 'A gem to provide semi-automatic database migration'
10
+
11
+ spec.authors = ['Igor Bochkariov']
12
+ spec.email = ['ujifgc@gmail.com']
13
+ spec.homepage = 'https://github.com/ujifgc/sequel-auto_migration'
14
+ spec.license = 'MIT'
15
+
16
+ spec.require_paths = ['lib']
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.test_files = spec.files.grep(%r{^test/})
19
+
20
+ spec.add_dependency 'sequel', '~> 3.0'
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
2
+
3
+ describe "DB#upgrade_table?" do
4
+ before do
5
+ @db = Sequel.mock
6
+ end
7
+
8
+ specify "should pass to create_table if the table does not exist" do
9
+ created = false
10
+ meta_def(@db, :table_exists?){|a| false}
11
+ meta_def(@db, :create_table){|*a| created = true}
12
+ @db.upgrade_table?(:cats){}
13
+ created.should == true
14
+ end
15
+
16
+ specify "should request schema if table exists" do
17
+ requested = false
18
+ meta_def(@db, :table_exists?){|a| true}
19
+ meta_def(@db, :schema){|*a| requested = true; []}
20
+ @db.upgrade_table?(:cats){}
21
+ requested.should == true
22
+ end
23
+
24
+ specify "should call add_column for non-existing column and only for it" do
25
+ meta_def(@db, :table_exists?){|a| true}
26
+ meta_def(@db, :schema){|*a| requested = true; [[:id, {:type=>:integer}], [:name, {:type=>:string}]]}
27
+ @db.upgrade_table?(:cats) do
28
+ primary_key :id
29
+ column :name, String
30
+ column :title, String, :default => 'foobar'
31
+ end
32
+ @db.sqls.should == ["ALTER TABLE cats ADD COLUMN title varchar(255) DEFAULT 'foobar'"]
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'sequel'
3
+ require 'sequel-auto_migration'
4
+
5
+ (defined?(RSpec) ? RSpec::Core::ExampleGroup : Spec::Example::ExampleGroup).class_eval do
6
+ def meta_def(obj, name, &block)
7
+ (class << obj; self end).send(:define_method, name, &block)
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-auto_migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Igor Bochkariov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Basic auto migration for Sequel
70
+ email:
71
+ - ujifgc@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/sequel-auto_migration.rb
83
+ - lib/sequel/auto_migration/version.rb
84
+ - lib/sequel/extensions/auto_migration.rb
85
+ - sequel-auto_migration.gemspec
86
+ - spec/schema_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/ujifgc/sequel-auto_migration
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A gem to provide semi-automatic database migration
112
+ test_files: []
113
+ has_rdoc: