From 9f35e2c1a0161c485dbd9f62d1928ea47cda4dfd Mon Sep 17 00:00:00 2001
From: Tzu-ping Chung <uranusjr@gmail.com>
Date: Thu, 10 Aug 2023 14:44:32 +0800
Subject: [PATCH 1/4] Ensure include_deferred is not nullable

This uses a multi-step process to introduce the NOT NULL constraint to
the column, needed due to MSSQL restrictions.
---
 .../0128_2_7_0_add_include_deferred_column_to_pool.py     | 5 ++++-
 docs/apache-airflow/img/airflow_erd.sha256                | 2 +-
 docs/apache-airflow/img/airflow_erd.svg                   | 8 ++++----
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
index bbafc6805795f..466092d83212a 100644
--- a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
+++ b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
@@ -39,7 +39,10 @@
 def upgrade():
     """Apply add include_deferred column to pool"""
     with op.batch_alter_table("slot_pool") as batch_op:
-        batch_op.add_column(sa.Column("include_deferred", sa.Boolean, default=False))
+        batch_op.add_column(sa.Column("include_deferred", sa.Boolean))
+    op.execute("UPDATE slot_pool SET include_deferred = FALSE")
+    with op.batch_alter_table("slot_pool") as batch_op:
+        batch_op.alter_column("include_deferred", existing_type=sa.Boolean, nullable=False)
 
 
 def downgrade():
diff --git a/docs/apache-airflow/img/airflow_erd.sha256 b/docs/apache-airflow/img/airflow_erd.sha256
index a7704c8fc5a34..3c99b6003e824 100644
--- a/docs/apache-airflow/img/airflow_erd.sha256
+++ b/docs/apache-airflow/img/airflow_erd.sha256
@@ -1 +1 @@
-52b0e45032fb1c60274374e0114cf199dfb759adc0a4fe32964136347457506a
\ No newline at end of file
+fb4afe5e2194e6c5127d65283091354e31df619e53797e10ee969d37d49bcf39
\ No newline at end of file
diff --git a/docs/apache-airflow/img/airflow_erd.svg b/docs/apache-airflow/img/airflow_erd.svg
index ba548c60fdddf..8bf922245e259 100644
--- a/docs/apache-airflow/img/airflow_erd.svg
+++ b/docs/apache-airflow/img/airflow_erd.svg
@@ -1228,28 +1228,28 @@
 <g id="edge48" class="edge">
 <title>task_instance&#45;&#45;xcom</title>
 <path fill="none" stroke="#7f7f7f" stroke-dasharray="5,2" d="M1166.17,-482.51C1190.54,-462.01 1215.43,-441.43 1239,-422 1245.31,-416.8 1251.76,-411.48 1258.29,-406.14"/>
-<text text-anchor="start" x="1227.29" y="-394.94" font-family="Times,serif" font-size="14.00">0..N</text>
+<text text-anchor="start" x="1248.29" y="-394.94" font-family="Times,serif" font-size="14.00">1</text>
 <text text-anchor="start" x="1166.17" y="-471.31" font-family="Times,serif" font-size="14.00">1</text>
 </g>
 <!-- task_instance&#45;&#45;xcom -->
 <g id="edge49" class="edge">
 <title>task_instance&#45;&#45;xcom</title>
 <path fill="none" stroke="#7f7f7f" stroke-dasharray="5,2" d="M1166.17,-500.3C1190.54,-480.01 1215.43,-459.43 1239,-440 1245.31,-434.8 1251.76,-429.48 1258.29,-424.12"/>
-<text text-anchor="start" x="1248.29" y="-412.92" font-family="Times,serif" font-size="14.00">1</text>
+<text text-anchor="start" x="1227.29" y="-412.92" font-family="Times,serif" font-size="14.00">0..N</text>
 <text text-anchor="start" x="1166.17" y="-489.1" font-family="Times,serif" font-size="14.00">1</text>
 </g>
 <!-- task_instance&#45;&#45;xcom -->
 <g id="edge50" class="edge">
 <title>task_instance&#45;&#45;xcom</title>
 <path fill="none" stroke="#7f7f7f" stroke-dasharray="5,2" d="M1166.17,-518.08C1190.54,-498.01 1215.43,-477.43 1239,-458 1246.11,-452.14 1253.42,-446.12 1260.79,-440.05"/>
-<text text-anchor="start" x="1229.79" y="-443.85" font-family="Times,serif" font-size="14.00">0..N</text>
+<text text-anchor="start" x="1250.79" y="-443.85" font-family="Times,serif" font-size="14.00">1</text>
 <text text-anchor="start" x="1166.17" y="-506.88" font-family="Times,serif" font-size="14.00">1</text>
 </g>
 <!-- task_instance&#45;&#45;xcom -->
 <g id="edge51" class="edge">
 <title>task_instance&#45;&#45;xcom</title>
 <path fill="none" stroke="#7f7f7f" stroke-dasharray="5,2" d="M1166.17,-535.87C1190.54,-516.01 1215.43,-495.43 1239,-476 1252.96,-464.49 1267.64,-452.42 1282.18,-440.26"/>
-<text text-anchor="start" x="1272.18" y="-444.06" font-family="Times,serif" font-size="14.00">1</text>
+<text text-anchor="start" x="1282.18" y="-444.06" font-family="Times,serif" font-size="14.00">0..N</text>
 <text text-anchor="start" x="1166.17" y="-524.67" font-family="Times,serif" font-size="14.00">1</text>
 </g>
 <!-- log_template -->

From a4562b29ceb1fbd58eab59e99dd6e49048572c03 Mon Sep 17 00:00:00 2001
From: Tzu-ping Chung <uranusjr@gmail.com>
Date: Thu, 10 Aug 2023 15:30:13 +0800
Subject: [PATCH 2/4] MSSQL does not even support boolean literals

---
 .../versions/0128_2_7_0_add_include_deferred_column_to_pool.py  | 2 +-
 docs/apache-airflow/img/airflow_erd.sha256                      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
index 466092d83212a..a795bbb7e74dc 100644
--- a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
+++ b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
@@ -40,7 +40,7 @@ def upgrade():
     """Apply add include_deferred column to pool"""
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.add_column(sa.Column("include_deferred", sa.Boolean))
-    op.execute("UPDATE slot_pool SET include_deferred = FALSE")
+    op.execute("UPDATE slot_pool SET include_deferred = 0")
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.alter_column("include_deferred", existing_type=sa.Boolean, nullable=False)
 
diff --git a/docs/apache-airflow/img/airflow_erd.sha256 b/docs/apache-airflow/img/airflow_erd.sha256
index 3c99b6003e824..585e86850b3ff 100644
--- a/docs/apache-airflow/img/airflow_erd.sha256
+++ b/docs/apache-airflow/img/airflow_erd.sha256
@@ -1 +1 @@
-fb4afe5e2194e6c5127d65283091354e31df619e53797e10ee969d37d49bcf39
\ No newline at end of file
+dd57171dd989fa225f94bf374422ae0df155b37ec2fa89ddee6eaf8252ba44c6
\ No newline at end of file

From 5f082f467b83a733db238c671e82ee02e4f7b0e9 Mon Sep 17 00:00:00 2001
From: Tzu-ping Chung <uranusjr@gmail.com>
Date: Thu, 10 Aug 2023 17:34:06 +0800
Subject: [PATCH 3/4] Postgres does not support 0 as boolean

This is fine. SQL is fun.
---
 .../versions/0128_2_7_0_add_include_deferred_column_to_pool.py | 3 ++-
 docs/apache-airflow/img/airflow_erd.sha256                     | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
index a795bbb7e74dc..befe6a2f89b65 100644
--- a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
+++ b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
@@ -40,7 +40,8 @@ def upgrade():
     """Apply add include_deferred column to pool"""
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.add_column(sa.Column("include_deferred", sa.Boolean))
-    op.execute("UPDATE slot_pool SET include_deferred = 0")
+    # Different databases support different literal for FALSE. This is fine.
+    op.execute(f"UPDATE slot_pool SET include_deferred = {sa.false().compile(op.get_bind())}")
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.alter_column("include_deferred", existing_type=sa.Boolean, nullable=False)
 
diff --git a/docs/apache-airflow/img/airflow_erd.sha256 b/docs/apache-airflow/img/airflow_erd.sha256
index 585e86850b3ff..cf3e37a0f5f30 100644
--- a/docs/apache-airflow/img/airflow_erd.sha256
+++ b/docs/apache-airflow/img/airflow_erd.sha256
@@ -1 +1 @@
-dd57171dd989fa225f94bf374422ae0df155b37ec2fa89ddee6eaf8252ba44c6
\ No newline at end of file
+7ecc7a5a1897839ec00ff68e25fca897a23b04514511f48ebbac5bb86f832a9d
\ No newline at end of file

From 348636e44cca45142489e6a04a3c573cb31fe0ab Mon Sep 17 00:00:00 2001
From: Tzu-ping Chung <uranusjr@gmail.com>
Date: Thu, 10 Aug 2023 18:17:05 +0800
Subject: [PATCH 4/4] Wrap raw SQL in text()

---
 .../versions/0128_2_7_0_add_include_deferred_column_to_pool.py  | 2 +-
 docs/apache-airflow/img/airflow_erd.sha256                      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
index befe6a2f89b65..9e000276708e0 100644
--- a/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
+++ b/airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py
@@ -41,7 +41,7 @@ def upgrade():
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.add_column(sa.Column("include_deferred", sa.Boolean))
     # Different databases support different literal for FALSE. This is fine.
-    op.execute(f"UPDATE slot_pool SET include_deferred = {sa.false().compile(op.get_bind())}")
+    op.execute(sa.text(f"UPDATE slot_pool SET include_deferred = {sa.false().compile(op.get_bind())}"))
     with op.batch_alter_table("slot_pool") as batch_op:
         batch_op.alter_column("include_deferred", existing_type=sa.Boolean, nullable=False)
 
diff --git a/docs/apache-airflow/img/airflow_erd.sha256 b/docs/apache-airflow/img/airflow_erd.sha256
index cf3e37a0f5f30..0594dd21e83bd 100644
--- a/docs/apache-airflow/img/airflow_erd.sha256
+++ b/docs/apache-airflow/img/airflow_erd.sha256
@@ -1 +1 @@
-7ecc7a5a1897839ec00ff68e25fca897a23b04514511f48ebbac5bb86f832a9d
\ No newline at end of file
+266a8533dca6d9c7e984341bfe29e99f29217e44529455a49e7c4a5faccdcdf3
\ No newline at end of file
