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

feat: support transfer address from user to user #484

Merged
merged 2 commits into from
Nov 14, 2024
Merged

Conversation

dreamhunter2333
Copy link
Owner

@dreamhunter2333 dreamhunter2333 commented Nov 14, 2024

User description

如果你是全新部署,不需要迁移数据,下面的内容可以忽略,可以离开本文档了。

如果要用户间转移邮箱地址,并需要之前的用户无法登录,就需要进行数据迁移操作。因为目前的表结构最大的 ID 会被重复使用,所以转移最大 id 的邮箱会导致 ID 相同,如果你并不在意这件事可以忽略下面的步骤。

下面是更改表结构的详细步骤

  1. 创建新表:
    为了增加新的特性(例如 AUTOINCREMENT)并且保留现有 id,您需要创建一个新表。确保新表结构与原始表完全一致,只是在 id 列上添加 AUTOINCREMENT。
    CREATE TABLE address_new (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT UNIQUE,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
    );

  2. 从旧表迁移数据到新表:
    这里,我们需要保留所有字段,包括 id 列。因此,插入时要明确指定各个字段:
    INSERT INTO address_new (id, name, created_at, updated_at)
    SELECT id, name, created_at, updated_at FROM address;

  3. 验证迁移数据:
    迁移后,我们需要验证新表中的数据是否与旧表一致,包括 id 值是否正确迁移:
    SELECT * FROM address_new;

  4. 重命名旧表:
    在迁移数据成功并验证之后,可以重命名旧表以备用:
    ALTER TABLE address RENAME TO address_old;

  5. 将新表重命名为原始表的名称:
    紧接着,将新表重命名为原始表名,以便应用程序继续使用相同的表名:
    ALTER TABLE address_new RENAME TO address;

  6. 最终验证:
    确认新命名的表结构和数据:
    PRAGMA table_info(address);
    SELECT * FROM address;

  7. 删除旧表(可选):
    确认一切工作正常并且不再需要旧表后,可以删除旧表以释放空间(此步骤是可选的):
    DROP TABLE address_old;

完整示例

以下是从创建新表、迁移数据到最终重命名表的完整 SQL 示例:

-- Step 1: 创建新的 table
CREATE TABLE address_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Step 2: 从旧表迁移数据,包括 id 列
INSERT INTO address_new (id, name, created_at, updated_at)
SELECT id, name, created_at, updated_at FROM address;

-- Step 3: 验证迁移
SELECT * FROM address_new;

-- Step 4: 重命名旧表
ALTER TABLE address RENAME TO address_old;

-- Step 5: 将新表重命名为旧表名称
ALTER TABLE address_new RENAME TO address;

-- Step 6: 最终验证
PRAGMA table_info(address);
SELECT * FROM address;

-- Step 7: 删除旧表(可选)
DROP TABLE address_old;

通过上述步骤,您将成功地迁移旧表的数据至新表,包括 id 列的完整保留,并有效实现 AUTOINCREMENT。


PR Type

Enhancement, Documentation


Description

  • Added new UI elements and logic in the frontend to support transferring addresses between users.
  • Implemented a new API endpoint in the backend to handle the transfer of addresses between users.
  • Updated the database schema to use AUTOINCREMENT for the id column in the address table.
  • Documented the new feature in the changelog.

Changes walkthrough 📝

Relevant files
Enhancement
AddressManagement.vue
Add UI and logic for transferring addresses between users

frontend/src/views/user/AddressManagement.vue

  • Added new UI elements for transferring addresses between users.
  • Added new modal for address transfer confirmation.
  • Added new methods for handling address transfer logic.
  • +65/-2   
    bind_address.ts
    Implement API endpoint and logic for address transfer       

    worker/src/user_api/bind_address.ts

  • Added new API endpoint for transferring addresses between users.
  • Implemented logic to check and transfer addresses.
  • Updated error handling for address unbinding.
  • +90/-1   
    index.ts
    Add route for address transfer API endpoint                           

    worker/src/user_api/index.ts

    • Added route for the new address transfer API endpoint.
    +1/-0     
    schema.sql
    Update address table schema to use AUTOINCREMENT                 

    db/schema.sql

  • Updated address table schema to use AUTOINCREMENT for the id column.
  • +1/-1     
    Documentation
    CHANGELOG.md
    Update changelog with address transfer feature                     

    CHANGELOG.md

  • Documented the new feature for transferring addresses between users.
  • +1/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Bug
    The transferAddress function does not handle the case where the API call fails. This could result in the UI not reflecting the failure state properly.

    Code Duplication
    The transferAddress function contains duplicated code for unbinding and binding addresses. Consider refactoring to reduce redundancy.

    Error Handling
    The transferAddress function does not log errors when they occur, making it difficult to debug issues in production.

    Database Schema Change
    The change to the address table schema (adding AUTOINCREMENT to the id field) should be carefully reviewed to ensure it does not affect existing data integrity.

    Copy link

    PR Code Suggestions ✨

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant