-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add core and space migration packages #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jgpruitt
wants to merge
18
commits into
main
Choose a base branch
from
multiplayer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
efc4dd5
feat: add core and space migration packages
jgpruitt f32cd9b
memory search takes effective tree access as arg
jgpruitt 249f405
most tables modeled
jgpruitt fbdfee1
runtime calculation of tree access
jgpruitt b1b041d
fix database migrations and diagnostics
jgpruitt 1c11069
split core idempotent migrations
jgpruitt fa2597e
docs: clarify principal agent names
jgpruitt 4c8376c
shorten principal kind values
jgpruitt 32ad49e
chore: make provision scripts a separate thing from incrementals
jgpruitt f8d811d
chore: simplify extension creation
jgpruitt 477a29e
fix check script web asset generation
jgpruitt 346121f
chore: format space migration imports
jgpruitt d027c75
run migrations when version is current
jgpruitt 02e38b3
remove core shard migration
jgpruitt 4f95e24
tune group member indexes
jgpruitt 05fdeaa
chore: reduce indexes on core.tree_access
jgpruitt 8605705
cover principal name indexes
jgpruitt b7a396e
simplify principal name indexes
jgpruitt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { type MigrateCoreOptions, migrateCore } from "./migrate/migrate"; | ||
| export { CORE_SCHEMA_VERSION } from "./version"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| -- generic trigger function to update updated_at timestamp | ||
| create or replace function core.update_updated_at() | ||
| returns trigger | ||
| as $func$ | ||
| begin | ||
| new.updated_at = pg_catalog.now(); | ||
| return new; | ||
| end; | ||
| $func$ language plpgsql volatile security definer | ||
| set search_path to core, pg_temp; | ||
|
|
||
| create or replace trigger space_before_update_trg | ||
| before update on core.space | ||
| for each row | ||
| execute function core.update_updated_at(); | ||
|
|
||
| create or replace trigger principal_before_update_trg | ||
| before update on core.principal | ||
| for each row | ||
| execute function core.update_updated_at(); | ||
|
|
||
| create or replace trigger principal_space_before_update_trg | ||
| before update on core.principal_space | ||
| for each row | ||
| execute function core.update_updated_at(); | ||
|
|
||
| create or replace trigger group_member_before_update_trg | ||
| before update on core.group_member | ||
| for each row | ||
| execute function core.update_updated_at(); | ||
|
|
||
| create or replace trigger tree_access_before_update_trg | ||
| before update on core.tree_access | ||
| for each row | ||
| execute function core.update_updated_at(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ------------------------------------------------------------------------------- | ||
| -- is_principal_in_space | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.is_principal_in_space | ||
| ( _principal_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns bool | ||
| as $func$ | ||
| select exists | ||
| ( | ||
| select 1 | ||
| from core.principal_space ps | ||
| where ps.principal_id = _principal_id | ||
| and ps.space_id = _space_id | ||
| ) | ||
| $func$ language sql stable security invoker | ||
| ; | ||
|
|
||
| ------------------------------------------------------------------------------- | ||
| -- is_principal_space_admin | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.is_principal_space_admin | ||
| ( _principal_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns bool | ||
| as $func$ | ||
| select coalesce | ||
| ( | ||
| ( | ||
| select ps.admin and (not p.kind = 'a') -- agents cannot be space admins | ||
| from core.principal_space ps | ||
| inner join core.principal p on (ps.principal_id = p.id) | ||
| where ps.principal_id = _principal_id | ||
| and ps.space_id = _space_id | ||
| ) | ||
| , false | ||
| ) | ||
| $func$ language sql stable security invoker | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| ------------------------------------------------------------------------------- | ||
| -- member_groups | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.member_groups | ||
| ( _member_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns table | ||
| ( group_id uuid | ||
| , admin bool | ||
| ) | ||
| as $func$ | ||
| select | ||
| gm.group_id | ||
| , gm.admin and (not m.kind = 'a') -- agent's cannot be group admins | ||
| from core.principal m -- the member | ||
| -- assert the member belongs to the space | ||
| inner join core.principal_space psm on (m.id = psm.principal_id and psm.space_id = _space_id) | ||
| -- find the groups the member belongs to in the space | ||
| inner join core.group_member gm on (m.member_id = gm.member_id and gm.space_id = _space_id) | ||
| -- assert the group belongs to the space | ||
| inner join core.principal_space psg on (gm.group_id = psg.principal_id and psg.space_id = _space_id) | ||
| where m.member_id = _member_id -- the member | ||
| $func$ language sql stable security invoker | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| ------------------------------------------------------------------------------- | ||
| -- member_tree_access | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.member_tree_access | ||
| ( _member_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns table | ||
| ( tree_path ltree | ||
| , access int | ||
| ) | ||
| as $func$ | ||
| -- member's grants via groups | ||
| select | ||
| ta.tree_path | ||
| , ta.access | ||
| from core.member_groups(_member_id, _space_id) mg | ||
| inner join core.tree_access ta on (mg.group_id = ta.principal_id and ta.space_id = _space_id) | ||
| $func$ language sql stable security invoker | ||
| ; | ||
|
|
||
| ------------------------------------------------------------------------------- | ||
| -- user_tree_access | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.user_tree_access | ||
| ( _user_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns table | ||
| ( tree_path ltree | ||
| , access int | ||
| ) | ||
| as $func$ | ||
| -- user's direct grants | ||
| select | ||
| ta.tree_path | ||
| , ta.access | ||
| from core.principal u | ||
| inner join core.principal_space psu on (u.id = psu.principal_id and psu.space_id = _space_id) | ||
| inner join core.tree_access ta on (u.id = ta.principal_id and ta.space_id = _space_id) | ||
| where u.user_id = _user_id | ||
| union | ||
| -- user's access via groups | ||
| select | ||
| x.tree_path | ||
| , x.access | ||
| from core.member_tree_access(_user_id, _space_id) x | ||
| $func$ language sql stable security invoker | ||
| ; | ||
|
|
||
| ------------------------------------------------------------------------------- | ||
| -- agent_tree_access | ||
| ------------------------------------------------------------------------------- | ||
| create or replace function core.agent_tree_access | ||
| ( _agent_id uuid | ||
| , _space_id uuid | ||
| ) | ||
| returns table | ||
| ( tree_path ltree | ||
| , access int | ||
| ) | ||
| as $func$ | ||
| with agent_access as materialized | ||
| ( | ||
| -- agent's direct grants | ||
| select | ||
| ta.tree_path | ||
| , ta.access | ||
| from core.principal a | ||
| inner join core.principal_space ps on (a.id = ps.principal_id and ps.space_id = _space_id) | ||
| inner join core.tree_access ta on (a.id = ta.principal_id and ta.space_id = _space_id) | ||
| where a.agent_id = _agent_id | ||
| union | ||
| -- agent's access via groups | ||
| select | ||
| x.tree_path | ||
| , x.access | ||
| from core.member_tree_access(_agent_id, _space_id) x | ||
| ) | ||
| , owner_access as materialized | ||
| ( | ||
| -- get the access for the user that owns the agent | ||
| select | ||
| x.tree_path | ||
| , x.access | ||
| from | ||
| ( | ||
| select p.owner_id | ||
| from core.principal p | ||
| where p.agent_id = _agent_id | ||
| ) a | ||
| cross join lateral core.user_tree_access(a.owner_id, _space_id) x | ||
| ) | ||
| select | ||
| x.tree_path | ||
| , max(x.access) | ||
| from | ||
| ( | ||
| -- take the agent's access when it is covered by the owner's access | ||
| select | ||
| aa.tree_path | ||
| , aa.access | ||
| from agent_access aa | ||
| where exists | ||
| ( | ||
| -- the owner must have access that is the same or greater than the agent's | ||
| select 1 | ||
| from owner_access oa | ||
| where oa.tree_path @> aa.tree_path | ||
| and oa.access >= aa.access | ||
| ) | ||
| union | ||
| -- when the agent has more access than the owner, take the owner's access | ||
| select | ||
| oa.tree_path | ||
| , oa.access | ||
| from owner_access oa | ||
| where exists | ||
| ( | ||
| select 1 | ||
| from agent_access aa | ||
| where aa.tree_path @> oa.tree_path | ||
| and aa.access >= oa.access | ||
| ) | ||
| ) x | ||
| group by x.tree_path | ||
| $func$ language sql stable security invoker | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare module "*.sql" { | ||
| const sql: string; | ||
| export default sql; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ------------------------------------------------------------------------------- | ||
| -- space | ||
| ------------------------------------------------------------------------------- | ||
| create table core.space | ||
| ( id uuid not null primary key default uuidv7() check (uuid_extract_version(id) = 7) | ||
| , slug text not null unique check (slug ~ '^[a-z0-9]{12}$') | ||
| , name citext not null | ||
| , language text not null default 'english' check (language ~ '^[a-z_]+$') | ||
| -- we likely need columns for embedding provider, model, dimensions | ||
| , created_at timestamptz not null default now() | ||
| , updated_at timestamptz | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not put language support in now
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one way or the other you have to configure pg_textsearch indexes with a language. The database migrations for the spaces are already wired to configure pg_textsearch languages.