· 8 years ago · Jul 27, 2018, 03:18 AM
1#!/usr/bin/env ruby
2require 'dm-core'
3
4DataMapper::Logger.new($stdout, :debug)
5DataMapper.setup(:default, 'sqlite3::memory:')
6
7# dm-core/migrations.rb
8module DataMapper
9 module Migrations
10 module SingletonMethods
11 # Monkey patch to support overwriting Model.auto_migrate!
12 # so that it will be "recognized" by DataMapper.auto_migrate!
13 def auto_migrate!(repository_name = nil)
14 # original code
15 # auto_migrate_down!(repository_name)
16 # auto_migrate_up!(repository_name)
17 # patched code
18 repository_execute(:auto_migrate!, repository_name)
19 end
20 end
21 end
22end
23
24class Person
25 include DataMapper::Resource
26
27 # properties
28 property :id, Serial
29
30 def self.auto_migrate!(repository_name = self.repository_name)
31 puts "Before Person.auto_migrate!"
32 super
33 end
34 def self.auto_migrate_up!(repository_name = self.repository_name)
35 puts "Before Person.auto_migrate_up!"
36 super
37 end
38 def self.auto_migrate_down!(repository_name = self.repository_name)
39 puts "Before Person.auto_migrate_down!"
40 super
41 end
42end
43
44DataMapper.auto_migrate!
45
46__END__
47
48ree-1.8.7-2010.01 mungo:dm-snippets snusnu$ bundle exec ruby 1225.rb
49Before Person.auto_migrate!
50Before Person.auto_migrate_down!
51 ~ (0.000051) SELECT sqlite_version(*)
52 ~ (0.000068) DROP TABLE IF EXISTS "people"
53Before Person.auto_migrate_up!
54 ~ (0.000011) PRAGMA table_info("people")
55 ~ (0.000270) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)