Skip to content

Commit

Permalink
Add report verification in auto migration smoketest
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Dec 31, 2024
1 parent dfed1f8 commit c6220cb
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 5 deletions.
1 change: 1 addition & 0 deletions smoketests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def publish_module(self, domain=None, *, clear=True, capture_stderr=True):
)
self.resolved_identity = re.search(r"identity: ([0-9a-fA-F]+)", publish_output)[1]
self.database_identity = domain if domain is not None else self.resolved_identity
return publish_output

@classmethod
def reset_config(cls):
Expand Down
145 changes: 140 additions & 5 deletions smoketests/tests/auto_migration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
from .. import Smoketest
import sys
import logging
import re

# 7-bit C1 ANSI sequences
ansi_escape = re.compile(r'''
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
''', re.VERBOSE)

def strip_ansi_escape_codes(text: str) -> str:
return ansi_escape.sub('', text)


class AddTableAutoMigration(Smoketest):
Expand All @@ -25,7 +42,11 @@ class AddTableAutoMigration(Smoketest):
}
#[spacetimedb::table(name = point_mass)]
#[index(name = point_masses_by_mass, btree(columns = position))]
pub struct PointMass {
#[primary_key]
#[auto_inc]
id: u64,
mass: f64,
/// This used to cause an error when check_compatible did not resolve types in a `ModuleDef`.
position: Vector2,
Expand All @@ -37,12 +58,82 @@ class AddTableAutoMigration(Smoketest):
y: f64,
}
#[spacetimedb::table(name = scheduled_table, scheduled(send_scheduled_message), public)]
pub struct ScheduledTable {
#[primary_key]
#[auto_inc]
scheduled_id: u64,
#[scheduled_at]
scheduled_at: spacetimedb::ScheduleAt,
text: String,
}
#[spacetimedb::reducer]
fn send_scheduled_message(_ctx: &ReducerContext, arg: ScheduledTable) {
let _ = arg.text;
let _ = arg.scheduled_at;
let _ = arg.scheduled_id;
}
spacetimedb::filter!("SELECT * FROM person");
"""

MODULE_CODE_UPDATED = (
MODULE_CODE
+ """
MODULE_CODE_UPDATED = """
use spacetimedb::{log, ReducerContext, Table, SpacetimeType};
#[spacetimedb::table(name = person)]
pub struct Person {
name: String,
}
#[spacetimedb::reducer]
pub fn add_person(ctx: &ReducerContext, name: String) {
ctx.db.person().insert(Person { name });
}
#[spacetimedb::reducer]
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
for person in ctx.db.person().iter() {
log::info!("{}: {}", prefix, person.name);
}
}
#[spacetimedb::table(name = point_mass, public)] // private -> public
// remove index
pub struct PointMass {
// remove primary_key and auto_inc
id: u64,
mass: f64,
/// This used to cause an error when check_compatible did not resolve types in a `ModuleDef`.
position: Vector2,
}
#[derive(SpacetimeType, Clone, Copy)]
pub struct Vector2 {
x: f64,
y: f64,
}
// TODO: once removing schedules is implemented, remove the schedule here.
#[spacetimedb::table(name = scheduled_table, scheduled(send_scheduled_message), public)]
pub struct ScheduledTable {
#[primary_key]
#[auto_inc]
scheduled_id: u64,
#[scheduled_at]
scheduled_at: spacetimedb::ScheduleAt,
text: String,
}
#[spacetimedb::reducer]
fn send_scheduled_message(_ctx: &ReducerContext, arg: ScheduledTable) {
let _ = arg.text;
let _ = arg.scheduled_at;
let _ = arg.scheduled_id;
}
spacetimedb::filter!("SELECT * FROM person");
#[spacetimedb::table(name = book)]
pub struct Book {
isbn: String,
Expand All @@ -61,8 +152,44 @@ class AddTableAutoMigration(Smoketest):
}
spacetimedb::filter!("SELECT * FROM book");
#[spacetimedb::table(name = parabolas)]
#[index(name = parabolas_by_b_c, btree(columns = [b, c]))]
pub struct Parabola {
#[primary_key]
#[auto_inc]
id: u64,
a: f64,
b: f64,
c: f64,
}
"""
)

EXPECTED_MIGRATION_REPORT = """--------------
Performed automatic migration
--------------
- Removed index `point_mass_id_idx_btree` on columns [`id`] of table `point_mass`
- Removed unique constraint `point_mass_id_key` on columns [`id`] of table `point_mass`
- Removed auto-increment constraint `point_mass_id_seq` on column `id` of table `point_mass`
- Created table: `book` (private)
- Columns:
- `isbn`: String
- Created table: `parabolas` (private)
- Columns:
- `id`: U64
- `a`: F64
- `b`: F64
- `c`: F64
- Unique constraints:
- `parabolas_id_key` on [`id`]
- Indexes:
- `parabolas_id_idx_btree` on [`id`]
- Auto-increment constraints:
- `parabolas_id_seq` on `id`
- Created row level security policy:
`SELECT * FROM book`
- Changed access for table `point_mass` (private -> public)"""


def assertSql(self, sql, expected):
self.maxDiff = None
Expand Down Expand Up @@ -98,7 +225,15 @@ def test_add_table_auto_migration(self):
)

self.write_module_code(self.MODULE_CODE_UPDATED)
self.publish_module(self.database_identity, clear=False)
output = self.publish_module(self.database_identity, clear=False)
output = strip_ansi_escape_codes(output)

print("got output\n", output)

# Remark: if this test ever fails mysteriously,
# try double-checking the pretty printing code for trailing spaces before newlines.
# Also make sure the pretty-printing is deterministic.
self.assertIn(self.EXPECTED_MIGRATION_REPORT, output)

logging.info("Updated")

Expand Down

0 comments on commit c6220cb

Please sign in to comment.