diff --git a/.cargo/config.toml b/.cargo/config.toml index c91c3f3..4714cce 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,5 @@ +[build] +rustflags = ["--cfg", "surrealdb_unstable"] + [net] git-fetch-with-cli = true diff --git a/Cargo.toml b/Cargo.toml index 29d89dd..c9d773b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ rust-ini = "0.20.0" serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" sqlparser = "0.43.1" -surrealdb = { version = "1.2.0", features = ["protocol-http"] } +surrealdb = { version = "1.2.0", features = ["protocol-http", "sql2"] } tokio = { version = "1.36.0", features = ["macros"] } [dev-dependencies] diff --git a/templates/blog/events/publish_post.surql b/templates/blog/events/publish_post.surql index dccbe34..aac6610 100644 --- a/templates/blog/events/publish_post.surql +++ b/templates/blog/events/publish_post.surql @@ -4,7 +4,7 @@ DEFINE TABLE publish_post SCHEMALESS FOR update, delete NONE; DEFINE FIELD post_id ON publish_post TYPE record; -DEFINE FIELD created_at ON publish_post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON publish_post TYPE datetime VALUE time::now() READONLY; DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( UPDATE post SET status = "PUBLISHED" WHERE id = $after.post_id diff --git a/templates/blog/events/unpublish_post.surql b/templates/blog/events/unpublish_post.surql index 3a37ed9..180253e 100644 --- a/templates/blog/events/unpublish_post.surql +++ b/templates/blog/events/unpublish_post.surql @@ -4,7 +4,7 @@ DEFINE TABLE unpublish_post SCHEMALESS FOR update, delete NONE; DEFINE FIELD post_id ON unpublish_post TYPE record; -DEFINE FIELD created_at ON unpublish_post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON unpublish_post TYPE datetime VALUE time::now() READONLY; DEFINE EVENT unpublish_post ON TABLE unpublish_post WHEN $event == "CREATE" THEN ( UPDATE post SET status = "DRAFT" WHERE id = $after.post_id diff --git a/templates/blog/schemas/comment.surql b/templates/blog/schemas/comment.surql index c28d778..7634ab7 100644 --- a/templates/blog/schemas/comment.surql +++ b/templates/blog/schemas/comment.surql @@ -7,4 +7,4 @@ DEFINE TABLE comment SCHEMALESS FOR update, delete WHERE in = $auth.id; DEFINE FIELD content ON comment TYPE string; -DEFINE FIELD created_at ON comment TYPE datetime DEFAULT time::now(); \ No newline at end of file +DEFINE FIELD created_at ON comment TYPE datetime VALUE time::now() READONLY; \ No newline at end of file diff --git a/templates/blog/schemas/permission.surql b/templates/blog/schemas/permission.surql index 108d124..36c52e0 100644 --- a/templates/blog/schemas/permission.surql +++ b/templates/blog/schemas/permission.surql @@ -4,6 +4,6 @@ DEFINE TABLE permission SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD name ON permission TYPE string; -DEFINE FIELD created_at ON permission TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON permission TYPE datetime VALUE time::now() READONLY; DEFINE INDEX unique_name ON permission COLUMNS name UNIQUE; \ No newline at end of file diff --git a/templates/blog/schemas/post.surql b/templates/blog/schemas/post.surql index 7d9fe76..48e3c70 100644 --- a/templates/blog/schemas/post.surql +++ b/templates/blog/schemas/post.surql @@ -7,5 +7,5 @@ DEFINE TABLE post SCHEMALESS DEFINE FIELD title ON post TYPE string; DEFINE FIELD content ON post TYPE string; DEFINE FIELD author ON post TYPE record; -DEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON post TYPE datetime VALUE time::now() READONLY; DEFINE FIELD status ON post TYPE string DEFAULT 'DRAFT' ASSERT $value IN ['DRAFT', 'PUBLISHED']; \ No newline at end of file diff --git a/templates/blog/schemas/script_migration.surql b/templates/blog/schemas/script_migration.surql index 90eb7a2..65e455b 100644 --- a/templates/blog/schemas/script_migration.surql +++ b/templates/blog/schemas/script_migration.surql @@ -4,4 +4,4 @@ DEFINE TABLE script_migration SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD script_name ON script_migration TYPE string; -DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now(); \ No newline at end of file +DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; \ No newline at end of file diff --git a/templates/blog/schemas/user.surql b/templates/blog/schemas/user.surql index 79fbed5..c986fec 100644 --- a/templates/blog/schemas/user.surql +++ b/templates/blog/schemas/user.surql @@ -7,7 +7,7 @@ DEFINE TABLE user SCHEMAFULL DEFINE FIELD username ON user TYPE string; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); DEFINE FIELD password ON user TYPE string; -DEFINE FIELD registered_at ON user TYPE datetime DEFAULT time::now(); +DEFINE FIELD registered_at ON user TYPE datetime VALUE time::now() READONLY; DEFINE FIELD avatar ON user TYPE option; DEFINE FIELD permissions ON user TYPE array> diff --git a/templates/ecommerce/schemas/customer.surql b/templates/ecommerce/schemas/customer.surql index 275b8bb..6091ad5 100644 --- a/templates/ecommerce/schemas/customer.surql +++ b/templates/ecommerce/schemas/customer.surql @@ -5,7 +5,7 @@ DEFINE TABLE customer SCHEMALESS FOR create, delete NONE; DEFINE FIELD name ON customer TYPE string; -DEFINE FIELD email ON customer TYPE string; +DEFINE FIELD email ON customer TYPE string ASSERT string::is::email($value); DEFINE FIELD password ON customer TYPE string; DEFINE FIELD addresses ON customer TYPE array>; diff --git a/templates/ecommerce/schemas/purchases.surql b/templates/ecommerce/schemas/purchases.surql index ee83e69..e98ec2f 100644 --- a/templates/ecommerce/schemas/purchases.surql +++ b/templates/ecommerce/schemas/purchases.surql @@ -7,7 +7,7 @@ DEFINE TABLE purchases SCHEMALESS DEFINE FIELD quantity ON purchases TYPE number; DEFINE FIELD shipping_address ON purchases TYPE option>; -DEFINE FIELD created_at ON purchases TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON purchases TYPE datetime VALUE time::now() READONLY; DEFINE FIELD shipped_at ON purchases TYPE option; DEFINE FIELD total ON purchases TYPE number; DEFINE FIELD status ON purchases TYPE string DEFAULT 'Pending' ASSERT $value IN ['Pending', 'Delivered']; \ No newline at end of file diff --git a/templates/ecommerce/schemas/script_migration.surql b/templates/ecommerce/schemas/script_migration.surql index 90eb7a2..65e455b 100644 --- a/templates/ecommerce/schemas/script_migration.surql +++ b/templates/ecommerce/schemas/script_migration.surql @@ -4,4 +4,4 @@ DEFINE TABLE script_migration SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD script_name ON script_migration TYPE string; -DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now(); \ No newline at end of file +DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; \ No newline at end of file diff --git a/templates/empty/schemas/script_migration.surql b/templates/empty/schemas/script_migration.surql index 90eb7a2..65e455b 100644 --- a/templates/empty/schemas/script_migration.surql +++ b/templates/empty/schemas/script_migration.surql @@ -4,4 +4,4 @@ DEFINE TABLE script_migration SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD script_name ON script_migration TYPE string; -DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now(); \ No newline at end of file +DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; \ No newline at end of file diff --git a/tests/cli/apply/e2e.rs b/tests/cli/apply/e2e.rs index 82d0863..1244196 100644 --- a/tests/cli/apply/e2e.rs +++ b/tests/cli/apply/e2e.rs @@ -637,14 +637,14 @@ DEFINE TABLE comment SCHEMALESS FOR update, delete WHERE in = $auth.id; DEFINE FIELD content ON comment TYPE string; -DEFINE FIELD created_at ON comment TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON comment TYPE datetime VALUE time::now() READONLY; DEFINE TABLE permission SCHEMAFULL PERMISSIONS FOR select FULL FOR create, update, delete NONE; DEFINE FIELD name ON permission TYPE string; -DEFINE FIELD created_at ON permission TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON permission TYPE datetime VALUE time::now() READONLY; DEFINE INDEX unique_name ON permission COLUMNS name UNIQUE; DEFINE TABLE post SCHEMALESS @@ -656,7 +656,7 @@ DEFINE TABLE post SCHEMALESS DEFINE FIELD title ON post TYPE string; DEFINE FIELD content ON post TYPE string; DEFINE FIELD author ON post TYPE record; -DEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON post TYPE datetime VALUE time::now() READONLY; DEFINE FIELD status ON post TYPE string DEFAULT 'DRAFT' ASSERT $value IN ['DRAFT', 'PUBLISHED']; DEFINE TABLE script_migration SCHEMAFULL PERMISSIONS @@ -664,7 +664,7 @@ DEFINE TABLE script_migration SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD script_name ON script_migration TYPE string; -DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now(); +DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; DEFINE TABLE user SCHEMAFULL PERMISSIONS FOR select FULL @@ -674,7 +674,7 @@ DEFINE TABLE user SCHEMAFULL DEFINE FIELD username ON user TYPE string; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); DEFINE FIELD password ON user TYPE string; -DEFINE FIELD registered_at ON user TYPE datetime DEFAULT time::now(); +DEFINE FIELD registered_at ON user TYPE datetime VALUE time::now() READONLY; DEFINE FIELD avatar ON user TYPE option; DEFINE FIELD permissions ON user TYPE array> @@ -705,7 +705,7 @@ const INITIAL_DEFINITION_EVENTS: &str = "DEFINE TABLE publish_post SCHEMALESS FOR update, delete NONE; DEFINE FIELD post_id ON publish_post TYPE record; -DEFINE FIELD created_at ON publish_post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON publish_post TYPE datetime VALUE time::now() READONLY; DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( UPDATE post SET status = \"PUBLISHED\" WHERE id = $after.post_id @@ -716,7 +716,7 @@ DEFINE TABLE unpublish_post SCHEMALESS FOR update, delete NONE; DEFINE FIELD post_id ON unpublish_post TYPE record; -DEFINE FIELD created_at ON unpublish_post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON unpublish_post TYPE datetime VALUE time::now() READONLY; DEFINE EVENT unpublish_post ON TABLE unpublish_post WHEN $event == \"CREATE\" THEN ( UPDATE post SET status = \"DRAFT\" WHERE id = $after.post_id @@ -728,7 +728,7 @@ const SECOND_MIGRATION_SCHEMAS: &str = "--- original +DEFINE TABLE category SCHEMALESS; + +DEFINE FIELD name ON category TYPE string; -+DEFINE FIELD created_at ON category TYPE datetime DEFAULT time::now(); ++DEFINE FIELD created_at ON category TYPE datetime VALUE time::now() READONLY; # in: user # out: post, comment DEFINE TABLE comment SCHEMALESS\n"; @@ -741,7 +741,7 @@ const THIRD_MIGRATION_SCHEMAS: &str = "--- original +DEFINE FIELD name ON archive TYPE string; +DEFINE FIELD from_date ON archive TYPE datetime; +DEFINE FIELD to_date ON archive TYPE datetime; -+DEFINE FIELD created_at ON archive TYPE datetime DEFAULT time::now(); ++DEFINE FIELD created_at ON archive TYPE datetime VALUE time::now() READONLY; DEFINE TABLE category SCHEMALESS; DEFINE FIELD name ON category TYPE string;\n"; diff --git a/tests/cli/branch/diff.rs b/tests/cli/branch/diff.rs index 6e7ab8b..8df262a 100644 --- a/tests/cli/branch/diff.rs +++ b/tests/cli/branch/diff.rs @@ -61,7 +61,7 @@ async fn diff_with_changes() -> Result<()> { ## category ## DEFINE TABLE category SCHEMALESS PERMISSIONS NONE -DEFINE FIELD created_at ON category TYPE datetime DEFAULT time::now() PERMISSIONS FULL +DEFINE FIELD created_at ON category TYPE datetime READONLY VALUE time::now() PERMISSIONS FULL DEFINE FIELD name ON category TYPE string PERMISSIONS FULL\n", ) })?; diff --git a/tests/cli/definitions.rs b/tests/cli/definitions.rs index 24f9acf..8c859f7 100644 --- a/tests/cli/definitions.rs +++ b/tests/cli/definitions.rs @@ -187,14 +187,14 @@ DEFINE TABLE comment SCHEMALESS FOR update, delete WHERE in = $auth.id; DEFINE FIELD content ON comment TYPE string; -DEFINE FIELD created_at ON comment TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON comment TYPE datetime VALUE time::now() READONLY; DEFINE TABLE permission SCHEMAFULL PERMISSIONS FOR select FULL FOR create, update, delete NONE; DEFINE FIELD name ON permission TYPE string; -DEFINE FIELD created_at ON permission TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON permission TYPE datetime VALUE time::now() READONLY; DEFINE INDEX unique_name ON permission COLUMNS name UNIQUE; DEFINE TABLE post SCHEMALESS @@ -206,7 +206,7 @@ DEFINE TABLE post SCHEMALESS DEFINE FIELD title ON post TYPE string; DEFINE FIELD content ON post TYPE string; DEFINE FIELD author ON post TYPE record; -DEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now(); +DEFINE FIELD created_at ON post TYPE datetime VALUE time::now() READONLY; DEFINE FIELD status ON post TYPE string DEFAULT 'DRAFT' ASSERT $value IN ['DRAFT', 'PUBLISHED']; DEFINE TABLE script_migration SCHEMAFULL PERMISSIONS @@ -214,7 +214,7 @@ DEFINE TABLE script_migration SCHEMAFULL FOR create, update, delete NONE; DEFINE FIELD script_name ON script_migration TYPE string; -DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now(); +DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; DEFINE TABLE user SCHEMAFULL PERMISSIONS FOR select FULL @@ -224,7 +224,7 @@ DEFINE TABLE user SCHEMAFULL DEFINE FIELD username ON user TYPE string; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); DEFINE FIELD password ON user TYPE string; -DEFINE FIELD registered_at ON user TYPE datetime DEFAULT time::now(); +DEFINE FIELD registered_at ON user TYPE datetime VALUE time::now() READONLY; DEFINE FIELD avatar ON user TYPE option; DEFINE FIELD permissions ON user TYPE array> diff --git a/tests/helpers/io.rs b/tests/helpers/io.rs index d7f60d7..7afb8e2 100644 --- a/tests/helpers/io.rs +++ b/tests/helpers/io.rs @@ -202,7 +202,7 @@ pub fn add_category_schema_file(path: &Path) -> Result<()> { const CONTENT: &str = "DEFINE TABLE category SCHEMALESS; DEFINE FIELD name ON category TYPE string; -DEFINE FIELD created_at ON category TYPE datetime DEFAULT time::now();"; +DEFINE FIELD created_at ON category TYPE datetime VALUE time::now() READONLY;"; fs::write(schema_file, CONTENT)?; } @@ -262,7 +262,7 @@ pub fn add_archive_schema_file(path: &Path) -> Result<()> { DEFINE FIELD name ON archive TYPE string; DEFINE FIELD from_date ON archive TYPE datetime; DEFINE FIELD to_date ON archive TYPE datetime; -DEFINE FIELD created_at ON archive TYPE datetime DEFAULT time::now();"; +DEFINE FIELD created_at ON archive TYPE datetime VALUE time::now() READONLY;"; fs::write(schema_file, CONTENT)?; }