public
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack.git
Click here to lend your support to: mack and make a donation at www.pledgie.com !
Further refactored db rake tasks. Added db:abort_if_pending_migrations 
rake task.
markbates (author)
Sun Apr 13 07:46:28 -0700 2008
commit  2d4fad9d258dd5f2b041ac03c8ed37f6ae1ad633
tree    c6694a1b79f6c8c61e9297db3cda0990b55b82da
parent  6a333a4b75d56fca60c2bb98ee6d294a4d755964
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
19
20
21
 
 
22
23
24
 
25
26
27
28
29
30
31
32
33
34
35
 
36
37
38
39
40
41
42
43
44
45
 
46
47
48
 
 
49
50
51
52
53
54
55
56
57
58
59
 
 
 
 
 
 
 
 
 
60
61
 
 
 
 
 
62
63
64
...
71
72
73
 
74
75
76
...
84
85
86
 
87
88
89
...
102
103
104
105
106
107
108
109
110
111
112
113
114
...
2
3
4
 
 
 
 
 
 
 
 
 
5
6
7
8
 
9
10
 
 
11
12
13
 
 
14
15
16
17
 
 
 
 
 
 
 
 
18
19
20
21
22
23
24
 
 
 
 
25
26
 
 
27
28
29
30
31
32
33
 
 
 
 
 
 
34
35
36
37
38
39
40
41
42
43
 
44
45
46
47
48
49
50
51
...
58
59
60
61
62
63
64
...
72
73
74
75
76
77
78
...
91
92
93
 
 
 
 
 
 
 
 
94
95
0
@@ -2,63 +2,50 @@ namespace :db do
0
   
0
   desc "Migrate the database through scripts in db/migrations"
0
   task :migrate => "db:schema:create" do
0
-
0
- if using_data_mapper?
0
- require 'data_mapper/migration'
0
- schema_info = data_mapper_schema_info
0
- elsif using_active_record?
0
- require 'active_record/migration'
0
- schema_info = active_record_schema_info
0
- end
0
-
0
     migration_files.each do |migration|
0
       require migration
0
       migration = File.basename(migration, ".rb")
0
       m_number = migration_number(migration)
0
- if m_number > schema_info.version
0
+ if m_number > @schema_info.version
0
         migration_name(migration).camelcase.constantize.up
0
- schema_info.version += 1
0
- schema_info.save
0
+ @schema_info.version += 1
0
+ @schema_info.save
0
       end
0
- end # glob
0
-
0
+ end # each
0
   end # migrate
0
   
0
   desc "Rolls the schema back to the previous version. Specify the number of steps with STEP=n"
0
- task :rollback => "db:schema:create" do
0
- if using_data_mapper?
0
- require 'data_mapper/migration'
0
- schema_info = data_mapper_schema_info
0
- elsif using_active_record?
0
- require 'active_record/migration'
0
- schema_info = active_record_schema_info
0
- end
0
+ task :rollback => ["db:schema:create", "db:abort_if_pending_migrations"] do
0
     migrations = migration_files.reverse
0
     (ENV["STEP"] || 1).to_i.times do |step|
0
       migration = migrations[step]
0
       require migration
0
       migration = File.basename(migration, ".rb")
0
       m_number = migration_number(migration)
0
- # if m_number > schema_info.version
0
- # raise Mack::Errors::UnrunMigrations.new(m_number - schema_info.version)
0
- # end
0
- if m_number == schema_info.version
0
+ if m_number == @schema_info.version
0
         migration_name(migration).camelcase.constantize.down
0
- schema_info.version -= 1
0
- schema_info.save
0
+ @schema_info.version -= 1
0
+ @schema_info.save
0
       end
0
     end
0
 
0
   end # rollback
0
   
0
- desc ""
0
- task :version => "db:schema:create" do
0
- if using_data_mapper?
0
- schema_info = data_mapper_schema_info
0
- elsif using_active_record?
0
- schema_info = active_record_schema_info
0
+ desc "Raises an error if there are pending migrations"
0
+ task :abort_if_pending_migrations do
0
+ migrations = migration_files.reverse
0
+ return if migrations.empty?
0
+ migration = migrations.first
0
+ migration = File.basename(migration, ".rb")
0
+ m_number = migration_number(migration)
0
+ if m_number > @schema_info.version
0
+ raise Mack::Errors::UnrunMigrations.new(m_number - @schema_info.version)
0
     end
0
- puts "\nYour database is currently at version: #{schema_info.version}\n"
0
+ end
0
+
0
+ desc "Displays the current schema version of your database"
0
+ task :version => "db:schema:create" do
0
+ puts "\nYour database is currently at version: #{@schema_info.version}\n"
0
   end
0
   
0
   private
0
@@ -71,6 +58,7 @@ namespace :db do
0
           DmSchemaInfo.table.create!
0
           DmSchemaInfo.create(:version => 0)
0
         end
0
+ @schema_info = DmSchemaInfo.first
0
       elsif using_active_record?
0
         require 'active_record/migration'
0
         class CreateArSchemaInfo < ActiveRecord::Migration # :nodoc:
0
@@ -84,6 +72,7 @@ namespace :db do
0
           CreateArSchemaInfo.up
0
           ArSchemaInfo.create(:version => 0)
0
         end
0
+ @schema_info = ArSchemaInfo.find(:first)
0
       end
0
     end # create
0
     
0
@@ -102,12 +91,4 @@ namespace :db do
0
     migration.match(/^\d+_(.+)/).captures.last
0
   end
0
   
0
- def data_mapper_schema_info
0
- DmSchemaInfo.first
0
- end # data_mapper_schema_info
0
-
0
- def active_record_schema_info
0
- ArSchemaInfo.find(:first)
0
- end # active_record_schema_info
0
-
0
 end # db
0
\ No newline at end of file
...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
86
87
...
166
167
168
 
169
170
171
...
63
64
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
166
167
168
169
170
171
172
0
@@ -63,25 +63,25 @@ class DbTasksTest < Test::Unit::TestCase
0
   end
0
   
0
   
0
- # def test_db_rollback_active_record_unrun_migrations
0
- # use_active_record do
0
- # test_db_migrate_active_record
0
- # MigrationGenerator.new("name" => "create_comments").run
0
- # si = ArSchemaInfo.find(:first)
0
- # assert_equal 2, si.version
0
- # assert_raise(Mack::Errors::UnrunMigrations) { rake_task("db:rollback") }
0
- # end
0
- # end
0
- #
0
- # def test_db_rollback_data_mapper_unrun_migrations
0
- # use_data_mapper do
0
- # test_db_migrate_data_mapper
0
- # MigrationGenerator.new("name" => "create_comments").run
0
- # si = DmSchemaInfo.first
0
- # assert_equal 2, si.version
0
- # assert_raise(Mack::Errors::UnrunMigrations) { rake_task("db:rollback") }
0
- # end
0
- # end
0
+ def test_db_rollback_active_record_unrun_migrations
0
+ use_active_record do
0
+ test_db_migrate_active_record
0
+ MigrationGenerator.new("name" => "create_comments").run
0
+ si = ArSchemaInfo.find(:first)
0
+ assert_equal 2, si.version
0
+ assert_raise(Mack::Errors::UnrunMigrations) { rake_task("db:rollback") }
0
+ end
0
+ end
0
+
0
+ def test_db_rollback_data_mapper_unrun_migrations
0
+ use_data_mapper do
0
+ test_db_migrate_data_mapper
0
+ MigrationGenerator.new("name" => "create_comments").run
0
+ si = DmSchemaInfo.first
0
+ assert_equal 2, si.version
0
+ assert_raise(Mack::Errors::UnrunMigrations) { rake_task("db:rollback") }
0
+ end
0
+ end
0
 
0
   def test_db_migrate_active_record
0
     use_active_record do
0
@@ -166,6 +166,7 @@ class DbTasksTest < Test::Unit::TestCase
0
   end
0
   
0
   private
0
+
0
   def ar_create_users_migration
0
     <<-MIG
0
 class ArCreateUsers < ActiveRecord::Migration

Comments

    No one has commented yet.