Skip to content

Commit

Permalink
feat: add created and updated columns to DB (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabreraalex authored Oct 25, 2023
1 parent 2a661ae commit 9d2f8b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
18 changes: 13 additions & 5 deletions backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ CREATE TABLE projects (
view text NOT NULL,
samples_per_page integer NOT NULL DEFAULT 10,
public boolean NOT NULL DEFAULT false,
description text NOT NULL DEFAULT ''
description text NOT NULL DEFAULT '',
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE charts (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
name text NOT NULL,
type text NOT NULL,
parameters text NOT NULL
parameters text NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE reports (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
owner_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
public boolean NOT NULL DEFAULT false,
description text NOT NULL DEFAULT ''
description text NOT NULL DEFAULT '',
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE report_elements (
Expand Down Expand Up @@ -117,11 +123,13 @@ CREATE TABLE organization_report (
CREATE TABLE report_like (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE project_like (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
37 changes: 21 additions & 16 deletions backend/zeno_backend/database/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def chart(chart: Chart, project: str):
"""
db = Database()
db.connect_execute(
"UPDATE charts SET project_uuid = %s, name = %s, type = %s, parameters = %s "
"WHERE id = %s;",
"UPDATE charts SET project_uuid = %s, name = %s, type = %s, parameters = %s, "
"updated_at = CURRENT_TIMESTAMP WHERE id = %s;",
[
project,
chart.name,
Expand Down Expand Up @@ -206,7 +206,7 @@ def project(project_config: Project):
db.execute(
"UPDATE projects SET name = %s, "
"view = %s, samples_per_page = %s, public = %s, "
"description = %s WHERE uuid = %s;",
"description = %s, updated_at = CURRENT_TIMESTAMP WHERE uuid = %s;",
[
project_config.name,
project_config.view,
Expand Down Expand Up @@ -250,8 +250,8 @@ def report(report: Report):
return
db = Database()
db.connect_execute(
"UPDATE reports SET name = %s, owner_id = %s, public = %s, description = %s "
"WHERE id = %s;",
"UPDATE reports SET name = %s, owner_id = %s, public = %s, description = %s, "
"updated_at = CURRENT_TIMESTAMP WHERE id = %s;",
[report.name, owner_id.id, report.public, report.description, report.id],
)

Expand Down Expand Up @@ -311,17 +311,22 @@ def report_element(element: ReportElement):
Args:
element (ReportElement): the element to be updated.
"""
db = Database()
db.connect_execute(
"UPDATE report_elements SET type = %s, data = %s, position = %s"
" WHERE id = %s;",
[
element.type,
element.data,
element.position,
element.id,
],
)
with Database() as db:
report_id = db.execute_return(
"UPDATE report_elements SET type = %s, data = %s, position = %s"
" WHERE id = %s RETURNING report_id;",
[
element.type,
element.data,
element.position,
element.id,
],
)
db.execute(
"UPDATE reports SET updated_at = CURRENT_TIMESTAMP WHERE id = %s;",
[report_id[0][0]],
)
db.commit()


def report_user(report_id: int, user: User):
Expand Down

0 comments on commit 9d2f8b5

Please sign in to comment.