public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
Cleaned up a little bit. [#87]
markbates (author)
Tue Aug 12 12:42:04 -0700 2008
commit  467e2c7ae9ebd850463b7a2a7758a6215f6cd38d
tree    3f7c7a17f5f0b152759c9160e423e156bd46ea86
parent  c408f55dc9210e4f601f6e5579199fa71dac0cbe
...
45
46
47
 
48
49
50
...
60
61
62
 
63
64
65
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 
289
...
45
46
47
48
49
50
51
...
61
62
63
64
65
66
67
...
133
134
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
137
138
0
@@ -45,6 +45,7 @@ module Mack
0
       end
0
     end
0
     
0
+    # Loads the structure of the given file into the database
0
     def self.load_structure(file, env = Mack.env, repis = :default)
0
       Mack::Database.establish_connection(env)
0
       dbs = db_settings(env)
0
@@ -60,6 +61,7 @@ module Mack
0
       end
0
     end
0
     
0
+    # Dumps the structure of the database to a file.
0
     def self.dump_structure(env = Mack.env, repis = :default)
0
       Mack::Database.establish_connection(env)
0
       dbs = db_settings(env)
0
@@ -131,157 +133,4 @@ module Mack
0
     end
0
     
0
   end # Database
0
-  
0
-end # Mack
0
-
0
-
0
-# #
0
-# # AR db create/drop.
0
-# # Currently it supports 3 adapters: sqlite3, postgresql, and mysql
0
-# #
0
-# # ds - July 2008
0
-# #
0
-# 
0
-# module Mack
0
-#   module Database
0
-#     
0
-#     module Migrator
0
-#       def self.version
0
-#         ActiveRecord::Migrator.current_version
0
-#       end
0
-#       
0
-#       def self.migrate
0
-#         ActiveRecord::Migrator.up(File.join(Mack.root, "db", "migrations"))
0
-#       end
0
-#       
0
-#       def self.rollback(step = 1)
0
-#         step = (ENV["STEP"] || step).to_i
0
-#         cur_version = version.to_i
0
-#         target_version = cur_version - step 
0
-#         target_version = 0 if target_version < 0
0
-#         
0
-#         ActiveRecord::Migrator.down(File.join(Mack.root, "db", "migrations"), target_version)
0
-#       end
0
-#       
0
-#     end
0
-#         
0
-#     def self.db_settings(env)
0
-#       dbs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
0
-#       dbs = dbs[env]
0
-#       dbs.symbolize_keys!
0
-#       return dbs
0
-#     end
0
-#     
0
-#     def self.establish_connection(env)
0
-#       dbs = db_settings(env)
0
-#       ActiveRecord::Base.establish_connection(dbs)
0
-#     end
0
-#     
0
-#     # Perform db create or drop
0
-#     # 
0
-#     # By default the mode is drop then create, but caller will be able to 
0
-#     # call this routine with a specific action (:drop, :create, or :drop_and_create)
0
-#     # 
0
-#     def self.drop_or_create_database(env, mode = :drop_and_create)
0
-#       dbs = db_settings(env)
0
-#       case dbs[:adapter]
0
-#         when "mysql"
0
-#           establish_mysql_connection
0
-#           drop_mysql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
-#           create_mysql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
-#           
0
-#         when "postgresql"
0
-#           ENV['PGHOST']     = dbs[:host] if dbs[:host]
0
-#           ENV['PGPORT']     = dbs[:port].to_s if dbs[:port]
0
-#           ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
0
-#           
0
-#           ActiveRecord::Base.clear_active_connections!
0
-#           drop_postgresql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
-#           create_postgresql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
-#           
0
-#         when "sqlite3"
0
-#           ActiveRecord::Base.clear_active_connections!
0
-#           FileUtils.rm_rf(dbs[:database]) if mode == :drop or mode == :drop_and_create
0
-#       end
0
-#     end
0
-#     
0
-#     def self.load_structure(file, env = Mack.env)
0
-#       Mack::Database.establish_connection(env)
0
-#       dbs = db_settings(env)
0
-#       sql = File.read(file)
0
-#       case dbs[:adapter]
0
-#       when "mysql"
0
-#         sql.split(";").each do |s|
0
-#           s.strip! 
0
-#           ActiveRecord::Base.connection.execute(s) unless s.blank?
0
-#         end
0
-#       else
0
-#         ActiveRecord::Base.connection.execute(sql) unless sql.blank?
0
-#       end
0
-#     end
0
-#     
0
-#     def self.dump_structure(env = Mack.env)
0
-#       Mack::Database.establish_connection(env)
0
-#       dbs = db_settings(env)
0
-#       structure = ""
0
-#       output_file = File.join(Mack.root, "db", "#{env}_schema_structure.sql")
0
-#       case dbs[:adapter]
0
-#       when "mysql"
0
-#         File.open(output_file, "w") {|f| f.puts ActiveRecord::Base.connection.structure_dump}
0
-#       when "postgresql"
0
-#         `pg_dump -i -U "#{dbs[:username]}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{dbs[:database]}`
0
-#       when "sqlite3"
0
-#         `sqlite3 #{dbs[:database]} .schema > #{output_file}`
0
-#       else
0
-#         raise "Task not supported for '#{dbs[:adapter]}'"
0
-#       end
0
-#     end
0
-#             
0
-#     private
0
-#     
0
-#     def self.drop_postgresql_db(env, dbs)
0
-#       begin
0
-#         puts "Dropping (PostgreSQL): #{dbs[:database]}"
0
-#         `dropdb -U "#{dbs[:username]}" #{dbs[:database]}`
0
-#       rescue Exception => e
0
-#         puts e
0
-#       end
0
-#     end
0
-#     
0
-#     def self.create_postgresql_db(env, dbs)
0
-#       begin
0
-#         enc_option = "-E #{dbs[:encoding]}" if dbs[:encoding]
0
-#         puts "Creating (PostgreSQL): #{dbs[:database]}"
0
-#         `createdb #{enc_option} -U "#{dbs[:username]}" #{dbs[:database]}`
0
-#       rescue Exception => e
0
-#         puts e
0
-#       end
0
-#     end
0
-#     
0
-#     def self.establish_mysql_connection
0
-#       # connect to mysql meta database
0
-#       ActiveRecord::Base.establish_connection(
0
-#         :adapter => "mysql",
0
-#         :host => "localhost",
0
-#         :database => "mysql",
0
-#         :username => ENV["DB_USERNAME"] || "root",
0
-#         :password => ENV["DB_PASSWORD"] || ""
0
-#       )
0
-#     end
0
-#     
0
-#     def self.create_mysql_db(env, dbs)
0
-#       if dbs[:collation]
0
-#         puts "Dropping (MySQL): #{dbs[:database]}"
0
-#         ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}` COLLATE `#{dbs[:collation]}`"
0
-#       else
0
-#         puts "Creating (MySQL): #{dbs[:database]}"
0
-#         ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}`"
0
-#       end
0
-#     end
0
-#     
0
-#     def self.drop_mysql_db(env, dbs)
0
-#       puts "Dropping (MySQL): #{dbs[:database]}"
0
-#       ActiveRecord::Base.connection.execute "DROP DATABASE IF EXISTS `#{dbs[:database]}`"
0
-#     end
0
-#   end
0
-# end
0
\ No newline at end of file
0
+end # Mack
0
\ No newline at end of file
...
2
3
4
 
5
6
7
8
 
9
10
11
...
13
14
15
 
16
17
18
19
 
20
21
22
...
2
3
4
5
6
7
8
9
10
11
12
13
...
15
16
17
18
19
 
20
21
22
23
24
25
0
@@ -2,10 +2,12 @@ module Mack
0
   module Database
0
     module Migrations
0
       
0
+      # Migrates the database to the latest version
0
       def self.migrate
0
         ActiveRecord::Migrator.up(File.join(Mack.root, "db", "migrations"))
0
       end
0
       
0
+      # Rolls back the database by the specified number of steps. Default is 1
0
       def self.rollback(step = 1)
0
         cur_version = version.to_i
0
         target_version = cur_version - step 
0
@@ -13,10 +15,11 @@ module Mack
0
         ActiveRecord::Migrator.down(File.join(Mack.root, "db", "migrations"), target_version)
0
       end
0
       
0
+      # Not implemented
0
       def self.abort_if_pending_migrations
0
-        # not implemented
0
       end
0
       
0
+      # Returns the current version of the database
0
       def self.version
0
         ActiveRecord::Migrator.current_version
0
       end
...
28
29
30
 
31
32
33
...
43
44
45
 
46
47
48
...
28
29
30
31
32
33
34
...
44
45
46
47
48
49
50
0
@@ -28,6 +28,7 @@ module Mack
0
       drop_database(repis)
0
     end
0
     
0
+    # Loads the structure of the given file into the database
0
     def self.load_structure(file, env = Mack.env, repis = :default)
0
       Mack::Database.establish_connection(env)
0
       adapter = repository(repis).adapter
0
@@ -43,6 +44,7 @@ module Mack
0
       end
0
     end
0
     
0
+    # Dumps the structure of the database to a file.
0
     def self.dump_structure(env = Mack.env, repis = :default)
0
       Mack::Database.establish_connection(env)
0
       adapter = repository(repis).adapter
...
4
5
6
 
7
8
9
...
11
12
13
 
14
15
16
...
4
5
6
7
8
9
10
...
12
13
14
15
16
17
18
0
@@ -4,6 +4,7 @@ module Mack
0
   module Database
0
     module Migrations
0
       
0
+      # Migrates the database to the latest version
0
       def self.migrate
0
         Mack::Database.establish_connection
0
         DataMapper::MigrationRunner.reset!
0
@@ -11,6 +12,7 @@ module Mack
0
         DataMapper::MigrationRunner.migrate_up!
0
       end
0
       
0
+      # Rolls back the database by the specified number of steps. Default is 1
0
       def self.rollback(step = 1)
0
         DataMapper::MigrationRunner.reset!
0
         migration_files.each { |mig| load mig }
...
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
...
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
0
@@ -4,32 +4,39 @@ module Mack
0
     # Sets up and establishes connections to the database based on the specified environment
0
     # and the settings in the database.yml file.
0
     def self.establish_connection(env = Mack.env)
0
-      # raise NoMethodError.new(:establish_connection)
0
-    end # establish_connection
0
+      raise NoMethodError.new(:establish_connection)
0
+    end
0
+    
0
+    # Clears connections to the database
0
+    def self.clear_connection(env = Mack.env)
0
+      raise NoMethodError.new(:clear_connection)
0
+    end
0
     
0
     # Creates a database, if it doesn't already exist for the specified environment
0
     def self.create(env = Mack.env, repis = :default)
0
-      raise NoMethodError.new(:create)
0
+      raise NoMethodError.new(:create)
0
     end
0
     
0
     # Drops a database, if it exists for the specified environment
0
     def self.drop(env = Mack.env, repis = :default)
0
-      raise NoMethodError.new(:drop)
0
+      raise NoMethodError.new(:drop)
0
     end
0
     
0
+    # Drops and then creates the database.
0
     def self.recreate(env = Mack.env, repis = :default)
0
       Mack::Database.drop(env, repis)
0
       Mack::Database.create(env, repis)
0
     end
0
     
0
+    # Loads the structure of the given file into the database
0
     def self.load_structure(file, env = Mack.env, repis = :default)
0
-      raise NoMethodError.new(:load_structure)
0
+      raise NoMethodError.new(:load_structure)
0
     end
0
     
0
+    # Dumps the structure of the database to a file.
0
     def self.dump_structure(env = Mack.env, repis = :default)
0
-      raise NoMethodError.new(:dump_structure)
0
+      raise NoMethodError.new(:dump_structure)
0
     end
0
     
0
   end # Database
0
-  
0
 end # Mack
0
\ No newline at end of file
...
2
3
4
 
5
6
 
7
8
9
10
 
 
 
11
12
13
14
 
15
16
 
 
 
 
 
 
17
18
19
...
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
0
@@ -2,18 +2,26 @@ module Mack
0
   module Database
0
     module Migrations
0
       
0
+      # Migrates the database to the latest version
0
       def self.migrate
0
-        raise NoMethodError.new(:migrate)
0
+        raise NoMethodError.new(:migrate)
0
       end
0
       
0
-      def self.rollback(step)
0
-        # raise NoMethodError.new(:rollback)
0
+      # Rolls back the database by the specified number of steps. Default is 1
0
+      def self.rollback(step = 1)
0
+        raise NoMethodError.new(:rollback)
0
       end
0
       
0
       def self.abort_if_pending_migrations
0
-        raise NoMethodError.new(:abort_if_pending_migrations)
0
+        raise NoMethodError.new(:abort_if_pending_migrations)
0
       end
0
       
0
+      # Returns the current version of the database
0
+      def self.version
0
+        raise NoMethodError.new(:version)
0
+      end
0
+      
0
+      # Returns a list of the all migration files.
0
       def self.migration_files
0
         Dir.glob(File.join(Mack.root, "db", "migrations", "*.rb"))
0
       end

Comments