-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add dataset model #24613
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
Merged
Merged
Add dataset model #24613
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
63f1ee9
Add Dataset model
dstandish 9971987
forbid airflow scheme
dstandish 07b5f68
Update airflow/models/dataset.py
dstandish 5f70b43
remove extraneous field
dstandish d335465
Update airflow/models/dataset.py
dstandish c2dc83e
Update airflow/migrations/versions/0113_2_4_0_add_dataset_model.py
dstandish 47255db
Update airflow/models/dataset.py
dstandish 5f2045b
add imports
dstandish f893dd0
kaxil updates
dstandish 93449d7
small
dstandish bf2d766
use ascii for mysql
dstandish 10f9f25
fix defaults
dstandish a9617e5
use latin1_general_cs
dstandish 520e146
Update airflow/models/dataset.py
dstandish 080fc5e
Update airflow/models/dataset.py
jedcunningham 57f221b
fix test
dstandish 579fb22
fix sqlite sequence table issue
dstandish 379e9b7
remove `extra` field for yagni reasons
dstandish 7e6a15e
Revert "remove `extra` field for yagni reasons"
dstandish 474b485
fix hash
dstandish ba5f935
add unique suffix
dstandish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
airflow/migrations/versions/0114_2_4_0_add_dataset_model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # | ||
| # 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. | ||
|
|
||
| """Add Dataset model | ||
|
|
||
| Revision ID: 0038cd0c28b4 | ||
| Revises: 44b7034f6bdc | ||
| Create Date: 2022-06-22 14:37:20.880672 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy import Integer, String | ||
|
|
||
| from airflow.migrations.db_types import TIMESTAMP | ||
| from airflow.utils.sqlalchemy import ExtendedJSON | ||
|
|
||
| revision = '0038cd0c28b4' | ||
| down_revision = '44b7034f6bdc' | ||
| branch_labels = None | ||
| depends_on = None | ||
| airflow_version = '2.4.0' | ||
|
|
||
|
|
||
| def upgrade(): | ||
| """Apply Add Dataset model""" | ||
| op.create_table( | ||
| 'dataset', | ||
| sa.Column('id', Integer, primary_key=True, autoincrement=True), | ||
| sa.Column( | ||
| 'uri', | ||
| String(length=3000).with_variant( | ||
| String( | ||
| length=3000, | ||
| # latin1 allows for more indexed length in mysql | ||
| # and this field should only be ascii chars | ||
| collation='latin1_general_cs', | ||
| ), | ||
| 'mysql', | ||
| ), | ||
| nullable=False, | ||
| ), | ||
| sa.Column('extra', ExtendedJSON, nullable=True), | ||
| sa.Column('created_at', TIMESTAMP, nullable=False), | ||
| sa.Column('updated_at', TIMESTAMP, nullable=False), | ||
| sqlite_autoincrement=True, # ensures PK values not reused | ||
| ) | ||
| op.create_index('idx_uri_unique', 'dataset', ['uri'], unique=True) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| """Unapply Add Dataset model""" | ||
| op.drop_table('dataset') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # | ||
| # 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. | ||
| from urllib.parse import urlparse | ||
|
|
||
| from sqlalchemy import Column, Index, Integer, String | ||
|
|
||
| from airflow.models.base import Base | ||
| from airflow.utils import timezone | ||
| from airflow.utils.sqlalchemy import ExtendedJSON, UtcDateTime | ||
|
|
||
|
|
||
| class Dataset(Base): | ||
| """ | ||
| A table to store datasets. | ||
|
|
||
| :param uri: a string that uniquely identifies the dataset | ||
| :param extra: JSON field for arbitrary extra info | ||
| """ | ||
|
|
||
| id = Column(Integer, primary_key=True, autoincrement=True) | ||
| uri = Column( | ||
| String(length=3000).with_variant( | ||
| String( | ||
| length=3000, | ||
| # latin1 allows for more indexed length in mysql | ||
| # and this field should only be ascii chars | ||
| collation='latin1_general_cs', | ||
| ), | ||
| 'mysql', | ||
| ), | ||
| nullable=False, | ||
| ) | ||
| extra = Column(ExtendedJSON, nullable=True) | ||
dstandish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| created_at = Column(UtcDateTime, default=timezone.utcnow, nullable=False) | ||
| updated_at = Column(UtcDateTime, default=timezone.utcnow, onupdate=timezone.utcnow, nullable=False) | ||
|
|
||
| __tablename__ = "dataset" | ||
| __table_args__ = ( | ||
| Index('idx_uri_unique', uri, unique=True), | ||
| {'sqlite_autoincrement': True}, # ensures PK values not reused | ||
| ) | ||
|
|
||
| def __init__(self, uri: str, **kwargs): | ||
| try: | ||
| uri.encode('ascii') | ||
| except UnicodeEncodeError: | ||
| raise ValueError('URI must be ascii') | ||
| parsed = urlparse(uri) | ||
| if parsed.scheme and parsed.scheme.lower() == 'airflow': | ||
| raise ValueError("Scheme `airflow` is reserved.") | ||
dstandish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| super().__init__(uri=uri, **kwargs) | ||
|
|
||
| def __eq__(self, other): | ||
| return self.uri == other.uri | ||
|
|
||
| def __hash__(self): | ||
| return hash(self.uri) | ||
|
|
||
| def __repr__(self): | ||
| return f"{self.__class__.__name__}(uri={self.uri!r}, extra={self.extra!r})" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to explicitly have unique here too since you are passing the same via Index on L54
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so i thought that we manage uniqueness just through the manually-created index, so that we can give it a name (so that we can manually deal with it in migrations if necessary) -- i don't know what happens if we also add unique keyword here -- will it create the constraint without our provided name? @ephraimbuddy you know what we should do here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having
unique=Trueon the column would create an implicit unnamed constraint. And, I feel the namedindexwithunique=Truewould create an index as well as an unnamed unique constraint in some databases like MSSQL but I'm not sure. I will verify thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. It works fine in MSSQL and postgres. It didn't create extra unique constraint. Just a unique index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so does it result in the creation of two indexes? or just one? cus we don't want to create two of them.