Skip to content

Web 34 repository layer expose doc metadata #371

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion backend/database/repositories/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (rep filesystemRepository) query(query string, input ...interface{}) (Files
err := rep.ctx.Query(query,
input,
&entity.EntityID, &entity.LogicalName, &entity.IsDocument, &entity.IsPublished,
&entity.CreatedAt, &entity.OwnerUserId, &entity.ParentFileID)
&entity.CreatedAt, &entity.MetadataID, &entity.OwnerUserId, &entity.ParentFileID)
if err != nil {
return FilesystemEntry{}, err
}
Expand Down Expand Up @@ -98,6 +98,26 @@ func (rep filesystemRepository) GetIDWithPath(path string) (uuid.UUID, error) {
return parent.EntityID, err
}

func (rep filesystemRepository) GetMetadataFromID(ID uuid.UUID) (MetadataEntry, error) {
// Get entry from ID
entry, err := rep.GetEntryWithID(ID)
if err != nil {
return MetadataEntry{}, err
}

// Get metadata
entity := MetadataEntry{}

err = rep.ctx.Query("SELECT * FROM metadata WHERE MetadataID = $1",
[]interface{}{entry.MetadataID},
&entity.MetadataID, &entity.CreatedAt)
if err != nil {
return MetadataEntry{}, err
}
return entity, nil

}

func (rep filesystemRepository) DeleteEntryWithID(ID uuid.UUID) error {
return rep.ctx.Exec("SELECT delete_entity($1)", []interface{}{ID})
}
Expand Down
15 changes: 15 additions & 0 deletions backend/database/repositories/mocks/repositories_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions backend/database/repositories/repository_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ type FilesystemEntry struct {
IsPublished bool
CreatedAt time.Time

MetadataID uuid.UUID

OwnerUserId int
ParentFileID uuid.UUID
ChildrenIDs []uuid.UUID
}

type MetadataEntry struct {
MetadataID uuid.UUID
CreatedAt time.Time
}

type (
// Repository interface that all valid filesystem repositories
// mocked/real should implement
Expand All @@ -32,6 +39,7 @@ type (
GetRoot() (FilesystemEntry, error)
GetEntryWithParentID(ID uuid.UUID) (FilesystemEntry, error)
GetIDWithPath(path string) (uuid.UUID, error)
GetMetadataFromID(ID uuid.UUID) (MetadataEntry, error)

CreateEntry(file FilesystemEntry) (FilesystemEntry, error)
DeleteEntryWithID(ID uuid.UUID) error
Expand Down
25 changes: 25 additions & 0 deletions backend/database/repositories/tests/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,31 @@ func TestGetIDWithPath(t *testing.T) {
})
}

func TestMetadataRetrieval(t *testing.T) {
assert := assert.New(t)

testContext.RunTest(func() {
// ==== Setup ====
repo, err := repositories.NewFilesystemRepo(frontendLogicalName, frontendURL, testContext)
assert.Nil(err)
root, _ := repo.GetRoot()
newDoc, err := repo.CreateEntry(repositories.FilesystemEntry{
LogicalName: "test_doc", ParentFileID: root.EntityID,
OwnerUserId: repositories.GROUPS_ADMIN, IsDocument: true,
})
// ==== Assertions ====
if err != nil {
log.Fatalf(err.Error())
}

// Query for metadata in database
if info, err := repo.GetMetadataFromID(newDoc.EntityID); assert.Nil(err) {
assert.Equal(newDoc.MetadataID, info.MetadataID)
assert.NotEmpty(info.CreatedAt)
}
})
}

func TestMultiApplications(t *testing.T) {
assert := assert.New(t)

Expand Down
10 changes: 6 additions & 4 deletions postgres/up/04-create_filesystem_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CREATE TABLE filesystem (
CreatedAt TIMESTAMP NOT NULL DEFAULT NOW(),

/* MetaData */
-- MetadataID uuid NOT NULL,
MetadataID uuid NOT NULL,

OwnedBy INT,

Expand All @@ -32,7 +32,7 @@ CREATE TABLE filesystem (
CONSTRAINT fk_owner FOREIGN KEY (OwnedBy)
REFERENCES groups(GroupID),

-- CONSTRAINT fk_meta FOREIGN KEY (MetadataID) REFERENCES metadata(MetadataID),
CONSTRAINT fk_meta FOREIGN KEY (MetadataID) REFERENCES metadata(MetadataID),

/* Unique name constraint: there should not exist an entity of the same type with the
same parent and logical name. */
Expand All @@ -48,14 +48,16 @@ AS $$
DECLARE
newEntityID filesystem.EntityID%type;
parentIsDocument BOOLEAN := (SELECT IsDocument FROM filesystem WHERE EntityID = parentP LIMIT 1);
newMetadataID filesystem.MetadataID%type;
BEGIN
IF parentIsDocument THEN
/* We shouldn't be declaring that a document is our parent */
RAISE EXCEPTION SQLSTATE '90001' USING MESSAGE = 'cannot make parent a document';
END If;
INSERT INTO metadata DEFAULT VALUES RETURNING MetadataID INTO newMetadataID;
WITH newEntity AS (
INSERT INTO filesystem (LogicalName, IsDocument, OwnedBy, Parent)
VALUES (logicalNameP, isDocumentP, ownedByP, parentP)
INSERT INTO filesystem (LogicalName, IsDocument, MetadataID, OwnedBy, Parent)
VALUES (logicalNameP, isDocumentP, newMetadataID, ownedByP, parentP)
RETURNING EntityID
)

Expand Down
6 changes: 4 additions & 2 deletions postgres/up/05-create_frontend_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ AS $$
DECLARE
frontendIDP frontend.ID%type;
frontendRoot frontend.Root%type;
newMetadataID filesystem.MetadataID%type;
BEGIN
-- TODO: Temporarily setting the OwnedBy field. Going to be deprecated soon.
INSERT INTO filesystem (LogicalName, IsDocument, Parent, OwnedBy)
VALUES (logicalNameP, false, uuid_nil(), 1)
INSERT INTO metadata DEFAULT VALUES RETURNING MetadataID INTO newMetadataID;
INSERT INTO filesystem (LogicalName, IsDocument, Parent, OwnedBy, MetadataID)
VALUES (logicalNameP, false, uuid_nil(), 1, newMetadataID)
RETURNING entityID INTO frontendRoot;

INSERT INTO frontend
Expand Down