Files
SpacetimeDB/test/tests/update-module.sh
T
Kim Altintop 00050967a9 Store the current module hash in a system table (#290)
* Store the current module hash in a system table

* Add more user logs

Fill the user-retrievable database log with more info about what is
going on while a database is being initialized or updated.

* Silence unused warning

* sats: Add `field_as_u64` to `ProductValue`

* db: Add `epoch` to st_module

Add a u64 field `epoch` to the st_module table, used to store a fencing
token.

* core: Add a way to obtain a keyed lock to `ControlDB`

* host: Thread through the fencing token

* standalone: Implement module lifecycle using the new locking facilities

* test: Fix update-module

Need another line of logs, as there is now more output.

* Refine API

Make getting and setting the program hash trait methods. Also widen the
epoch / fencing token to fit stdb sequences.

* db: Hand out opaque index / sequence ids instead of numbers

This allows to actually use relational db methods taking those types as
arguments outside the core crate.

Also make `next_sequence` and `create_sequence` not take `&mut self`
unnecessarily.

* Fix type error

* Fix test
2023-09-28 09:46:32 +02:00

123 lines
3.1 KiB
Bash

#!/bin/bash
if [ "$DESCRIBE_TEST" = 1 ] ; then
echo "This tests publishing a module without the --clear-database option"
exit
fi
set -euox pipefail
source "./test/lib.include"
cat > "${PROJECT_PATH}/src/lib.rs" << EOF
use spacetimedb::{println, spacetimedb};
#[spacetimedb(table)]
pub struct Person {
#[primarykey]
#[autoinc]
id: u64,
name: String,
}
#[spacetimedb(reducer)]
pub fn add(name: String) {
Person::insert(Person { id: 0, name }).unwrap();
}
#[spacetimedb(reducer)]
pub fn say_hello() {
for person in Person::iter() {
println!("Hello, {}!", person.name);
}
println!("Hello, World!");
}
EOF
IDENT=$(basename "$PROJECT_PATH")
run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT"
[ "1" == "$(grep -c "reated new database" "$TEST_OUT")" ]
run_test cargo run call "$IDENT" add Robert
run_test cargo run call "$IDENT" add Julie
run_test cargo run call "$IDENT" add Samantha
run_test cargo run call "$IDENT" say_hello
run_test cargo run logs "$IDENT" 100
[ ' Hello, Samantha!' == "$(grep 'Samantha' "$TEST_OUT" | tail -n 4 | cut -d: -f4-)" ]
[ ' Hello, Julie!' == "$(grep 'Julie' "$TEST_OUT" | tail -n 4 | cut -d: -f4-)" ]
[ ' Hello, Robert!' == "$(grep 'Robert' "$TEST_OUT" | tail -n 4 | cut -d: -f4-)" ]
[ ' Hello, World!' == "$(grep 'World' "$TEST_OUT" | tail -n 4 | cut -d: -f4-)" ]
# Unchanged module is ok
run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT"
[ "1" == "$(grep -c "Updated database" "$TEST_OUT")" ]
# Changing an existing table isn't
cat > "${PROJECT_PATH}/src/lib.rs" <<EOF
use spacetimedb::spacetimedb;
#[spacetimedb(table)]
pub struct Person {
#[primarykey]
#[autoinc]
id: u64,
name: String,
age: u8,
}
EOF
run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT" || true
[ "1" == "$(grep -c "Error: Database update rejected" "$TEST_OUT")" ]
# Adding an index is fine, too, and invokes update
cat > "${PROJECT_PATH}/src/lib.rs" <<EOF
use spacetimedb::{println, spacetimedb};
#[spacetimedb(table)]
#[spacetimedb(index(btree, name = "name", name))]
pub struct Person {
#[primarykey]
#[autoinc]
id: u64,
name: String,
}
#[spacetimedb(update)]
pub fn on_module_update() {
println!("INDEX ADDED");
}
EOF
run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT"
[ "1" == "$(grep -c "Updated database" "$TEST_OUT")" ]
run_test cargo run logs "$IDENT" 2
[ ' INDEX ADDED' == "$(grep 'INDEX ADDED' "$TEST_OUT" | tail -n 1 | cut -d: -f4-)" ]
# Adding a table is ok, and invokes update
cat > "${PROJECT_PATH}/src/lib.rs" <<EOF
use spacetimedb::{println, spacetimedb};
#[spacetimedb(table)]
pub struct Person {
#[primarykey]
#[autoinc]
id: u64,
name: String,
}
#[spacetimedb(table)]
pub struct Pet {
species: String,
}
#[spacetimedb(update)]
pub fn on_module_update() {
println!("MODULE UPDATED");
}
EOF
run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT"
[ "1" == "$(grep -c "Updated database" "$TEST_OUT")" ]
run_test cargo run logs "$IDENT" 2
[ ' MODULE UPDATED' == "$(grep 'MODULE UPDATED' "$TEST_OUT" | tail -n 1 | cut -d: -f4-)" ]