migrer 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -51,7 +51,7 @@ Create a data migration:
51
51
 
52
52
  $ bundle exec rails generate data_migration MyFirstDataMigration "optional description"
53
53
 
54
- This will create the file: lib/tasks/data_migrations/<timestamp>_my_first_data_migration.rb
54
+ This will create the file: db/data_migrate/<timestamp>_my_first_data_migration.rb
55
55
 
56
56
  Open this file and replace "TODO" with your data migration code!
57
57
 
@@ -89,6 +89,10 @@ ActiveRecord migrations):**
89
89
 
90
90
  bundle exec rake data:unmark VERSION=<version>
91
91
 
92
+ **View all unprocessed data migrations by filename:**
93
+
94
+ bundle exec rake data:pending
95
+
92
96
  ## Contributing
93
97
 
94
98
  1. Fork it
@@ -1,13 +1,15 @@
1
- class DataMigration
2
- def self.all
3
- filenames = Dir.entries("#{Rails.root}/lib/tasks/data_migrations").select { |f| /^\d+.*\.rb$/ === f }
1
+ class Migrer::DataMigrationVersion < ActiveRecord::Base
2
+
3
+ attr_accessible :version
4
+
5
+ def self.all_from_files
6
+ filenames = Dir.entries("#{Rails.root}/db/data_migrate").select { |f| /^\d+.*\.rb$/ === f }
4
7
  data_migrations = {}
5
8
 
6
9
  filenames.each do |f|
7
10
  match_data = /^(?<version>\d+)_(?<name>.+)\.rb$/.match(f)
8
11
 
9
- record = ActiveRecord::Base.connection.execute(
10
- "SELECT * FROM data_migration_versions WHERE version = '#{match_data[:version]}'").first
12
+ record = Migrer::DataMigrationVersion.find_by_version(match_data[:version])
11
13
 
12
14
  data_migrations.merge!(
13
15
  match_data[:version] => {
@@ -8,4 +8,4 @@ Example:
8
8
  rails generate data_migration DataMigrationName "Optional description"
9
9
 
10
10
  This will create:
11
- lib/tasks/data_migrations/<timestamp>_data_migration_name.rb
11
+ db/data_migrate/<timestamp>_data_migration_name.rb
@@ -6,11 +6,11 @@ class DataMigrationGenerator < Rails::Generators::NamedBase
6
6
 
7
7
  def generate_data_migration
8
8
  template "data_migration.rb",
9
- "lib/tasks/data_migrations/#{file_name}"
9
+ "db/data_migrate/#{file_name}"
10
10
  end
11
11
 
12
12
  private
13
13
  def file_name
14
- "#{ActiveRecord::Generators::Base.next_migration_number('lib/tasks/data_migrations')}_#{name.underscore}.rb"
14
+ "#{ActiveRecord::Generators::Base.next_migration_number('db/data_migrate')}_#{name.underscore}.rb"
15
15
  end
16
16
  end
@@ -1,8 +1,6 @@
1
- class <%= name %> < DataMigration
2
-
3
- <%= "# #{description}" %>
4
-
5
- def self.run
6
- #TODO: data_migration code
7
- end
8
- end
1
+ class <%= name %>
2
+ <%= "\n # #{description}\n" if description.present? %>
3
+ def self.run
4
+ #TODO: data_migration code
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ class String
2
+ def migrer_green
3
+ "\033[32m#{self}\033[0m"
4
+ end
5
+
6
+ def migrer_red
7
+ "\033[31m#{self}\033[0m"
8
+ end
9
+
10
+ def migrer_yellow
11
+ "\033[33m#{self}\033[0m"
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Migrer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/migrer.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "migrer/version"
2
2
 
3
3
  module Migrer
4
+ def self.table_name_prefix
5
+ ''
6
+ end
4
7
  end
5
8
 
6
9
  # Require our engine
@@ -1,19 +1,19 @@
1
- require 'data_migration'
1
+ require 'migrer/ansi_colors'
2
2
 
3
3
  namespace :data do
4
- desc "Data migration tasks"
5
4
 
5
+ desc 'Run all unprocessed data migrations or a single data migration if VERSION is specified.'
6
6
  task migrate: :environment do
7
- data_migrations = DataMigration.all
7
+ data_migrations = Migrer::DataMigrationVersion.all_from_files
8
8
 
9
9
  if (version = ENV['VERSION'])
10
10
  data_migration = data_migrations[version]
11
11
 
12
12
  if data_migration.present?
13
13
  if data_migration[:processed]
14
- puts "Data migration already processed. Do you want to run it anyway? (responses other than 'yes' will exit)"
14
+ puts "Data migration already processed. Do you want to run it anyway? (responses other than 'yes' will exit)".migrer_yellow
15
15
  else
16
- puts "Starting data migration #{data_migration[:class_name]}. Do you wish to continue? (responses other than 'yes' will exit)"
16
+ puts "Starting data migration #{data_migration[:class_name]}. Do you wish to continue? (responses other than 'yes' will exit)".migrer_yellow
17
17
  end
18
18
 
19
19
  prompt = $stdin.gets.chomp
@@ -22,134 +22,142 @@ namespace :data do
22
22
  puts "#{data_migration[:class_name]}: migrating"
23
23
  t_start = Time.now
24
24
 
25
- require "#{Rails.root}/lib/tasks/data_migrations/#{data_migration[:basefilename]}"
25
+ require "#{Rails.root}/db/data_migrate/#{data_migration[:basefilename]}"
26
26
  eval(data_migration[:class_name]).run
27
27
 
28
28
  t_end = Time.now
29
29
 
30
30
  unless data_migration[:processed]
31
- ActiveRecord::Base.connection.execute(
32
- "INSERT INTO data_migration_versions
33
- VALUES (NULL, '#{version}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" )
31
+ Migrer::DataMigrationVersion.create(version: version)
34
32
  end
35
33
 
36
- puts "#{data_migration[:class_name]}: migrated (#{t_end - t_start}s)"
34
+ puts "#{data_migration[:class_name]}:" + " migrated (#{t_end - t_start}s)".migrer_green
37
35
  end
38
36
  else
39
- puts "No data migration found matching version: #{version}"
37
+ puts "No data migration found matching version: #{version}".migrer_red
40
38
  end
41
39
  else
42
40
  data_migrations.each do |k, v|
43
41
  unless v[:processed]
44
- puts "Starting data migration #{v[:class_name]}. Do you wish to continue? (responses other than 'yes' will exit)"
42
+ puts "Starting data migration #{v[:class_name]}. Do you wish to continue? (responses other than 'yes' will exit)".migrer_yellow
45
43
  prompt = $stdin.gets.chomp
46
44
  if prompt == "yes"
47
45
  puts "#{v[:class_name]}: migrating"
48
46
  t_start = Time.now
49
47
 
50
- require "#{Rails.root}/lib/tasks/data_migrations/#{v[:basefilename]}"
48
+ require "#{Rails.root}/db/data_migrate/#{v[:basefilename]}"
51
49
  eval(v[:class_name]).run
52
50
 
53
51
  t_end = Time.now
54
52
 
55
- ActiveRecord::Base.connection.execute(
56
- "INSERT INTO data_migration_versions
57
- VALUES (NULL, '#{k}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" )
53
+ Migrer::DataMigrationVersion.create(version: k)
58
54
 
59
- puts "#{v[:class_name]}: migrated (#{t_end - t_start}s)"
55
+ puts "#{v[:class_name]}:" + " migrated (#{t_end - t_start}s)".migrer_green
60
56
  end
61
57
  end
62
58
  end
63
59
  end
64
60
  end
65
61
 
62
+ desc 'Mark a single migration as already processed (requires VERSION).'
66
63
  task mark: :environment do
67
- data_migrations = DataMigration.all
64
+ data_migrations = Migrer::DataMigrationVersion.all_from_files
68
65
 
69
66
  if (version = ENV['VERSION'])
70
67
  data_migration = data_migrations[version]
71
68
 
72
69
  if data_migration.present?
73
70
  if data_migration[:processed]
74
- puts "Data migration already processed."
71
+ puts "Data migration already processed.".migrer_yellow
75
72
  else
76
- puts "Data migration #{data_migration[:class_name]} will be marked as processed. Continue? (responses other than 'yes' will exit)"
73
+ puts "Data migration #{data_migration[:class_name]} will be marked as processed. Continue? (responses other than 'yes' will exit)".migrer_yellow
77
74
 
78
75
  prompt = $stdin.gets.chomp
79
76
 
80
77
  if prompt == "yes"
81
- ActiveRecord::Base.connection.execute(
82
- "INSERT INTO data_migration_versions
83
- VALUES (NULL, '#{version}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" )
84
-
85
- puts "#{data_migration[:class_name]}: marked as migrated"
78
+ Migrer::DataMigrationVersion.create(version: version)
79
+ puts "#{data_migration[:class_name]}:" + " marked as migrated".migrer_green
86
80
  end
87
81
  end
88
82
  else
89
- puts "No data migration found matching version: #{version}"
83
+ puts "No data migration found matching version: #{version}".migrer_red
90
84
  end
91
85
  else
92
- puts "VERSION must be supplied."
86
+ puts "VERSION must be supplied.".migrer_red
93
87
  end
94
88
  end
95
89
 
90
+ desc 'Mark all data migrations as already processed.'
96
91
  task mark_all: :environment do
97
- unprocessed_data_migrations = DataMigration.all.select { |k, v| !v[:processed] }
92
+ unprocessed_data_migrations = Migrer::DataMigrationVersion.all_from_files.select { |k, v| !v[:processed] }
98
93
 
99
- puts "This will mark all data migrations as already processed. Continue? (responses other than 'yes' will exit)"
94
+ puts "This will mark all data migrations as already processed. Continue? (responses other than 'yes' will exit)".migrer_yellow
100
95
 
101
96
  prompt = $stdin.gets.chomp
102
97
  if prompt == "yes"
103
98
  unprocessed_data_migrations.each do |k, v|
104
- ActiveRecord::Base.connection.execute(
105
- "INSERT INTO data_migration_versions
106
- VALUES (NULL, '#{k}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" )
107
-
108
- puts "#{v[:class_name]}: marked as migrated"
99
+ Migrer::DataMigrationVersion.create(version: k)
100
+ puts "#{v[:class_name]}:" + " marked as migrated".migrer_green
109
101
  end
110
102
  end
111
103
  end
112
104
 
105
+ desc 'Revert a data migration to an unprocessed state (requires VERSION).'
113
106
  task unmark: :environment do
114
- data_migrations = DataMigration.all
107
+ data_migrations = Migrer::DataMigrationVersion.all_from_files
115
108
 
116
109
  if (version = ENV['VERSION'])
117
110
  data_migration = data_migrations[version]
118
111
 
119
112
  if data_migration.present?
120
113
  if !data_migration[:processed]
121
- puts "Data migration not yet processed."
114
+ puts "Data migration not yet processed.".migrer_yellow
122
115
  else
123
- puts "Data migration #{data_migration[:class_name]} will be unmarked as processed. Continue? (responses other than 'yes' will exit)"
116
+ puts "Data migration #{data_migration[:class_name]} will be unmarked as processed. Continue? (responses other than 'yes' will exit)".migrer_yellow
124
117
 
125
118
  prompt = $stdin.gets.chomp
126
119
 
127
120
  if prompt == "yes"
128
- ActiveRecord::Base.connection.execute(
129
- "DELETE FROM data_migration_versions
130
- WHERE version = '#{version}'" )
131
-
132
- puts "#{data_migration[:class_name]}: unmarked as migrated"
121
+ Migrer::DataMigrationVersion.find_by_version(version).destroy
122
+ puts "#{data_migration[:class_name]}:" + " unmarked as migrated".migrer_green
133
123
  end
134
124
  end
135
125
  else
136
- puts "No data migration found matching version: #{version}"
126
+ puts "No data migration found matching version: #{version}".migrer_red
137
127
  end
138
128
  else
139
- puts "VERSION must be supplied."
129
+ puts "VERSION must be supplied.".migrer_red
140
130
  end
141
131
  end
142
132
 
133
+ desc 'Revert all data migrations to an unprocessed state.'
143
134
  task unmark_all: :environment do
144
- puts "All data migrations will be unmarked as processed. Continue? (responses other than 'yes' will exit)"
135
+ puts "All data migrations will be unmarked as processed. Continue? (responses other than 'yes' will exit)".migrer_yellow
145
136
 
146
137
  prompt = $stdin.gets.chomp
147
138
 
148
139
  if prompt == "yes"
149
- ActiveRecord::Base.connection.execute(
150
- "DELETE FROM data_migration_versions" )
140
+ Migrer::DataMigrationVersion.destroy_all
141
+ puts "Data migration records cleared".migrer_green
142
+ end
143
+ end
151
144
 
152
- puts "Data migration records cleared"
145
+ desc 'View all unprocessed data migrations by filename.'
146
+ task pending: :environment do
147
+ data_migrations = Migrer::DataMigrationVersion.all_from_files
148
+ pending = data_migrations.reject {|k, v| v[:processed]}
149
+
150
+ if data_migrations.present?
151
+ if pending.present?
152
+ puts "The following migrations have not yet been processed:".migrer_yellow
153
+ pending.each do |k, v|
154
+ puts v[:filename]
155
+ end
156
+ else
157
+ puts "All existing data migrations have been processed.".migrer_yellow
158
+ end
159
+ else
160
+ puts "No data migrations found.".migrer_yellow
153
161
  end
154
162
  end
155
163
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: migrer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-13 00:00:00.000000000 Z
13
+ date: 2013-01-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -40,12 +40,13 @@ files:
40
40
  - LICENSE.txt
41
41
  - README.md
42
42
  - Rakefile
43
+ - app/models/migrer/data_migration_version.rb
43
44
  - db/migrate/20121229002105_create_migrer_table.rb
44
- - lib/data_migration.rb
45
45
  - lib/generators/data_migration/USAGE
46
46
  - lib/generators/data_migration/data_migration_generator.rb
47
47
  - lib/generators/data_migration/templates/data_migration.rb
48
48
  - lib/migrer.rb
49
+ - lib/migrer/ansi_colors.rb
49
50
  - lib/migrer/engine.rb
50
51
  - lib/migrer/version.rb
51
52
  - lib/tasks/migrer.rake