Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default value for new column as autogenerate SQL was missing default 0 #34335

Conversation

abhishekbhakat
Copy link
Contributor

@abhishekbhakat abhishekbhakat commented Sep 13, 2023

closes: #34332
related: #34126

@abhishekbhakat
Copy link
Contributor Author

CC @syun64

@abhishekbhakat
Copy link
Contributor Author

Do we not change generated migrations to make sure it generates right SQL ?
I'll take a look at the base model to see if this migration can be autogenerated.

@Taragolis Taragolis added this to the Airflow 2.8.0 milestone Sep 13, 2023
@Taragolis Taragolis added the affected_version:main_branch Issues Reported for main branch label Sep 13, 2023
@abhishekbhakat
Copy link
Contributor Author

abhishekbhakat commented Sep 13, 2023

Maybe we can change:

clear_number = Column(Integer, default=0, nullable=False)

to say server_default="0" so that auto-generated migration picks it up.

I don't know if it's worth doing at this stage ¯\_(ツ)_/¯.

@@ -43,7 +43,7 @@ def upgrade():
sa.Column(
"clear_number",
sa.Integer,
default=0,
server_default="0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle MSSQL default in the downgrade if we go with server_default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL Query generated by this is ALTER TABLE dag_run ADD COLUMN clear_number INTEGER NOT NULL DEFAULT 0; (for postgres, so thinking it would be same for MSSQL as well).

So I think a simple drop should work.

Any resources where I can read more about what you are referring for MSSQL?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is support of MSSQL would removed in Airflow 2.8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I've found answer in comments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had to spend some time figuring out how to make this work with MSSQL, and I think this is an approach that 'works':

https://github.com/apache/airflow/pull/34344/files - the PR is currently failing because of failing ER diagram and black pre-commit checks, but the Migration CI seems to be working. I don't have access to my laptop right now, but will be able to fix the pre-commit check later this evening... or @abhishekbhakat feel free to adopt the logic into this PR to merge it in, if you are able to get to it faster than I am 👍

Other seemingly more 'correct' ways of handling the migration for MSSQL were failing.

For example, using the following drop_column command still fails for MSSQL:

batch_op.drop_column("clear_number", mssql_drop_default=True)

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Incorrect syntax near 'constraint'. (102) (SQLExecDirectW)")
[SQL: declare @const_name varchar(256)
select @const_name = QUOTENAME([name]) from sys.default_constraints
where parent_object_id = object_id('dag_run')
and col_name(parent_object_id, parent_column_id) = 'clear_number'
exec('alter table dag_run drop constraint ' + @const_name)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! ER Diagram is failing.

@ephraimbuddy ephraimbuddy added the full tests needed We need to run full set of tests for this PR to merge label Sep 13, 2023
@syun64
Copy link
Contributor

syun64 commented Sep 13, 2023

Hi @abhishekbhakat thank you for flagging this.

I noticed that my PR was merged with a failing CI on MSSQL, but thought that might have been related to the recent vote from the community to remove MSSQL as a supported backend. Interestingly, the CI tests for airflow db migrate on PostgreSQL passed on the PR, and maybe that's because we are migrating an empty airflow DB in the CI: no records means no null values on the new column.

Given that we will be dropping support for MSSQSL (possibly by 2.8.0) I wonder if it would be safe to just disregard the failing tests on MSSQL, with the assumption that it will be removed in 2.8.0?

@ephraimbuddy
Copy link
Contributor

Hi @abhishekbhakat thank you for flagging this.

I noticed that my PR was merged with a failing CI on MSSQL, but thought that might have been related to the recent vote from the community to remove MSSQL as a supported backend. Interestingly, the CI tests for airflow db migrate on PostgreSQL passed on the PR, and maybe that's because we are migrating an empty airflow DB in the CI: no records means no null values on the new column.

Given that we will be dropping support for MSSQSL (possibly by 2.8.0) I wonder if it would be safe to just disregard the failing tests on MSSQL, with the assumption that it will be removed in 2.8.0?

I think we should still fix the test since MSSQL support is still there in the CI

@eladkal eladkal removed the affected_version:main_branch Issues Reported for main branch label Sep 13, 2023
@jscheffl
Copy link
Contributor

jscheffl commented Sep 13, 2023

I was running over the PR list and other PRs related to this as well and had a hard time until I understood what the field "clear_number" is for - as you have a fix PR for this, can you add some documentation hints into the model description what this is for? When reading the column name at least for me it was not self-explanatory.

@abhishekbhakat
Copy link
Contributor Author

Closing this PR as it's completed in #34344.

@ephraimbuddy ephraimbuddy removed this from the Airflow 2.8.0 milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full tests needed We need to run full set of tests for this PR to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default value for clear_number column not applied via sqlalchemy
6 participants