Skip to content

Commit

Permalink
♻️ update templates to use READONLY keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Feb 24, 2024
1 parent addcb50 commit 6801527
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
@@ -1,2 +1,5 @@
[build]
rustflags = ["--cfg", "surrealdb_unstable"]

[net]
git-fetch-with-cli = true
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/events/publish_post.surql
Expand Up @@ -4,7 +4,7 @@ DEFINE TABLE publish_post SCHEMALESS
FOR update, delete NONE;

DEFINE FIELD post_id ON publish_post TYPE record<post>;
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
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/events/unpublish_post.surql
Expand Up @@ -4,7 +4,7 @@ DEFINE TABLE unpublish_post SCHEMALESS
FOR update, delete NONE;

DEFINE FIELD post_id ON unpublish_post TYPE record<post>;
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
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/schemas/comment.surql
Expand Up @@ -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();
DEFINE FIELD created_at ON comment TYPE datetime VALUE time::now() READONLY;
2 changes: 1 addition & 1 deletion templates/blog/schemas/permission.surql
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion templates/blog/schemas/post.surql
Expand Up @@ -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<user>;
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'];
2 changes: 1 addition & 1 deletion templates/blog/schemas/script_migration.surql
Expand Up @@ -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();
DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;
2 changes: 1 addition & 1 deletion templates/blog/schemas/user.surql
Expand Up @@ -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<string>;

DEFINE FIELD permissions ON user TYPE array<record<permission>>
Expand Down
2 changes: 1 addition & 1 deletion templates/ecommerce/schemas/customer.surql
Expand Up @@ -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<record<address>>;

Expand Down
2 changes: 1 addition & 1 deletion templates/ecommerce/schemas/purchases.surql
Expand Up @@ -7,7 +7,7 @@ DEFINE TABLE purchases SCHEMALESS

DEFINE FIELD quantity ON purchases TYPE number;
DEFINE FIELD shipping_address ON purchases TYPE option<record<address>>;
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<datetime>;
DEFINE FIELD total ON purchases TYPE number;
DEFINE FIELD status ON purchases TYPE string DEFAULT 'Pending' ASSERT $value IN ['Pending', 'Delivered'];
2 changes: 1 addition & 1 deletion templates/ecommerce/schemas/script_migration.surql
Expand Up @@ -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();
DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;
2 changes: 1 addition & 1 deletion templates/empty/schemas/script_migration.surql
Expand Up @@ -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();
DEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;
18 changes: 9 additions & 9 deletions tests/cli/apply/e2e.rs
Expand Up @@ -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
Expand All @@ -656,15 +656,15 @@ 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<user>;
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
FOR select FULL
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
Expand All @@ -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<string>;
DEFINE FIELD permissions ON user TYPE array<record<permission>>
Expand Down Expand Up @@ -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<post>;
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
Expand All @@ -716,7 +716,7 @@ DEFINE TABLE unpublish_post SCHEMALESS
FOR update, delete NONE;
DEFINE FIELD post_id ON unpublish_post TYPE record<post>;
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
Expand All @@ -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";
Expand All @@ -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";
2 changes: 1 addition & 1 deletion tests/cli/branch/diff.rs
Expand Up @@ -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",
)
})?;
Expand Down
10 changes: 5 additions & 5 deletions tests/cli/definitions.rs
Expand Up @@ -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
Expand All @@ -206,15 +206,15 @@ 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<user>;
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
FOR select FULL
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
Expand All @@ -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<string>;
DEFINE FIELD permissions ON user TYPE array<record<permission>>
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/io.rs
Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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)?;
}
Expand Down

0 comments on commit 6801527

Please sign in to comment.