(
replica_ctx: &ReplicaContext,
module_def: &ModuleDef,
program: Program,
call_reducer: impl FnOnce(Option<MutTxId>, CallReducerParams) -> (ReducerCallResult, bool),
)
| 580 | } |
| 581 | |
| 582 | fn init_database_inner( |
| 583 | replica_ctx: &ReplicaContext, |
| 584 | module_def: &ModuleDef, |
| 585 | program: Program, |
| 586 | call_reducer: impl FnOnce(Option<MutTxId>, CallReducerParams) -> (ReducerCallResult, bool), |
| 587 | ) -> anyhow::Result<(Option<ReducerCallResult>, bool)> { |
| 588 | log::debug!("init database"); |
| 589 | let timestamp = Timestamp::now(); |
| 590 | let stdb = replica_ctx.relational_db(); |
| 591 | let logger = replica_ctx.logger.system_logger(); |
| 592 | let owner_identity = replica_ctx.database.owner_identity; |
| 593 | |
| 594 | let tx = stdb.begin_mut_tx(IsolationLevel::Serializable, Workload::Internal); |
| 595 | let auth_ctx = AuthCtx::for_current(owner_identity); |
| 596 | let (tx, ()) = stdb |
| 597 | .with_auto_rollback(tx, |tx| { |
| 598 | // Create all in-memory tables defined by the module, |
| 599 | // with IDs ordered lexicographically by the table names. |
| 600 | let mut table_defs: Vec<_> = module_def.tables().collect(); |
| 601 | table_defs.sort_by_key(|x| &x.name); |
| 602 | for def in table_defs { |
| 603 | logger.info(&format!("Creating table `{}`", &def.name)); |
| 604 | spacetimedb_engine::update::create_table_from_def(stdb, tx, module_def, def)?; |
| 605 | } |
| 606 | |
| 607 | // Create all in-memory views defined by the module. |
| 608 | let mut view_defs: Vec<_> = module_def.views().collect(); |
| 609 | view_defs.sort_by_key(|x| &x.name); |
| 610 | for def in view_defs { |
| 611 | logger.info(&format!("Creating table for view `{}`", &def.name)); |
| 612 | create_table_from_view_def(stdb, tx, module_def, def)?; |
| 613 | } |
| 614 | |
| 615 | // Insert the late-bound row-level security expressions. |
| 616 | for rls in module_def.row_level_security() { |
| 617 | logger.info(&format!("Creating row level security `{}`", rls.sql)); |
| 618 | |
| 619 | let rls = RowLevelExpr::build_row_level_expr(tx, &auth_ctx, rls) |
| 620 | .with_context(|| format!("failed to create row-level security: `{}`", rls.sql))?; |
| 621 | let table_id = rls.def.table_id; |
| 622 | let sql = rls.def.sql.clone(); |
| 623 | stdb.create_row_level_security(tx, rls.def) |
| 624 | .with_context(|| format!("failed to create row-level security for table `{table_id}`: `{sql}`",))?; |
| 625 | } |
| 626 | |
| 627 | stdb.set_initialized(tx, program)?; |
| 628 | |
| 629 | anyhow::Ok(()) |
| 630 | }) |
| 631 | .inspect_err(|e| log::error!("{e:?}"))?; |
| 632 | |
| 633 | let rcr = match module_def.lifecycle_reducer(Lifecycle::Init) { |
| 634 | None => { |
| 635 | if let Some((_tx_offset, tx_data, tx_metrics, reducer)) = stdb.commit_tx(tx)? { |
| 636 | stdb.report_mut_tx_metrics(reducer, tx_metrics, Some(tx_data)); |
| 637 | } |
| 638 | (None, false) |
| 639 | } |
no test coverage detected
searching dependent graphs…