-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.rb
83 lines (70 loc) · 1.65 KB
/
migrate.rb
1
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection({
:adapter => 'sqlite3',
:dbfile => 'db/database-new.sqlite3'
})
class Object #:nodoc:
def meta_def(m,&b) #:nodoc:
(class<<self;self end).send(:define_method,m,&b)
end
end
class Migration
class SchemaInfo < ActiveRecord::Base
# Nothing...
end
def self.create_schema(opts = {})
opts[:assume] ||= 0
opts[:version] ||= @final
if @migrations
unless SchemaInfo.table_exists?
ActiveRecord::Schema.define do
create_table SchemaInfo.table_name do |t|
t.float :version
end
end
si = SchemaInfo.find(:first) || SchemaInfo.new(:version => opts[:assume])
if si.version < opts[:version]
@migrations.each do |k|
k.migrate(:up) if si.version < k.version and k.version <= opts[:version]
k.migrate(:down) if si.version > k.version and k.version > opts[:version]
end
puts "Version to go to: "
puts opts[:version]
puts "Assumed version: "
puts opts[:assume]
end
si.update_attributes(:version => opts[:version])
end
end
end
def self.V(n)
@final = [n, @final.to_i].max
m = (@migrations ||= [])
Class.new(ActiveRecord::Migration) do
meta_def(:version) { n }
meta_def(:inherited) { |k| m << k }
end
end
def self.debug
puts "Migrations: "
puts @migrations.to_a
puts "Final Version: "
puts @final.to_i
puts "Migration version: "
puts @migrations[0].version
end
end
class CreateFactoids < Migration::V(1.0)
def up
create_table :factoids do |t|
t.string :key
t.string :value
t.timestamps
end
end
def down
end
end
Migration::debug
Migration::create_schema