| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- use yii\db\Migration;
- class m261009_000002_allow_empty_declaration_payer extends Migration
- {
- public function safeUp()
- {
- $this->setPayerNullability(false);
- }
- public function safeDown()
- {
- $table = '{{%APPROACH_DEC_ORDER}}';
- $nullCount = (int) (new \yii\db\Query())
- ->from($table)
- ->where(['USER_ID' => null])
- ->count('*', $this->db);
- if ($nullCount > 0) {
- throw new \RuntimeException('Cannot restore APPROACH_DEC_ORDER.USER_ID NOT NULL while guest declaration rows exist.');
- }
- $this->setPayerNullability(true);
- }
- private function setPayerNullability(bool $notNull): void
- {
- $table = '{{%APPROACH_DEC_ORDER}}';
- $schema = $this->db->getTableSchema($table, true);
- if (!$schema || !isset($schema->columns['USER_ID'])) {
- throw new \RuntimeException('APPROACH_DEC_ORDER.USER_ID column was not found.');
- }
- $column = $schema->columns['USER_ID'];
- if ((bool) $column->allowNull === !$notNull) {
- return;
- }
- $definition = $this->db->getSchema()->createColumnSchemaBuilder($column->type, $column->size);
- $definition->notNull($notNull);
- $this->alterColumn($table, 'USER_ID', $definition);
- }
- }
|