def ready(self):
from django.core.management import call_command
from morango.models import InstanceIDModel
from morango.certificates import ScopeDefinition
from .signals import add_to_deleted_models # noqa: F401
# NOTE: Warning: https://docs.djangoproject.com/en/1.10/ref/applications/#django.apps.AppConfig.ready
# its recommended not to execute queries in this method, but we are producing the same result after the first call, so its OK
# call this on app load up to get most recent system config settings
try:
InstanceIDModel.get_or_create_current_instance()
if not ScopeDefinition.objects.filter():
call_command("loaddata", "scopedefinitions")
# we catch this error in case the database has not been migrated, b/c we can't query it until its been created
except (OperationalError, ProgrammingError):
pass
# add models to be synced by profile
add_syncable_models()
python类OperationalError()的实例源码
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def check_database_connected(app_configs, **kwargs):
"""
A Django check to see if connecting to the configured default
database backend succeeds.
"""
errors = []
try:
connection.ensure_connection()
except OperationalError as e:
msg = 'Could not connect to database: {!s}'.format(e)
errors.append(checks.Error(msg, id=ERROR_CANNOT_CONNECT_DATABASE))
except ImproperlyConfigured as e:
msg = 'Datbase misconfigured: "{!s}"'.format(e)
errors.append(checks.Error(msg, id=ERROR_MISCONFIGURED_DATABASE))
else:
if not connection.is_usable():
errors.append(checks.Error('Database connection is not usable',
id=ERROR_UNUSABLE_DATABASE))
return errors
def ready(self):
from pretalx.event.models import Event
from pretalx.common.tasks import regenerate_css
from django.db import connection, utils
if Event._meta.db_table not in connection.introspection.table_names():
# commands like `compilemessages` execute ready(), but do not
# require a database to be present. Bail out early, if the Event
# table has not been created yet.
return
try:
for event in Event.objects.all():
regenerate_css.apply_async(args=(event.pk,))
except (utils.OperationalError, utils.ProgrammingError):
pass
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
has_missing_migrations.py 文件源码
项目:django-react-boilerplate
作者: vintasoftware
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def handle(self, *args, **options):
changed = set()
self.stdout.write("Checking...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
sys.exit("Unable to check migrations: cannot connect to database\n")
autodetector = MigrationAutodetector(
executor.loader.project_state(),
ProjectState.from_apps(apps),
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
changed -= set(options['ignore'])
if changed:
sys.exit(
"Apps with model changes but no corresponding migration file: %(changed)s\n" % {
'changed': list(changed)
})
else:
sys.stdout.write("All migration files present\n")
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def create_spatial_indexes(self):
for sql in self.geometry_sql:
try:
self.execute(sql)
except OperationalError:
logger.error(
"Cannot create SPATIAL INDEX %s. Only MyISAM and (as of "
"MySQL 5.7.5) InnoDB support them." % sql
)
self.geometry_sql = []
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def reconnect_db(logfunc, default=None, retry_seconds=None):
'''
Catch Operational errors from the database and try to reconnect.
'''
def _decorator(func):
def _wrapper(*args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except OperationalError as e:
logfunc(e)
close_old_connections()
if retry_seconds is None:
return default
else:
time.sleep(retry_seconds)
_wrapper.__doc__ = func.__doc__
return _wrapper
return _decorator
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
default_plan = Plan.objects.get(pk=Plan.DEFAULT_PK)
up = Profile(user=user, plan=default_plan)
up.save()
try:
for tg in TalkGroupAccess.objects.filter(default_group=True):
up.talkgroup_access.add(tg)
except OperationalError:
pass
try:
new_user_email = SiteOption.objects.get(name='SEND_ADMIN_EMAIL_ON_NEW_USER')
if new_user_email.value_boolean_or_string() == True:
send_mail(
'New {} User {}'.format(settings.SITE_TITLE, user.username),
'New User {} {} Username {} Email {} just registered'.format(user.first_name, user.last_name, user.username, user.email),
settings.SERVER_EMAIL,
[ mail for name, mail in settings.ADMINS],
fail_silently=False,
)
except (SiteOption.DoesNotExist, OperationalError):
pass
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them)." % sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def reset_connection_on_interface_error(func):
"""Decorates function to reset the current thread's Django database
connection on exceptions that appear to come from connection resets.
"""
def _reset(*args, **kwargs):
try:
return func(*args, **kwargs)
except (InterfaceError, OperationalError,
DjangoInterfaceError, DjangoOperationalError) as error:
thread = threading.current_thread()
_logger.warning("it appears this thread's database connection was "
"dropped, resetting it now - you may see further "
"errors about this until the situation is fully "
"resolved for all threads "
"(this thread is '%s', error was '%s')",
thread.name, error)
django.db.connection.connection = None
raise ResetDBConnectionError("The database connection was reset",
error)
return wraps(func)(_reset)
def generate_response(msg):
"""
indices_list = []
try:
indices_list = QueryResults.objects.get(keyname = sno)
except OperationalError:
pass
"""
if 'search' in msg:
q = ''.join([ix for ix in msg.split('search', 1)[1]])
#result = get_data(q) #display the data resulting from a query search
return get_data(q)
elif int(msg) in range(1,100): #fetch the link of that video
link = get_video(int(msg))
return link
else:
return "Use search 'search-term' to look for the video you want"
#return msg
def handle(self, *args, **options):
try:
connection = connections[options['database']]
except ConnectionDoesNotExist:
raise CommandError('Database "%s" does not exist in settings' % options['database'])
for i in range(0, options['retries']):
try:
connection.cursor()
except OperationalError:
i += 1
self.stdout.write('{} / {}: Waiting for database...'.format(i, options['retries']))
sleep(options['sleep_time'])
else:
self.stdout.write(self.style.SUCCESS('Successfully connected to database'))
return
raise CommandError('Number of retries reached, exiting')
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)
def remove_field(self, model, field):
if isinstance(field, GeometryField) and field.spatial_index:
qn = self.connection.ops.quote_name
sql = self.sql_drop_spatial_index % {
'index': qn(self._create_spatial_index_name(model, field)),
'table': qn(model._meta.db_table),
}
try:
self.execute(sql)
except OperationalError:
logger.error(
"Couldn't remove spatial index: %s (may be expected "
"if your storage engine doesn't support them).", sql
)
super(MySQLGISSchemaEditor, self).remove_field(model, field)