feat: support transfer address from user to user #484
Merged
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.
User description
如果你是全新部署,不需要迁移数据,下面的内容可以忽略,可以离开本文档了。
如果要用户间转移邮箱地址,并需要之前的用户无法登录,就需要进行数据迁移操作。因为目前的表结构最大的 ID 会被重复使用,所以转移最大 id 的邮箱会导致 ID 相同,如果你并不在意这件事可以忽略下面的步骤。
下面是更改表结构的详细步骤
创建新表:
为了增加新的特性(例如 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
);
从旧表迁移数据到新表:
这里,我们需要保留所有字段,包括 id 列。因此,插入时要明确指定各个字段:
INSERT INTO address_new (id, name, created_at, updated_at)
SELECT id, name, created_at, updated_at FROM address;
验证迁移数据:
迁移后,我们需要验证新表中的数据是否与旧表一致,包括 id 值是否正确迁移:
SELECT * FROM address_new;
重命名旧表:
在迁移数据成功并验证之后,可以重命名旧表以备用:
ALTER TABLE address RENAME TO address_old;
将新表重命名为原始表的名称:
紧接着,将新表重命名为原始表名,以便应用程序继续使用相同的表名:
ALTER TABLE address_new RENAME TO address;
最终验证:
确认新命名的表结构和数据:
PRAGMA table_info(address);
SELECT * FROM address;
删除旧表(可选):
确认一切工作正常并且不再需要旧表后,可以删除旧表以释放空间(此步骤是可选的):
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
id
column in theaddress
table.Changes walkthrough 📝
AddressManagement.vue
Add UI and logic for transferring addresses between users
frontend/src/views/user/AddressManagement.vue
bind_address.ts
Implement API endpoint and logic for address transfer
worker/src/user_api/bind_address.ts
index.ts
Add route for address transfer API endpoint
worker/src/user_api/index.ts
schema.sql
Update address table schema to use AUTOINCREMENT
db/schema.sql
address
table schema to use AUTOINCREMENT for theid
column.CHANGELOG.md
Update changelog with address transfer feature
CHANGELOG.md