Skip to content

Commit

Permalink
bootstrapped surreal migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Jan 31, 2024
1 parent 7abb5af commit d5ed75e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
use flake "./engine"
dotenv engine/.env
2 changes: 1 addition & 1 deletion engine/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
cargoArtifacts = engine_deps;
});

surreal_deps = [ pkgs.surrealdb ];
surreal_deps = with pkgs; [ surrealdb surrealdb-migrations ];
rust_dev_deps = [ pkgs.bacon ];
in {
defaultPackage = engine;
Expand Down
1 change: 1 addition & 0 deletions engine/migrations/migrations/definitions/_initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemas":"DEFINE 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();\nDEFINE TABLE user SCHEMALESS\n PERMISSIONS\n FOR select FULL\n FOR update WHERE id = $auth.id\n FOR create, delete NONE;\n\nDEFINE FIELD name ON user TYPE string;\nDEFINE FIELD email ON user TYPE string;\nDEFINE FIELD password ON user TYPE string;\n\nDEFINE INDEX unique_email ON user COLUMNS email UNIQUE;\n\nDEFINE SCOPE user_scope\n SESSION 30d\n SIGNUP (\n CREATE user\n SET\n name = $name,\n email = $email,\n password = crypto::argon2::generate($password)\n )\n SIGNIN (\n SELECT *\n FROM user\n WHERE email = $email AND crypto::argon2::compare(password, $password)\n );\n","events":""}
7 changes: 7 additions & 0 deletions engine/migrations/schemas/script_migration.surql
Original file line number Diff line number Diff line change
@@ -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();
26 changes: 26 additions & 0 deletions engine/migrations/schemas/user.surrealql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
DEFINE TABLE user SCHEMALESS
PERMISSIONS
FOR select FULL
FOR update WHERE id = $auth.id
FOR create, delete NONE;

DEFINE FIELD name ON user TYPE string;
DEFINE FIELD email ON user TYPE string;
DEFINE FIELD password ON user TYPE string;

DEFINE INDEX unique_email ON user COLUMNS email UNIQUE;

DEFINE SCOPE user_scope
SESSION 30d
SIGNUP (
CREATE user
SET
name = $name,
email = $email,
password = crypto::argon2::generate($password)
)
SIGNIN (
SELECT *
FROM user
WHERE email = $email AND crypto::argon2::compare(password, $password)
);
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ surreal:
mkdir /tmp/surreal_data -p && surreal start file:/tmp/surreal_data --log debug --auth --username=root --password=pass
wipe-surreal:
rm -rf /tmp/surreal_data
apply-surreal:
cd engine/migrations && surrealdb-migrations apply --username $SURREALDB_ROOT_USER --password $SURREALDB_ROOT_PASS

0 comments on commit d5ed75e

Please sign in to comment.