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

[Dev] 導入 SQLAlchemy 並改寫 pt_repository 的 upsert_user #60

Merged
merged 7 commits into from Jan 7, 2023

Conversation

s9891326
Copy link
Contributor

@s9891326 s9891326 commented Jan 3, 2023

Why need this change? / Root cause:

Changes made:

  • repository/models.py:對應到create_tables.sql的3張表(index沒建,其他都先參照原本的屬性)
  • repository/database.py:創建sqlalchemy跟postgresql的連線
  • repository/init.py:各個model的CRUD行為
  • pt_service.py:更改原本的upsert_user()
  • .pylintrc:增加import-outside-toplevel,因為pt_service裡面有一些func底下有import pt_bot的行為,如果抽到最上面import,那跑app.py的時候就會重複啟用bot了,或者要把那邊的程式解偶一下

Test Scope / Change impact:

# 原本
def upsert_user(user_id, chat_id):
    return pt_repository.upsert_user(user_id, chat_id)

# 改後
def upsert_user(user_id, chat_id):
    return repository.upsert_user(user_id, chat_id)

@s9891326
Copy link
Contributor Author

s9891326 commented Jan 6, 2023

增加CUD的使用範例

@auto_commit
def insert_user(**kwargs):
    session: Session = kwargs["session"]
    data = User(id="1234", chat_id="12", state=1)
    session.add(data)


@auto_commit
def update_user(**kwargs):
    session: Session = kwargs["session"]
    user = session.query(User).filter(User.id == "1234").first()
    user.state = 0


@auto_commit
def delete_user(**kwargs):
    session: Session = kwargs["session"]
    user = session.query(User).filter(User.id == "1234").first()
    session.delete(user)

Comment on lines 7 to 10
Engine = create_engine(
f"postgresql://{pt_config.DB_USER}:{pt_config.DB_PASSWORD}@"
f"{pt_config.DB_HOST}:5432/{pt_config.DB_NAME}"
)
Copy link
Owner

Choose a reason for hiding this comment

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

5432 port 可以使用目前抽出的 environment DB_PORT ,不過會需要先 rebase。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有rebase了~,但我local的licese怪怪的XD

@s9891326 s9891326 force-pushed the feature/add_sqlalchemy_usage_and_refactor_upsert_user branch from 9b1e108 to f51ed04 Compare January 7, 2023 02:58
Copy link
Owner

@chen-tf chen-tf left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@chen-tf chen-tf merged commit 2bd9e76 into main Jan 7, 2023
@chen-tf chen-tf deleted the feature/add_sqlalchemy_usage_and_refactor_upsert_user branch January 7, 2023 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants