1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
from contextlib import contextmanager
from typing import List
from unittest import mock
from django.apps import apps
from django.db import connection, migrations
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner
from django.db.migrations.state import ProjectState
from psqlextra.backend.schema import PostgresSchemaEditor
from .fake_model import define_fake_model
@contextmanager
def filtered_schema_editor(*filters: List[str]):
"""Gets a schema editor, but filters executed SQL statements based on the
specified text filters.
Arguments:
filters:
List of strings to filter SQL
statements on.
"""
with connection.schema_editor() as schema_editor:
wrapper_for = schema_editor.execute
with mock.patch.object(
PostgresSchemaEditor, "execute", wraps=wrapper_for
) as execute:
filter_results = {}
yield filter_results
for filter_text in filters:
filter_results[filter_text] = [
call for call in execute.mock_calls if filter_text in str(call)
]
def apply_migration(operations, state=None, backwards: bool = False):
"""Executes the specified migration operations using the specified schema
editor.
Arguments:
operations:
The migration operations to execute.
state:
The state state to use during the
migrations.
backwards:
Whether to apply the operations
in reverse (backwards).
"""
state = state or migrations.state.ProjectState.from_apps(apps)
class Migration(migrations.Migration):
pass
Migration.operations = operations
migration = Migration("migration", "tests")
executor = MigrationExecutor(connection)
if not backwards:
executor.apply_migration(state, migration)
else:
executor.unapply_migration(state, migration)
return migration
def make_migration(app_label="tests", from_state=None, to_state=None):
"""Generates migrations based on the specified app's state."""
app_labels = [app_label]
loader = MigrationLoader(None, ignore_no_migrations=True)
loader.check_consistent_history(connection)
questioner = NonInteractiveMigrationQuestioner(
specified_apps=app_labels, dry_run=False
)
autodetector = MigrationAutodetector(
from_state or loader.project_state(),
to_state or ProjectState.from_apps(apps),
questioner,
)
changes = autodetector.changes(
graph=loader.graph,
trim_to_apps=app_labels or None,
convert_apps=app_labels or None,
migration_name="test",
)
changes_for_app = changes.get(app_label)
if not changes_for_app or len(changes_for_app) == 0:
return None
return changes_for_app[0]
@contextmanager
def create_drop_model(field, filters: List[str]):
"""Creates and drops a model with the specified field.
Arguments:
field:
The field to include on the
model to create and drop.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model({"title": field})
with filtered_schema_editor(*filters) as calls:
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
),
migrations.DeleteModel(model.__name__),
]
)
yield calls
@contextmanager
def alter_db_table(field, filters: List[str]):
"""Creates a model with the specified field and then renames the database
table.
Arguments:
field:
The field to include into the
model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AlterModelTable(model.__name__, "NewTableName")], state
)
yield calls
@contextmanager
def add_field(field, filters: List[str]):
"""Adds the specified field to a model.
Arguments:
field:
The field to add to a model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration([migrations.CreateModel(model.__name__, fields=[])], state)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AddField(model.__name__, "title", field)], state
)
yield calls
@contextmanager
def remove_field(field, filters: List[str]):
"""Removes the specified field from a model.
Arguments:
field:
The field to remove from a model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model({"title": field})
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.RemoveField(model.__name__, "title")], state
)
yield calls
@contextmanager
def alter_field(old_field, new_field, filters: List[str]):
"""Alters a field from one state to the other.
Arguments:
old_field:
The field before altering it.
new_field:
The field after altering it.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model({"title": old_field})
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", old_field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AlterField(model.__name__, "title", new_field)], state
)
yield calls
@contextmanager
def rename_field(field, filters: List[str]):
"""Renames a field from one name to the other.
Arguments:
field:
Field to be renamed.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model({"title": field})
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.RenameField(model.__name__, "title", "newtitle")], state
)
yield calls
|