data_migrations 0.0.1

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.
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ platforms :mri_18 do
7
+ # required as linecache uses it but does not have it as a dep
8
+ gem "require_relative", "~> 1.0.1"
9
+ gem 'ruby-debug'
10
+ end
11
+
12
+ unless RUBY_VERSION == '1.9.3' && RUBY_PLATFORM !~ /darwin/
13
+ # will need to install ruby-debug19 manually for 1.9.3:
14
+ # gem install ruby-debug19 -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-preview1
15
+ gem 'ruby-debug19', :platforms => :mri_19
16
+ end
17
+ end
18
+
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data_migrations (0.0.1)
5
+ activerecord
6
+ rake
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.9)
12
+ activesupport (= 3.0.9)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.5.0)
15
+ activerecord (3.0.9)
16
+ activemodel (= 3.0.9)
17
+ activesupport (= 3.0.9)
18
+ arel (~> 2.0.10)
19
+ tzinfo (~> 0.3.23)
20
+ activesupport (3.0.9)
21
+ archive-tar-minitar (0.5.2)
22
+ arel (2.0.10)
23
+ builder (2.1.2)
24
+ capture_stdout (0.0.1)
25
+ columnize (0.3.4)
26
+ i18n (0.5.0)
27
+ linecache (0.46)
28
+ rbx-require-relative (> 0.0.4)
29
+ linecache19 (0.5.12)
30
+ ruby_core_source (>= 0.1.4)
31
+ mocha (0.9.12)
32
+ pg (0.11.0)
33
+ rake (0.9.2)
34
+ rbx-require-relative (0.0.5)
35
+ require_relative (1.0.2)
36
+ ruby-debug (0.10.4)
37
+ columnize (>= 0.1)
38
+ ruby-debug-base (~> 0.10.4.0)
39
+ ruby-debug-base (0.10.4)
40
+ linecache (>= 0.3)
41
+ ruby-debug-base19 (0.11.25)
42
+ columnize (>= 0.3.1)
43
+ linecache19 (>= 0.5.11)
44
+ ruby_core_source (>= 0.1.4)
45
+ ruby-debug19 (0.11.6)
46
+ columnize (>= 0.3.1)
47
+ linecache19 (>= 0.5.11)
48
+ ruby-debug-base19 (>= 0.11.19)
49
+ ruby_core_source (0.1.5)
50
+ archive-tar-minitar (>= 0.5.2)
51
+ test_declarative (0.0.5)
52
+ tzinfo (0.3.29)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ capture_stdout
59
+ data_migrations!
60
+ mocha
61
+ pg
62
+ require_relative (~> 1.0.1)
63
+ ruby-debug
64
+ ruby-debug19
65
+ test_declarative
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) Sven Fuchs <svenfuchs@artweb-design.de>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # data\_migrations [![Build Status](https://secure.travis-ci.org/svenfuchs/data_migrations.png)](http://travis-ci.org/svenfuchs/data_migrations)
2
+
3
+ class CreateBuilds < ActiveRecord::Migration
4
+ def self.up
5
+ create_table :tests do |t|
6
+ # ...
7
+ end
8
+
9
+ migrate_table :builds, :to => :tests |t|
10
+ t.where 'parent_id IS NULL'
11
+ t.move :number
12
+ t.copy :parent_id, :to => :build_id
13
+ t.copy :commit, :result, :to => [:hash, :status]
14
+ t.copy :all
15
+
16
+ # t.copy :all, :except => :foo
17
+ # t.exec 'UPDATE foo ...'
18
+ # t.set :source, 'github'
19
+ end
20
+ end
21
+
22
+ def self.down
23
+ # revert the whole thing. not sure we can derive this automatically?
24
+
25
+ migrate_table :builds, :to => :tests |t|
26
+ t.move :number
27
+ t.copy :build_id, :to => :parent_id
28
+ t.copy :hash, :status, :to => [:commit, :result]
29
+ end
30
+
31
+ drop_table :tests
32
+ end
33
+ end
34
+
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib' << 'test'
7
+ t.pattern = 'test/all.rb'
8
+ t.verbose = false
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ require 'active_record'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/core_ext/array/extract_options'
4
+
5
+ module DataMigrations
6
+ autoload :Column, 'data_migrations/column'
7
+ autoload :Instruction, 'data_migrations/instruction'
8
+ autoload :Migration, 'data_migrations/migration'
9
+ autoload :Table, 'data_migrations/table'
10
+
11
+ def migrate_table(name, options, &block)
12
+ Migration.new(name, options, &block).run!
13
+ end
14
+ alias :migrate_data :migrate_table
15
+
16
+ ActiveRecord::Migration.send(:extend, self)
17
+ end
@@ -0,0 +1,47 @@
1
+ module DataMigrations
2
+ class Column
3
+ attr_reader :table, :name, :alias
4
+
5
+ def initialize(table, name, alias_ = nil)
6
+ @table = table
7
+ @name = name
8
+ @alias = alias_
9
+ end
10
+
11
+ def definition
12
+ [quoted_alias_name, type].join(' ')
13
+ end
14
+
15
+ def type
16
+ column.sql_type
17
+ end
18
+
19
+ def column
20
+ table.column(name)
21
+ end
22
+
23
+ def aliased_name
24
+ self.alias.present? ? "#{quote(name)} AS #{quote(self.alias)}" : quote(name)
25
+ end
26
+
27
+ def quoted_name
28
+ quote(name)
29
+ end
30
+
31
+ def quoted_alias_name
32
+ quote(self.alias.present? ? self.alias : name)
33
+ end
34
+
35
+ def quote_value(value)
36
+ table.connection.quote(value, column)
37
+ end
38
+
39
+ def quote(name)
40
+ table.connection.quote_column_name(name)
41
+ end
42
+
43
+ def ==(other)
44
+ name == other.name
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,9 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ autoload :Base, 'data_migrations/instruction/base'
4
+ autoload :Copy, 'data_migrations/instruction/copy'
5
+ autoload :Exec, 'data_migrations/instruction/exec'
6
+ autoload :Remove, 'data_migrations/instruction/remove'
7
+ autoload :Set, 'data_migrations/instruction/set'
8
+ end
9
+ end
@@ -0,0 +1,71 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ class Base
4
+ attr_accessor :columns, :options
5
+
6
+ delegate :connection, :to => :'ActiveRecord::Base'
7
+ delegate :condition, :to => :migration
8
+
9
+ attr_reader :migration
10
+
11
+ def initialize(migration)
12
+ @migration = migration
13
+ end
14
+
15
+ def columns=(columns)
16
+ @columns = normalize_columns(columns).map do |column, alias_|
17
+ Column.new(migration.source, column, alias_)
18
+ end
19
+ end
20
+
21
+ def execute
22
+ statements.each do |statement|
23
+ puts "Executing: #{statement}"
24
+ connection.execute(statement)
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def source
31
+ migration.source
32
+ end
33
+
34
+ def target
35
+ migration.target
36
+ end
37
+
38
+ def alias_names
39
+ columns.map(&:quoted_alias_name).join(', ')
40
+ end
41
+
42
+ def alias_setters
43
+ columns.map { |column| "#{column.quoted_alias_name} = source.#{column.quoted_name}" }.join(', ')
44
+ end
45
+
46
+ def quoted_column_names
47
+ columns.map(&:quoted_name).join(', ')
48
+ end
49
+
50
+ def aliased_column_names
51
+ columns.map(&:aliased_name).join(', ')
52
+ end
53
+
54
+ def column_definitions
55
+ columns.map(&:definition).join(', ')
56
+ end
57
+
58
+ def normalize_columns(columns)
59
+ columns = columns.map(&:to_s)
60
+ columns = source.column_names - instructed_columns if columns.include?('all')
61
+ columns -= Array(options[:except]).map(&:to_s) if options[:except]
62
+ columns.zip(Array(options[:to]).map(&:to_s))
63
+ end
64
+
65
+ def instructed_columns
66
+ migration.instructions.map(&:columns).flatten.map(&:name)
67
+ end
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,39 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ class Copy < Base
4
+ def initialize(migration, *columns)
5
+ super(migration)
6
+ self.options = columns.extract_options!
7
+ options[:to] = [nil].concat(Array(options[:to]) || [])
8
+ self.columns = ['id'].concat(columns)
9
+ end
10
+
11
+ def statements
12
+ [update_statement, insert_statement]
13
+ end
14
+
15
+ protected
16
+
17
+ def update_statement
18
+ statement = "UPDATE #{target.quoted_name} SET #{alias_setters} FROM ("
19
+ statement << "SELECT #{quoted_column_names} FROM #{source.quoted_name} "
20
+ statement << "WHERE #{source.quoted_name}.id IN (SELECT id FROM #{target.quoted_name})"
21
+ statement << " AND #{condition}" if condition
22
+ statement << ") AS source WHERE #{target.quoted_name}.id = source.id"
23
+ statement
24
+ end
25
+
26
+ def insert_statement
27
+ statement = "INSERT INTO #{target.quoted_name} (#{alias_names}) "
28
+ statement << "SELECT #{aliased_column_names} FROM #{source.quoted_name} "
29
+ statement << "WHERE #{source.quoted_name}.id NOT IN (SELECT id FROM #{target.quoted_name})"
30
+ statement << " AND #{condition}" if condition
31
+ statement
32
+ end
33
+
34
+ def instructed_columns
35
+ super - ['id']
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ class Exec < Base
4
+ attr_reader :statement
5
+
6
+ def initialize(migration, statement)
7
+ super(migration)
8
+ @statement = statement
9
+ end
10
+
11
+ def statements
12
+ [statement]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ class Remove < Base
4
+ def initialize(migration, *columns)
5
+ super(migration)
6
+ self.options = columns.extract_options!
7
+ self.columns = columns
8
+ end
9
+
10
+ def statements
11
+ remove_statements
12
+ end
13
+
14
+ protected
15
+
16
+ def remove_statements
17
+ columns.map do |column|
18
+ "ALTER TABLE #{source.quoted_name} DROP COLUMN #{column.quoted_name}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module DataMigrations
2
+ class Instruction
3
+ class Set < Base
4
+ attr_reader :column, :value
5
+
6
+ def initialize(migration, column, value)
7
+ super(migration)
8
+ @column = Column.new(migration.target, column)
9
+ @value = value
10
+ end
11
+
12
+ def statements
13
+ [update_statement]
14
+ end
15
+
16
+ protected
17
+
18
+ def update_statement
19
+ "UPDATE #{target.quoted_name} SET #{column.quoted_name} = #{column.quote_value(value)}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ module DataMigrations
2
+ class Migration
3
+ attr_reader :source, :target, :instructions
4
+
5
+ def initialize(source, options = {})
6
+ @source = Table.new(source)
7
+ @target = Table.new(options[:to])
8
+ @instructions = []
9
+
10
+ yield self
11
+ end
12
+
13
+ def run!
14
+ instructions.each(&:execute)
15
+ end
16
+
17
+ def condition(condition = nil)
18
+ condition ? @condition = condition : @condition
19
+ end
20
+ alias :where :condition
21
+
22
+ def copy(*args)
23
+ instructions << Instruction::Copy.new(self, *args)
24
+ end
25
+
26
+ def move(*args)
27
+ copy(*args)
28
+ remove(*args)
29
+ end
30
+
31
+ def remove(*args)
32
+ instructions << Instruction::Remove.new(self, *args)
33
+ end
34
+
35
+ def set(*args)
36
+ instructions << Instruction::Set.new(self, *args)
37
+ end
38
+
39
+ def exec(*args)
40
+ instructions << Instruction::Exec.new(self, *args)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ module DataMigrations
2
+ module Setup
3
+ def setup_data_migrations
4
+ unless @setup
5
+ install_upsert
6
+ @setup = true
7
+ end
8
+ end
9
+
10
+ def install_upsert
11
+ ActiveRecord::Base.connection.execute <<-sql
12
+ CREATE FUNCTION upsert (sql_update TEXT, sql_insert TEXT)
13
+ RETURNS VOID
14
+ LANGUAGE plpgsql
15
+ AS $$
16
+ BEGIN
17
+ LOOP
18
+ -- first try to update
19
+ EXECUTE sql_update;
20
+ -- check if the row is found
21
+ IF FOUND THEN
22
+ RETURN;
23
+ END IF;
24
+ -- not found so insert the row
25
+ BEGIN
26
+ EXECUTE sql_insert;
27
+ RETURN;
28
+ EXCEPTION WHEN unique_violation THEN
29
+ -- do nothing and loop
30
+ END;
31
+ END LOOP;
32
+ END;
33
+ $$;
34
+ sql
35
+ rescue ActiveRecord::StatementInvalid
36
+ # ignore duplicate installs
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ module DataMigrations
2
+ class Table
3
+ delegate :connection, :to => :'ActiveRecord::Base'
4
+
5
+ attr_reader :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+
11
+ def columns
12
+ connection.columns(name)
13
+ end
14
+
15
+ def column_names
16
+ columns.map(&:name)
17
+ end
18
+
19
+ def column(name)
20
+ columns.detect { |column| column.name.to_s == name.to_s } || raise_column_not_found(name)
21
+ end
22
+
23
+ def quoted_name
24
+ connection.quote_table_name(name)
25
+ end
26
+
27
+ def raise_column_not_found(name)
28
+ raise "could not find column #{name} on #{self.name}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module DataMigrations
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sven Fuchs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70269578402120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70269578402120
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70269578401300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70269578401300
36
+ - !ruby/object:Gem::Dependency
37
+ name: test_declarative
38
+ requirement: &70269578400700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70269578400700
47
+ - !ruby/object:Gem::Dependency
48
+ name: capture_stdout
49
+ requirement: &70269578416160 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70269578416160
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ requirement: &70269578415520 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70269578415520
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: &70269578414500 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70269578414500
80
+ description: ! '[description]'
81
+ email: svenfuchs@artweb-design.de
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - lib/data_migrations/column.rb
87
+ - lib/data_migrations/instruction/base.rb
88
+ - lib/data_migrations/instruction/copy.rb
89
+ - lib/data_migrations/instruction/exec.rb
90
+ - lib/data_migrations/instruction/remove.rb
91
+ - lib/data_migrations/instruction/set.rb
92
+ - lib/data_migrations/instruction.rb
93
+ - lib/data_migrations/migration.rb
94
+ - lib/data_migrations/setup.rb
95
+ - lib/data_migrations/table.rb
96
+ - lib/data_migrations/version.rb
97
+ - lib/data_migrations.rb
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - LICENSE
101
+ - Rakefile
102
+ - README.md
103
+ homepage: https://github.com/svenfuchs/data_migrations
104
+ licenses: []
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project: ! '[none]'
123
+ rubygems_version: 1.8.6
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: ! '[summary]'
127
+ test_files: []