data_transformation 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/debug.log
6
+ *.swp
7
+ *.sqlite3
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use data_transformation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in data_transformations.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,7 @@
1
+ == Using the Migrator
2
+ = It's just like migrating, moreso than you probably originally thought.
3
+
4
+ rails g data_transformation transform_name = make the transform
5
+
6
+ rake db:transform = run the unrun transforms
7
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "data_transformation/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "data_transformation"
7
+ s.version = DataTransformations::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Chris Barton"]
10
+ s.email = ["cbarton@inigral.com"]
11
+ s.homepage = "https://github.com/inigral/data_transformation"
12
+ s.summary = "Rails 3 Data Transformations"
13
+ s.description = "To keep the necessity of changing data out of migrations, transformations keep track of that"
14
+
15
+ s.rubyforge_project = "data_transformation"
16
+ s.required_rubygems_version = ">= 1.3.6"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_path = "lib"
22
+
23
+ s.add_dependency "activerecord", "~> 3.0.0"
24
+ s.add_development_dependency "bundler", "~> 1.0.0"
25
+ s.add_development_dependency "rspec", "~> 2.5.0"
26
+ s.add_development_dependency "generator_spec", "~> 0.8.2"
27
+ s.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record/migration'
2
+
3
+ module DataTransformation
4
+ class Transformation < ActiveRecord::Migration
5
+ def transform(direction)
6
+ migrate(direction)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_record/migration'
2
+
3
+ module DataTransformation
4
+ class Transformer < ActiveRecord::Migrator
5
+ class << self
6
+ def transform(transforms_path, target_version=nil)
7
+ migrate(transforms_path, target_version)
8
+ end
9
+
10
+ def migrations_paths
11
+ @migrations_paths ||= ['db/transforms']
12
+ Array.wrap(@migrations_paths)
13
+ end
14
+
15
+ def migrations_path
16
+ migrations_paths.first
17
+ end
18
+
19
+ def down
20
+ raise "Unimplemented as a Transformation"
21
+ end
22
+
23
+ def forward
24
+ raise "Unimplemented as a Transformation"
25
+ end
26
+
27
+ def rollback
28
+ raise "Unimplemented as a Transformation"
29
+ end
30
+ end
31
+
32
+
33
+ def schema_migrations_table_name
34
+ ActiveRecord::Base.table_name_prefix + "schema_transforms" + ActiveRecord::Base.table_name_suffix
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module DataTransformations
2
+ VERSION = "1.0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "data_transformation/version"
2
+ require 'data_transformation/transformer'
3
+ require "data_transformation/transformation"
4
+
5
+ module DataTransformation
6
+ class Railtie < Rails::Railtie
7
+ rake_tasks do
8
+ load 'tasks/data_transformations.rake'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Stubs out a new database data transformation. Pass the transform name, either
3
+ CamelCased or under_scored, and an optional list of attribute pairs as arguments.
4
+
5
+ A transform class is generated in db/transforms prefixed by a timestamp of the current date and time.
6
+
7
+ Example:
8
+ `rails generate data_transformation PopulateUsers`
9
+
10
+ If the current date is May 14, 2008 and the current time 09:09:12, this creates the PopulateUsers transformation
11
+ db/tranforms/20080514090912_populate_users.rb
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+ require 'fileutils'
4
+
5
+ class DataTransformationGenerator < Rails::Generators::NamedBase
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.dirname(__FILE__) + '/templates'
10
+ end
11
+
12
+ def create_transform_file
13
+ create_transforms_folder
14
+ migration_template "transform.rb", "db/transforms/#{file_name}.rb"
15
+ end
16
+
17
+ def self.next_migration_number(dirname)
18
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
19
+ end
20
+
21
+ def create_transforms_folder
22
+ FileUtils.mkdir("db/transforms") unless Dir.exists?("db/transforms")
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ class <%= migration_class_name %> < DataTransformation::Transformation
2
+ def self.up
3
+ end
4
+ end
@@ -0,0 +1,46 @@
1
+ namespace :db do
2
+ task :load_config => :rails_env do
3
+ DataTransformation::Transformer.migration_paths = Rails.application.paths['db/transforms'].to_a
4
+ end
5
+
6
+ desc "Transform the database (options: VERSION=x, VERBOSE=false)."
7
+ task :transform => :environment do
8
+ module ActiveRecord
9
+ class Migrator
10
+ def self.schema_migrations_table_name
11
+ Base.table_name_prefix + "schema_transforms" + Base.table_name_suffix
12
+ end
13
+ end
14
+ end
15
+
16
+ module ActiveRecord
17
+ module ConnectionAdapters
18
+ module SchemaStatements
19
+ def initialize_schema_migrations_table
20
+ sm_table = ActiveRecord::Migrator.schema_migrations_table_name
21
+
22
+ unless table_exists?(sm_table)
23
+ create_table(sm_table, :id => false) do |schema_migrations_table|
24
+ schema_migrations_table.column :version, :string, :null => false
25
+ end
26
+ add_index sm_table, :version, :unique => true,
27
+ :name => "#{Base.table_name_prefix}unique_schema_transforms#{Base.table_name_suffix}"
28
+
29
+ # Backwards-compatibility: if we find schema_info, assume we've
30
+ # migrated up to that point:
31
+ si_table = Base.table_name_prefix + 'schema_info' + Base.table_name_suffix
32
+
33
+ if table_exists?(si_table)
34
+ old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i
35
+ assume_migrated_upto_version(old_version)
36
+ drop_table(si_table)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ DataTransformation::Transformation.verbose = ENV['VERBOSE'] ? ENV['VERBOSE'] == "true" : true
44
+ DataTransformation::Transformer.transform('db/transforms/', ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
45
+ end
46
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ class Person < ActiveRecord::Base
4
+ set_table_name :persons
5
+ end
6
+
7
+ describe DataTransformation do
8
+ before(:all) do
9
+ CreatePerson.up
10
+ end
11
+ after(:all) do
12
+ CreatePerson.down
13
+ end
14
+
15
+ it "should transform the data" do
16
+ Person.first.name.should == "Bob Lee"
17
+ ChangePeeps.up
18
+ Person.first.name.should == "George Lee"
19
+ end
20
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: data_transformation.sqlite3
@@ -0,0 +1,13 @@
1
+ class CreatePerson < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :persons do |t|
4
+ t.string :name
5
+ end
6
+
7
+ Person.create(:name => "Bob Lee")
8
+ end
9
+
10
+ def self.down
11
+ drop_table :persons
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ class ChangePeeps < DataTransformation::Transformation
2
+ def self.up
3
+ p = Person.find_by_name("Bob Lee")
4
+ p.update_attribute(:name, "George Lee")
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'logger'
4
+ require 'rails/all'
5
+ require 'rspec'
6
+
7
+ require 'data_transformation'
8
+ require 'generators/data_transformation/data_transformation_generator'
9
+
10
+ Dir[File.expand_path(File.dirname(__FILE__) + '/db')+"/*/*.rb"].each { |f| require f}
11
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
12
+ ActiveRecord::Base.configurations = YAML::load_file(File.dirname(__FILE__) + '/database.yml')
13
+ ActiveRecord::Base.establish_connection(ENV['DB'] || 'sqlite3')
14
+
15
+ RSpec.configure do |config|
16
+ config.mock_with :rspec
17
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_transformation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Barton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-05 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: &2157659100 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2157659100
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &2157658640 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2157658640
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2157658180 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.5.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2157658180
48
+ - !ruby/object:Gem::Dependency
49
+ name: generator_spec
50
+ requirement: &2157657720 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 0.8.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2157657720
59
+ - !ruby/object:Gem::Dependency
60
+ name: sqlite3
61
+ requirement: &2157657340 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *2157657340
70
+ description: To keep the necessity of changing data out of migrations, transformations
71
+ keep track of that
72
+ email:
73
+ - cbarton@inigral.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .rvmrc
80
+ - Gemfile
81
+ - README
82
+ - Rakefile
83
+ - data_transformation.gemspec
84
+ - lib/data_transformation.rb
85
+ - lib/data_transformation/transformation.rb
86
+ - lib/data_transformation/transformer.rb
87
+ - lib/data_transformation/version.rb
88
+ - lib/generators/data_transformation/USAGE
89
+ - lib/generators/data_transformation/data_transformation_generator.rb
90
+ - lib/generators/data_transformation/templates/transform.rb
91
+ - lib/tasks/data_transformations.rake
92
+ - spec/data_transformation_spec.rb
93
+ - spec/database.yml
94
+ - spec/db/migrate/123_create_person.rb
95
+ - spec/db/transforms/123_change_peeps.rb
96
+ - spec/spec_helper.rb
97
+ has_rdoc: true
98
+ homepage: https://github.com/inigral/data_transformation
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: 1.3.6
116
+ requirements: []
117
+ rubyforge_project: data_transformation
118
+ rubygems_version: 1.6.2
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Rails 3 Data Transformations
122
+ test_files:
123
+ - spec/data_transformation_spec.rb
124
+ - spec/database.yml
125
+ - spec/db/migrate/123_create_person.rb
126
+ - spec/db/transforms/123_change_peeps.rb
127
+ - spec/spec_helper.rb