Loading...
Loading...
Compare original and translation side by side
"Migrate my changes to the deployed world""Upgrade my project to Dojo v1.8.0""将我的变更迁移至已部署的World""将我的项目升级至Dojo v1.8.0"sozo inspectsozo inspectsozo build
sozo testsozo build
sozo testundefinedundefinedundefinedundefined// New model - safe to add
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct NewFeature {
#[key]
pub player: ContractAddress,
pub data: u32,
}// New system - safe to add
#[dojo::contract]
pub mod new_system {
// Implementation
}// Adding field - existing data will have default (zero) value
struct Position {
#[key] player: ContractAddress,
x: u32,
y: u32,
z: u32, // New field
}// 新增模型 - 可安全添加
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct NewFeature {
#[key]
pub player: ContractAddress,
pub data: u32,
}// 新增系统 - 可安全添加
#[dojo::contract]
pub mod new_system {
// 实现代码
}// 新增字段 - 现有数据将使用默认值(零)
struct Position {
#[key] player: ContractAddress,
x: u32,
y: u32,
z: u32, // 新增字段
}// Old
struct Position {
#[key] player: ContractAddress,
x: u32, y: u32,
}
// New - BREAKING! Different key structure
struct Position {
#[key] entity_id: u32, // Changed key
x: u32, y: u32,
}// Old
struct Stats {
#[key] player: ContractAddress,
health: u8,
mana: u8,
}
// New - BREAKING! Data loss
struct Stats {
#[key] player: ContractAddress,
health: u8,
// mana removed
}// Old
struct Position {
#[key] player: ContractAddress,
x: u32,
y: u32,
}
// New - BREAKING! Type incompatible
struct Position {
#[key] player: ContractAddress,
x: u128, // Changed type
y: u128,
}// 旧版本
struct Position {
#[key] player: ContractAddress,
x: u32, y: u32,
}
// 新版本 - 破坏性变更!关键字段结构不同
struct Position {
#[key] entity_id: u32, // 已修改关键字段
x: u32, y: u32,
}// 旧版本
struct Stats {
#[key] player: ContractAddress,
health: u8,
mana: u8,
}
// 新版本 - 破坏性变更!会丢失数据
struct Stats {
#[key] player: ContractAddress,
health: u8,
// mana已移除
}// 旧版本
struct Position {
#[key] player: ContractAddress,
x: u32,
y: u32,
}
// 新版本 - 破坏性变更!类型不兼容
struct Position {
#[key] player: ContractAddress,
x: u128, // 已修改类型
y: u128,
}undefinedundefined
```bash
sozo build && sozo migrate
```bash
sozo build && sozo migrate// Keep old model
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct PositionV1 {
#[key] player: ContractAddress,
x: u32, y: u32,
}
// Add new model
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct PositionV2 {
#[key] entity_id: u32,
x: u32, y: u32, z: u32,
}// 保留旧模型
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct PositionV1 {
#[key] player: ContractAddress,
x: u32, y: u32,
}
// 新增新模型
#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct PositionV2 {
#[key] entity_id: u32,
x: u32, y: u32, z: u32,
}#[dojo::contract]
pub mod migrator {
fn migrate_positions(ref self: ContractState, players: Array<ContractAddress>) {
let mut world = self.world_default();
for player in players {
// Read old format
let old_pos: PositionV1 = world.read_model(player);
// Transform to new format
let new_pos = PositionV2 {
entity_id: world.uuid(),
x: old_pos.x,
y: old_pos.y,
z: 0,
};
// Write new format
world.write_model(@new_pos);
}
}
}#[dojo::contract]
pub mod migrator {
fn migrate_positions(ref self: ContractState, players: Array<ContractAddress>) {
let mut world = self.world_default();
for player in players {
// 读取旧格式数据
let old_pos: PositionV1 = world.read_model(player);
// 转换为新格式
let new_pos = PositionV2 {
entity_id: world.uuid(),
x: old_pos.x,
y: old_pos.y,
z: 0,
};
// 写入新格式数据
world.write_model(@new_pos);
}
}
}Scarb.toml[dependencies]
dojo = "1.8.0"
[dev-dependencies]
dojo_cairo_test = "1.8.0"sozo build
sozo testsozo migrateScarb.toml[dependencies]
dojo = "1.8.0"
[dev-dependencies]
dojo_cairo_test = "1.8.0"sozo build
sozo testsozo migratesozo inspectsozo inspectsozo buildsozo testsozo migratesozo buildsozo testsozo migrateundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedsozo buildtarget/sozo buildtarget/sozo inspectsozo inspectsozo build --typescriptdojo-indexersozo build --typescriptdojo-indexer