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: ability to skip migration of certain contracts/models #2026

Merged
merged 18 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{Context, Result};
use clap::Args;
use dojo_bindgen::{BuiltinPlugins, PluginManager};
use dojo_lang::scarb_internal::compile_workspace;
use dojo_world::metadata::dojo_metadata_from_workspace;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use scarb::core::{Config, TargetKind};
Expand Down Expand Up @@ -110,9 +111,12 @@ impl BuildArgs {
};
trace!(pluginManager=?bindgen, "Generating bindings.");

let ws = scarb::ops::read_workspace(config.manifest_path(), config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws);

tokio::runtime::Runtime::new()
.unwrap()
.block_on(bindgen.generate())
.block_on(bindgen.generate(dojo_metadata.skip_migration))
.expect("Error generating bindings");

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions bin/sozo/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl CleanArgs {
#[cfg(test)]
mod tests {
use dojo_test_utils::compiler;
use dojo_world::metadata::dojo_metadata_from_workspace;
use dojo_world::migration::TxnConfig;
use katana_runner::KatanaRunner;
use sozo_ops::migration;
Expand All @@ -84,6 +85,8 @@ mod tests {

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();

let dojo_metadata = dojo_metadata_from_workspace(&ws);

// Plan the migration to generate some manifests other than base.
config.tokio_handle().block_on(async {
migration::migrate(
Expand All @@ -94,6 +97,7 @@ mod tests {
"dojo_examples",
true,
TxnConfig::default(),
dojo_metadata.skip_migration,
)
.await
.unwrap()
Expand Down
24 changes: 21 additions & 3 deletions bin/sozo/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
#[command(flatten)]
pub account: AccountOptions,
}

impl DevArgs {
pub fn run(self, config: &Config) -> Result<()> {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let dojo_metadata = dojo_metadata_from_workspace(&ws);

Check warning on line 54 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L54

Added line #L54 was not covered by tests

let env_metadata = if config.manifest_path().exists() {
dojo_metadata_from_workspace(&ws).env().cloned()
dojo_metadata.env().cloned()

Check warning on line 57 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L57

Added line #L57 was not covered by tests
} else {
trace!("Manifest path does not exist.");
None
Expand Down Expand Up @@ -98,6 +98,7 @@
&name,
&context.ws,
previous_manifest.clone(),
dojo_metadata.skip_migration.clone(),

Check warning on line 101 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L101

Added line #L101 was not covered by tests
)) {
Ok((manifest, address)) => {
previous_manifest = Some(manifest);
Expand Down Expand Up @@ -132,6 +133,7 @@
&name,
&context.ws,
previous_manifest.clone(),
dojo_metadata.skip_migration.clone(),

Check warning on line 136 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L136

Added line #L136 was not covered by tests
)) {
Ok((manifest, address)) => {
previous_manifest = Some(manifest);
Expand Down Expand Up @@ -223,12 +225,14 @@
Ok(())
}

// TODO: fix me
async fn migrate<P, S>(
mut world_address: Option<FieldElement>,
account: &SingleOwnerAccount<P, S>,
name: &str,
ws: &Workspace<'_>,
previous_manifest: Option<DeploymentManifest>,
skip_migration: Option<Vec<String>>,

Check warning on line 235 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L235

Added line #L235 was not covered by tests
) -> Result<(DeploymentManifest, Option<FieldElement>)>
where
P: Provider + Sync + Send + 'static,
Expand All @@ -244,9 +248,23 @@
return Err(anyhow!("Build project using `sozo build` first"));
}

let new_manifest =
let mut new_manifest =

Check warning on line 251 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L251

Added line #L251 was not covered by tests
BaseManifest::load_from_path(&manifest_dir.join(MANIFESTS_DIR).join(BASE_DIR))?;

if let Some(skip_manifests) = skip_migration {
for contract_or_model in skip_manifests {
if let Some(index) =
new_manifest.contracts.iter().position(|c| c.name == contract_or_model)
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
{
new_manifest.contracts.remove(index);
} else if let Some(index) =
new_manifest.models.iter().position(|m| m.name == contract_or_model)
{
new_manifest.models.remove(index);
};

Check warning on line 264 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L254-L264

Added lines #L254 - L264 were not covered by tests
}
}

Check warning on line 266 in bin/sozo/src/commands/dev.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/dev.rs#L266

Added line #L266 was not covered by tests

let diff = WorldDiff::compute(new_manifest.clone(), previous_manifest);
let total_diffs = diff.count_diffs();
let config = ws.config();
Expand Down
17 changes: 14 additions & 3 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
pub fn run(self, config: &Config) -> Result<()> {
trace!(args = ?self);
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let dojo_metadata = dojo_metadata_from_workspace(&ws);

Check warning on line 59 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L59

Added line #L59 was not covered by tests

let env_metadata = if config.manifest_path().exists() {
dojo_metadata_from_workspace(&ws).env().cloned()
dojo_metadata.env().cloned()

Check warning on line 62 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L62

Added line #L62 was not covered by tests
} else {
trace!("Manifest path does not exist.");
None
Expand Down Expand Up @@ -89,15 +90,25 @@
&name,
true,
TxnConfig::default(),
dojo_metadata.skip_migration,

Check warning on line 93 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L93

Added line #L93 was not covered by tests
)
.await
}),
MigrateCommand::Apply { transaction } => config.tokio_handle().block_on(async {
trace!(name, "Applying migration.");
let txn_config: TxnConfig = transaction.into();

migration::migrate(&ws, world_address, rpc_url, account, &name, false, txn_config)
.await
migration::migrate(
&ws,
world_address,
rpc_url,
account,
&name,
false,
txn_config,
dojo_metadata.skip_migration,
)
.await

Check warning on line 111 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L101-L111

Added lines #L101 - L111 were not covered by tests
}),
}
}
Expand Down
9 changes: 6 additions & 3 deletions bin/sozo/tests/register_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod utils;
use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_test_utils::migration::prepare_migration;
use dojo_world::metadata::dojo_metadata_from_workspace;
use dojo_world::migration::TxnConfig;
use katana_runner::KatanaRunner;
use scarb::ops;
Expand All @@ -20,11 +21,13 @@ async fn reregister_models() {

let ws = ops::read_workspace(config.manifest_path(), &config)
.unwrap_or_else(|op| panic!("Error building workspace: {op:?}"));
let dojo_metadata = dojo_metadata_from_workspace(&ws);

let base = config.manifest_path().parent().unwrap();
let target_dir = format!("{}/target/dev", base);
let target_path =
ws.target_dir().path_existent().unwrap().join(ws.config().profile().to_string());

let migration = prepare_migration(base.into(), target_dir.into()).unwrap();
let migration =
prepare_migration(source_project_dir, target_path, dojo_metadata.skip_migration).unwrap();

let sequencer = KatanaRunner::new().expect("Failed to start runner.");

Expand Down
4 changes: 4 additions & 0 deletions crates/dojo-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ thiserror.workspace = true

cainome.workspace = true
dojo-world = { path = "../dojo-world", features = [ "manifest" ] }

[dev-dependencies]
dojo-test-utils = { path = "../dojo-test-utils", features = [ "build-examples" ] }
scarb = { workspace = true }
49 changes: 39 additions & 10 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@

impl PluginManager {
/// Generates the bindings for all the given Plugin.
pub async fn generate(&self) -> BindgenResult<()> {
pub async fn generate(&self, skip_migration: Option<Vec<String>>) -> BindgenResult<()> {
if self.builtin_plugins.is_empty() && self.plugins.is_empty() {
return Ok(());
}

let data =
gather_dojo_data(&self.manifest_path, &self.root_package_name, &self.profile_name)?;
let data = gather_dojo_data(
&self.manifest_path,
&self.root_package_name,
&self.profile_name,
skip_migration,
)?;

for plugin in &self.builtin_plugins {
// Get the plugin builder from the plugin enum.
Expand Down Expand Up @@ -111,10 +115,25 @@
manifest_path: &Utf8PathBuf,
root_package_name: &str,
profile_name: &str,
skip_migration: Option<Vec<String>>,
) -> BindgenResult<DojoData> {
let root_dir: Utf8PathBuf = manifest_path.parent().unwrap().into();
let base_manifest_dir: Utf8PathBuf = root_dir.join("manifests").join(profile_name).join("base");
let base_manifest = BaseManifest::load_from_path(&base_manifest_dir)?;
let mut base_manifest = BaseManifest::load_from_path(&base_manifest_dir)?;

if let Some(skip_manifests) = skip_migration {
for contract_or_model in skip_manifests {
if let Some(index) =
base_manifest.contracts.iter().position(|c| c.name == contract_or_model)
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
{
base_manifest.contracts.remove(index);
} else if let Some(index) =
base_manifest.models.iter().position(|m| m.name == contract_or_model)
{
base_manifest.models.remove(index);
};
}
}

Check warning on line 136 in crates/dojo-bindgen/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/lib.rs#L136

Added line #L136 was not covered by tests

let mut models = HashMap::new();
let mut contracts = HashMap::new();
Expand Down Expand Up @@ -244,6 +263,9 @@

#[cfg(test)]
mod tests {
use dojo_test_utils::compiler;
use dojo_world::metadata::dojo_metadata_from_workspace;

use super::*;

#[test]
Expand All @@ -254,12 +276,19 @@

#[test]
fn gather_data_ok() {
let data = gather_dojo_data(
&Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml"),
"dojo_example",
"dev",
)
.unwrap();
let manifest_path = Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml");

let config = compiler::copy_tmp_config(
&Utf8PathBuf::from("../../examples/spawn-and-move"),
&Utf8PathBuf::from("../dojo-core"),
);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws);

let data =
gather_dojo_data(&manifest_path, "dojo_example", "dev", dojo_metadata.skip_migration)
.unwrap();

assert_eq!(data.models.len(), 6);

Expand Down
21 changes: 14 additions & 7 deletions crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
constructor(contractAddress: string, account?: Account) {{
super(contractAddress, account);
}}

{}
}}
",
Expand Down Expand Up @@ -630,6 +630,8 @@
use std::io::Read;

use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_world::metadata::dojo_metadata_from_workspace;

use super::*;
use crate::gather_dojo_data;
Expand All @@ -646,12 +648,17 @@
let expected_output_without_header =
expected_output.lines().skip(1).collect::<Vec<&str>>().join("\n");

let data = gather_dojo_data(
&Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml"),
"dojo_examples",
"dev",
)
.unwrap();
let manifest_path = Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml");
let config = compiler::copy_tmp_config(
&Utf8PathBuf::from("../../examples/spawn-and-move"),
&Utf8PathBuf::from("../dojo-core"),
);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws);
let data =
gather_dojo_data(&manifest_path, "dojo_examples", "dev", dojo_metadata.skip_migration)
.unwrap();

Check warning on line 661 in crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs#L651-L661

Added lines #L651 - L661 were not covered by tests

let actual_output = TypeScriptV2Plugin::generate_code_content(&data);
let actual_output_without_header =
Expand Down
14 changes: 14 additions & 0 deletions crates/dojo-test-utils/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet::macros::felt;
pub fn prepare_migration(
manifest_dir: Utf8PathBuf,
target_dir: Utf8PathBuf,
skip_migration: Option<Vec<String>>,
) -> Result<MigrationStrategy> {
// In testing, profile name is always dev.
let profile_name = "dev";
Expand All @@ -20,6 +21,19 @@ pub fn prepare_migration(
)
.unwrap();

if let Some(skip_manifests) = skip_migration {
for contract_or_model in skip_manifests {
if let Some(index) = manifest.contracts.iter().position(|c| c.name == contract_or_model)
{
manifest.contracts.remove(index);
} else if let Some(index) =
manifest.models.iter().position(|m| m.name == contract_or_model)
{
manifest.models.remove(index);
};
}
}

let overlay_manifest = OverlayManifest::load_from_path(
&manifest_dir.join(MANIFESTS_DIR).join(profile_name).join(OVERLAYS_DIR),
)
Expand Down
8 changes: 7 additions & 1 deletion crates/dojo-world/src/contracts/model_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starknet::macros::felt;
use crate::contracts::model::ModelReader;
use crate::contracts::world::test::deploy_world;
use crate::contracts::world::WorldContractReader;
use crate::metadata::dojo_metadata_from_workspace;

#[tokio::test(flavor = "multi_thread")]
async fn test_model() {
Expand All @@ -24,7 +25,12 @@ async fn test_model() {
let manifest_dir = config.manifest_path().parent().unwrap();
let target_dir = manifest_dir.join("target").join("dev");

let world_address = deploy_world(&runner, &manifest_dir.into(), &target_dir).await;
let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws);

let world_address =
deploy_world(&runner, &manifest_dir.into(), &target_dir, dojo_metadata.skip_migration)
.await;

let world = WorldContractReader::new(world_address, provider);
let position = world.model_reader("Position").await.unwrap();
Expand Down
Loading
Loading