Skip to content

Commit 6191d7c

Browse files
committed
typescript module migration smoketest
1 parent 81120b9 commit 6191d7c

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

crates/smoketests/tests/smoketests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ mod sql;
3838
mod sql_connect_hook;
3939
mod templates;
4040
mod timestamp_route;
41+
mod typescript_index_source_name;
4142
mod views;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use spacetimedb_smoketests::{random_string, require_local_server, require_pnpm, Smoketest};
2+
3+
const TYPESCRIPT_MODULE_WITHOUT_NEW_COLUMNS: &str = r#"import { schema, table, t } from "spacetimedb/server";
4+
5+
const targetableProperties_v1 = table(
6+
{ name: "targetable_properties_v1", public: false },
7+
{
8+
id: t.u64().primaryKey().autoInc(),
9+
canonicalAddress: t.string(),
10+
googlePlaceId: t.string().index("btree"),
11+
googlePlacePhotoName: t.string().optional(),
12+
},
13+
);
14+
15+
const spacetimedb = schema({
16+
targetableProperties_v1,
17+
});
18+
export default spacetimedb;
19+
20+
export const insert_targetable_property = spacetimedb.reducer(
21+
{
22+
canonicalAddress: t.string(),
23+
googlePlaceId: t.string(),
24+
googlePlacePhotoName: t.string(),
25+
},
26+
(ctx, { canonicalAddress, googlePlaceId, googlePlacePhotoName }) => {
27+
ctx.db.targetableProperties_v1.insert({
28+
id: 0n,
29+
canonicalAddress,
30+
googlePlaceId,
31+
googlePlacePhotoName,
32+
});
33+
},
34+
);
35+
"#;
36+
37+
const TYPESCRIPT_MODULE_WITH_NEW_COLUMNS: &str = r#"import { schema, table, t } from "spacetimedb/server";
38+
39+
const targetableProperties_v1 = table(
40+
{ name: "targetable_properties_v1", public: false },
41+
{
42+
id: t.u64().primaryKey().autoInc(),
43+
canonicalAddress: t.string(),
44+
googlePlaceId: t.string().index("btree"),
45+
googlePlacePhotoName: t.string().optional(),
46+
latitude: t.number().optional().default(undefined),
47+
longitude: t.number().optional().default(undefined),
48+
sqft: t.number().optional().default(undefined),
49+
hasPhotos: t.bool().default(false).index(),
50+
},
51+
);
52+
53+
const spacetimedb = schema({
54+
targetableProperties_v1,
55+
});
56+
export default spacetimedb;
57+
58+
export const touch_targetable_properties = spacetimedb.reducer(
59+
{ googlePlaceId: t.string() },
60+
(ctx, { googlePlaceId }) => {
61+
let count = 0;
62+
for (const _row of ctx.db.targetableProperties_v1.googlePlaceId.filter(googlePlaceId)) {
63+
count += 1;
64+
}
65+
console.info(`matched ${count}`);
66+
},
67+
);
68+
69+
export const touch_has_photos_index = spacetimedb.reducer(
70+
{ hasPhotos: t.bool() },
71+
(ctx, { hasPhotos }) => {
72+
let count = 0;
73+
for (const _row of ctx.db.targetableProperties_v1.hasPhotos.filter(hasPhotos)) {
74+
count += 1;
75+
}
76+
console.info(`matched hasPhotos ${count}`);
77+
},
78+
);
79+
"#;
80+
81+
#[test]
82+
fn test_typescript_add_optional_columns() {
83+
require_pnpm!();
84+
require_local_server!();
85+
86+
let mut test = Smoketest::builder().autopublish(false).build();
87+
let module_name = format!("typescript-add-optional-columns-{}", random_string());
88+
89+
let database_identity = test
90+
.publish_typescript_module_source(
91+
"typescript-add-optional-columns-v1",
92+
&module_name,
93+
TYPESCRIPT_MODULE_WITHOUT_NEW_COLUMNS,
94+
)
95+
.unwrap();
96+
97+
test.call("insert_targetable_property", &["123 Main St", "place_123", "photo_123"])
98+
.unwrap();
99+
100+
test.restart_server();
101+
102+
test.publish_typescript_module_source_clear(
103+
"typescript-add-optional-columns-v2",
104+
&database_identity,
105+
TYPESCRIPT_MODULE_WITH_NEW_COLUMNS,
106+
false,
107+
)
108+
.unwrap();
109+
110+
test.call("touch_targetable_properties", &["place_123"]).unwrap();
111+
test.call("touch_has_photos_index", &["false"]).unwrap();
112+
}

0 commit comments

Comments
 (0)