From 86b0a1afc9790df96ae8a7b7848badba2aac11b5 Mon Sep 17 00:00:00 2001 From: mAAdhaTTah Date: Sat, 28 Oct 2023 09:30:51 -0400 Subject: [PATCH] Add migrations with surrealdb-migrations Break up the schema.sql file into schemas and run them on app startup. --- Cargo.lock | 420 ++++++++++++++++++++++++++- Cargo.toml | 3 +- db/schema.sql | 20 -- events/.gitkeep | 0 migrations/definitions/_initial.json | 1 + schemas/link.surql | 8 + schemas/script_migration.surql | 7 + schemas/token.surql | 8 + schemas/user.surql | 5 + src/main.rs | 6 + tests/api_tests.rs | 11 +- 11 files changed, 450 insertions(+), 39 deletions(-) delete mode 100644 db/schema.sql create mode 100644 events/.gitkeep create mode 100644 migrations/definitions/_initial.json create mode 100644 schemas/link.surql create mode 100644 schemas/script_migration.surql create mode 100644 schemas/token.surql create mode 100644 schemas/user.surql diff --git a/Cargo.lock b/Cargo.lock index 80cc73a..42fad7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "any_ascii" version = "0.3.2" @@ -237,6 +285,17 @@ dependencies = [ "critical-section", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -552,7 +611,7 @@ checksum = "78feec9b7820c34adc75e873c1fc0edb74921d8e519aa638290f212b2d04cb4a" dependencies = [ "cedar-policy-core", "cedar-policy-validator", - "itertools", + "itertools 0.10.5", "lalrpop-util", "ref-cast", "serde", @@ -569,7 +628,7 @@ checksum = "488935107bb2a3db66fcbbb83952c838e5b0b48efe9de72c1e9fdc5ecac31d6e" dependencies = [ "either", "ipnet", - "itertools", + "itertools 0.10.5", "lalrpop", "lalrpop-util", "lazy_static", @@ -591,7 +650,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2751580a3b9297906e8f685ce5a46f391f5bf0a0b09e8a7ef801056b9c68748c" dependencies = [ "cedar-policy-core", - "itertools", + "itertools 0.10.5", "serde", "serde_json", "serde_with", @@ -622,6 +681,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "chrono-human-duration" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19283df3a144dfdeee6568e42ad7f0939d3c261bcdfe954b1a1098f6f7c1b908" +dependencies = [ + "chrono", +] + [[package]] name = "cipher" version = "0.4.4" @@ -632,6 +700,114 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_derive 3.2.25", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" +dependencies = [ + "clap_builder", + "clap_derive 4.4.7", +] + +[[package]] +name = "clap_builder" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" +dependencies = [ + "anstream", + "anstyle", + "clap_lex 0.6.0", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "cli-table" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adfbb116d9e2c4be7011360d0c0bee565712c11e969c9609b25b619366dc379d" +dependencies = [ + "cli-table-derive", + "csv", + "termcolor", + "unicode-width", +] + +[[package]] +name = "cli-table-derive" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af3bfb9da627b0a6c467624fb7963921433774ed435493b5c08a3053e829ad4" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "concurrent-queue" version = "2.3.0" @@ -653,7 +829,7 @@ dependencies = [ "nom", "pathdiff", "ron", - "rust-ini", + "rust-ini 0.18.0", "serde", "serde_json", "toml", @@ -666,6 +842,35 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" +[[package]] +name = "const-random" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11df32a13d7892ec42d51d3d175faba5211ffe13ed25d4fb348ac9e9ce835593" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom", + "once_cell", + "tiny-keccak", +] + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -732,6 +937,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" +dependencies = [ + "memchr", +] + [[package]] name = "darling" version = "0.20.3" @@ -812,6 +1038,15 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "diffy" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e616e59155c92257e84970156f506287853355f58cd4a6eb167385722c32b790" +dependencies = [ + "nu-ansi-term", +] + [[package]] name = "digest" version = "0.10.7" @@ -851,6 +1086,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +[[package]] +name = "dlv-list" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] + [[package]] name = "dmp" version = "0.2.0" @@ -872,7 +1116,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0812b44697951d35fde8fcb0da81c9de7e809e825a66bbf1ecb79d9829d4ca3d" dependencies = [ - "itertools", + "itertools 0.10.5", "num-traits", ] @@ -1001,6 +1245,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "fst" version = "0.4.7" @@ -1300,6 +1550,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.3" @@ -1485,6 +1744,25 @@ dependencies = [ "bitmaps", ] +[[package]] +name = "include_dir" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -1528,7 +1806,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.3", "rustix", "windows-sys", ] @@ -1542,6 +1820,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1593,7 +1880,7 @@ dependencies = [ "diff", "ena", "is-terminal", - "itertools", + "itertools 0.10.5", "lalrpop-util", "petgraph", "pico-args", @@ -1701,6 +1988,7 @@ dependencies = [ "sha2", "strum_macros", "surrealdb", + "surrealdb-migrations", "tokio", "tower-http", "tracing", @@ -1838,6 +2126,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "names" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bddcd3bf5144b6392de80e04c347cd7fab2508f6df16a85fc496ecd5cec39bc" +dependencies = [ + "clap 3.2.25", + "rand", +] + [[package]] name = "nanoid" version = "0.4.0" @@ -1974,7 +2272,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.3", "libc", ] @@ -2174,10 +2472,26 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" dependencies = [ - "dlv-list", + "dlv-list 0.3.0", "hashbrown 0.12.3", ] +[[package]] +name = "ordered-multimap" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e" +dependencies = [ + "dlv-list 0.5.2", + "hashbrown 0.13.2", +] + +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "overload" version = "0.1.1" @@ -2486,7 +2800,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", @@ -2927,7 +3241,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" dependencies = [ "cfg-if", - "ordered-multimap", + "ordered-multimap 0.4.3", +] + +[[package]] +name = "rust-ini" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091" +dependencies = [ + "cfg-if", + "ordered-multimap 0.6.0", ] [[package]] @@ -3404,6 +3728,15 @@ dependencies = [ "der", ] +[[package]] +name = "sqlparser" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0272b7bb0a225320170c99901b4b5fb3a4384e255a7f2cc228f61e2ba3893e75" +dependencies = [ + "log", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -3476,7 +3809,8 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "surrealdb" version = "1.0.0" -source = "git+https://github.com/surrealdb/surrealdb.git?tag=v1.0.0#54aedcdbdc1c50a55dfaec8f206c4c715ad3cb40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46fb62fbf4b5f0f28c52e919c7a0f5eb4aa4cd6b92b1e25f2e71a7f2d9f92524" dependencies = [ "addr", "any_ascii", @@ -3573,6 +3907,32 @@ dependencies = [ "simple_asn1", ] +[[package]] +name = "surrealdb-migrations" +version = "1.0.0-preview.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c65428fe850374149714beacba587be1bd8205fef5353b280464fb2e4c8d76a" +dependencies = [ + "anyhow", + "chrono", + "chrono-human-duration", + "clap 4.4.7", + "cli-table", + "convert_case", + "diffy", + "fs_extra", + "include_dir", + "itertools 0.11.0", + "names", + "regex", + "rust-ini 0.19.0", + "serde", + "serde_json", + "sqlparser", + "surrealdb", + "tokio", +] + [[package]] name = "syn" version = "1.0.109" @@ -3652,6 +4012,21 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "thiserror" version = "1.0.49" @@ -4181,6 +4556,12 @@ dependencies = [ "unicode-script", ] +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "unicode-width" version = "0.1.11" @@ -4228,6 +4609,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.4.1" @@ -4416,6 +4803,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index b63b709..77ecd96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ serde-aux = "4" serde_json = "1" serde_with = "3" # DB -surrealdb = { git = "https://github.com/surrealdb/surrealdb.git", tag = "v1.0.0", features = [ +surrealdb = { version = "1.0.0", features = [ "protocol-http", "kv-mem", ] } @@ -62,6 +62,7 @@ jsonwebtoken = "8.3.0" lazy-regex = "2" strum_macros = "0.24" uuid = "1.3.3" +surrealdb-migrations = "1.0.0-preview.1" [dev-dependencies] reqwest = "0.11.18" diff --git a/db/schema.sql b/db/schema.sql deleted file mode 100644 index 4ed8894..0000000 --- a/db/schema.sql +++ /dev/null @@ -1,20 +0,0 @@ -DEFINE TABLE user SCHEMAFULL; -DEFINE FIELD username ON TABLE user TYPE string; -DEFINE FIELD password ON TABLE user TYPE string; -DEFINE INDEX idx_username ON TABLE user COLUMNS username UNIQUE; - -DEFINE TABLE token SCHEMAFULL; -DEFINE FIELD token_hash ON TABLE token TYPE string; -DEFINE FIELD name ON TABLE token TYPE string; -DEFINE FIELD short_token ON TABLE token TYPE string; -DEFINE FIELD user ON TABLE token TYPE record (user); -DEFINE INDEX idx_hash ON TABLE token COLUMNS token_hash UNIQUE; -DEFINE INDEX idx_user ON TABLE token COLUMNS user; - -DEFINE TABLE link SCHEMAFULL; -DEFINE FIELD url ON TABLE link TYPE string; -DEFINE FIELD title ON TABLE link TYPE string; -DEFINE FIELD note ON TABLE link TYPE string; -DEFINE FIELD user ON TABLE link TYPE record (user); -DEFINE FIELD bookmarked_at ON TABLE link TYPE datetime DEFAULT time::now(); -DEFINE INDEX idx_user ON TABLE link COLUMNS user; diff --git a/events/.gitkeep b/events/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/definitions/_initial.json b/migrations/definitions/_initial.json new file mode 100644 index 0000000..150a2e3 --- /dev/null +++ b/migrations/definitions/_initial.json @@ -0,0 +1 @@ +{"schemas":"DEFINE TABLE link SCHEMAFULL;\n\nDEFINE FIELD url ON TABLE link TYPE string;\nDEFINE FIELD title ON TABLE link TYPE string;\nDEFINE FIELD note ON TABLE link TYPE string;\nDEFINE FIELD user ON TABLE link TYPE record (user);\nDEFINE FIELD bookmarked_at ON TABLE link TYPE datetime DEFAULT time::now();\nDEFINE INDEX idx_user ON TABLE link COLUMNS user;\n\nDEFINE TABLE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD script_name ON script_migration TYPE string;\nDEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now();\n\nDEFINE TABLE token SCHEMAFULL;\n\nDEFINE FIELD token_hash ON TABLE token TYPE string;\nDEFINE FIELD name ON TABLE token TYPE string;\nDEFINE FIELD short_token ON TABLE token TYPE string;\nDEFINE FIELD user ON TABLE token TYPE record (user);\nDEFINE INDEX idx_hash ON TABLE token COLUMNS token_hash UNIQUE;\nDEFINE INDEX idx_user ON TABLE token COLUMNS user;\n\nDEFINE TABLE user SCHEMAFULL;\n\nDEFINE FIELD username ON TABLE user TYPE string;\nDEFINE FIELD password ON TABLE user TYPE string;\nDEFINE INDEX idx_username ON TABLE user COLUMNS username UNIQUE;\n","events":""} \ No newline at end of file diff --git a/schemas/link.surql b/schemas/link.surql new file mode 100644 index 0000000..10ded61 --- /dev/null +++ b/schemas/link.surql @@ -0,0 +1,8 @@ +DEFINE TABLE link SCHEMAFULL; + +DEFINE FIELD url ON TABLE link TYPE string; +DEFINE FIELD title ON TABLE link TYPE string; +DEFINE FIELD note ON TABLE link TYPE string; +DEFINE FIELD user ON TABLE link TYPE record (user); +DEFINE FIELD bookmarked_at ON TABLE link TYPE datetime DEFAULT time::now(); +DEFINE INDEX idx_user ON TABLE link COLUMNS user; diff --git a/schemas/script_migration.surql b/schemas/script_migration.surql new file mode 100644 index 0000000..90a1774 --- /dev/null +++ b/schemas/script_migration.surql @@ -0,0 +1,7 @@ +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(); diff --git a/schemas/token.surql b/schemas/token.surql new file mode 100644 index 0000000..8f9e8c0 --- /dev/null +++ b/schemas/token.surql @@ -0,0 +1,8 @@ +DEFINE TABLE token SCHEMAFULL; + +DEFINE FIELD token_hash ON TABLE token TYPE string; +DEFINE FIELD name ON TABLE token TYPE string; +DEFINE FIELD short_token ON TABLE token TYPE string; +DEFINE FIELD user ON TABLE token TYPE record (user); +DEFINE INDEX idx_hash ON TABLE token COLUMNS token_hash UNIQUE; +DEFINE INDEX idx_user ON TABLE token COLUMNS user; diff --git a/schemas/user.surql b/schemas/user.surql new file mode 100644 index 0000000..66b7eaa --- /dev/null +++ b/schemas/user.surql @@ -0,0 +1,5 @@ +DEFINE TABLE user SCHEMAFULL; + +DEFINE FIELD username ON TABLE user TYPE string; +DEFINE FIELD password ON TABLE user TYPE string; +DEFINE INDEX idx_username ON TABLE user COLUMNS username UNIQUE; diff --git a/src/main.rs b/src/main.rs index 44aef6c..d5e3659 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use linkstowr::{ types::AppState, }; use surrealdb::{engine::any::Any, opt::auth::Root, Surreal}; +use surrealdb_migrations::MigrationRunner; use tracing::info; #[tokio::main] @@ -65,6 +66,11 @@ async fn get_db(configuration: &Settings) -> Surreal { .await .unwrap(); + MigrationRunner::new(&db) + .up() + .await + .expect("Failed to apply migrations"); + info!( "Connected to namespace: {}, database: {}", &configuration.database.ns, &configuration.database.db diff --git a/tests/api_tests.rs b/tests/api_tests.rs index 6255a31..67a6045 100644 --- a/tests/api_tests.rs +++ b/tests/api_tests.rs @@ -1,4 +1,4 @@ -use std::{fs, net::TcpListener}; +use std::net::TcpListener; use linkstowr::{ app::get_app, @@ -13,6 +13,7 @@ use linkstowr::{ }; use serde_json::{json, Value}; use surrealdb::sql::thing; +use surrealdb_migrations::MigrationRunner; use uuid::Uuid; const TEST_USER_PASSWORD: &str = "password"; @@ -30,12 +31,10 @@ async fn spawn_app() -> TestApp { db.use_ns("test").use_db("test").await.unwrap(); // Initialize schema - let schema = - fs::read_to_string("db/schema.sql").expect("Failed to read schema file from db/schema.sql"); - - db.query(schema) + MigrationRunner::new(&db) + .up() .await - .expect("Failed to initialize the DB schema"); + .expect("Failed to apply migrations"); // Setup env var for JWT std::env::set_var("JWT_ENCODING_SECRET", JWT_ENCODING_SECRET);