From 97acec656dab3d237b0914f56ebe2a70ce94a9dc Mon Sep 17 00:00:00 2001 From: Alex Cabrera Date: Tue, 24 Oct 2023 16:14:34 -0700 Subject: [PATCH 1/2] feat: add created and updated columns to DB --- backend/init.sql | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/init.sql b/backend/init.sql index 44b31577..eaad6f2b 100644 --- a/backend/init.sql +++ b/backend/init.sql @@ -12,7 +12,9 @@ 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 ( @@ -20,7 +22,9 @@ CREATE TABLE charts ( 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 ( @@ -28,7 +32,9 @@ CREATE TABLE reports ( 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 ( @@ -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 ); \ No newline at end of file From b15fa79e8367114e728eaf3a0975f09d883defef Mon Sep 17 00:00:00 2001 From: Alex Cabrera Date: Tue, 24 Oct 2023 16:24:18 -0700 Subject: [PATCH 2/2] updates --- backend/zeno_backend/database/update.py | 37 ++++++++++++++----------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/backend/zeno_backend/database/update.py b/backend/zeno_backend/database/update.py index 172b181c..83a88170 100644 --- a/backend/zeno_backend/database/update.py +++ b/backend/zeno_backend/database/update.py @@ -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, @@ -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, @@ -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], ) @@ -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):