unschema 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/unschema +19 -0
- data/lib/unschema/base.rb +89 -0
- data/lib/unschema/schema_intermediator.rb +35 -0
- data/lib/unschema/version.rb +3 -0
- data/lib/unschema.rb +12 -0
- data/test/helper.rb +7 -0
- data/test/integration/end_to_end_test.rb +64 -0
- data/test/integration/fixtures/schema.rb +37 -0
- data/test/integration/target/.keep +0 -0
- data/unschema.gemspec +19 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jakob Holderbaum
|
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,29 @@
|
|
1
|
+
# Unschema
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'unschema'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install unschema
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/unschema
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# gets replaced by rubygems anyway ^^
|
3
|
+
|
4
|
+
# Commandline interface for the unschema tool
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
|
7
|
+
|
8
|
+
require 'unschema'
|
9
|
+
|
10
|
+
if ARGV.size != 3
|
11
|
+
puts "usage: unschema [SCHEMA_FILE] [MIGRATIONS_DIR] [START_VERSION]"
|
12
|
+
exit 1
|
13
|
+
end
|
14
|
+
schema_file = ARGV[0]
|
15
|
+
migrations_path = ARGV[1]
|
16
|
+
start_version = ARGV[2].to_i
|
17
|
+
|
18
|
+
puts "Processing #{schema_file.inspect}"
|
19
|
+
Unschema::Base.process!(schema_file, migrations_path, start_version, true)
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Unschema
|
2
|
+
class Base < Struct.new(:schema_file, :migrations_path, :start_version, :verbose)
|
3
|
+
def self.process!(*args)
|
4
|
+
new(*args).process
|
5
|
+
end
|
6
|
+
|
7
|
+
def verbose?
|
8
|
+
!!verbose
|
9
|
+
end
|
10
|
+
|
11
|
+
def process
|
12
|
+
disarm_schema_loader!
|
13
|
+
|
14
|
+
load schema_file
|
15
|
+
|
16
|
+
calls_for_tables = Hash.new{|hash, key| hash[key] = []}
|
17
|
+
|
18
|
+
ActiveRecord::Schema.intermediator.calls.each do |call|
|
19
|
+
table_name = call.args.first.to_s if [:create_table, :add_index].include?(call.name)
|
20
|
+
raise "Don't now how to process #{call.name.inspect}" unless table_name
|
21
|
+
|
22
|
+
calls_for_tables[table_name] << call
|
23
|
+
end
|
24
|
+
|
25
|
+
puts "Found #{calls_for_tables.keys.size} tables" if verbose?
|
26
|
+
|
27
|
+
calls_for_tables.sort.each do |table_name, calls|
|
28
|
+
puts " Dumping #{table_name.inspect}" if verbose?
|
29
|
+
dump_table_calls table_name, calls
|
30
|
+
end
|
31
|
+
puts "Done" if verbose?
|
32
|
+
end
|
33
|
+
|
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
|
+
|
48
|
+
def dump_table_calls(table_name, calls)
|
49
|
+
File.open(File.join(migrations_path, "#{next_migration}_create_#{table_name}.rb"), "w") do |f|
|
50
|
+
f << "class Create#{table_name.gsub(/^(\w)/){|s| s.upcase }.gsub(/(_\w)/){|s| s[-1, 1].upcase} } < ActiveRecord::Migration\n"
|
51
|
+
|
52
|
+
f << " def change\n"
|
53
|
+
|
54
|
+
calls.each do |call|
|
55
|
+
str = " #{call.name} #{stringify_args call.args}"
|
56
|
+
|
57
|
+
if call.block
|
58
|
+
receiver = call.block.name
|
59
|
+
str += " do |#{receiver}|\n"
|
60
|
+
|
61
|
+
call.block.calls.each do |block_call|
|
62
|
+
str += " #{receiver}.#{block_call.name} #{stringify_args block_call.args}\n"
|
63
|
+
end
|
64
|
+
|
65
|
+
str += " end\n"
|
66
|
+
else
|
67
|
+
str += "\n"
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
f << str
|
72
|
+
end
|
73
|
+
|
74
|
+
f << " end\nend"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def stringify_args(args)
|
79
|
+
args.inspect.gsub(/^\[|\]$/,"")
|
80
|
+
end
|
81
|
+
|
82
|
+
def next_migration
|
83
|
+
@next_migration ||= start_version
|
84
|
+
|
85
|
+
@next_migration += 1
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Unschema
|
2
|
+
class SchemaIntermediator
|
3
|
+
attr_reader :root
|
4
|
+
|
5
|
+
def process(*args, &block)
|
6
|
+
@root = Call.new(:root, *args)
|
7
|
+
root.instance_eval(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def calls
|
11
|
+
root.calls
|
12
|
+
end
|
13
|
+
|
14
|
+
class Call
|
15
|
+
attr_reader :name, :args, :block, :calls
|
16
|
+
def initialize(name, *args, &block)
|
17
|
+
@name = name
|
18
|
+
@args = args
|
19
|
+
@calls = []
|
20
|
+
|
21
|
+
process_block!(block) if block
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(name, *args, &block)
|
25
|
+
@calls << Call.new(name, *args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def process_block!(block)
|
30
|
+
@block = self.class.new("t")
|
31
|
+
block.call(@block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/unschema.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'pathname'
|
4
|
+
|
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
|
+
|
11
|
+
Dir[migrations_path.join "*.rb"].map { |file| File.delete(file) }
|
12
|
+
|
13
|
+
Unschema::Base.process!(schema_file, migrations_path.to_s, 1000)
|
14
|
+
|
15
|
+
assert_equal ["1001_create_abc.rb", "1002_create_table1.rb", "1003_create_the_table2.rb"], Dir[migrations_path.join "*.rb"].map{|path| File.basename(path)}.sort
|
16
|
+
|
17
|
+
migration = File.read(File.join(migrations_path, "1001_create_abc.rb"))
|
18
|
+
expectation = <<-MIGRATION
|
19
|
+
class CreateAbc < ActiveRecord::Migration
|
20
|
+
def change
|
21
|
+
create_table "abc" do |t|
|
22
|
+
t.string "defgh"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
MIGRATION
|
27
|
+
|
28
|
+
assert_equal expectation.chomp.gsub(/^\s*/,""), migration.gsub(/^\s*/,"")
|
29
|
+
|
30
|
+
migration = File.read(File.join(migrations_path, "1002_create_table1.rb"))
|
31
|
+
expectation = <<-MIGRATION
|
32
|
+
class CreateTable1 < ActiveRecord::Migration
|
33
|
+
def change
|
34
|
+
create_table "table1", {:force=>true} do |t|
|
35
|
+
t.string "str"
|
36
|
+
t.integer "int"
|
37
|
+
t.datetime "created_at"
|
38
|
+
t.datetime "updated_at"
|
39
|
+
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
|
+
end
|
43
|
+
end
|
44
|
+
MIGRATION
|
45
|
+
|
46
|
+
assert_equal expectation.chomp.gsub(/^\s*/,""), migration.gsub(/^\s*/,"")
|
47
|
+
|
48
|
+
migration = File.read(File.join(migrations_path, "1003_create_the_table2.rb"))
|
49
|
+
expectation = <<-MIGRATION
|
50
|
+
class CreateTheTable2 < ActiveRecord::Migration
|
51
|
+
def change
|
52
|
+
create_table "the_table2", {:force=>true} do |t|
|
53
|
+
t.date "date"
|
54
|
+
t.integer "max_online", {:default=>0}
|
55
|
+
end
|
56
|
+
add_index "the_table2", ["date"], {:name=>"index_statistics_on_date_and"}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
MIGRATION
|
60
|
+
|
61
|
+
assert_equal expectation.chomp.gsub(/^\s*/,""), migration.gsub(/^\s*/,"")
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended to check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(:version => 20130222131355) do
|
15
|
+
|
16
|
+
create_table "table1", :force => true do |t|
|
17
|
+
t.string "str"
|
18
|
+
t.integer "int"
|
19
|
+
t.datetime "created_at"
|
20
|
+
t.datetime "updated_at"
|
21
|
+
end
|
22
|
+
|
23
|
+
add_index "table1", ["id"], :name => "the_index_1", :unique => true
|
24
|
+
add_index "table1", ["doesnt_make_sende"], :name => "the_index_2", :unique => true, :wrong_attr => 1
|
25
|
+
|
26
|
+
create_table "the_table2", :force => true do |t|
|
27
|
+
t.date "date"
|
28
|
+
t.integer "max_online", :default => 0
|
29
|
+
end
|
30
|
+
|
31
|
+
add_index "the_table2", ["date"], :name => "index_statistics_on_date_and"
|
32
|
+
|
33
|
+
create_table "abc" do |t|
|
34
|
+
t.string "defgh"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
File without changes
|
data/unschema.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unschema/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "unschema"
|
8
|
+
gem.version = Unschema::VERSION
|
9
|
+
gem.authors = ["Jakob Holderbaum"]
|
10
|
+
gem.email = ["jh@neopoly.de"]
|
11
|
+
gem.description = %q{Splits yout current schema.rb into per-table migrations. Think of it as >rebase< for your migrations.}
|
12
|
+
gem.summary = %q{Splits yout current schema.rb into per-table migrations}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unschema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jakob Holderbaum
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Splits yout current schema.rb into per-table migrations. Think of it
|
15
|
+
as >rebase< for your migrations.
|
16
|
+
email:
|
17
|
+
- jh@neopoly.de
|
18
|
+
executables:
|
19
|
+
- unschema
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/unschema
|
29
|
+
- lib/unschema.rb
|
30
|
+
- lib/unschema/base.rb
|
31
|
+
- lib/unschema/schema_intermediator.rb
|
32
|
+
- lib/unschema/version.rb
|
33
|
+
- test/helper.rb
|
34
|
+
- test/integration/end_to_end_test.rb
|
35
|
+
- test/integration/fixtures/schema.rb
|
36
|
+
- test/integration/target/.keep
|
37
|
+
- unschema.gemspec
|
38
|
+
homepage: ''
|
39
|
+
licenses: []
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.24
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Splits yout current schema.rb into per-table migrations
|
62
|
+
test_files:
|
63
|
+
- test/helper.rb
|
64
|
+
- test/integration/end_to_end_test.rb
|
65
|
+
- test/integration/fixtures/schema.rb
|
66
|
+
- test/integration/target/.keep
|