unschema 0.0.2 → 0.0.4
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 +7 -0
- data/.travis.yml +6 -0
- data/README.md +9 -2
- data/Rakefile +10 -1
- data/bin/unschema +3 -4
- data/lib/unschema.rb +1 -9
- data/lib/unschema/base.rb +5 -19
- data/lib/unschema/fake_schema.rb +17 -0
- data/lib/unschema/schema_intermediator.rb +5 -4
- data/lib/unschema/version.rb +1 -1
- data/test/helper.rb +7 -0
- data/test/integration/end_to_end_test.rb +44 -40
- data/unschema.gemspec +2 -0
- metadata +24 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 01ed70f4c6013133bd2b75219bad66d570bc5d78
|
4
|
+
data.tar.gz: 0ada28339c5538bccde776ddf5c7017db209f65e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6245c40160baf94f84f8736e4aceb0a1d27b5beed15aaa356efdf58cd4acbb38a5c224b0e2097e321b93e41eb341883b6ee72058a51c6943ebcbb0621c37923c
|
7
|
+
data.tar.gz: 888c0f5114435aec00325a6beac2227ed198fdf9433bc73c6d2a81f994409aaa48bfdf04eeecf0a19f29172530557c55bec81db01134ca41e628c0f37b0a459f
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Unschema - rebase your schema.rb
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/neopoly/unschema) [](http://badge.fury.io/rb/unschema) [](https://codeclimate.com/github/neopoly/unschema)
|
4
|
+
|
5
|
+
Splits your schema.rb into separate migrations per table.
|
4
6
|
|
5
7
|
Every table migration contains a create_table and additional add_index calls.
|
6
8
|
|
@@ -22,7 +24,12 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
|
-
|
27
|
+
usage: unschema [SCHEMA_FILE] [MIGRATIONS_DIR]
|
28
|
+
|
29
|
+
## TODO
|
30
|
+
|
31
|
+
* Auto-detect Rails directory and use correct defaults.
|
32
|
+
* Generate prettier migrations (ident, options hashes, newlines)
|
26
33
|
|
27
34
|
## Contributing
|
28
35
|
|
data/Rakefile
CHANGED
data/bin/unschema
CHANGED
@@ -7,13 +7,12 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
|
|
7
7
|
|
8
8
|
require 'unschema'
|
9
9
|
|
10
|
-
if ARGV.size !=
|
11
|
-
puts "usage: unschema [SCHEMA_FILE] [MIGRATIONS_DIR]
|
10
|
+
if ARGV.size != 2
|
11
|
+
puts "usage: unschema [SCHEMA_FILE] [MIGRATIONS_DIR]"
|
12
12
|
exit 1
|
13
13
|
end
|
14
14
|
schema_file = ARGV[0]
|
15
15
|
migrations_path = ARGV[1]
|
16
|
-
start_version = ARGV[2].to_i
|
17
16
|
|
18
17
|
puts "Processing #{schema_file.inspect}"
|
19
|
-
Unschema::Base.process!(schema_file, migrations_path,
|
18
|
+
Unschema::Base.process!(schema_file, migrations_path, true)
|
data/lib/unschema.rb
CHANGED
data/lib/unschema/base.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Unschema
|
2
|
-
class Base < Struct.new(:schema_file, :migrations_path, :
|
2
|
+
class Base < Struct.new(:schema_file, :migrations_path, :verbose)
|
3
3
|
def self.process!(*args)
|
4
4
|
new(*args).process
|
5
5
|
end
|
@@ -9,10 +9,10 @@ module Unschema
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def process
|
12
|
-
disarm_schema_loader!
|
13
|
-
|
14
12
|
load schema_file
|
15
13
|
|
14
|
+
@version = ActiveRecord::Schema.intermediator.version
|
15
|
+
|
16
16
|
calls_for_tables = Hash.new{|hash, key| hash[key] = []}
|
17
17
|
|
18
18
|
ActiveRecord::Schema.intermediator.calls.each do |call|
|
@@ -32,23 +32,11 @@ module Unschema
|
|
32
32
|
end
|
33
33
|
|
34
34
|
private
|
35
|
-
def disarm_schema_loader!
|
36
|
-
ActiveRecord::Schema.class_eval do
|
37
|
-
class << self
|
38
|
-
attr_accessor :intermediator
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.define(*args, &block)
|
42
|
-
self.intermediator ||= Unschema::SchemaIntermediator.new
|
43
|
-
self.intermediator.process(*args, &block)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
35
|
|
48
36
|
def dump_table_calls(table_name, calls)
|
49
37
|
File.open(File.join(migrations_path, "#{next_migration}_create_#{table_name}.rb"), "w") do |f|
|
50
38
|
f << "class Create#{table_name.gsub(/^(\w)/){|s| s.upcase }.gsub(/(_\w)/){|s| s[-1, 1].upcase} } < ActiveRecord::Migration\n"
|
51
|
-
|
39
|
+
|
52
40
|
f << " def change\n"
|
53
41
|
|
54
42
|
calls.each do |call|
|
@@ -80,9 +68,7 @@ module Unschema
|
|
80
68
|
end
|
81
69
|
|
82
70
|
def next_migration
|
83
|
-
@
|
84
|
-
|
85
|
-
@next_migration += 1
|
71
|
+
@version += 1
|
86
72
|
end
|
87
73
|
|
88
74
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Mock ActiveRecord::Schema for schema.rb.
|
2
|
+
#
|
3
|
+
# This mock intercepts all method calls in `ActiveRecord::Schema.define`.
|
4
|
+
#
|
5
|
+
# See your schema.rb.
|
6
|
+
module ActiveRecord
|
7
|
+
class Schema
|
8
|
+
class << self
|
9
|
+
attr_accessor :intermediator
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.define(options={}, &block)
|
13
|
+
self.intermediator ||= Unschema::SchemaIntermediator.new
|
14
|
+
self.intermediator.process(options, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module Unschema
|
2
2
|
class SchemaIntermediator
|
3
|
-
attr_reader :root
|
3
|
+
attr_reader :root, :version
|
4
4
|
|
5
|
-
def process(
|
6
|
-
@
|
7
|
-
root.
|
5
|
+
def process(options, &block)
|
6
|
+
@version = options[:version] || 0
|
7
|
+
@root = Call.new(:root, options)
|
8
|
+
@root.instance_eval(&block)
|
8
9
|
end
|
9
10
|
|
10
11
|
def calls
|
data/lib/unschema/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -3,5 +3,12 @@ require 'minitest/autorun'
|
|
3
3
|
require 'unschema'
|
4
4
|
|
5
5
|
class TestCase < MiniTest::Unit::TestCase
|
6
|
+
end
|
6
7
|
|
8
|
+
class String
|
9
|
+
def unindent
|
10
|
+
matched = %r{^\s*}.match self.split("\n").first
|
11
|
+
indent = matched.end(0)
|
12
|
+
self.gsub(%r{^\s{#{indent}}}, '')
|
13
|
+
end
|
7
14
|
end
|
@@ -3,62 +3,66 @@ require 'fileutils'
|
|
3
3
|
require 'pathname'
|
4
4
|
|
5
5
|
class EndToEndTest < TestCase
|
6
|
-
|
7
|
-
def test_schema_to_migrations
|
8
|
-
schema_file = File.expand_path("../fixtures/schema.rb", __FILE__)
|
9
|
-
migrations_path = Pathname.new File.expand_path("../target", __FILE__)
|
10
|
-
|
6
|
+
def setup
|
11
7
|
Dir[migrations_path.join "*.rb"].map { |file| File.delete(file) }
|
8
|
+
end
|
12
9
|
|
13
|
-
|
10
|
+
def test_schema_to_migrations
|
11
|
+
Unschema::Base.process!(schema_file, migrations_path.to_s)
|
14
12
|
|
15
|
-
assert_equal ["
|
13
|
+
assert_equal ["20130222131356_create_abc.rb", "20130222131357_create_table1.rb", "20130222131358_create_the_table2.rb"], Dir[migrations_path.join "*.rb"].map{|path| File.basename(path)}.sort
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
assert_migration "20130222131356_create_abc.rb", <<-MIGRATION
|
16
|
+
class CreateAbc < ActiveRecord::Migration
|
17
|
+
def change
|
18
|
+
create_table "abc" do |t|
|
19
|
+
t.string "defgh"
|
20
|
+
end
|
23
21
|
end
|
24
22
|
end
|
25
|
-
end
|
26
23
|
MIGRATION
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
25
|
+
assert_migration "20130222131357_create_table1.rb", <<-MIGRATION
|
26
|
+
class CreateTable1 < ActiveRecord::Migration
|
27
|
+
def change
|
28
|
+
create_table "table1", {:force=>true} do |t|
|
29
|
+
t.string "str"
|
30
|
+
t.integer "int"
|
31
|
+
t.datetime "created_at"
|
32
|
+
t.datetime "updated_at"
|
33
|
+
end
|
34
|
+
add_index "table1", ["id"], {:name=>"the_index_1", :unique=>true}
|
35
|
+
add_index "table1", ["doesnt_make_sende"], {:name=>"the_index_2", :unique=>true, :wrong_attr=>1}
|
39
36
|
end
|
40
|
-
add_index "table1", ["id"], {:name=>"the_index_1", :unique=>true}
|
41
|
-
add_index "table1", ["doesnt_make_sende"], {:name=>"the_index_2", :unique=>true, :wrong_attr=>1}
|
42
37
|
end
|
43
|
-
end
|
44
38
|
MIGRATION
|
45
39
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
t.integer "max_online", {:default=>0}
|
40
|
+
assert_migration "20130222131358_create_the_table2.rb", <<-MIGRATION
|
41
|
+
class CreateTheTable2 < ActiveRecord::Migration
|
42
|
+
def change
|
43
|
+
create_table "the_table2", {:force=>true} do |t|
|
44
|
+
t.date "date"
|
45
|
+
t.integer "max_online", {:default=>0}
|
46
|
+
end
|
47
|
+
add_index "the_table2", ["date"], {:name=>"index_statistics_on_date_and"}
|
55
48
|
end
|
56
|
-
add_index "the_table2", ["date"], {:name=>"index_statistics_on_date_and"}
|
57
49
|
end
|
58
|
-
end
|
59
50
|
MIGRATION
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def assert_migration(path, expect)
|
56
|
+
actual = File.read(migrations_path.join path)
|
60
57
|
|
61
|
-
assert_equal
|
58
|
+
assert_equal expect.unindent.chomp, actual.unindent
|
62
59
|
end
|
63
60
|
|
61
|
+
def schema_file
|
62
|
+
File.expand_path("../fixtures/schema.rb", __FILE__)
|
63
|
+
end
|
64
|
+
|
65
|
+
def migrations_path
|
66
|
+
Pathname.new File.expand_path("../target", __FILE__)
|
67
|
+
end
|
64
68
|
end
|
data/unschema.gemspec
CHANGED
metadata
CHANGED
@@ -1,16 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unschema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jakob Holderbaum
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
11
|
+
date: 2013-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
14
27
|
description: Splits your current schema.rb into per-table migrations. Think of it
|
15
28
|
as >rebase< for your migrations.
|
16
29
|
email:
|
@@ -21,6 +34,7 @@ extensions: []
|
|
21
34
|
extra_rdoc_files: []
|
22
35
|
files:
|
23
36
|
- .gitignore
|
37
|
+
- .travis.yml
|
24
38
|
- Gemfile
|
25
39
|
- LICENSE.txt
|
26
40
|
- README.md
|
@@ -28,6 +42,7 @@ files:
|
|
28
42
|
- bin/unschema
|
29
43
|
- lib/unschema.rb
|
30
44
|
- lib/unschema/base.rb
|
45
|
+
- lib/unschema/fake_schema.rb
|
31
46
|
- lib/unschema/schema_intermediator.rb
|
32
47
|
- lib/unschema/version.rb
|
33
48
|
- test/helper.rb
|
@@ -37,27 +52,26 @@ files:
|
|
37
52
|
- unschema.gemspec
|
38
53
|
homepage: https://github.com/neopoly/unschema
|
39
54
|
licenses: []
|
55
|
+
metadata: {}
|
40
56
|
post_install_message:
|
41
57
|
rdoc_options: []
|
42
58
|
require_paths:
|
43
59
|
- lib
|
44
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
61
|
requirements:
|
47
|
-
- -
|
62
|
+
- - '>='
|
48
63
|
- !ruby/object:Gem::Version
|
49
64
|
version: '0'
|
50
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
66
|
requirements:
|
53
|
-
- -
|
67
|
+
- - '>='
|
54
68
|
- !ruby/object:Gem::Version
|
55
69
|
version: '0'
|
56
70
|
requirements: []
|
57
71
|
rubyforge_project:
|
58
|
-
rubygems_version:
|
72
|
+
rubygems_version: 2.0.3
|
59
73
|
signing_key:
|
60
|
-
specification_version:
|
74
|
+
specification_version: 4
|
61
75
|
summary: Splits your current schema.rb into per-table migrations
|
62
76
|
test_files:
|
63
77
|
- test/helper.rb
|