Merge lp:~thumper/charms/precise/python-django/tweaks into lp:charms/python-django

Proposed by Tim Penhey
Status: Merged
Merged at revision: 35
Proposed branch: lp:~thumper/charms/precise/python-django/tweaks
Merge into: lp:charms/python-django
Diff against target: 159 lines (+36/-40)
3 files modified
hooks/hooks.py (+32/-38)
templates/conf_injection.tmpl (+2/-1)
templates/urls_injection.tmpl (+2/-1)
To merge this branch: bzr merge lp:~thumper/charms/precise/python-django/tweaks
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+245807@code.launchpad.net

Description of the change

Extract out a method for the database sync calls.

This means that when the charm is updated to support Django 1.7, there is only one place that needs to deal with it. The existing behaviour has been kept, even though it is a little weird in one place.

Also the injection templates for urls and settings is updated to make the execution call a little more helpful if there are errors as filenames are shown along-side the code.

To post a comment you must log in.
Revision history for this message
Charles Butler (lazypower) wrote :

my cursory glance on this is good, but I cannot verify this didn't produce test weirdness. Waiting on the automated testing infra to kick this off and give us some results; as well as let Patrick have some time to look this over as he just landed the pure-python branch earlier today.

Thanks for the submission Thumper, awesome to see you're submitting charm patches now.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/hooks.py'
2--- hooks/hooks.py 2014-10-09 19:55:08 +0000
3+++ hooks/hooks.py 2015-01-08 00:39:19 +0000
4@@ -238,6 +238,33 @@
5 inject_file.write(str(template))
6
7
8+def update_database_schema(exit_on_error=True, swallow_exceptions=False):
9+ """Run the appropriate syncdb/migrate calls.
10+
11+ This will make sure that the database reflects the currently installed
12+ applications.
13+ """
14+ django_admin_cmd = find_django_admin_cmd()
15+ # TODO: only run syncdb if Django version is < 1.7
16+ try:
17+ run("%s syncdb --noinput --settings=%s" %
18+ (django_admin_cmd, settings_module),
19+ exit_on_error=exit_on_error, cwd=working_dir)
20+ except:
21+ if not swallow_exceptions:
22+ raise
23+
24+ # TODO: check should be (django_south or Django version > 1.7)
25+ if django_south:
26+ try:
27+ run("%s migrate --settings=%s" %
28+ (django_admin_cmd, settings_module),
29+ exit_on_error=exit_on_error, cwd=working_dir)
30+ except:
31+ if not swallow_exceptions:
32+ raise
33+
34+
35 ###############################################################################
36 # Hook functions
37 ###############################################################################
38@@ -413,20 +440,8 @@
39
40 run('chown -R %s:%s %s' % (wsgi_user, wsgi_group, vcs_clone_dir))
41
42- try:
43- run("%s syncdb --noinput --settings=%s" %
44- (django_admin_cmd, settings_module),
45- exit_on_error=False, cwd=working_dir)
46- except:
47- pass
48-
49- if django_south:
50- try:
51- run("%s migrate --settings=%s" %
52- (django_admin_cmd, settings_module),
53- exit_on_error=False, cwd=working_dir)
54- except:
55- pass
56+ # It isn't entirely clear to me why we don't want to exit on an error here.
57+ update_database_schema(exit_on_error=False, swallow_exceptions=True)
58
59 # Trigger WSGI reloading
60 for relid in relation_ids('wsgi'):
61@@ -441,7 +456,6 @@
62 @hooks.hook('pgsql-relation-joined', 'pgsql-relation-changed')
63 def pgsql_relation_joined_changed():
64 os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
65- django_admin_cmd = find_django_admin_cmd()
66
67 packages = ["python-psycopg2", "postgresql-client"]
68 apt_install(packages, options=['--force-yes'])
69@@ -461,17 +475,11 @@
70 process_template('pgsql_engine.tmpl', templ_vars,
71 settings_database_path % {'engine_name': 'pgsql'})
72
73- run("%s syncdb --noinput --settings=%s" %
74- (django_admin_cmd, settings_module),
75- cwd=working_dir)
76-
77 if django_south:
78 south_config_file = os.path.join(settings_dir_path, '50-south.py')
79 process_template('south.tmpl', {}, south_config_file)
80
81- run("%s migrate --settings=%s" %
82- (django_admin_cmd, settings_module),
83- cwd=working_dir)
84+ update_database_schema()
85
86 run('chown -R %s:%s %s' % (wsgi_user, wsgi_group, vcs_clone_dir))
87
88@@ -494,7 +502,6 @@
89 'mysql-shared-relation-joined', 'mysql-shared-relation-changed')
90 def mysql_relation_joined_changed():
91 os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
92- django_admin_cmd = find_django_admin_cmd()
93
94 packages = ["python-mysqldb", "mysql-client"]
95 apt_install(packages, options=['--force-yes'])
96@@ -513,17 +520,11 @@
97
98 process_template('mysql_engine.tmpl', templ_vars, settings_database_path % {'engine_name': 'mysql'})
99
100- run("%s syncdb --noinput --settings=%s" %
101- (django_admin_cmd, settings_module),
102- cwd=working_dir)
103-
104 if django_south:
105 south_config_file = os.path.join(settings_dir_path, '50-south.py')
106 process_template('south.tmpl', {}, south_config_file)
107
108- run("%s migrate --settings=%s" %
109- (django_admin_cmd, settings_module),
110- cwd=working_dir)
111+ update_database_schema()
112
113 run('chown -R %s:%s %s' % (wsgi_user, wsgi_group, vcs_clone_dir))
114
115@@ -633,7 +634,6 @@
116 @hooks.hook('amqp-relation-changed')
117 def amqp_relation_changed():
118 os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
119- django_admin_cmd = find_django_admin_cmd()
120
121 host = relation_get("hostname")
122 if not host:
123@@ -653,13 +653,7 @@
124
125 run('chown -R %s:%s %s' % (wsgi_user, wsgi_group, vcs_clone_dir))
126
127- run("%s syncdb --noinput --settings=%s" %
128- (django_admin_cmd, settings_module),
129- cwd=working_dir)
130-
131- if django_south:
132- run("%s migrate --settings=%s" % (django_admin_cmd, settings_module),
133- cwd=working_dir)
134+ update_database_schema()
135
136 # Trigger WSGI reloading
137 for relid in relation_ids('wsgi'):
138
139=== modified file 'templates/conf_injection.tmpl'
140--- templates/conf_injection.tmpl 2014-04-16 16:06:59 +0000
141+++ templates/conf_injection.tmpl 2015-01-08 00:39:19 +0000
142@@ -10,4 +10,5 @@
143 from django.conf.global_settings import *
144
145 for f in conffiles:
146- exec(open(abspath(f)).read())
147+ filename = abspath(f)
148+ exec(compile(open(filename, "rb").read(), filename, 'exec'))
149
150=== modified file 'templates/urls_injection.tmpl'
151--- templates/urls_injection.tmpl 2014-04-16 16:06:59 +0000
152+++ templates/urls_injection.tmpl 2015-01-08 00:39:19 +0000
153@@ -10,4 +10,5 @@
154 urlpatterns = ()
155
156 for f in conffiles:
157- exec(open(abspath(f)).read())
158+ filename = abspath(f)
159+ exec(compile(open(filename, "rb").read(), filename, 'exec'))

Subscribers

People subscribed via source and target branches