Commit 38507bec619ea90526fcc03a9933e6108114494e
1 parent
00247e8c
Exists in
master
and in
39 other branches
Fixed proxy migration
Signed-off-by : Alex Campelo <chevalier.beaumont@gmail.com> Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Showing
1 changed file
with
171 additions
and
2 deletions
Show diff stats
src/proxy/migrations/0001_initial.py
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import datetime |
3 | +from django.db import connections | |
3 | 4 | from south.db import db |
4 | 5 | from south.v2 import SchemaMigration |
5 | 6 | from django.db import models |
... | ... | @@ -8,10 +9,177 @@ from django.db import models |
8 | 9 | class Migration(SchemaMigration): |
9 | 10 | |
10 | 11 | def forwards(self, orm): |
12 | + connection = connections['trac'] | |
13 | + | |
14 | + cursor = connection.cursor() | |
15 | + cursor.execute(''' | |
16 | + CREATE OR REPLACE VIEW wiki_view AS SELECT | |
17 | + wiki.name AS name, | |
18 | + (SELECT wiki2.text FROM wiki AS wiki2 WHERE wiki2.name = wiki.name | |
19 | + AND wiki2.version = MAX(wiki.version)) AS wiki_text, | |
20 | + (SELECT wiki3.author FROM wiki AS wiki3 WHERE wiki3.name = wiki.name | |
21 | + AND wiki3.version = 1) AS author, | |
22 | + string_agg(DISTINCT wiki.author, ', ') AS collaborators, | |
23 | + TIMESTAMP WITH TIME ZONE 'epoch' + (MAX(wiki.time)/1000000) * INTERVAL '1s' AS created, | |
24 | + TIMESTAMP WITH TIME ZONE 'epoch' + (MIN(wiki.time)/1000000) * INTERVAL '1s' AS modified | |
25 | + FROM wiki | |
26 | + GROUP BY wiki.name; | |
27 | + | |
28 | + CREATE OR REPLACE VIEW ticket_view AS SELECT | |
29 | + ticket.id AS id, | |
30 | + ticket.summary as summary, | |
31 | + ticket.description as description, | |
32 | + ticket.milestone as milestone, | |
33 | + ticket.priority as priority, | |
34 | + ticket.component as component, | |
35 | + ticket.version as version, | |
36 | + ticket.severity as severity, | |
37 | + ticket.reporter as reporter, | |
38 | + ticket.reporter as author, | |
39 | + ticket.status as status, | |
40 | + ticket.keywords as keywords, | |
41 | + (SELECT | |
42 | + string_agg(DISTINCT ticket_change.author, ', ') | |
43 | + FROM ticket_change WHERE ticket_change.ticket = ticket.id | |
44 | + GROUP BY ticket_change.ticket) as collaborators, | |
45 | + TIMESTAMP WITH TIME ZONE 'epoch' + (time/1000000)* INTERVAL '1s' AS created, | |
46 | + TIMESTAMP WITH TIME ZONE 'epoch' + (changetime/1000000) * INTERVAL '1s' AS modified | |
47 | + FROM ticket; | |
48 | + | |
49 | + CREATE OR REPLACE VIEW revision_view AS SELECT | |
50 | + revision.rev, | |
51 | + revision.author, | |
52 | + revision.message, | |
53 | + repository.value AS repository_name, | |
54 | + TIMESTAMP WITH TIME ZONE 'epoch' + (revision.time/1000000) * INTERVAL '1s' AS created | |
55 | + FROM revision | |
56 | + INNER JOIN repository ON( | |
57 | + repository.id = revision.repos | |
58 | + AND repository.name = 'name' | |
59 | + AND repository.value != '' | |
60 | + ); | |
61 | + ''') | |
62 | + cursor.execute(''' | |
63 | +CREATE OR REPLACE VIEW revision_view AS SELECT | |
64 | +revision.rev, | |
65 | +revision.author, | |
66 | +revision.message, | |
67 | +repository.value AS repository_name, | |
68 | +TIMESTAMP WITH TIME ZONE 'epoch' + (revision.time/1000000) * INTERVAL '1s' AS created, | |
69 | +CONCAT(revision.repos, '-', revision.rev) AS key | |
70 | +FROM revision | |
71 | +INNER JOIN repository ON( | |
72 | +repository.id = revision.repos | |
73 | +AND repository.name = 'name' | |
74 | +AND repository.value != '' | |
75 | +); | |
76 | +''') | |
77 | + cursor.execute(''' | |
78 | +CREATE OR REPLACE VIEW attachment_view AS SELECT | |
79 | +CONCAT(attachment.type, '/' , attachment.id, '/', attachment.filename) AS url, | |
80 | +attachment.type AS used_by, | |
81 | +attachment.filename AS filename, | |
82 | +attachment.id as attach_id, | |
83 | +(SELECT LOWER(SUBSTRING(attachment.filename FROM '\.(\w+)$'))) AS mimetype, | |
84 | +attachment.author AS author, | |
85 | +attachment.description AS description, | |
86 | +attachment.size AS size, | |
87 | +TIMESTAMP WITH TIME ZONE 'epoch' + (attachment.time/1000000)* INTERVAL '1s' AS created | |
88 | +FROM attachment; | |
89 | +''') | |
90 | + cursor.execute(''' | |
91 | +CREATE OR REPLACE VIEW wiki_view AS SELECT | |
92 | +wiki.name AS name, | |
93 | +(SELECT wiki2.text FROM wiki AS wiki2 WHERE wiki2.name = wiki.name | |
94 | +AND wiki2.version = MAX(wiki.version)) AS wiki_text, | |
95 | +(SELECT wiki3.author FROM wiki AS wiki3 WHERE wiki3.name = wiki.name | |
96 | +AND wiki3.version = 1) AS author, | |
97 | +string_agg(DISTINCT wiki.author, ', ') AS collaborators, | |
98 | +TIMESTAMP WITH TIME ZONE 'epoch' + (MIN(wiki.time)/1000000) * INTERVAL '1s' AS created, | |
99 | +TIMESTAMP WITH TIME ZONE 'epoch' + (MAX(wiki.time)/1000000) * INTERVAL '1s' AS modified | |
100 | +FROM wiki | |
101 | +GROUP BY wiki.name; | |
102 | +''') | |
103 | + cursor.execute(''' | |
104 | +CREATE OR REPLACE VIEW wiki_view AS SELECT | |
105 | +wiki.name AS name, | |
106 | +(SELECT wiki2.text FROM wiki AS wiki2 WHERE wiki2.name = wiki.name | |
107 | +AND wiki2.version = MAX(wiki.version)) AS wiki_text, | |
108 | +(SELECT wiki3.author FROM wiki AS wiki3 WHERE wiki3.name = wiki.name | |
109 | +AND wiki3.version = 1) AS author, | |
110 | +string_agg(DISTINCT wiki.author, ', ') AS collaborators, | |
111 | +TIMESTAMP WITH TIME ZONE 'epoch' + (MIN(wiki.time)/1000000) * INTERVAL '1s' AS created, | |
112 | +TIMESTAMP WITH TIME ZONE 'epoch' + (MAX(wiki.time)/1000000) * INTERVAL '1s' AS modified, | |
113 | +(SELECT wiki4.author FROM wiki AS wiki4 WHERE wiki4.name = wiki.name | |
114 | +AND wiki4.version = MAX(wiki.version)) AS modified_by | |
115 | +FROM wiki | |
116 | +GROUP BY wiki.name; | |
117 | + | |
118 | +CREATE OR REPLACE VIEW ticket_view AS SELECT | |
119 | +ticket.id AS id, | |
120 | +ticket.summary as summary, | |
121 | +ticket.description as description, | |
122 | +ticket.milestone as milestone, | |
123 | +ticket.priority as priority, | |
124 | +ticket.component as component, | |
125 | +ticket.version as version, | |
126 | +ticket.severity as severity, | |
127 | +ticket.reporter as reporter, | |
128 | +ticket.reporter as author, | |
129 | +ticket.status as status, | |
130 | +ticket.keywords as keywords, | |
131 | +(SELECT | |
132 | +string_agg(DISTINCT ticket_change.author, ', ') | |
133 | +FROM ticket_change WHERE ticket_change.ticket = ticket.id | |
134 | +GROUP BY ticket_change.ticket) as collaborators, | |
135 | +TIMESTAMP WITH TIME ZONE 'epoch' + (time/1000000)* INTERVAL '1s' AS created, | |
136 | +TIMESTAMP WITH TIME ZONE 'epoch' + (changetime/1000000) * INTERVAL '1s' AS modified, | |
137 | +(SELECT | |
138 | +ticket_change.author | |
139 | +FROM ticket_change | |
140 | +WHERE ticket_change.ticket = ticket.id | |
141 | +AND ticket_change.time = ticket.changetime | |
142 | +LIMIT 1 | |
143 | +) AS modified_by | |
144 | +FROM ticket; | |
145 | +''') | |
146 | + cursor.execute(''' | |
147 | +CREATE OR REPLACE VIEW ticket_collab_count_view AS | |
148 | +SELECT | |
149 | +COALESCE (t1.author, t2.author) as author, | |
150 | +(COALESCE(t1.count, 0) + COALESCE(t2.count, 0)) as count | |
151 | +FROM | |
152 | +(SELECT author, count(*) as count | |
153 | +FROM ticket_change | |
154 | +GROUP BY author | |
155 | +ORDER BY author | |
156 | +) AS t1 | |
157 | +FULL OUTER JOIN | |
158 | +(SELECT reporter as author, count(*) as count | |
159 | +FROM ticket | |
160 | +GROUP BY reporter | |
161 | +ORDER BY reporter | |
162 | +) AS t2 | |
163 | +ON t1.author = t2.author; | |
164 | + | |
165 | +CREATE OR REPLACE VIEW wiki_collab_count_view AS | |
166 | +SELECT author, count(*) from wiki GROUP BY author; | |
167 | +''') | |
168 | + | |
11 | 169 | pass |
12 | 170 | |
13 | 171 | def backwards(self, orm): |
14 | - pass | |
172 | + connection = connections['trac'] | |
173 | + | |
174 | + cursor = connection.cursor() | |
175 | + cursor.execute(''' | |
176 | +DROP VIEW IF EXISTS revision_view; | |
177 | +DROP VIEW IF EXISTS ticket_view; | |
178 | +DROP VIEW IF EXISTS wiki_view; | |
179 | +''') | |
180 | + cursor.execute('DROP VIEW IF EXISTS attachment_view;') | |
181 | + | |
182 | + pass | |
15 | 183 | |
16 | 184 | models = { |
17 | 185 | u'proxy.attachment': { |
... | ... | @@ -76,4 +244,5 @@ class Migration(SchemaMigration): |
76 | 244 | } |
77 | 245 | } |
78 | 246 | |
79 | - complete_apps = ['proxy'] | |
80 | 247 | \ No newline at end of file |
248 | + complete_apps = ['proxy'] | |
249 | + symmetrical = True | ... | ... |