| 34 | await operation.run(app_label, state, dry_run, schema_editor) |
| 35 | |
| 36 | async def apply( |
| 37 | self, |
| 38 | state: State, |
| 39 | *, |
| 40 | dry_run: bool = False, |
| 41 | schema_editor: BaseSchemaEditor | None = None, |
| 42 | collect_sql: bool = False, |
| 43 | ) -> State: |
| 44 | supports_transactions = ( |
| 45 | schema_editor is not None and schema_editor.client.capabilities.supports_transactions |
| 46 | ) |
| 47 | need_old_state = (collect_sql and schema_editor) or ( |
| 48 | not dry_run and schema_editor is not None |
| 49 | ) |
| 50 | for operation in self.operations: |
| 51 | old_state = state.clone() if need_old_state else None |
| 52 | operation.state_forward(self.app_label, state) |
| 53 | if collect_sql and schema_editor: |
| 54 | schema_editor.collected_sql.append("--") |
| 55 | schema_editor.collected_sql.append(f"-- {operation.describe()}") |
| 56 | schema_editor.collected_sql.append("--") |
| 57 | if not operation.reduces_to_sql: |
| 58 | schema_editor.collected_sql.append( |
| 59 | "-- THIS OPERATION CANNOT BE REPRESENTED AS SQL" |
| 60 | ) |
| 61 | continue |
| 62 | before = len(schema_editor.collected_sql) |
| 63 | await self._run_database_forward( |
| 64 | operation, |
| 65 | old_state, # type: ignore[arg-type] |
| 66 | state, |
| 67 | schema_editor, |
| 68 | supports_transactions, |
| 69 | ) |
| 70 | if len(schema_editor.collected_sql) == before: |
| 71 | schema_editor.collected_sql.append("-- (no-op)") |
| 72 | continue |
| 73 | if dry_run or not schema_editor: |
| 74 | continue |
| 75 | await self._run_database_forward( |
| 76 | operation, |
| 77 | old_state, # type: ignore[arg-type] |
| 78 | state, |
| 79 | schema_editor, |
| 80 | supports_transactions, |
| 81 | ) |
| 82 | state.validate_relations_initialized() |
| 83 | return state |
| 84 | |
| 85 | async def unapply( |
| 86 | self, |