def save_new_form(form): """ Save a previously unsaved form """ assert not form.is_saved(), 'form already saved' logging.debug('Saving new form: %s', form) unsaved_attachments = getattr(form, 'unsaved_attachments', []) if unsaved_attachments: for unsaved_attachment in unsaved_attachments: unsaved_attachment.form = form operations = form.get_tracked_models_to_create(XFormOperationSQL) for operation in operations: operation.form = form with get_cursor(XFormInstanceSQL) as cursor: cursor.execute( 'SELECT form_pk FROM save_new_form_and_related_models(%s, %s, %s, %s)', [form.form_id, form, unsaved_attachments, operations] ) result = fetchone_as_namedtuple(cursor) form.id = result.form_pk try: del form.unsaved_attachments except AttributeError: pass form.clear_tracked_models()
def _index_exists(db, index_name): with connections[db].cursor() as cursor: sql = "SELECT to_regclass('{}') IS NOT NULL as index_exists".format( index_name) log_sql(sql) cursor.execute(sql) return fetchone_as_namedtuple(cursor).index_exists
def _index_exists(db, index_name): with CommCareCaseIndexSQL.get_cursor_for_partition_db( db).cursor() as cursor: sql = "SELECT to_regclass('{}') IS NOT NULL as index_exists".format( index_name) log_sql(sql) cursor.execute(sql) return fetchone_as_namedtuple(cursor).index_exists
def save_case(case): transactions_to_save = case.get_tracked_models_to_create( CaseTransaction) indices_to_save_or_update = case.get_tracked_models_to_create( CommCareCaseIndexSQL) indices_to_save_or_update.extend( case.get_tracked_models_to_update(CommCareCaseIndexSQL)) index_ids_to_delete = [ index.id for index in case.get_tracked_models_to_delete( CommCareCaseIndexSQL) ] attachments_to_save = case.get_tracked_models_to_create( CaseAttachmentSQL) attachment_ids_to_delete = [ att.id for att in case.get_tracked_models_to_delete(CaseAttachmentSQL) ] for index in indices_to_save_or_update: index.domain = case.domain # ensure domain is set on indices # cast arrays that can be empty to appropriate type query = """SELECT case_pk FROM save_case_and_related_models( %s, %s, %s, %s::{}[], %s::{}[], %s::INTEGER[], %s::INTEGER[] )""" query = query.format(CommCareCaseIndexSQL_DB_TABLE, CaseAttachmentSQL_DB_TABLE) with get_cursor(CommCareCaseSQL) as cursor: try: cursor.execute(query, [ case.case_id, case, transactions_to_save, indices_to_save_or_update, attachments_to_save, index_ids_to_delete, attachment_ids_to_delete ]) result = fetchone_as_namedtuple(cursor) case.id = result.case_pk case.clear_tracked_models() except InternalError as e: if logging.root.isEnabledFor(logging.DEBUG): msg = 'save_case_and_related_models called with args: \n{}, {}, {}, {} ,{} ,{}'.format( case_adapter(case).getquoted(), [ case_transaction_adapter(t).getquoted() for t in transactions_to_save ], [ case_index_adapter(i).getquoted() for i in indices_to_save_or_update ], [ case_attachment_adapter(a).getquoted() for a in attachments_to_save ], index_ids_to_delete, attachment_ids_to_delete) logging.debug(msg) raise CaseSaveError(e) else: for attachment in case.get_tracked_models_to_delete( CaseAttachmentSQL): attachment.delete_content()
def case_modified_since(case_id, server_modified_on): """ Return True if a case has been modified since the given modification date. Assumes that the case exists in the DB. """ with get_cursor(CommCareCaseSQL) as cursor: cursor.execute('SELECT case_modified FROM case_modified_since(%s, %s)', [case_id, server_modified_on]) result = fetchone_as_namedtuple(cursor) return result.case_modified
def get_doc_count(self, from_db): """Get the doc count from the given DB :param from_db: The DB alias to query """ from_db = 'default' if from_db is None else from_db sql_query = "SELECT reltuples FROM pg_class WHERE oid = '{}'::regclass" db_cursor = connections[from_db].cursor() with db_cursor as cursor: cursor.execute(sql_query.format(self.model_class._meta.db_table)) return int(fetchone_as_namedtuple(cursor).reltuples)
def hash_doc_uuid_sql(doc_uuid): """Get the hash for a UUID from PostgreSQL This is used to ensure the python version is consistent with what's being used by PL/Proxy """ assert settings.USE_PARTITIONED_DATABASE if not isinstance(doc_uuid, UUID): raise ValueError("Expected an instance of UUID") query = "SELECT hash_string(CAST(%s AS bytea), 'siphash24') AS hash" with get_cursor(XFormInstanceSQL) as cursor: doc_uuid_before_cast = '\\x%s' % doc_uuid.hex cursor.execute(query, [doc_uuid_before_cast]) return fetchone_as_namedtuple(cursor).hash
def save_case(case): transactions_to_save = case.get_tracked_models_to_create(CaseTransaction) indices_to_save_or_update = case.get_tracked_models_to_create(CommCareCaseIndexSQL) indices_to_save_or_update.extend(case.get_tracked_models_to_update(CommCareCaseIndexSQL)) index_ids_to_delete = [index.id for index in case.get_tracked_models_to_delete(CommCareCaseIndexSQL)] attachments_to_save = case.get_tracked_models_to_create(CaseAttachmentSQL) attachment_ids_to_delete = [att.id for att in case.get_tracked_models_to_delete(CaseAttachmentSQL)] for index in indices_to_save_or_update: index.domain = case.domain # ensure domain is set on indices # cast arrays that can be empty to appropriate type query = """SELECT case_pk FROM save_case_and_related_models( %s, %s, %s, %s::{}[], %s::{}[], %s::INTEGER[], %s::INTEGER[] )""" query = query.format(CommCareCaseIndexSQL_DB_TABLE, CaseAttachmentSQL_DB_TABLE) with get_cursor(CommCareCaseSQL) as cursor: try: cursor.execute(query, [ case.case_id, case, transactions_to_save, indices_to_save_or_update, attachments_to_save, index_ids_to_delete, attachment_ids_to_delete ]) result = fetchone_as_namedtuple(cursor) case.id = result.case_pk case.clear_tracked_models() except InternalError as e: if logging.root.isEnabledFor(logging.DEBUG): msg = 'save_case_and_related_models called with args: \n{}, {}, {}, {} ,{} ,{}'.format( case_adapter(case).getquoted(), [case_transaction_adapter(t).getquoted() for t in transactions_to_save], [case_index_adapter(i).getquoted() for i in indices_to_save_or_update], [case_attachment_adapter(a).getquoted() for a in attachments_to_save], index_ids_to_delete, attachment_ids_to_delete ) logging.debug(msg) raise CaseSaveError(e) else: for attachment in case.get_tracked_models_to_delete(CaseAttachmentSQL): attachment.delete_content()
def save_new_form(form): """ Save a previously unsaved form """ assert not form.is_saved(), 'form already saved' logging.debug('Saving new form: %s', form) unsaved_attachments = getattr(form, 'unsaved_attachments', []) if unsaved_attachments: del form.unsaved_attachments for unsaved_attachment in unsaved_attachments: unsaved_attachment.form = form operations = form.get_tracked_models_to_create(XFormOperationSQL) for operation in operations: operation.form = form form.clear_tracked_models() with get_cursor(XFormInstanceSQL) as cursor: cursor.execute( 'SELECT form_pk FROM save_new_form_and_related_models(%s, %s, %s, %s)', [form.form_id, form, unsaved_attachments, operations] ) result = fetchone_as_namedtuple(cursor) form.id = result.form_pk
def form_exists(form_id, domain=None): with get_cursor(XFormInstanceSQL) as cursor: cursor.execute('SELECT * FROM check_form_exists(%s, %s)', [form_id, domain]) result = fetchone_as_namedtuple(cursor) return result.form_exists
def form_with_id_exists(form_id, domain=None): with get_cursor(XFormInstanceSQL) as cursor: cursor.execute('SELECT * FROM check_form_exists(%s, %s)', [form_id, domain]) result = fetchone_as_namedtuple(cursor) return result.form_exists
def _index_exists(db, index_name): with connections[db].cursor() as cursor: sql = "SELECT to_regclass('{}') IS NOT NULL as index_exists".format(index_name) log_sql(sql) cursor.execute(sql) return fetchone_as_namedtuple(cursor).index_exists