sharingan-no-kakashi commented on a change in pull request #16953: URL: https://github.com/apache/airflow/pull/16953#discussion_r670475890
########## File path: airflow/models/task_note.py ########## @@ -0,0 +1,219 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import logging +from typing import Any, Iterable, Optional, Union + +import pendulum +from sqlalchemy import Column, String, Text, and_ +from sqlalchemy.orm import Query, Session + +from airflow.models.base import COLLATION_ARGS, ID_LEN, Base +from airflow.utils.helpers import is_container +from airflow.utils.session import provide_session +from airflow.utils.sqlalchemy import UtcDateTime + +log = logging.getLogger(__name__) + + +class TaskNote(Base): + """Model that stores a note for a task id.""" + + __tablename__ = "task_notes" + + task_id = Column(String(ID_LEN, **COLLATION_ARGS), primary_key=True) + dag_id = Column(String(ID_LEN, **COLLATION_ARGS), primary_key=True) + execution_date = Column(UtcDateTime, primary_key=True) + timestamp = Column(UtcDateTime, primary_key=True) + user_name = Column(String(ID_LEN)) + task_note = Column(Text()) + + def __repr__(self): + return str(self.__key()) + + def __key(self): + return self.dag_id, self.task_id, self.execution_date, self.timestamp, self.user_name, self.task_note + + def __hash__(self): + return hash(self.__key()) + + def __eq__(self, other): + if isinstance(other, TaskNote): + return self.__key() == other.__key() + return False Review comment: not AFAIK, but i'll double check! ########## File path: airflow/models/task_note.py ########## @@ -0,0 +1,219 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import logging +from typing import Any, Iterable, Optional, Union + +import pendulum +from sqlalchemy import Column, String, Text, and_ +from sqlalchemy.orm import Query, Session + +from airflow.models.base import COLLATION_ARGS, ID_LEN, Base +from airflow.utils.helpers import is_container +from airflow.utils.session import provide_session +from airflow.utils.sqlalchemy import UtcDateTime + +log = logging.getLogger(__name__) + + +class TaskNote(Base): + """Model that stores a note for a task id.""" + + __tablename__ = "task_notes" + + task_id = Column(String(ID_LEN, **COLLATION_ARGS), primary_key=True) + dag_id = Column(String(ID_LEN, **COLLATION_ARGS), primary_key=True) + execution_date = Column(UtcDateTime, primary_key=True) + timestamp = Column(UtcDateTime, primary_key=True) + user_name = Column(String(ID_LEN)) + task_note = Column(Text()) + + def __repr__(self): + return str(self.__key()) + + def __key(self): + return self.dag_id, self.task_id, self.execution_date, self.timestamp, self.user_name, self.task_note + + def __hash__(self): + return hash(self.__key()) + + def __eq__(self, other): + if isinstance(other, TaskNote): + return self.__key() == other.__key() + return False + + @classmethod + @provide_session + def set(cls, task_note, timestamp, user_name, execution_date, task_id, dag_id, session=None): + """ + Store a TaskNote + :return: None + """ + session.expunge_all() + + # remove any duplicate TaskNote + session.query(cls).filter( + cls.execution_date == execution_date, + cls.user_name == user_name, + cls.task_id == task_id, + cls.dag_id == dag_id, + cls.timestamp == timestamp, + ).delete() + + # insert new TaskNote + session.add( + TaskNote( + timestamp=timestamp, + task_note=task_note, + user_name=user_name, + execution_date=execution_date, + task_id=task_id, + dag_id=dag_id, + ) + ) + + session.commit() + + @classmethod + @provide_session + def delete(cls, notes, session=None): + """Delete TaskNote""" + if isinstance(notes, TaskNote): + notes = [notes] + for note in notes: + if not isinstance(note, TaskNote): + raise TypeError(f'Expected TaskNote; received {note.__class__.__name__}') + session.delete(note) + session.commit() Review comment: True. -- 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]
