Merge pull request #1493 from washort/more-commits

Fix: more `db.commit()` calls
This commit is contained in:
Arik Fraimovich 2017-01-02 16:08:29 +02:00 committed by GitHub
commit 7d8b8a16fd
14 changed files with 34 additions and 35 deletions

View File

@ -21,6 +21,7 @@ class AlertResource(BaseResource):
require_admin_or_owner(alert.user.id) require_admin_or_owner(alert.user.id)
self.update_model(alert, params) self.update_model(alert, params)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'edit', 'action': 'edit',
@ -57,6 +58,8 @@ class AlertListResource(BaseResource):
models.db.session.add(alert) models.db.session.add(alert)
models.db.session.flush() models.db.session.flush()
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'create', 'action': 'create',
'timestamp': int(time.time()), 'timestamp': int(time.time()),
@ -64,9 +67,7 @@ class AlertListResource(BaseResource):
'object_type': 'alert' 'object_type': 'alert'
}) })
a = alert.to_dict() return alert.to_dict()
models.db.session.commit()
return a
@require_permission('list_alerts') @require_permission('list_alerts')
def get(self): def get(self):
@ -87,6 +88,8 @@ class AlertSubscriptionListResource(BaseResource):
subscription = models.AlertSubscription(**kwargs) subscription = models.AlertSubscription(**kwargs)
models.db.session.add(subscription) models.db.session.add(subscription)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'subscribe', 'action': 'subscribe',
'timestamp': int(time.time()), 'timestamp': int(time.time()),
@ -96,7 +99,6 @@ class AlertSubscriptionListResource(BaseResource):
}) })
d = subscription.to_dict() d = subscription.to_dict()
models.db.session.commit()
return d return d
def get(self, alert_id): def get(self, alert_id):
@ -113,10 +115,11 @@ class AlertSubscriptionResource(BaseResource):
subscription = models.AlertSubscription.query.get_or_404(subscriber_id) subscription = models.AlertSubscription.query.get_or_404(subscriber_id)
require_admin_or_owner(subscription.user.id) require_admin_or_owner(subscription.user.id)
models.db.session.delete(subscription) models.db.session.delete(subscription)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'unsubscribe', 'action': 'unsubscribe',
'timestamp': int(time.time()), 'timestamp': int(time.time()),
'object_id': alert_id, 'object_id': alert_id,
'object_type': 'alert' 'object_type': 'alert'
}) })
models.db.session.commit()

View File

@ -66,7 +66,6 @@ class DashboardResource(BaseResource):
require_object_modify_permission(dashboard, self.current_user) require_object_modify_permission(dashboard, self.current_user)
updates = project(dashboard_properties, ('name', 'layout', 'version', updates = project(dashboard_properties, ('name', 'layout', 'version',
'is_draft')) 'is_draft'))
@ -116,6 +115,8 @@ class DashboardShareResource(BaseResource):
require_admin_or_owner(dashboard.user_id) require_admin_or_owner(dashboard.user_id)
api_key = models.ApiKey.create_for_object(dashboard, self.current_user) api_key = models.ApiKey.create_for_object(dashboard, self.current_user)
models.db.session.flush() models.db.session.flush()
models.db.session.commit()
public_url = url_for('redash.public_dashboard', token=api_key.api_key, org_slug=self.current_org.slug, _external=True) public_url = url_for('redash.public_dashboard', token=api_key.api_key, org_slug=self.current_org.slug, _external=True)
self.record_event({ self.record_event({
@ -134,10 +135,10 @@ class DashboardShareResource(BaseResource):
if api_key: if api_key:
api_key.active = False api_key.active = False
models.db.session.add(api_key) models.db.session.add(api_key)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'deactivate_api_key', 'action': 'deactivate_api_key',
'object_id': dashboard.id, 'object_id': dashboard.id,
'object_type': 'dashboard', 'object_type': 'dashboard',
}) })
models.db.session.commit()

View File

@ -127,30 +127,24 @@ class DataSourcePauseResource(BaseResource):
reason = request.args.get('reason') reason = request.args.get('reason')
data_source.pause(reason) data_source.pause(reason)
models.db.session.add(data_source)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'pause', 'action': 'pause',
'object_id': data_source.id, 'object_id': data_source.id,
'object_type': 'datasource' 'object_type': 'datasource'
}) })
return data_source.to_dict() return data_source.to_dict()
@require_admin @require_admin
def delete(self, data_source_id): def delete(self, data_source_id):
data_source = get_object_or_404(models.DataSource.get_by_id_and_org, data_source_id, self.current_org) data_source = get_object_or_404(models.DataSource.get_by_id_and_org, data_source_id, self.current_org)
data_source.resume() data_source.resume()
models.db.session.add(data_source)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'resume', 'action': 'resume',
'object_id': data_source.id, 'object_id': data_source.id,
'object_type': 'datasource' 'object_type': 'datasource'
}) })
return data_source.to_dict() return data_source.to_dict()

View File

@ -32,14 +32,14 @@ class DestinationResource(BaseResource):
try: try:
destination.options.set_schema(schema) destination.options.set_schema(schema)
destination.options.update(req['options']) destination.options.update(req['options'])
models.db.session.add(destination)
models.db.session.commit()
except ValidationError: except ValidationError:
abort(400) abort(400)
destination.type = req['type'] destination.type = req['type']
destination.name = req['name'] destination.name = req['name']
models.db.session.add(destination)
return destination.to_dict(all=True) return destination.to_dict(all=True)
@require_admin @require_admin
@ -88,5 +88,5 @@ class DestinationListResource(BaseResource):
user=self.current_user) user=self.current_user)
models.db.session.add(destination) models.db.session.add(destination)
models.db.session.commit()
return destination.to_dict(all=True) return destination.to_dict(all=True)

View File

@ -57,6 +57,7 @@ def run_query_sync(data_source, parameter_values, query_text, max_age=0):
query_hash, query_text, data, query_hash, query_text, data,
run_time, utils.utcnow()) run_time, utils.utcnow())
models.db.session.commit()
return data return data
except Exception, e: except Exception, e:
if max_age > 0: if max_age > 0:
@ -79,6 +80,7 @@ def embed(query_id, visualization_id, org_slug=None):
}) })
full_path = safe_join(settings.STATIC_ASSETS_PATHS[-2], 'index.html') full_path = safe_join(settings.STATIC_ASSETS_PATHS[-2], 'index.html')
models.db.session.commit()
return send_file(full_path, **dict(cache_timeout=0, conditional=True)) return send_file(full_path, **dict(cache_timeout=0, conditional=True))
@ -94,6 +96,6 @@ def public_dashboard(token, org_slug=None):
# 'headless': 'embed' in request.args, # 'headless': 'embed' in request.args,
# 'referer': request.headers.get('Referer') # 'referer': request.headers.get('Referer')
# }) # })
# models.db.session.commit()
full_path = safe_join(settings.STATIC_ASSETS_PATHS[-2], 'index.html') full_path = safe_join(settings.STATIC_ASSETS_PATHS[-2], 'index.html')
return send_file(full_path, **dict(cache_timeout=0, conditional=True)) return send_file(full_path, **dict(cache_timeout=0, conditional=True))

View File

@ -9,4 +9,3 @@ class EventResource(BaseResource):
for event in events_list: for event in events_list:
self.record_event(event) self.record_event(event)

View File

@ -42,6 +42,7 @@ class GroupResource(BaseResource):
abort(400, message="Can't modify built-in groups.") abort(400, message="Can't modify built-in groups.")
group.name = request.json['name'] group.name = request.json['name']
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'edit', 'action': 'edit',
@ -77,7 +78,6 @@ class GroupMemberListResource(BaseResource):
user = models.User.get_by_id_and_org(user_id, self.current_org) user = models.User.get_by_id_and_org(user_id, self.current_org)
group = models.Group.get_by_id_and_org(group_id, self.current_org) group = models.Group.get_by_id_and_org(group_id, self.current_org)
user.group_ids.append(group.id) user.group_ids.append(group.id)
models.db.session.commit() models.db.session.commit()
self.record_event({ self.record_event({
@ -87,7 +87,6 @@ class GroupMemberListResource(BaseResource):
'object_type': 'group', 'object_type': 'group',
'member_id': user.id 'member_id': user.id
}) })
return user.to_dict() return user.to_dict()
@require_permission('list_users') @require_permission('list_users')
@ -104,7 +103,6 @@ class GroupMemberResource(BaseResource):
def delete(self, group_id, user_id): def delete(self, group_id, user_id):
user = models.User.get_by_id_and_org(user_id, self.current_org) user = models.User.get_by_id_and_org(user_id, self.current_org)
user.group_ids.remove(int(group_id)) user.group_ids.remove(int(group_id))
models.db.session.commit() models.db.session.commit()
self.record_event({ self.record_event({
@ -130,7 +128,6 @@ class GroupDataSourceListResource(BaseResource):
group = models.Group.get_by_id_and_org(group_id, self.current_org) group = models.Group.get_by_id_and_org(group_id, self.current_org)
data_source_group = data_source.add_group(group) data_source_group = data_source.add_group(group)
models.db.session.commit() models.db.session.commit()
self.record_event({ self.record_event({
@ -164,7 +161,6 @@ class GroupDataSourceResource(BaseResource):
view_only = request.json['view_only'] view_only = request.json['view_only']
data_source_group = data_source.update_group_permission(group, view_only) data_source_group = data_source.update_group_permission(group, view_only)
models.db.session.commit() models.db.session.commit()
self.record_event({ self.record_event({
@ -184,6 +180,7 @@ class GroupDataSourceResource(BaseResource):
group = models.Group.get_by_id_and_org(group_id, self.current_org) group = models.Group.get_by_id_and_org(group_id, self.current_org)
data_source.remove_group(group) data_source.remove_group(group)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'remove_data_source', 'action': 'remove_data_source',

View File

@ -55,13 +55,14 @@ class ObjectPermissionsListResource(BaseResource):
abort(400, message='User not found.') abort(400, message='User not found.')
permission = AccessPermission.grant(obj, access_type, grantee, self.current_user) permission = AccessPermission.grant(obj, access_type, grantee, self.current_user)
db.session.commit()
self.record_event({ self.record_event({
'action': 'grant_permission', 'action': 'grant_permission',
'object_id': object_id, 'object_id': object_id,
'object_type': object_type, 'object_type': object_type,
'grantee': grantee.id,
'access_type': access_type, 'access_type': access_type,
'grantee': grantee.id
}) })
return permission.to_dict() return permission.to_dict()
@ -80,7 +81,9 @@ class ObjectPermissionsListResource(BaseResource):
grantee = User.query.get(req['user_id']) grantee = User.query.get(req['user_id'])
if grantee is None: if grantee is None:
abort(400, message='User not found.') abort(400, message='User not found.')
AccessPermission.revoke(obj, grantee, access_type) AccessPermission.revoke(obj, grantee, access_type)
db.session.commit()
self.record_event({ self.record_event({
'action': 'revoke_permission', 'action': 'revoke_permission',
@ -89,7 +92,6 @@ class ObjectPermissionsListResource(BaseResource):
'access_type': access_type, 'access_type': access_type,
'grantee_id': grantee_id 'grantee_id': grantee_id
}) })
db.session.commit()
class CheckPermissionResource(BaseResource): class CheckPermissionResource(BaseResource):

View File

@ -121,8 +121,7 @@ class QueryResource(BaseResource):
except StaleDataError: except StaleDataError:
abort(409) abort(409)
result = query.to_dict(with_visualizations=True) return query.to_dict(with_visualizations=True)
return result
@require_permission('view_query') @require_permission('view_query')
def get(self, query_id): def get(self, query_id):
@ -138,6 +137,7 @@ class QueryResource(BaseResource):
query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org) query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org)
require_admin_or_owner(query.user_id) require_admin_or_owner(query.user_id)
query.archive(self.current_user) query.archive(self.current_user)
models.db.session.commit()
class QueryForkResource(BaseResource): class QueryForkResource(BaseResource):
@ -145,6 +145,7 @@ class QueryForkResource(BaseResource):
def post(self, query_id): def post(self, query_id):
query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org) query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org)
forked_query = query.fork(self.current_user) forked_query = query.fork(self.current_user)
models.db.session.commit()
return forked_query.to_dict(with_visualizations=True) return forked_query.to_dict(with_visualizations=True)

View File

@ -71,7 +71,6 @@ class QueryResultListResource(BaseResource):
'object_type': 'data_source', 'object_type': 'data_source',
'query': query 'query': query
}) })
return run_query(data_source, parameter_values, query, query_id, max_age) return run_query(data_source, parameter_values, query, query_id, max_age)

View File

@ -21,13 +21,13 @@ class QuerySnippetResource(BaseResource):
require_admin_or_owner(snippet.user.id) require_admin_or_owner(snippet.user.id)
self.update_model(snippet, params) self.update_model(snippet, params)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'edit', 'action': 'edit',
'object_id': snippet.id, 'object_id': snippet.id,
'object_type': 'query_snippet' 'object_type': 'query_snippet'
}) })
models.db.session.commit()
return snippet.to_dict() return snippet.to_dict()
def delete(self, snippet_id): def delete(self, snippet_id):
@ -58,13 +58,14 @@ class QuerySnippetListResource(BaseResource):
) )
models.db.session.add(snippet) models.db.session.add(snippet)
models.db.session.commit()
self.record_event({ self.record_event({
'action': 'create', 'action': 'create',
'object_id': snippet.id, 'object_id': snippet.id,
'object_type': 'query_snippet' 'object_type': 'query_snippet'
}) })
models.db.session.commit()
return snippet.to_dict() return snippet.to_dict()
def get(self): def get(self):

View File

@ -110,7 +110,7 @@ class UserResource(BaseResource):
try: try:
self.update_model(user, params) self.update_model(user, params)
# TODO: this won't be triggered at this point. Need to call db.session.commit? models.db.session.commit()
except IntegrityError as e: except IntegrityError as e:
if "email" in e.message: if "email" in e.message:
message = "Email already taken." message = "Email already taken."

View File

@ -49,7 +49,7 @@ class WidgetListResource(BaseResource):
widget.dashboard.layout = json.dumps(layout) widget.dashboard.layout = json.dumps(layout)
models.db.session.add(widget.dashboard) models.db.session.add(widget.dashboard)
models.db.session.commit()
return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row, 'version': dashboard.version} return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row, 'version': dashboard.version}
@ -61,14 +61,13 @@ class WidgetResource(BaseResource):
require_object_modify_permission(widget.dashboard, self.current_user) require_object_modify_permission(widget.dashboard, self.current_user)
widget_properties = request.get_json(force=True) widget_properties = request.get_json(force=True)
widget.text = widget_properties['text'] widget.text = widget_properties['text']
models.db.session.commit()
return widget.to_dict() return widget.to_dict()
@require_permission('edit_dashboard') @require_permission('edit_dashboard')
def delete(self, widget_id): def delete(self, widget_id):
widget = models.Widget.get_by_id_and_org(widget_id, self.current_org) widget = models.Widget.get_by_id_and_org(widget_id, self.current_org)
require_object_modify_permission(widget.dashboard, self.current_user) require_object_modify_permission(widget.dashboard, self.current_user)
widget.delete() widget.delete()
models.db.session.commit()
return {'layout': widget.dashboard.layout, 'version': widget.dashboard.version} return {'layout': widget.dashboard.layout, 'version': widget.dashboard.version}

View File

@ -13,6 +13,7 @@ logger = get_task_logger(__name__)
def record_event(event): def record_event(event):
original_event = event.copy() original_event = event.copy()
models.Event.record(event) models.Event.record(event)
models.db.session.commit()
for hook in settings.EVENT_REPORTING_WEBHOOKS: for hook in settings.EVENT_REPORTING_WEBHOOKS:
logger.debug("Forwarding event to: %s", hook) logger.debug("Forwarding event to: %s", hook)
try: try: