From d1ca70110f49f0be90206c8da516ac16aebc8c75 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 25 Sep 2014 19:59:03 +0200 Subject: [PATCH] Factorized schema_editor() at BaseDatabaseWrapper level --- django/contrib/gis/db/backends/mysql/base.py | 6 ++---- django/contrib/gis/db/backends/oracle/base.py | 5 ++--- django/contrib/gis/db/backends/postgis/base.py | 6 ++---- django/contrib/gis/db/backends/spatialite/base.py | 6 ++---- django/db/backends/__init__.py | 10 ++++++++-- django/db/backends/mysql/base.py | 5 +---- django/db/backends/oracle/base.py | 5 +---- django/db/backends/postgresql_psycopg2/base.py | 5 +---- django/db/backends/sqlite3/base.py | 4 +--- 9 files changed, 20 insertions(+), 32 deletions(-) diff --git a/django/contrib/gis/db/backends/mysql/base.py b/django/contrib/gis/db/backends/mysql/base.py index d237efc114..802f7206f4 100644 --- a/django/contrib/gis/db/backends/mysql/base.py +++ b/django/contrib/gis/db/backends/mysql/base.py @@ -20,13 +20,11 @@ class DatabaseFeatures(BaseSpatialFeatures, MySQLDatabaseFeatures): class DatabaseWrapper(MySQLDatabaseWrapper): + SchemaEditorClass = MySQLGISSchemaEditor + def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures(self) self.creation = MySQLCreation(self) self.ops = MySQLOperations(self) self.introspection = MySQLIntrospection(self) - - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return MySQLGISSchemaEditor(self, *args, **kwargs) diff --git a/django/contrib/gis/db/backends/oracle/base.py b/django/contrib/gis/db/backends/oracle/base.py index fad281a3f9..56db33a9e4 100644 --- a/django/contrib/gis/db/backends/oracle/base.py +++ b/django/contrib/gis/db/backends/oracle/base.py @@ -15,12 +15,11 @@ class DatabaseFeatures(BaseSpatialFeatures, OracleDatabaseFeatures): class DatabaseWrapper(OracleDatabaseWrapper): + SchemaEditorClass = OracleGISSchemaEditor + def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures(self) self.ops = OracleOperations(self) self.creation = OracleCreation(self) self.introspection = OracleIntrospection(self) - - def schema_editor(self, *args, **kwargs): - return OracleGISSchemaEditor(self, *args, **kwargs) diff --git a/django/contrib/gis/db/backends/postgis/base.py b/django/contrib/gis/db/backends/postgis/base.py index 7a2a374f10..338748bc4a 100644 --- a/django/contrib/gis/db/backends/postgis/base.py +++ b/django/contrib/gis/db/backends/postgis/base.py @@ -16,6 +16,8 @@ class DatabaseFeatures(BaseSpatialFeatures, Psycopg2DatabaseFeatures): class DatabaseWrapper(Psycopg2DatabaseWrapper): + SchemaEditorClass = PostGISSchemaEditor + def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) if kwargs.get('alias', '') != NO_DB_ALIAS: @@ -23,7 +25,3 @@ class DatabaseWrapper(Psycopg2DatabaseWrapper): self.creation = PostGISCreation(self) self.ops = PostGISOperations(self) self.introspection = PostGISIntrospection(self) - - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return PostGISSchemaEditor(self, *args, **kwargs) diff --git a/django/contrib/gis/db/backends/spatialite/base.py b/django/contrib/gis/db/backends/spatialite/base.py index f3c22bdb20..571d37cec9 100644 --- a/django/contrib/gis/db/backends/spatialite/base.py +++ b/django/contrib/gis/db/backends/spatialite/base.py @@ -22,6 +22,8 @@ class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures): class DatabaseWrapper(SQLiteDatabaseWrapper): + SchemaEditorClass = SpatialiteSchemaEditor + def __init__(self, *args, **kwargs): # Before we get too far, make sure pysqlite 2.5+ is installed. if Database.version_info < (2, 5, 0): @@ -47,10 +49,6 @@ class DatabaseWrapper(SQLiteDatabaseWrapper): self.creation = SpatiaLiteCreation(self) self.introspection = SpatiaLiteIntrospection(self) - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return SpatialiteSchemaEditor(self, *args, **kwargs) - def get_new_connection(self, conn_params): conn = super(DatabaseWrapper, self).get_new_connection(conn_params) # Enabling extension loading on the SQLite connection. diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 1f0e7b255c..9873d9cba6 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -37,6 +37,7 @@ class BaseDatabaseWrapper(object): """ ops = None vendor = 'unknown' + SchemaEditorClass = None queries_limit = 9000 @@ -479,8 +480,13 @@ class BaseDatabaseWrapper(object): ) def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - raise NotImplementedError('subclasses of BaseDatabaseWrapper may require a schema_editor() method') + """ + Returns a new instance of this backend's SchemaEditor. + """ + if self.SchemaEditorClass is None: + raise NotImplementedError( + 'The SchemaEditorClass attribute of this database wrapper is still None') + return self.SchemaEditorClass(self, *args, **kwargs) class BaseDatabaseFeatures(object): diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index aefe4262e6..7223780081 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -434,6 +434,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): } Database = Database + SchemaEditorClass = DatabaseSchemaEditor def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) @@ -557,10 +558,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): table_name, column_name, bad_row[1], referenced_table_name, referenced_column_name)) - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return DatabaseSchemaEditor(self, *args, **kwargs) - def is_usable(self): try: self.connection.ping() diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index e48c82f0d8..083ba18daa 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -593,6 +593,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): }) Database = Database + SchemaEditorClass = DatabaseSchemaEditor def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) @@ -693,10 +694,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return DatabaseSchemaEditor(self, *args, **kwargs) - # Oracle doesn't support releasing savepoints. But we fake them when query # logging is enabled to keep query counts consistent with other backends. def _savepoint_commit(self, sid): diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 975eee4df2..7de52d7592 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -91,6 +91,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): } Database = Database + SchemaEditorClass = DatabaseSchemaEditor def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) @@ -198,10 +199,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): else: return True - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return DatabaseSchemaEditor(self, *args, **kwargs) - @cached_property def psycopg2_version(self): version = psycopg2.__version__.split(' ', 1)[0] diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 0e1070e6e5..349fae0253 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -351,6 +351,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): } Database = Database + SchemaEditorClass = DatabaseSchemaEditor def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) @@ -489,9 +490,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): """ self.cursor().execute("BEGIN") - def schema_editor(self, *args, **kwargs): - "Returns a new instance of this backend's SchemaEditor" - return DatabaseSchemaEditor(self, *args, **kwargs) FORMAT_QMARK_REGEX = re.compile(r'(?