Commit 24afe1d3099f8c61ac0426e87c05920a9ebbcc74
Committed by
Sergio Oliveira
1 parent
d52ff5ac
Exists in
trac_indexing
Migration
Showing
2 changed files
with
114 additions
and
129 deletions
Show diff stats
colab/proxy/trac/migrations/0001_initial.py
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | from __future__ import unicode_literals |
3 | 3 | |
4 | -from django.db import models, migrations, connections | |
5 | - | |
6 | - | |
7 | -def create_views(apps, schema_editor): | |
8 | - connection = connections['trac'] | |
9 | - | |
10 | - cursor = connection.cursor() | |
11 | - | |
12 | - # revision_view | |
13 | - cursor.execute(''' | |
14 | - CREATE OR REPLACE VIEW revision_view AS SELECT | |
15 | - revision.rev, | |
16 | - revision.author, | |
17 | - revision.message, | |
18 | - repository.value AS repository_name, | |
19 | - TIMESTAMP WITH TIME ZONE 'epoch' + (revision.time/1000000) * INTERVAL '1s' AS created, | |
20 | - CONCAT(revision.repos, '-', revision.rev) AS key | |
21 | - FROM revision | |
22 | - INNER JOIN repository ON( | |
23 | - repository.id = revision.repos | |
24 | - AND repository.name = 'name' | |
25 | - AND repository.value != '' | |
26 | - ); | |
27 | - ''') | |
28 | - | |
29 | - # attachment_view | |
30 | - cursor.execute(''' | |
31 | - CREATE OR REPLACE VIEW attachment_view AS SELECT | |
32 | - CONCAT(attachment.type, '/' , attachment.id, '/', attachment.filename) AS url, | |
33 | - attachment.type AS used_by, | |
34 | - attachment.filename AS filename, | |
35 | - attachment.id as attach_id, | |
36 | - (SELECT LOWER(SUBSTRING(attachment.filename FROM '\.(\w+)$'))) AS mimetype, | |
37 | - attachment.author AS author, | |
38 | - attachment.description AS description, | |
39 | - attachment.size AS size, | |
40 | - TIMESTAMP WITH TIME ZONE 'epoch' + (attachment.time/1000000)* INTERVAL '1s' AS created | |
41 | - FROM attachment; | |
42 | - ''') | |
43 | - | |
44 | - # wiki_view | |
45 | - cursor.execute(''' | |
46 | - CREATE OR REPLACE VIEW wiki_view AS SELECT | |
47 | - wiki.name AS name, | |
48 | - (SELECT wiki2.text FROM wiki AS wiki2 WHERE wiki2.name = wiki.name | |
49 | - AND wiki2.version = MAX(wiki.version)) AS wiki_text, | |
50 | - (SELECT wiki3.author FROM wiki AS wiki3 WHERE wiki3.name = wiki.name | |
51 | - AND wiki3.version = 1) AS author, | |
52 | - string_agg(DISTINCT wiki.author, ', ') AS collaborators, | |
53 | - TIMESTAMP WITH TIME ZONE 'epoch' + (MIN(wiki.time)/1000000) * INTERVAL '1s' AS created, | |
54 | - TIMESTAMP WITH TIME ZONE 'epoch' + (MAX(wiki.time)/1000000) * INTERVAL '1s' AS modified, | |
55 | - (SELECT wiki4.author FROM wiki AS wiki4 WHERE wiki4.name = wiki.name | |
56 | - AND wiki4.version = MAX(wiki.version)) AS modified_by | |
57 | - FROM wiki | |
58 | - GROUP BY wiki.name; | |
59 | - ''') | |
60 | - | |
61 | - # ticket_view | |
62 | - cursor.execute(''' | |
63 | - CREATE OR REPLACE VIEW ticket_view AS SELECT | |
64 | - ticket.id AS id, | |
65 | - ticket.summary as summary, | |
66 | - ticket.description as description, | |
67 | - ticket.milestone as milestone, | |
68 | - ticket.priority as priority, | |
69 | - ticket.component as component, | |
70 | - ticket.version as version, | |
71 | - ticket.severity as severity, | |
72 | - ticket.reporter as reporter, | |
73 | - ticket.reporter as author, | |
74 | - ticket.status as status, | |
75 | - ticket.keywords as keywords, | |
76 | - (SELECT | |
77 | - string_agg(DISTINCT ticket_change.author, ', ') | |
78 | - FROM ticket_change WHERE ticket_change.ticket = ticket.id | |
79 | - GROUP BY ticket_change.ticket) as collaborators, | |
80 | - TIMESTAMP WITH TIME ZONE 'epoch' + (time/1000000)* INTERVAL '1s' AS created, | |
81 | - TIMESTAMP WITH TIME ZONE 'epoch' + (changetime/1000000) * INTERVAL '1s' AS modified, | |
82 | - (SELECT | |
83 | - ticket_change.author | |
84 | - FROM ticket_change | |
85 | - WHERE ticket_change.ticket = ticket.id | |
86 | - AND ticket_change.time = ticket.changetime | |
87 | - LIMIT 1 | |
88 | - ) AS modified_by | |
89 | - FROM ticket; | |
90 | - ''') | |
91 | - | |
92 | - # ticket_collab_count_view | |
93 | - cursor.execute(''' | |
94 | - CREATE OR REPLACE VIEW ticket_collab_count_view AS | |
95 | - SELECT | |
96 | - COALESCE (t1.author, t2.author) as author, | |
97 | - (COALESCE(t1.count, 0) + COALESCE(t2.count, 0)) as count | |
98 | - FROM | |
99 | - (SELECT author, count(*) as count | |
100 | - FROM ticket_change | |
101 | - GROUP BY author | |
102 | - ORDER BY author | |
103 | - ) AS t1 | |
104 | - FULL OUTER JOIN | |
105 | - (SELECT reporter as author, count(*) as count | |
106 | - FROM ticket | |
107 | - GROUP BY reporter | |
108 | - ORDER BY reporter | |
109 | - ) AS t2 | |
110 | - ON t1.author = t2.author; | |
111 | - ''') | |
112 | - | |
113 | - # wiki_collab_count_view | |
114 | - cursor.execute(''' | |
115 | - CREATE OR REPLACE VIEW wiki_collab_count_view AS | |
116 | - SELECT author, count(*) from wiki GROUP BY author; | |
117 | - ''') | |
118 | - | |
119 | - | |
120 | -def drop_views(apps, schema_editor): | |
121 | - connection = connections['trac'] | |
122 | - | |
123 | - cursor = connection.cursor() | |
124 | - cursor.execute(''' | |
125 | - DROP VIEW IF EXISTS revision_view; | |
126 | - DROP VIEW IF EXISTS ticket_view; | |
127 | - DROP VIEW IF EXISTS wiki_view; | |
128 | - DROP VIEW IF EXISTS ticket_collab_count_view; | |
129 | - DROP VIEW IF EXISTS wiki_collab_count_view; | |
130 | - DROP VIEW IF EXISTS attachment_view; | |
131 | - ''') | |
4 | +from django.db import models, migrations | |
5 | +import django.db.models.deletion | |
6 | +import hitcounter.models | |
7 | +from django.conf import settings | |
132 | 8 | |
133 | 9 | |
134 | 10 | class Migration(migrations.Migration): |
135 | 11 | |
136 | 12 | dependencies = [ |
13 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | |
137 | 14 | ] |
138 | 15 | |
139 | 16 | operations = [ |
140 | - migrations.RunPython(code=create_views, reverse_code=drop_views) | |
17 | + migrations.CreateModel( | |
18 | + name='Attachment', | |
19 | + fields=[ | |
20 | + ('url', models.TextField(serialize=False, primary_key=True)), | |
21 | + ('attach_id', models.TextField()), | |
22 | + ('used_by', models.TextField()), | |
23 | + ('filename', models.TextField()), | |
24 | + ('author', models.TextField(blank=True)), | |
25 | + ('title', models.TextField(blank=True)), | |
26 | + ('description', models.TextField(blank=True)), | |
27 | + ('modified', models.DateTimeField(blank=True)), | |
28 | + ('mimetype', models.TextField(blank=True)), | |
29 | + ('size', models.IntegerField(blank=True)), | |
30 | + ], | |
31 | + options={ | |
32 | + 'verbose_name': 'Attachment', | |
33 | + 'verbose_name_plural': 'Attachment', | |
34 | + }, | |
35 | + bases=(models.Model, hitcounter.models.HitCounterModelMixin), | |
36 | + ), | |
37 | + migrations.CreateModel( | |
38 | + name='Revision', | |
39 | + fields=[ | |
40 | + ('key', models.TextField(serialize=False, primary_key=True, blank=True)), | |
41 | + ('rev', models.TextField(blank=True)), | |
42 | + ('author', models.TextField(blank=True)), | |
43 | + ('message', models.TextField(blank=True)), | |
44 | + ('description', models.TextField(blank=True)), | |
45 | + ('repository_name', models.TextField(blank=True)), | |
46 | + ('created', models.DateTimeField(null=True, blank=True)), | |
47 | + ('modified', models.DateTimeField(null=True, blank=True)), | |
48 | + ], | |
49 | + options={ | |
50 | + 'db_table': 'trac_revision', | |
51 | + 'verbose_name': 'Attachment', | |
52 | + 'verbose_name_plural': 'Attachment', | |
53 | + }, | |
54 | + bases=(models.Model, hitcounter.models.HitCounterModelMixin), | |
55 | + ), | |
56 | + migrations.CreateModel( | |
57 | + name='Ticket', | |
58 | + fields=[ | |
59 | + ('id', models.IntegerField(serialize=False, primary_key=True)), | |
60 | + ('summary', models.TextField(blank=True)), | |
61 | + ('description_ticket', models.TextField(blank=True)), | |
62 | + ('milestone', models.TextField(blank=True)), | |
63 | + ('priority', models.TextField(blank=True)), | |
64 | + ('component', models.TextField(blank=True)), | |
65 | + ('version', models.TextField(blank=True)), | |
66 | + ('severity', models.TextField(blank=True)), | |
67 | + ('reporter', models.TextField(blank=True)), | |
68 | + ('author', models.TextField(blank=True)), | |
69 | + ('status', models.TextField(blank=True)), | |
70 | + ('tag', models.TextField(blank=True)), | |
71 | + ('keywords', models.TextField(blank=True)), | |
72 | + ('collaborators', models.TextField(blank=True)), | |
73 | + ('created', models.DateTimeField(null=True, blank=True)), | |
74 | + ('modified', models.DateTimeField(null=True, blank=True)), | |
75 | + ('modified_by', models.TextField(blank=True)), | |
76 | + ], | |
77 | + options={ | |
78 | + 'verbose_name': 'Attachment', | |
79 | + 'verbose_name_plural': 'Attachment', | |
80 | + }, | |
81 | + bases=(models.Model, hitcounter.models.HitCounterModelMixin), | |
82 | + ), | |
83 | + migrations.CreateModel( | |
84 | + name='TicketCollabCount', | |
85 | + fields=[ | |
86 | + ('author', models.TextField(serialize=False, primary_key=True)), | |
87 | + ('count', models.IntegerField()), | |
88 | + ], | |
89 | + options={ | |
90 | + 'verbose_name': 'Attachment', | |
91 | + 'verbose_name_plural': 'Attachment', | |
92 | + }, | |
93 | + bases=(models.Model,), | |
94 | + ), | |
95 | + migrations.CreateModel( | |
96 | + name='Wiki', | |
97 | + fields=[ | |
98 | + ('title', models.TextField(serialize=False, primary_key=True)), | |
99 | + ('wiki_text', models.TextField(blank=True)), | |
100 | + ('author', models.TextField(blank=True)), | |
101 | + ('collaborators', models.TextField(blank=True)), | |
102 | + ('created', models.DateTimeField(null=True, blank=True)), | |
103 | + ('modified', models.DateTimeField(null=True, blank=True)), | |
104 | + ('user', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, null=True)), | |
105 | + ], | |
106 | + options={ | |
107 | + 'verbose_name': 'Attachment', | |
108 | + 'verbose_name_plural': 'Attachment', | |
109 | + }, | |
110 | + bases=(models.Model, hitcounter.models.HitCounterModelMixin), | |
111 | + ), | |
112 | + migrations.CreateModel( | |
113 | + name='WikiCollabCount', | |
114 | + fields=[ | |
115 | + ('author', models.TextField(serialize=False, primary_key=True)), | |
116 | + ('count', models.IntegerField()), | |
117 | + ], | |
118 | + options={ | |
119 | + 'verbose_name': 'Attachment', | |
120 | + 'verbose_name_plural': 'Attachment', | |
121 | + }, | |
122 | + bases=(models.Model,), | |
123 | + ), | |
141 | 124 | ] | ... | ... |
colab/proxy/trac/tests/test_trac.py
... | ... | @@ -162,6 +162,8 @@ class WikiTest(TestCase): |
162 | 162 | wiki.collaborators = 'collaborators' |
163 | 163 | wiki.created = '1994-11-05T08:15:30-05:00' |
164 | 164 | wiki.modified = '1994-11-05T08:15:30-05:00' |
165 | + from django.conf import settings | |
166 | + print settings.INSTALLED_APPS | |
165 | 167 | wiki.save() |
166 | 168 | |
167 | 169 | return wiki | ... | ... |