Skip to content

datastore triggers #3428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 11, 2017
Merged

datastore triggers #3428

merged 21 commits into from
May 11, 2017

Conversation

wardi
Copy link
Contributor

@wardi wardi commented Feb 7, 2017

  1. Add an API call for syadmins (only) to define PL/pgSQL triggers in the datastore db
  2. Extend datastore_create to allow applying those triggers when creating or modifying tables

Triggers are great for cleaning data, enforcing validation and/or keeping modification records as data is being loaded. Using triggers instead of validation logic at the python level lets us enforce those rules regardless of how the data is loaded (bulk loading data straight to postgres is very nice for large datasets)

@mattfullerton
Copy link
Contributor

👍

@wardi wardi added the WIP label Feb 7, 2017
@wardi wardi removed the WIP label Feb 28, 2017
@wardi
Copy link
Contributor Author

wardi commented Feb 28, 2017

@amercader this is feature complete with test coverage now.

@wardi
Copy link
Contributor Author

wardi commented Feb 28, 2017

For issues on #3414 please report them there and I'll fix on that branch and merge back into this one.

wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
wardi added a commit to open-data/ckan that referenced this pull request Feb 28, 2017
@amercader amercader self-assigned this Mar 2, 2017
@wardi wardi added the WIP label Mar 14, 2017
@wardi
Copy link
Contributor Author

wardi commented Mar 14, 2017

need to fix quantified code error and exceptions aren't being caught by upsert.

@wardi wardi removed the WIP label Apr 18, 2017
@wardi
Copy link
Contributor Author

wardi commented Apr 18, 2017

@amercader this is ready for review

Copy link
Member

@amercader amercader left a comment

Choose a reason for hiding this comment

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

Great stuff @wardi, see only my minor comments

# Make a copy of the Pylons config, so we can restore it in teardown.
cls._original_config = dict(config)
cls._apply_config_changes(config)
cls._get_test_app()
for plugin in getattr(cls, '_load_plugins', []):
Copy link
Member

Choose a reason for hiding this comment

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

I like this!

or "for_each" parameters from triggers list.
'''
existing = connection.execute(
u'SELECT tgname FROM pg_trigger WHERE tgrelid = %s::regclass',
Copy link
Member

Choose a reason for hiding this comment

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

Should we use datastore_helpers.identifier here? Maybe not, I'm not familiar with ::regclass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the execute() call is quoting the resource_id parameter for us. I use identifier when I can't avoid hand-rolling sql.

:param triggers: triggers to apply to this table, eg: [
{"function": "trigger_clean_reference"},
{"function": "trigger_check_codes"}]
:type triggers: list of dictionaries
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add a note that says that these triggers can be created with :py:func:`datastore_function_create` so docs are linked. Are they limited to the ones created via this action? Or can you invoke triggers manually created on Postgres?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. You can use any postgresql function that returns type trigger, whether created by datastore_function_create or manually.

@amercader amercader merged commit 784a6e1 into master May 11, 2017
@amercader amercader deleted the 3428-datastore-triggers branch May 11, 2017 19:46
@wardi
Copy link
Contributor Author

wardi commented May 11, 2017

Thanks @amercader !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants