Skip to content

Commit

Permalink
[release/v1.0.0-rc2-hotfix2]: Preserve reducer order in schema conver…
Browse files Browse the repository at this point in the history
…sion (#1987)

Signed-off-by: Ingvar Stepanyan <me@rreverser.com>
Co-authored-by: james gilles <jameshgilles@gmail.com>
  • Loading branch information
2 people authored and bfops committed Dec 24, 2024
1 parent 6b1d0c3 commit 37db460
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions crates/core/src/host/module_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,12 @@ impl ModuleInfo {
}

/// A bidirectional map between `Identifiers` (reducer names) and `ReducerId`s.
/// Invariant: the reducer names are in alphabetical order.
/// Invariant: the reducer names are in the same order as they were declared in the `ModuleDef`.
pub struct ReducersMap(IndexSet<Box<str>>);

impl<'a> FromIterator<&'a str> for ReducersMap {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
let mut sorted = Vec::from_iter(iter);
sorted.sort();
ReducersMap(sorted.into_iter().map_into().collect())
Self(iter.into_iter().map_into().collect())
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spacetimedb-data-structures.workspace = true
spacetimedb-sql-parser.workspace = true

anyhow.workspace = true
indexmap.workspace = true
itertools.workspace = true
lazy_static.workspace = true
thiserror.workspace = true
Expand Down
7 changes: 5 additions & 2 deletions crates/schema/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::schema::{Schema, TableSchema};
use crate::type_for_generate::{AlgebraicTypeUse, ProductTypeDef, TypespaceForGenerate};
use deserialize::ReducerArgsDeserializeSeed;
use hashbrown::Equivalent;
use indexmap::IndexMap;
use itertools::Itertools;
use spacetimedb_data_structures::error_stream::{CollectAllErrors, CombineErrors, ErrorStream};
use spacetimedb_data_structures::map::HashMap;
Expand Down Expand Up @@ -84,7 +85,9 @@ pub struct ModuleDef {
tables: IdentifierMap<TableDef>,

/// The reducers of the module definition.
reducers: IdentifierMap<ReducerDef>,
/// Note: this is using IndexMap because reducer order is important
/// and must be preserved for future calls to `__call_reducer__`.
reducers: IndexMap<Identifier, ReducerDef>,

/// The type definitions of the module definition.
types: HashMap<ScopedTypeName, TypeDef>,
Expand Down Expand Up @@ -355,7 +358,7 @@ impl From<ModuleDef> for RawModuleDefV9 {

RawModuleDefV9 {
tables: to_raw(tables, |table: &RawTableDefV9| &table.name),
reducers: to_raw(reducers, |reducer: &RawReducerDefV9| &reducer.name),
reducers: reducers.into_iter().map(|(_, def)| def.into()).collect(),
types: to_raw(types, |type_: &RawTypeDefV9| &type_.name),
misc_exports: vec![],
typespace,
Expand Down
2 changes: 1 addition & 1 deletion crates/schema/src/def/validate/v9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ fn identifier(name: Box<str>) -> Result<Identifier> {

fn check_scheduled_reducers_exist(
tables: &IdentifierMap<TableDef>,
reducers: &IdentifierMap<ReducerDef>,
reducers: &IndexMap<Identifier, ReducerDef>,
) -> Result<()> {
tables
.values()
Expand Down

0 comments on commit 37db460

Please sign in to comment.