data_transformation 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ module DataTransformation
2
+ class Schema < DataTransformation::Transformation
3
+ def self.define(info={})
4
+ unless info[:version].blank?
5
+ initialize_schema_migrations_table
6
+ assume_migrated_upto_version(info[:version], DataTransformation::Transformer.migrations_path)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -27,11 +27,10 @@ module DataTransformation
27
27
  def rollback
28
28
  raise "Unimplemented as a Transformation"
29
29
  end
30
- end
31
-
32
30
 
33
- def schema_migrations_table_name
34
- ActiveRecord::Base.table_name_prefix + "schema_transforms" + ActiveRecord::Base.table_name_suffix
31
+ def schema_migrations_table_name
32
+ ActiveRecord::Base.table_name_prefix + "schema_transforms" + ActiveRecord::Base.table_name_suffix
33
+ end
35
34
  end
36
35
  end
37
36
  end
@@ -1,3 +1,3 @@
1
1
  module DataTransformations
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "data_transformation/version"
2
2
  require 'data_transformation/transformer'
3
3
  require "data_transformation/transformation"
4
+ require "data_transformation/schema"
4
5
 
5
6
  module DataTransformation
6
7
  class Railtie < Rails::Railtie
@@ -5,7 +5,7 @@ Description:
5
5
  A transform class is generated in db/transforms prefixed by a timestamp of the current date and time.
6
6
 
7
7
  Example:
8
- `rails generate data_transformation PopulateUsers`
8
+ `rails generate transform PopulateUsers`
9
9
 
10
10
  If the current date is May 14, 2008 and the current time 09:09:12, this creates the PopulateUsers transformation
11
11
  db/tranforms/20080514090912_populate_users.rb
@@ -2,7 +2,7 @@ require 'rails/generators'
2
2
  require 'rails/generators/active_record'
3
3
  require 'fileutils'
4
4
 
5
- class DataTransformationGenerator < Rails::Generators::NamedBase
5
+ class TransformGenerator < Rails::Generators::NamedBase
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  def self.source_root
@@ -1,42 +1,77 @@
1
+ desc "Hacks in the transform to migrations"
2
+ task :hack_in_ar do
3
+ class << ActiveRecord::Migrator
4
+ alias_method :original_schema_migrations_table_name, :schema_migrations_table_name
5
+ def schema_migrations_table_name
6
+ ActiveRecord::Base.table_name_prefix + "schema_transforms" + ActiveRecord::Base.table_name_suffix
7
+ end
8
+ end
9
+
10
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
11
+ alias_method :original_initialize_schema_migrations_table, :initialize_schema_migrations_table
12
+ def initialize_schema_migrations_table
13
+ sm_table = ActiveRecord::Migrator.schema_migrations_table_name
14
+
15
+ unless table_exists?(sm_table)
16
+ create_table(sm_table, :id => false) do |schema_migrations_table|
17
+ schema_migrations_table.column :version, :string, :null => false
18
+ end
19
+ add_index sm_table, :version, :unique => true,
20
+ :name => "#{ActiveRecord::Base.table_name_prefix}unique_schema_transforms#{ActiveRecord::Base.table_name_suffix}"
21
+
22
+ # Backwards-compatibility: if we find schema_info, assume we've
23
+ # migrated up to that point:
24
+ si_table = ActiveRecord::Base.table_name_prefix + 'schema_info' + ActiveRecord::Base.table_name_suffix
25
+
26
+ if table_exists?(si_table)
27
+ old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i
28
+ assume_migrated_upto_version(old_version)
29
+ drop_table(si_table)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ desc "Undoes the hack in Active Record for the migrations"
37
+ task :undo_hack_in_ar do
38
+ class << ActiveRecord::Migrator
39
+ alias_method :schema_migrations_table_name, :original_schema_migrations_table_name
40
+ end
41
+
42
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
43
+ alias_method :initialize_schema_migrations_table, :original_initialize_schema_migrations_table
44
+ end
45
+ end
46
+
1
47
  namespace :db do
2
48
  desc "Transform the database (options: VERSION=x, VERBOSE=false)."
3
- task :transform => :environment do
4
- module ActiveRecord
5
- class Migrator
6
- def self.schema_migrations_table_name
7
- Base.table_name_prefix + "schema_transforms" + Base.table_name_suffix
8
- end
49
+ task :transform => [:environment, :hack_in_ar] do
50
+ DataTransformation::Transformation.verbose = ENV['VERBOSE'] ? ENV['VERBOSE'] == "true" : true
51
+ DataTransformation::Transformer.transform('db/transforms/', ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
52
+ Rake::Task["db:transform:dump"].invoke
53
+ end
54
+
55
+ namespace :transform do
56
+ desc "Dump the Transform schema."
57
+ task :dump => [:environment, :hack_in_ar] do
58
+ filename = ENV['SCHEMA'] || "#{Rails.root}/db/transform_schema.rb"
59
+ File.open(filename, 'w') do |f|
60
+ version = DataTransformation::Transformer::current_version rescue nil
61
+ f.puts "DataTransformation::Schema.define(:version => #{version})"
9
62
  end
63
+ Rake::Task['undo_hack_in_ar'].invoke
10
64
  end
11
65
 
12
- module ActiveRecord
13
- module ConnectionAdapters
14
- module SchemaStatements
15
- def initialize_schema_migrations_table
16
- sm_table = ActiveRecord::Migrator.schema_migrations_table_name
17
-
18
- unless table_exists?(sm_table)
19
- create_table(sm_table, :id => false) do |schema_migrations_table|
20
- schema_migrations_table.column :version, :string, :null => false
21
- end
22
- add_index sm_table, :version, :unique => true,
23
- :name => "#{Base.table_name_prefix}unique_schema_transforms#{Base.table_name_suffix}"
24
-
25
- # Backwards-compatibility: if we find schema_info, assume we've
26
- # migrated up to that point:
27
- si_table = Base.table_name_prefix + 'schema_info' + Base.table_name_suffix
28
-
29
- if table_exists?(si_table)
30
- old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i
31
- assume_migrated_upto_version(old_version)
32
- drop_table(si_table)
33
- end
34
- end
35
- end
36
- end
66
+ desc "Load the Transform schema."
67
+ task :load => [:environment, :hack_in_ar] do
68
+ filename = ENV['SCHEMA'] || "#{Rails.root}/db/transform_schema.rb"
69
+ if File.exists?(filename)
70
+ load(filename)
71
+ else
72
+ abort %{#{filename} doesn't exist yet. Run 'rake db:transform' to create it and then try again.}
37
73
  end
74
+ Rake::Task['undo_hack_in_ar'].invoke
38
75
  end
39
- DataTransformation::Transformation.verbose = ENV['VERBOSE'] ? ENV['VERBOSE'] == "true" : true
40
- DataTransformation::Transformer.transform('db/transforms/', ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
41
76
  end
42
77
  end
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,7 @@ require 'rails/all'
5
5
  require 'rspec'
6
6
 
7
7
  require 'data_transformation'
8
- require 'generators/data_transformation/data_transformation_generator'
8
+ require 'generators/transform/transform_generator'
9
9
 
10
10
  Dir[File.expand_path(File.dirname(__FILE__) + '/db')+"/*/*.rb"].each { |f| require f}
11
11
  ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_transformation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-06 00:00:00.000000000 -07:00
13
- default_executable:
12
+ date: 2011-09-12 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activerecord
17
- requirement: &2153460420 !ruby/object:Gem::Requirement
16
+ requirement: &2151892640 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: 3.0.0
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2153460420
24
+ version_requirements: *2151892640
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: bundler
28
- requirement: &2153459960 !ruby/object:Gem::Requirement
27
+ requirement: &2151891580 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 1.0.0
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *2153459960
35
+ version_requirements: *2151891580
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: rspec
39
- requirement: &2153459500 !ruby/object:Gem::Requirement
38
+ requirement: &2151890260 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ~>
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: 2.5.0
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *2153459500
46
+ version_requirements: *2151890260
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: generator_spec
50
- requirement: &2153459040 !ruby/object:Gem::Requirement
49
+ requirement: &2151889140 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ~>
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: 0.8.2
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *2153459040
57
+ version_requirements: *2151889140
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: sqlite3
61
- requirement: &2153458660 !ruby/object:Gem::Requirement
60
+ requirement: &2151887900 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
63
  - - ! '>='
@@ -66,7 +65,7 @@ dependencies:
66
65
  version: '0'
67
66
  type: :development
68
67
  prerelease: false
69
- version_requirements: *2153458660
68
+ version_requirements: *2151887900
70
69
  description: To keep the necessity of changing data out of migrations, transformations
71
70
  keep track of that
72
71
  email:
@@ -82,19 +81,19 @@ files:
82
81
  - Rakefile
83
82
  - data_transformation.gemspec
84
83
  - lib/data_transformation.rb
84
+ - lib/data_transformation/schema.rb
85
85
  - lib/data_transformation/transformation.rb
86
86
  - lib/data_transformation/transformer.rb
87
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
88
+ - lib/generators/transform/USAGE
89
+ - lib/generators/transform/templates/transform.rb
90
+ - lib/generators/transform/transform_generator.rb
91
91
  - lib/tasks/data_transformations.rake
92
92
  - spec/data_transformation_spec.rb
93
93
  - spec/database.yml
94
94
  - spec/db/migrate/123_create_person.rb
95
95
  - spec/db/transforms/123_change_peeps.rb
96
96
  - spec/spec_helper.rb
97
- has_rdoc: true
98
97
  homepage: https://github.com/inigral/data_transformation
99
98
  licenses: []
100
99
  post_install_message:
@@ -115,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
114
  version: 1.3.6
116
115
  requirements: []
117
116
  rubyforge_project: data_transformation
118
- rubygems_version: 1.6.2
117
+ rubygems_version: 1.8.10
119
118
  signing_key:
120
119
  specification_version: 3
121
120
  summary: Rails 3 Data Transformations