Skip to content
This repository was archived by the owner on Apr 16, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fluff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import fluff.util


default_app_config = 'fluff.app_config.FluffAppConfig'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat (had to go read the docs to figure out what this was doing)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global magic!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I didn't really want to go this route but also didn't want to have to coordinate with an update to settings.py



REDUCE_TYPES = set(['sum', 'count', 'min', 'max', 'sumsqr'])
TYPE_INTEGER = 'integer'
TYPE_DECIMAL = 'decimal'
Expand Down
10 changes: 10 additions & 0 deletions fluff/app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from fluff.signals import catch_signal


class FluffAppConfig(AppConfig):
name = 'fluff'

def ready(self):
post_migrate.connect(catch_signal, sender=self)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this means the signal only gets fired once (for this app) instead of for every app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if you think we need to worry about this:

The ready() method may be executed more than once during testing, so you may want to guard your signals from duplication, especially if you’re planning to send them within tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so but could be good practice to do it anyway.

On 19 October 2015 at 19:46, Ethan Soergel notifications@github.com wrote:

In fluff/app_config.py
#105 (comment):

@@ -0,0 +1,10 @@
+from django.apps import AppConfig
+from django.db.models.signals import post_migrate
+from fluff.signals import catch_signal
+
+
+class FluffAppConfig(AppConfig):

  • name = 'fluff'
  • def ready(self):
  •    post_migrate.connect(catch_signal, sender=self)
    

Curious if you think we need to worry about this
https://docs.djangoproject.com/en/1.8/topics/signals/#connecting-receiver-functions
:

The ready() method may be executed more than once during testing, so you
may want to guard your signals from duplication
https://docs.djangoproject.com/en/1.8/topics/signals/#preventing-duplicate-signals,
especially if you’re planning to send them within tests.


Reply to this email directly or view it on GitHub
https://github.com/dimagi/fluff/pull/105/files#r42402361.

Simon Kelly
Senior Engineer | Dimagi South Africa

53 changes: 24 additions & 29 deletions fluff/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,37 @@ class RebuildTableException(Exception):
pass


def catch_signal(app, **kwargs):
def catch_signal(sender, **kwargs):
if settings.UNIT_TESTING:
return

app_name = app.__name__.rsplit('.', 1)[0]
if app_name == 'fluff':
from fluff import FluffPillow
table_pillow_map = {}
pillows = list(chain.from_iterable(settings.PILLOWTOPS.values()))
for pillow_string in pillows:
pillow_class = get_pillow_class(pillow_string)
if issubclass(pillow_class, FluffPillow):
doc = pillow_class.indicator_class()
if doc.save_direct_to_sql:
table_pillow_map[doc._table.name] = {
'doc': doc,
'pillow': pillow_class
}
from fluff import FluffPillow
table_pillow_map = {}
pillows = list(chain.from_iterable(settings.PILLOWTOPS.values()))
for pillow_string in pillows:
pillow_class = get_pillow_class(pillow_string)
if issubclass(pillow_class, FluffPillow):
doc = pillow_class.indicator_class()
if doc.save_direct_to_sql:
table_pillow_map[doc._table.name] = {
'doc': doc,
'pillow': pillow_class
}

print '\tchecking fluff SQL tables for schema changes'
engine = sqlalchemy.create_engine(settings.SQL_REPORTING_DATABASE_URL)

print '\tchecking fluff SQL tables for schema changes'
engine = sqlalchemy.create_engine(settings.SQL_REPORTING_DATABASE_URL)

with engine.begin() as connection:
migration_context = get_migration_context(connection, table_pillow_map.keys())
diffs = compare_metadata(migration_context, fluff_metadata)
with engine.begin() as connection:
migration_context = get_migration_context(connection, table_pillow_map.keys())
diffs = compare_metadata(migration_context, fluff_metadata)

tables_to_rebuild = get_tables_to_rebuild(diffs, table_pillow_map.keys())
tables_to_rebuild = get_tables_to_rebuild(diffs, table_pillow_map.keys())

for table in tables_to_rebuild:
info = table_pillow_map[table]
rebuild_table(engine, info['pillow'], info['doc'])
for table in tables_to_rebuild:
info = table_pillow_map[table]
rebuild_table(engine, info['pillow'], info['doc'])

engine.dispose()
engine.dispose()


def get_tables_to_rebuild(diffs, table_names):
Expand Down Expand Up @@ -103,6 +101,3 @@ def get_migration_context(connection, table_names):

def include_symbol(names_to_include, table_name, schema):
return table_name in names_to_include


signals.post_syncdb.connect(catch_signal)