Beispiel #1
0
            if database['ENGINE'] == 'postgresql_psycopg2':
                full_engine = 'django.contrib.gis.db.backends.postgis'
            elif database['ENGINE'] == 'sqlite3':
                full_engine = 'django.contrib.gis.db.backends.spatialite'
            else:
                full_engine = 'django.contrib.gis.db.backends.%s' % database['ENGINE']
        else:
            warnings.warn(
                "Short names for ENGINE in database configurations are deprecated. "
                "Prepend %s.ENGINE with 'django.db.backends.'" % alias,
                PendingDeprecationWarning
            )
            full_engine = "django.db.backends.%s" % database['ENGINE']
        database['ENGINE'] = full_engine

connections = ConnectionHandler(settings.DATABASES)

router = ConnectionRouter(settings.DATABASE_ROUTERS)

# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits.

# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
# we manually create the dictionary from the settings, passing only the
# settings that the database backends care about. Note that TIME_ZONE is used
# by the PostgreSQL backends.
# we load all these up for backwards compatibility, you should use
# connections['default'] instead.
connection = connections[DEFAULT_DB_ALIAS]
backend = load_backend(connection.settings_dict['ENGINE'])
Beispiel #2
0
from django.core import signals
from django.db.utils import (DEFAULT_DB_ALIAS, DataError, OperationalError,
    IntegrityError, InternalError, ProgrammingError, NotSupportedError,
    DatabaseError, InterfaceError, Error, load_backend,
    ConnectionHandler, ConnectionRouter)
from django.utils.functional import cached_property


__all__ = [
    'backend', 'connection', 'connections', 'router', 'DatabaseError',
    'IntegrityError', 'InternalError', 'ProgrammingError', 'DataError',
    'NotSupportedError', 'Error', 'InterfaceError', 'OperationalError',
    'DEFAULT_DB_ALIAS'
]

connections = ConnectionHandler()

router = ConnectionRouter()


# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits.

# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
# we manually create the dictionary from the settings, passing only the
# settings that the database backends care about. Note that TIME_ZONE is used
# by the PostgreSQL backends.
# We load all these up for backwards compatibility, you should use
# connections['default'] instead.
class DefaultConnectionProxy(object):
    """
Beispiel #3
0
from django.db.utils import ConnectionHandler
from django.test import SimpleTestCase, TestCase, TransactionTestCase

from django_mysql.management.commands.fix_datetime_columns import (
    parse_create_table, )
from django_mysql.utils import connection_is_mariadb

# Can't use @override_settings to swap out DATABASES, instead just mock.patch
# a new ConnectionHandler into the command module

command_connections = (
    'django_mysql.management.commands.fix_datetime_columns.connections')

sqlite = ConnectionHandler({
    'default': {
        'ENGINE': 'django.db.backends.sqlite3'
    },
})


def run_it(*args, **kwargs):
    run_args = ['fix_datetime_columns']
    run_args.extend(args)

    out = StringIO()
    run_kwargs = {'stdout': out, 'skip_checks': True}
    run_kwargs.update(kwargs)

    call_command(*run_args, **run_kwargs)

    return out.getvalue()
    def handle_inspection(self, options):
        dbs = copy.copy(settings.DATABASES)
        dbs['brapischemadbfile'] = {'ENGINE': 'django.db.backends.sqlite3',
                                    'NAME': options['dbfile']}
        conns = ConnectionHandler(dbs)
        connection = conns['brapischemadbfile']

        # connection = connections[options['database']]
        # 'table_name_filter' is a stealth option
        table_name_filter = options.get('table_name_filter')

        def table2model(table_name):
            return re.sub(r'[^a-zA-Z0-9]', '', table_name.title())

        def strip_prefix(s):
            return s[1:] if s.startswith("u'") else s

        with connection.cursor() as cursor:
            yield "# This is an auto-generated Django model module."
            yield "# You'll have to do the following manually to clean this up:"
            yield "#   * Rearrange models' order"
            yield "#   * Make sure each model has one field with primary_key=True"
            yield "#   * Make sure each ForeignKey has `on_delete` set to the desired behavior."
            yield (
                "#   * Remove `managed = False` lines if you wish to allow "
                "Django to create, modify, and delete the table"
            )
            yield "# Feel free to rename the models, but don't rename db_table values or field names."
            yield "from __future__ import unicode_literals"
            yield ''
            yield 'from %s import models' % self.db_module
            known_models = []
            tables_to_introspect = options['table'] or connection.introspection.table_names(cursor)

            for table_name in tables_to_introspect:
                if table_name_filter is not None and callable(table_name_filter):
                    if not table_name_filter(table_name):
                        continue
                try:
                    try:
                        relations = connection.introspection.get_relations(cursor, table_name)
                    except NotImplementedError:
                        relations = {}
                    try:
                        constraints = connection.introspection.get_constraints(cursor, table_name)
                    except NotImplementedError:
                        constraints = {}
                    primary_key_column = connection.introspection.get_primary_key_column(cursor, table_name)
                    unique_columns = [
                        c['columns'][0] for c in constraints.values()
                        if c['unique'] and len(c['columns']) == 1
                    ]
                    table_description = connection.introspection.get_table_description(cursor, table_name)
                except Exception as e:
                    yield "# Unable to inspect table '%s'" % table_name
                    yield "# The error was: %s" % force_text(e)
                    continue

                yield ''
                yield ''
                yield 'class %s(models.Model):' % table2model(table_name)
                known_models.append(table2model(table_name))
                used_column_names = []  # Holds column names used in the table so far
                column_to_field_name = {}  # Maps column names to names of model fields
                for row in table_description:
                    comment_notes = []  # Holds Field notes, to be displayed in a Python comment.
                    extra_params = OrderedDict()  # Holds Field parameters such as 'db_column'.
                    column_name = row[0]
                    is_relation = column_name in relations

                    att_name, params, notes = self.normalize_col_name(
                        column_name, used_column_names, is_relation)
                    extra_params.update(params)
                    comment_notes.extend(notes)

                    used_column_names.append(att_name)
                    column_to_field_name[column_name] = att_name

                    # Add primary_key and unique, if necessary.
                    # print (('------>', column_name, primary_key_column))
                    if column_name == primary_key_column or ('dbid' in column_name.lower() and column_name.lower().replace('dbid', '') == table_name.lower()) or column_name == 'id':
                        extra_params['primary_key'] = True
                        # extra_params['unique'] = True
                        # extra_params['blank'] = True
                        # print (('--->',column_name))
                    elif column_name in unique_columns:
                        extra_params['unique'] = True

                    if is_relation:
                        rel_to = (
                            "self" if relations[column_name][1] == table_name
                            else table2model(relations[column_name][1])
                        )
                        if rel_to in known_models:
                            field_type = "ForeignKey('%s', on_delete=models.CASCADE" % rel_to
                        else:
                            field_type = "ForeignKey('%s', on_delete=models.CASCADE" % rel_to
                    else:
                        # Calling `get_field_type` to get the field type string and any
                        # additional parameters and notes.
                        field_type, field_params, field_notes = self.get_field_type(connection, table_name, row)
                        extra_params.update(field_params)
                        comment_notes.extend(field_notes)

                        field_type += '('

                    # Don't output 'id = meta.AutoField(primary_key=True)', because
                    # that's assumed if it doesn't exist.
                    if att_name == 'id' and extra_params == {'primary_key': True}:
                        if field_type == 'AutoField(':
                            continue
                        elif field_type == 'IntegerField(' and not connection.features.can_introspect_autofield:
                            comment_notes.append('AutoField?')

                    # Add 'null' and 'blank', if the 'null_ok' flag was present in the
                    # table description.
                    if row[6]:  # If it's NULL...
                        if field_type == 'BooleanField(':
                            field_type = 'NullBooleanField('
                        else:
                            if not field_type.startswith('ForeignKey'):
                                extra_params['blank'] = True
                            # extra_params['null'] = True

                    field_desc = '%s = %s%s' % (
                        att_name,
                        # Custom fields will have a dotted path
                        '' if '.' in field_type else 'models.',
                        field_type,
                    )
                    # if field_type.startswith('ForeignKey('):
                    #     field_desc += ', models.DO_NOTHING'
                    extra_params['verbose_name'] = ' ' + column_name  # add one initial space to avoid capitalization in admin UI

                    if extra_params:
                        if not field_desc.endswith('('):
                            field_desc += ', '
                        field_desc += ', '.join(
                            '%s=%s' % (k, strip_prefix(repr(v)))
                            for k, v in extra_params.items())
                    field_desc += ')'
                    if comment_notes:
                        field_desc += '  # ' + ' '.join(comment_notes)
                    yield '    %s' % field_desc
                for meta_line in self.get_meta(table_name, constraints, column_to_field_name):
                    yield meta_line
 def override_database_connections(databases):
     with mock.patch('analyticsdataserver.views.connections',
                     ConnectionHandler(databases)):
         yield
Beispiel #6
0
 def setUp(self):
     super(DBTestSettingsRenamedTests, self).setUp()
     self.handler = ConnectionHandler()
     self.db_settings = {'default': {}}
Beispiel #7
0
from django.core.management import CommandError, call_command
from django.db import connection
from django.db.utils import ConnectionHandler
from django.test import SimpleTestCase, TestCase, TransactionTestCase

from django_mysql.management.commands.fix_datetime_columns import parse_create_table
from django_mysql.utils import connection_is_mariadb

# Can't use @override_settings to swap out DATABASES, instead just mock.patch
# a new ConnectionHandler into the command module

command_connections = (
    "django_mysql.management.commands.fix_datetime_columns.connections")

sqlite = ConnectionHandler(
    {"default": {
        "ENGINE": "django.db.backends.sqlite3"
    }})


def run_it(*args, **kwargs):
    run_args = ["fix_datetime_columns"]
    run_args.extend(args)

    out = StringIO()
    run_kwargs = {"stdout": out, "skip_checks": True}
    run_kwargs.update(kwargs)

    call_command(*run_args, **run_kwargs)

    return out.getvalue()
Beispiel #8
0
 def test_no_default_database(self):
     DATABASES = {"other": {}}
     conns = ConnectionHandler(DATABASES)
     msg = "You must define a 'default' database."
     with self.assertRaisesMessage(ImproperlyConfigured, msg):
         conns["other"].ensure_connection()
Beispiel #9
0
def reload_settings(settings, databases=None):
    """Special routine to reload django settings.

    Including:
    urlconf module, context processor, templatetags settings, database settings.
    """
    if databases:
        settings.DATABASES.update(databases)

    # check if there's settings to reload
    if hasattr(settings, 'ROOT_URLCONF'):
        if settings.ROOT_URLCONF in sys.modules:
            imp.reload(sys.modules[settings.ROOT_URLCONF])
        import django
        if hasattr(django, 'setup'):
            django.setup()
        import_module(settings.ROOT_URLCONF)
        set_urlconf(settings.ROOT_URLCONF)
        settings.LANGUAGE_CODE = 'en'  # all tests should be run with English by default

        # Make the ConnectionHandler use the new settings, otherwise the ConnectionHandler will have old configuraton.
        from django.db.utils import ConnectionHandler
        import django.db
        from django.db.utils import load_backend
        import django.db.transaction
        import django.db.models
        import django.db.models.sql.query
        import django.core.management.commands.syncdb
        import django.db.models.sql.compiler
        import django.db.backends
        import django.db.backends.mysql.base
        import django.core.management.commands.loaddata

        # all modules which imported django.db.connections should be changed to get new ConnectionHanlder
        django.db.models.sql.compiler.connections = django.db.models.connections = \
            django.core.management.commands.loaddata.connections = \
            django.db.backends.connections = django.db.backends.mysql.base.connections = \
            django.core.management.commands.syncdb.connections = django.db.transaction.connections = \
            django.db.connections = django.db.models.base.connections = django.db.models.sql.query.connections = \
            ConnectionHandler(settings.DATABASES)

        # default django connection and backend should be also changed
        django.db.connection = django.db.connections[
            django.db.DEFAULT_DB_ALIAS]
        django.db.backend = load_backend(
            django.db.connection.settings_dict['ENGINE'])

        import django.core.cache
        django.core.cache.cache = django.core.cache._create_cache(
            django.core.cache.DEFAULT_CACHE_ALIAS)

        # clear django urls cache
        clear_url_caches()
        # clear django contextprocessors cache
        context._standard_context_processors = None
        # clear django templatetags cache
        base.templatetags_modules = None

        # reload translation files
        imp.reload(translation)
        imp.reload(trans_real)

        # clear django template loaders cache
        loader.template_source_loaders = None
        from django.template.loaders import app_directories
        imp.reload(app_directories)
    from django.template.base import get_templatetags_modules
    get_templatetags_modules.cache_clear()
    import django.apps
    import django
    import django.template
    django.template.engines.__dict__.pop('templates', None)
    django.template.engines._templates = None
    django.template.engines._engines = {}
    if django.apps.apps.ready:
        django.apps.apps.set_installed_apps(settings.INSTALLED_APPS)
    django.setup()