|
4 | 4 | # license that can be found in the LICENSE file or at |
5 | 5 | # https://developers.google.com/open-source/licenses/bsd |
6 | 6 |
|
| 7 | +from django.db import NotSupportedError |
7 | 8 | from django.db.backends.base.schema import BaseDatabaseSchemaEditor |
8 | 9 |
|
9 | 10 |
|
@@ -202,6 +203,35 @@ def quote_value(self, value): |
202 | 203 | # https://github.com/orijtech/django-spanner/issues/227 |
203 | 204 | return str(value) |
204 | 205 |
|
| 206 | + def _alter_field(self, model, old_field, new_field, old_type, new_type, |
| 207 | + old_db_params, new_db_params, strict=False): |
| 208 | + # Spanner requires dropping indexes before changing the nullability |
| 209 | + # of a column. |
| 210 | + nullability_changed = old_field.null != new_field.null |
| 211 | + if nullability_changed: |
| 212 | + index_names = self._constraint_names( |
| 213 | + model, [old_field.column], index=True, |
| 214 | + ) |
| 215 | + if index_names and not old_field.db_index: |
| 216 | + raise NotSupportedError( |
| 217 | + "Changing nullability of a field with an index other than " |
| 218 | + "Field(db_index=True) isn't yet supported." |
| 219 | + ) |
| 220 | + if len(index_names) > 1: |
| 221 | + raise NotSupportedError( |
| 222 | + "Changing nullability of a field with more than one " |
| 223 | + "index isn't yet supported." |
| 224 | + ) |
| 225 | + for index_name in index_names: |
| 226 | + self.execute(self._delete_index_sql(model, index_name)) |
| 227 | + super()._alter_field( |
| 228 | + model, old_field, new_field, old_type, new_type, |
| 229 | + old_db_params, new_db_params, strict=False, |
| 230 | + ) |
| 231 | + # Recreate the index that was dropped earlier. |
| 232 | + if nullability_changed and new_field.db_index: |
| 233 | + self.execute(self._create_index_sql(model, [new_field])) |
| 234 | + |
205 | 235 | def _alter_column_type_sql(self, model, old_field, new_field, new_type): |
206 | 236 | # Spanner needs to use sql_alter_column_not_null if the field is |
207 | 237 | # NOT NULL, otherwise the constraint is dropped. |
|
0 commit comments