-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.rb
185 lines (161 loc) · 4.73 KB
/
migration.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
require 'sqlite3'
require_relative 'tableDiff.rb'
require_relative 'connection.rb'
require_relative 'utils.rb'
class AddTable
attr_accessor :tableName, :newDef
def initialize(name, tableDef)
@tableName = name
@newDef = tableDef
end
def to_sql(ctx, platform)
return @newDef.to_sql(ctx, platform)
end
end
class ChangeTable
attr_accessor :tableName, :changeTable, :newDef
def initialize(name, changes, tableDef)
@tableName = name
@changeTable = changes
@newDef = tableDef
end
def length
return @changeTable.length
end
def to_s
res = ""
for k, changes in @changeTable
for change in changes
res += k.to_s + ": " + change.to_s + "\n"
end
end
return res
end
def to_sql(ctx, platform)
arr = []
for _, changes in @changeTable
for change in changes
arr.append(change.to_sql(ctx, platform))
end
end
return arr.map{|x| " " + x}.join(",\n") + ";"
end
end
class DropTable
attr_accessor :tableName
def initialize(name)
@tableName = name
end
def to_sql(ctx, platform)
return "DROP TABLE " + @tableName + "_ CASCADE"
end
end
class Migration
attr_accessor :tables
def initialize(tableList)
if tableList.is_a? Array
tableList = Schema.new tableList
end
@tables = tableList.to_dict
end
def contains?(key)
return @tables.has_key?(key)
end
def diff(newSchema)
oldSchema = self
entireChanges = {}
for k, v in newSchema.tables
u2 = v
if oldSchema.contains?(k)
u1 = oldSchema.tables[k]
changeDict = {}
for kChange, changes in tableDiff(u1, u2)
changeDict[kChange] = []
for change in changes
changeDict[kChange].append(change)
end
end
entireChanges[k] = ChangeTable.new(k, changeDict, u2)
else
entireChanges[k] = AddTable.new(k, u2)
end
end
for k, v in oldSchema.tables
if not newSchema.contains?(k)
entireChanges[k] = DropTable.new(k)
end
end
return entireChanges
end
def to_sql(newScheme, platform)
changes = self.diff(newScheme)
str = ""
ctx = Context.new([], [])
for k, v in changes
if v.is_a? ChangeTable
if v.changeTable.length != 0
str += "ALTER TABLE " + k + "_ \n"
str += v.to_sql(ctx, platform)
end
elsif v.is_a? AddTable
str += "CREATE TABLE " + k + "_ (\n" + indent(v.to_sql(ctx, platform)) + "\n);\n"
else
return v.to_sql(ctx, platform) + ";\n"
end
end
return ctx.generate(str)
end
def migrate_to(dbAuth, newSchema, platform)
sql = self.to_sql(newSchema, platform)
pg_query(dbAuth, sql)
end
end
class Migrations
attr_accessor :name, :db
def initialize(name)
@name = name
@db = SQLite3::Database.new "migrations.db"
db.execute "CREATE TABLE IF NOT EXISTS #{self.migration_name} (id INTEGER PRIMARY KEY AUTOINCREMENT, migration TEXT NOT NULL)"
end
def migration_name
return "#{@name}_migrations"
end
def current_migration
results = db.query "SELECT migration FROM #{self.migration_name} ORDER BY id DESC LIMIT 1"
results.each do |row|
migration = row[0]
tables = JSON.parse(migration)
classes = []
for _, table in tables
classes.append(Table.json_create(table))
end
return Migration.new(classes)
end
return Migration.new([])
end
def should_migrate(schema)
for k, v in schema
if v.is_a?(AddTable) or v.is_a?(DropTable)
return true
end
end
for k, v in schema
if v.length > 0
return true
end
end
return false
end
def migrate(dbAuth, newSchema, platform)
migration = self.current_migration
if self.should_migrate(migration.diff(Migration.new(newSchema)))
migration.migrate_to(dbAuth, Migration.new(newSchema), platform)
self.save(newSchema)
end
end
def save(newSchema)
tables = newSchema.to_dict
jsonSchema = JSON.dump(tables)
db.execute "INSERT INTO #{self.migration_name} (migration) VALUES ('#{jsonSchema}');"
end
end