def initialize_schema(self): # Create schema here = os.path.abspath(os.path.dirname(__file__)) schema = open(os.path.join(here, 'schema.sql')).read() with self.connect() as cursor: cursor.execute(schema) logger.info('Created PostgreSQL permission tables')
def setup_listeners(config): write_actions = (ACTIONS.CREATE, ACTIONS.UPDATE, ACTIONS.DELETE) settings = config.get_settings() listeners = aslist(settings['event_listeners']) for name in listeners: logger.info('Setting up %r listener') prefix = 'event_listeners.%s.' % name try: listener_mod = config.maybe_dotted(name) prefix = 'event_listeners.%s.' % name.split('.')[-1] listener = listener_mod.load_from_config(config, prefix) except (ImportError, AttributeError): listener_mod = config.maybe_dotted(settings[prefix + 'use']) listener = listener_mod.load_from_config(config, prefix) actions = aslist(settings.get(prefix + 'actions', '')) or write_actions resource_names = aslist(settings.get(prefix + 'resources', '')) decorated = _filter_events(listener, actions, resource_names) if ACTIONS.READ in actions: config.add_subscriber(decorated, ResourceRead) if len(actions) == 1: return config.add_subscriber(decorated, ResourceChanged)
def initialize_schema(self): # Create schema here = os.path.abspath(os.path.dirname(__file__)) schema = open(os.path.join(here, 'schema.sql')).read() # Since called outside request, force commit. with self.client.connect(force_commit=True) as conn: conn.execute(schema) logger.info('Created PostgreSQL cache tables')
def response_error(context, request): """Catch response error from Sync and trace them.""" message = '%s %s: %s' % (context.response.status_code, context.response.reason, context.response.text) # XXX: Make sure these HTTPError exception are coming from SyncClient. statsd_count(request, "syncclient.status_code.%s" % context.response.status_code) if context.response.status_code in (400, 401, 403, 404): # For this code we also want to log the info about the error. logger.info(context, exc_info=True) # For this specific code we do not want to log the error. if context.response.status_code == 304: response = httpexceptions.HTTPNotModified() elif context.response.status_code == 400: response = http_error(httpexceptions.HTTPBadRequest(), errno=ERRORS.INVALID_PARAMETERS, message=message) elif context.response.status_code == 401: response = http_error(httpexceptions.HTTPUnauthorized(), errno=ERRORS.INVALID_AUTH_TOKEN, message=message) # Forget the current user credentials. response.headers.extend(forget(request)) elif context.response.status_code == 403: response = http_error(httpexceptions.HTTPForbidden(), errno=ERRORS.FORBIDDEN, message=message) elif context.response.status_code == 404: response = http_error(httpexceptions.HTTPNotFound(), errno=ERRORS.INVALID_RESOURCE_ID, message=message) elif context.response.status_code == 412: message = 'Resource was modified meanwhile' response = http_error(httpexceptions.HTTPPreconditionFailed(), errno=ERRORS.MODIFIED_MEANWHILE, message=message) else: # For this code we also want to log the error. logger.error(context, exc_info=True) response = service_unavailable( httpexceptions.HTTPServiceUnavailable(), request) request.response = response export_headers(context.response, request) return reapply_cors(request, response)
def initialize_schema(self): """Create PostgreSQL tables, and run necessary schema migrations. .. note:: Relies on JSONB fields, available in recent versions of PostgreSQL. """ version = self._get_installed_version() if not version: # Create full schema. self._check_database_encoding() self._check_database_timezone() # Create full schema. self._execute_sql_file('schema.sql') logger.info('Created PostgreSQL storage tables ' '(version %s).' % self.schema_version) return logger.debug('Detected PostgreSQL schema version %s.' % version) migrations = [(v, v + 1) for v in range(version, self.schema_version)] if not migrations: logger.info('Schema is up-to-date.') for migration in migrations: # Check order of migrations. expected = migration[0] current = self._get_installed_version() error_msg = "Expected version %s. Found version %s." assert expected == current, error_msg % (expected, current) logger.info('Migrate schema from version %s to %s.' % migration) filepath = 'migration_%03d_%03d.sql' % migration self._execute_sql_file(os.path.join('migrations', filepath)) logger.info('Schema migration done.')
def on_new_response(event): response = event.response request = event.request # Compute the request processing time in msec (-1 if unknown) current = utils.msec_time() duration = current - getattr(request, "_received_at", current - 1) isotimestamp = datetime.fromtimestamp(current / 1000).isoformat() # Bind infos for request summary logger. logger.bind(time=isotimestamp, code=response.status_code, t=duration) # Ouput application request summary. logger.info("request.summary")
def initialize_schema(self): """Create PostgreSQL tables, and run necessary schema migrations. .. note:: Relies on JSONB fields, available in recent versions of PostgreSQL. """ version = self._get_installed_version() if not version: # Create full schema. self._check_database_encoding() self._check_database_timezone() # Create full schema. self._execute_sql_file('schema.sql') logger.info('Created PostgreSQL storage tables ' '(version %s).' % self.schema_version) return logger.debug('Detected PostgreSQL schema version %s.' % version) migrations = [(v, v + 1) for v in range(version, self.schema_version)] if not migrations: logger.info('Schema is up-to-date.') for migration in migrations: # Check order of migrations. expected = migration[0] current = self._get_installed_version() error_msg = "Expected version %s. Found version %s." if expected != current: raise AssertionError(error_msg % (expected, current)) logger.info('Migrate schema from version %s to %s.' % migration) filepath = 'migration_%03d_%03d.sql' % migration self._execute_sql_file(os.path.join('migrations', filepath)) logger.info('Schema migration done.')
def process_error(self): logger.info('Generic error returned. Submitted data was %s, %s', self.error, self.collection) raise BackendError(original=self.collection, message='Validation error while creating object. Please report ' 'this to support')