unschema 0.0.4 → 0.0.5
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 +4 -4
- data/.travis.yml +8 -2
- data/README.md +1 -1
- data/lib/unschema.rb +1 -0
- data/lib/unschema/base.rb +3 -31
- data/lib/unschema/migration_dumper.rb +58 -0
- data/lib/unschema/schema_intermediator.rb +9 -6
- data/lib/unschema/version.rb +1 -1
- data/test/helper.rb +18 -4
- data/test/integration/end_to_end_test.rb +7 -7
- data/test/unit/helper_test.rb +38 -0
- data/test/unit/migration_dumper_test.rb +71 -0
- data/unschema.gemspec +1 -0
- metadata +28 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4243d394a3ed056790202261451bc521418c20aa
|
4
|
+
data.tar.gz: ad8f5409d7b724f00c0d9aead913859260cbdc10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e27fa30ad4f8a21f290ff0214b50dccb8c6cdb6751db48317a6e7410016365fcf27fee5110eda9c448af5ec2217b2eaec8a3c6b2c83aacf73cab37fcf28531b
|
7
|
+
data.tar.gz: 657a908a1433219e1e1b30f4cdd73be2f90c7780e897234d0a723922dc7851dbb5dff5bac5ebc39507b12369b7774c8c1760439b49cab77c59f5bfb9865d91a3
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Unschema - rebase your schema.rb
|
2
2
|
|
3
|
-
[](https://travis-ci.org/neopoly/unschema) [](https://rubygems.org/gems/unschema) [](https://codeclimate.com/github/neopoly/unschema) [](http://inch-ci.org/github/neopoly/unschema)
|
4
4
|
|
5
5
|
Splits your schema.rb into separate migrations per table.
|
6
6
|
|
data/lib/unschema.rb
CHANGED
data/lib/unschema/base.rb
CHANGED
@@ -34,42 +34,14 @@ module Unschema
|
|
34
34
|
private
|
35
35
|
|
36
36
|
def dump_table_calls(table_name, calls)
|
37
|
-
File.
|
38
|
-
|
39
|
-
|
40
|
-
f << " def change\n"
|
41
|
-
|
42
|
-
calls.each do |call|
|
43
|
-
str = " #{call.name} #{stringify_args call.args}"
|
44
|
-
|
45
|
-
if call.block
|
46
|
-
receiver = call.block.name
|
47
|
-
str += " do |#{receiver}|\n"
|
48
|
-
|
49
|
-
call.block.calls.each do |block_call|
|
50
|
-
str += " #{receiver}.#{block_call.name} #{stringify_args block_call.args}\n"
|
51
|
-
end
|
52
|
-
|
53
|
-
str += " end\n"
|
54
|
-
else
|
55
|
-
str += "\n"
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
f << str
|
60
|
-
end
|
61
|
-
|
62
|
-
f << " end\nend"
|
37
|
+
file = File.join(migrations_path, "#{next_migration}_create_#{table_name}.rb")
|
38
|
+
File.open(file, "w") do |f|
|
39
|
+
MigrationDumper.new(table_name, calls).dump_to(f)
|
63
40
|
end
|
64
41
|
end
|
65
42
|
|
66
|
-
def stringify_args(args)
|
67
|
-
args.inspect.gsub(/^\[|\]$/,"")
|
68
|
-
end
|
69
|
-
|
70
43
|
def next_migration
|
71
44
|
@version += 1
|
72
45
|
end
|
73
|
-
|
74
46
|
end
|
75
47
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Unschema
|
2
|
+
class MigrationDumper
|
3
|
+
def initialize(table_name, calls)
|
4
|
+
@table_name = table_name
|
5
|
+
@calls = calls
|
6
|
+
end
|
7
|
+
|
8
|
+
def dump_to(f)
|
9
|
+
f << "class Create#{table_name_camelcased} < ActiveRecord::Migration\n"
|
10
|
+
f << " def change\n"
|
11
|
+
|
12
|
+
@calls.each do |call|
|
13
|
+
f << dump_call(call)
|
14
|
+
end
|
15
|
+
|
16
|
+
f << " end\n"
|
17
|
+
f << "end"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def dump_call(call)
|
23
|
+
str = " #{call.name} #{stringify_call call}"
|
24
|
+
|
25
|
+
if call.block
|
26
|
+
receiver = call.block.name
|
27
|
+
str << " do |#{receiver}|\n"
|
28
|
+
|
29
|
+
call.block.calls.each do |block_call|
|
30
|
+
str << " #{receiver}.#{block_call.name} #{stringify_call block_call}\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
str << " end"
|
34
|
+
end
|
35
|
+
|
36
|
+
str << "\n"
|
37
|
+
str
|
38
|
+
end
|
39
|
+
|
40
|
+
def table_name_camelcased
|
41
|
+
@table_name.gsub(/^(\w)/){|s| s.upcase }.gsub(/(_\w)/) { |s| s[-1, 1].upcase }
|
42
|
+
end
|
43
|
+
|
44
|
+
def stringify_call(call)
|
45
|
+
args = stringify_args(call.args)
|
46
|
+
options = stringify_options(call.options) unless call.options.empty?
|
47
|
+
[ args, options ].compact.join(", ")
|
48
|
+
end
|
49
|
+
|
50
|
+
def stringify_args(args)
|
51
|
+
args.inspect.gsub(/^\[|\]$/,"")
|
52
|
+
end
|
53
|
+
|
54
|
+
def stringify_options(options)
|
55
|
+
options.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Unschema
|
2
2
|
class SchemaIntermediator
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :version
|
4
4
|
|
5
5
|
def process(options, &block)
|
6
6
|
@version = options[:version] || 0
|
@@ -9,15 +9,17 @@ module Unschema
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def calls
|
12
|
-
root.calls
|
12
|
+
@root.calls
|
13
13
|
end
|
14
14
|
|
15
15
|
class Call
|
16
|
-
attr_reader :name, :args, :block, :calls
|
16
|
+
attr_reader :name, :args, :options, :block, :calls
|
17
|
+
|
17
18
|
def initialize(name, *args, &block)
|
18
|
-
@name
|
19
|
-
@args
|
20
|
-
@
|
19
|
+
@name = name
|
20
|
+
@args = args
|
21
|
+
@options = Hash === args.last ? args.pop : {}
|
22
|
+
@calls = []
|
21
23
|
|
22
24
|
process_block!(block) if block
|
23
25
|
end
|
@@ -27,6 +29,7 @@ module Unschema
|
|
27
29
|
end
|
28
30
|
|
29
31
|
private
|
32
|
+
|
30
33
|
def process_block!(block)
|
31
34
|
@block = self.class.new("t")
|
32
35
|
block.call(@block)
|
data/lib/unschema/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -2,13 +2,27 @@ require 'minitest/autorun'
|
|
2
2
|
|
3
3
|
require 'unschema'
|
4
4
|
|
5
|
-
class TestCase <
|
5
|
+
class TestCase < Minitest::Test
|
6
|
+
def assert_string(actual, expect)
|
7
|
+
assert_equal expect.unindent.chomp, actual.unindent
|
8
|
+
end
|
6
9
|
end
|
7
10
|
|
8
11
|
class String
|
9
12
|
def unindent
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
gsub(%r{^\s{#{indentation}}}, '')
|
14
|
+
end
|
15
|
+
|
16
|
+
def indentation
|
17
|
+
first_line = split("\n").first
|
18
|
+
if match = %r{^\s*}.match(first_line)
|
19
|
+
match.end(0)
|
20
|
+
else
|
21
|
+
0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def indent(level)
|
26
|
+
gsub(%r{^}, " " * level)
|
13
27
|
end
|
14
28
|
end
|
@@ -25,14 +25,14 @@ class EndToEndTest < TestCase
|
|
25
25
|
assert_migration "20130222131357_create_table1.rb", <<-MIGRATION
|
26
26
|
class CreateTable1 < ActiveRecord::Migration
|
27
27
|
def change
|
28
|
-
create_table "table1",
|
28
|
+
create_table "table1", :force => true do |t|
|
29
29
|
t.string "str"
|
30
30
|
t.integer "int"
|
31
31
|
t.datetime "created_at"
|
32
32
|
t.datetime "updated_at"
|
33
33
|
end
|
34
|
-
add_index "table1", ["id"],
|
35
|
-
add_index "table1", ["doesnt_make_sende"],
|
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
|
36
36
|
end
|
37
37
|
end
|
38
38
|
MIGRATION
|
@@ -40,11 +40,11 @@ class EndToEndTest < TestCase
|
|
40
40
|
assert_migration "20130222131358_create_the_table2.rb", <<-MIGRATION
|
41
41
|
class CreateTheTable2 < ActiveRecord::Migration
|
42
42
|
def change
|
43
|
-
create_table "the_table2",
|
43
|
+
create_table "the_table2", :force => true do |t|
|
44
44
|
t.date "date"
|
45
|
-
t.integer "max_online",
|
45
|
+
t.integer "max_online", :default => 0
|
46
46
|
end
|
47
|
-
add_index "the_table2", ["date"],
|
47
|
+
add_index "the_table2", ["date"], :name => "index_statistics_on_date_and"
|
48
48
|
end
|
49
49
|
end
|
50
50
|
MIGRATION
|
@@ -55,7 +55,7 @@ class EndToEndTest < TestCase
|
|
55
55
|
def assert_migration(path, expect)
|
56
56
|
actual = File.read(migrations_path.join path)
|
57
57
|
|
58
|
-
|
58
|
+
assert_string actual, expect
|
59
59
|
end
|
60
60
|
|
61
61
|
def schema_file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class HelperTest < TestCase
|
4
|
+
def test_unindent
|
5
|
+
expect = <<-STR
|
6
|
+
foo
|
7
|
+
bar
|
8
|
+
STR
|
9
|
+
actual = <<-STR
|
10
|
+
foo
|
11
|
+
bar
|
12
|
+
STR
|
13
|
+
|
14
|
+
assert_equal expect, actual.unindent
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_identatition
|
18
|
+
assert_equal 0, "".indentation
|
19
|
+
assert_equal 1, " ".indentation
|
20
|
+
assert_equal 2, <<-STR.indentation
|
21
|
+
foo
|
22
|
+
bar
|
23
|
+
STR
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_indent
|
27
|
+
expect = <<-STR
|
28
|
+
foo
|
29
|
+
bar
|
30
|
+
STR
|
31
|
+
actual = <<-STR
|
32
|
+
foo
|
33
|
+
bar
|
34
|
+
STR
|
35
|
+
|
36
|
+
assert_equal expect, actual.indent(2)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
class MigrationDumperTest < TestCase
|
5
|
+
def test_without_calls
|
6
|
+
create_dumper
|
7
|
+
|
8
|
+
assert_change <<-STR
|
9
|
+
STR
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_create_table_call
|
13
|
+
create_dumper \
|
14
|
+
create_call(:create_table, "table", :force => true) { |t|
|
15
|
+
t.string "uid", :limit => 32, :unique => true
|
16
|
+
t.integer "amount"
|
17
|
+
}
|
18
|
+
|
19
|
+
assert_change <<-STR
|
20
|
+
create_table "table", :force => true do |t|
|
21
|
+
t.string "uid", :limit => 32, :unique => true
|
22
|
+
t.integer "amount"
|
23
|
+
end
|
24
|
+
STR
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_index_call
|
28
|
+
create_dumper \
|
29
|
+
create_call(:add_index, "table", "uid", :unique => true),
|
30
|
+
create_call(:add_index, "table", %w[composite index])
|
31
|
+
|
32
|
+
assert_change <<-STR
|
33
|
+
add_index "table", "uid", :unique => true
|
34
|
+
add_index "table", ["composite", "index"]
|
35
|
+
STR
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def create_dumper(*calls)
|
41
|
+
@dumper = Unschema::MigrationDumper.new("foo_bar", calls)
|
42
|
+
end
|
43
|
+
|
44
|
+
def create_call(name, *args, &block)
|
45
|
+
Unschema::SchemaIntermediator::Call.new(name, *args, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def assert_change(inner)
|
49
|
+
expect = <<-STR
|
50
|
+
class CreateFooBar < ActiveRecord::Migration
|
51
|
+
def change
|
52
|
+
STR
|
53
|
+
|
54
|
+
unless inner.empty?
|
55
|
+
expect << inner.unindent.indent(expect.indentation + 4)
|
56
|
+
end
|
57
|
+
|
58
|
+
expect << <<-STR
|
59
|
+
end
|
60
|
+
end
|
61
|
+
STR
|
62
|
+
|
63
|
+
assert_dump expect
|
64
|
+
end
|
65
|
+
|
66
|
+
def assert_dump(expect)
|
67
|
+
io = StringIO.new
|
68
|
+
@dumper.dump_to(io)
|
69
|
+
assert_string io.string, expect
|
70
|
+
end
|
71
|
+
end
|
data/unschema.gemspec
CHANGED
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unschema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakob Holderbaum
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.3.5
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.3.5
|
27
41
|
description: Splits your current schema.rb into per-table migrations. Think of it
|
28
42
|
as >rebase< for your migrations.
|
29
43
|
email:
|
@@ -33,8 +47,8 @@ executables:
|
|
33
47
|
extensions: []
|
34
48
|
extra_rdoc_files: []
|
35
49
|
files:
|
36
|
-
- .gitignore
|
37
|
-
- .travis.yml
|
50
|
+
- ".gitignore"
|
51
|
+
- ".travis.yml"
|
38
52
|
- Gemfile
|
39
53
|
- LICENSE.txt
|
40
54
|
- README.md
|
@@ -43,12 +57,15 @@ files:
|
|
43
57
|
- lib/unschema.rb
|
44
58
|
- lib/unschema/base.rb
|
45
59
|
- lib/unschema/fake_schema.rb
|
60
|
+
- lib/unschema/migration_dumper.rb
|
46
61
|
- lib/unschema/schema_intermediator.rb
|
47
62
|
- lib/unschema/version.rb
|
48
63
|
- test/helper.rb
|
49
64
|
- test/integration/end_to_end_test.rb
|
50
65
|
- test/integration/fixtures/schema.rb
|
51
66
|
- test/integration/target/.keep
|
67
|
+
- test/unit/helper_test.rb
|
68
|
+
- test/unit/migration_dumper_test.rb
|
52
69
|
- unschema.gemspec
|
53
70
|
homepage: https://github.com/neopoly/unschema
|
54
71
|
licenses: []
|
@@ -59,17 +76,17 @@ require_paths:
|
|
59
76
|
- lib
|
60
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
78
|
requirements:
|
62
|
-
- -
|
79
|
+
- - ">="
|
63
80
|
- !ruby/object:Gem::Version
|
64
81
|
version: '0'
|
65
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
83
|
requirements:
|
67
|
-
- -
|
84
|
+
- - ">="
|
68
85
|
- !ruby/object:Gem::Version
|
69
86
|
version: '0'
|
70
87
|
requirements: []
|
71
88
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.
|
89
|
+
rubygems_version: 2.2.2
|
73
90
|
signing_key:
|
74
91
|
specification_version: 4
|
75
92
|
summary: Splits your current schema.rb into per-table migrations
|
@@ -78,3 +95,5 @@ test_files:
|
|
78
95
|
- test/integration/end_to_end_test.rb
|
79
96
|
- test/integration/fixtures/schema.rb
|
80
97
|
- test/integration/target/.keep
|
98
|
+
- test/unit/helper_test.rb
|
99
|
+
- test/unit/migration_dumper_test.rb
|