Ticket #6148: generic-db_schema-r8308.diff

File generic-db_schema-r8308.diff, 7.3 KB (added by crippledcanary@…, 7 years ago)

patch to add support for db_schema in model.meta for mysql at least

  • django/core/management/commands/syncdb.py

     
    6161            app_name = app.__name__.split('.')[-2]
    6262            model_list = models.get_models(app)
    6363            for model in model_list:
     64                # Add model defined schema tables if anny
     65                if connection.features.supports_schemas and model._meta.db_schema:
     66                    tables += connection.introspection.schema_table_names(model._meta.db_schema)
    6467                # Create the model's database table, if it doesn't already exist.
    6568                if verbosity >= 2:
    6669                    print "Processing %s.%s model" % (app_name, model._meta.object_name)
  • django/core/management/sql.py

     
    7272
    7373    # Figure out which tables already exist
    7474    if cursor:
    75         table_names = connection.introspection.get_table_list(cursor)
     75        table_names = connection.introspection.get_table_list(cursor)   
    7676    else:
    7777        table_names = []
    7878
     
    8484    references_to_delete = {}
    8585    app_models = models.get_models(app)
    8686    for model in app_models:
     87        # Find aditional tables in model defined schemas
     88        if connection.features.supports_schemas and model._meta.db_schema:
     89            table_names += connection.introspection.get_schema_table_list(cursor, model._meta.db_schema)
    8790        if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names:
    8891            # The table exists, so it needs to be dropped
    8992            opts = model._meta
  • django/db/backends/__init__.py

     
    5555    update_can_self_select = True
    5656    interprets_empty_strings_as_nulls = False
    5757    can_use_chunked_reads = True
     58    supports_schemas = False
    5859
    5960class BaseDatabaseOperations(object):
    6061    """
     
    345346        "Returns a list of names of all tables that exist in the database."
    346347        cursor = self.connection.cursor()
    347348        return self.get_table_list(cursor)
     349       
     350    def schema_table_names(self, schema):
     351        "Returns a list of names of all tables that exist in the database schema."
     352        if self.connection.features.supports_schemas:
     353            cursor = self.connection.cursor()
     354            return self.get_schema_table_list(cursor, schema)
     355        else:
     356            return []
    348357
    349358    def django_table_names(self, only_existing=False):
    350359        """
  • django/db/backends/creation.py

     
    243243                    tablespace_sql = ''
    244244            else:
    245245                tablespace_sql = ''
     246            # Use original db_table in index name if schema is provided
     247            if self.connection.features.supports_schemas and model._meta.db_schema:
     248                index_table_name = model._meta._db_table
     249            else:
     250                index_table_name = model._meta.db_table
     251               
    246252            output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
    247                 style.SQL_TABLE(qn('%s_%s' % (model._meta.db_table, f.column))) + ' ' +
     253                style.SQL_TABLE(qn('%s_%s' % (index_table_name, f.column))) + ' ' +
    248254                style.SQL_KEYWORD('ON') + ' ' +
    249255                style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
    250256                "(%s)" % style.SQL_FIELD(qn(f.column)) +
  • django/db/backends/mysql/base.py

     
    6767class DatabaseFeatures(BaseDatabaseFeatures):
    6868    empty_fetchmany_value = ()
    6969    update_can_self_select = False
     70    supports_schemas = True
    7071
    7172class DatabaseOperations(BaseDatabaseOperations):
    7273    def date_extract_sql(self, lookup_type, field_name):
     
    99100    def quote_name(self, name):
    100101        if name.startswith("`") and name.endswith("`"):
    101102            return name # Quoting once is enough.
    102         return "`%s`" % name
     103        # add support for tablenames passed that also have their schema in their name
     104        return "`%s`" % name.replace('.','`.`')
    103105
    104106    def random_function_sql(self):
    105107        return 'RAND()'
  • django/db/backends/mysql/introspection.py

     
    3131        "Returns a list of table names in the current database."
    3232        cursor.execute("SHOW TABLES")
    3333        return [row[0] for row in cursor.fetchall()]
     34   
     35    def get_schema_table_list(self, cursor, schema):
     36        cursor.execute("SHOW TABLES FROM %s" % self.connection.ops.quote_name(schema))
     37        return [schema + "." + row[0] for row in cursor.fetchall()]
    3438
    3539    def get_table_description(self, cursor, table_name):
    3640        "Returns a description of the table, with the DB-API cursor.description interface."
  • django/db/models/options.py

     
    2121DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
    2222                 'unique_together', 'permissions', 'get_latest_by',
    2323                 'order_with_respect_to', 'app_label', 'db_tablespace',
    24                  'abstract')
     24                 'abstract', 'db_schema')
    2525
    2626class Options(object):
    2727    def __init__(self, meta, app_label=None):
     
    2929        self.module_name, self.verbose_name = None, None
    3030        self.verbose_name_plural = None
    3131        self.db_table = ''
     32        self.db_schema = ''
    3233        self.ordering = []
    3334        self.unique_together =  []
    3435        self.permissions =  []
     
    8990        else:
    9091            self.verbose_name_plural = string_concat(self.verbose_name, 's')
    9192        del self.meta
     93       
     94               
    9295
    9396        # If the db_table wasn't provided, use the app_label + module_name.
    9497        if not self.db_table:
    95             self.db_table = "%s_%s" % (self.app_label, self.module_name)
     98            self.db_table = "%s_%s" % (self.app_label, self.module_name)   
    9699            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
    97 
    98 
     100       
     101        # Patch db_table with the schema if provided and allowed
     102        if self.db_schema and connection.features.supports_schemas:
     103            # Store original db_table in a save place first
     104            self._db_table = self.db_table
     105            self.db_table = "%s.%s" % (self.db_schema, self.db_table)
     106   
     107   
    99108    def _prepare(self, model):
    100109        if self.order_with_respect_to:
    101110            self.order_with_respect_to = self.get_field(self.order_with_respect_to)
Back to Top