blag commented on code in PR #27828:
URL: https://github.com/apache/airflow/pull/27828#discussion_r1032146294


##########
airflow/migrations/versions/0122_2_5_0_add_is_orphaned_to_datasetmodel.py:
##########
@@ -39,9 +39,16 @@
 
 def upgrade():
     """Add is_orphaned to DatasetModel"""
-    op.add_column("dataset", sa.Column("is_orphaned", sa.Boolean, 
nullable=False, server_default="False"))
+    # We pass in sa.sql.False_() for server_default, since that handles the 
string
+    # representation of False for different databases for us
+    # 
https://github.com/miguelgrinberg/Flask-Migrate/issues/265#issuecomment-937057519
+    with op.batch_alter_table("dataset") as batch_op:
+        batch_op.add_column(
+            sa.Column("is_orphaned", sa.Boolean, nullable=False, 
server_default=sa.sql.False_())

Review Comment:
   Nope, when I remove `server_default` entirely, I get:
   
   ```
   root@d68c383f6ab2:/opt/airflow# airflow db upgrade
   DB: postgresql+psycopg2://postgres:***@postgres/airflow
   Performing upgrade with database 
postgresql+psycopg2://postgres:***@postgres/airflow
   [2022-11-25 09:02:46,160] {migration.py:204} INFO - Context impl 
PostgresqlImpl.
   [2022-11-25 09:02:46,160] {migration.py:211} INFO - Will assume 
transactional DDL.
   [2022-11-25 09:02:46,167] {db.py:1569} INFO - Creating tables
   INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
   INFO  [alembic.runtime.migration] Will assume transactional DDL.
   INFO  [alembic.runtime.migration] Running upgrade e07f49787c9d -> 
ee8d93fcc81e, Add updated_at column to DagRun and TaskInstance
   INFO  [alembic.runtime.migration] Running upgrade ee8d93fcc81e -> 
65a852f26899, Add user comment to task_instance and dag_run.
   INFO  [alembic.runtime.migration] Running upgrade 65a852f26899 -> 
290244fb8b83, Add is_orphaned to DatasetModel
   Traceback (most recent call last):
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 1803, in _execute_context
       cursor, statement, parameters, context
     File 
"/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 
719, in do_execute
       cursor.execute(statement, parameters)
   psycopg2.errors.NotNullViolation: column "is_orphaned" contains null values
   
   
   The above exception was the direct cause of the following exception:
   
   Traceback (most recent call last):
     File "/usr/local/bin/airflow", line 33, in <module>
       sys.exit(load_entry_point('apache-airflow', 'console_scripts', 
'airflow')())
     File "/opt/airflow/airflow/__main__.py", line 39, in main
       args.func(args)
     File "/opt/airflow/airflow/cli/cli_parser.py", line 52, in command
       return func(*args, **kwargs)
     File "/opt/airflow/airflow/utils/cli.py", line 108, in wrapper
       return f(*args, **kwargs)
     File "/opt/airflow/airflow/cli/commands/db_command.py", line 88, in 
upgradedb
       reserialize_dags=args.reserialize_dags,
     File "/opt/airflow/airflow/utils/session.py", line 75, in wrapper
       return func(*args, session=session, **kwargs)
     File "/opt/airflow/airflow/utils/db.py", line 1576, in upgradedb
       command.upgrade(config, revision=to_revision or "heads")
     File "/usr/local/lib/python3.7/site-packages/alembic/command.py", line 
322, in upgrade
       script.run_env()
     File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 
569, in run_env
       util.load_python_file(self.dir, "env.py")
     File "/usr/local/lib/python3.7/site-packages/alembic/util/pyfiles.py", 
line 94, in load_python_file
       module = load_module_py(module_id, path)
     File "/usr/local/lib/python3.7/site-packages/alembic/util/pyfiles.py", 
line 110, in load_module_py
       spec.loader.exec_module(module)  # type: ignore
     File "<frozen importlib._bootstrap_external>", line 728, in exec_module
     File "<frozen importlib._bootstrap>", line 219, in 
_call_with_frames_removed
     File "/opt/airflow/airflow/migrations/env.py", line 117, in <module>
       run_migrations_online()
     File "/opt/airflow/airflow/migrations/env.py", line 111, in 
run_migrations_online
       context.run_migrations()
     File "<string>", line 8, in run_migrations
     File 
"/usr/local/lib/python3.7/site-packages/alembic/runtime/environment.py", line 
853, in run_migrations
       self.get_context().run_migrations(**kw)
     File 
"/usr/local/lib/python3.7/site-packages/alembic/runtime/migration.py", line 
623, in run_migrations
       step.migration_fn(**kw)
     File 
"/opt/airflow/airflow/migrations/versions/0122_2_5_0_add_is_orphaned_to_datasetmodel.py",
 line 46, in upgrade
       batch_op.add_column(sa.Column("is_orphaned", sa.Boolean, nullable=False))
     File "/usr/local/lib/python3.7/contextlib.py", line 119, in __exit__
       next(self.gen)
     File "/usr/local/lib/python3.7/site-packages/alembic/operations/base.py", 
line 381, in batch_alter_table
       impl.flush()
     File "/usr/local/lib/python3.7/site-packages/alembic/operations/batch.py", 
line 111, in flush
       fn(*arg, **kw)
     File "/usr/local/lib/python3.7/site-packages/alembic/ddl/impl.py", line 
322, in add_column
       self._exec(base.AddColumn(table_name, column, schema=schema))
     File "/usr/local/lib/python3.7/site-packages/alembic/ddl/impl.py", line 
195, in _exec
       return conn.execute(construct, multiparams)
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 1289, in execute
       return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/ddl.py", line 
78, in _execute_on_connection
       self, multiparams, params, execution_options
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 1387, in _execute_ddl
       compiled,
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 1846, in _execute_context
       e, statement, parameters, cursor, context
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 2027, in _handle_dbapi_exception
       sqlalchemy_exception, with_traceback=exc_info[2], from_=e
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", 
line 207, in raise_
       raise exception
     File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", 
line 1803, in _execute_context
       cursor, statement, parameters, context
     File 
"/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 
719, in do_execute
       cursor.execute(statement, parameters)
   sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) column 
"is_orphaned" contains null values
   
   [SQL: ALTER TABLE dataset ADD COLUMN is_orphaned BOOLEAN NOT NULL]
   (Background on this error at: https://sqlalche.me/e/14/gkpj)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to