Skip to content

Commit

Permalink
Merge pull request #5825 from sagemathinc/pg14-extract-5824
Browse files Browse the repository at this point in the history
postgres 14: EXTRACT returns a numeric, which is converted to a strin…
  • Loading branch information
williamstein authored Apr 4, 2022
2 parents 2830a66 + 95f22ec commit 7c751ca
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/packages/database/pool/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/

// Postgres 14 is different than 13 an earlier.
// extract(epoch from timestamp) returns a "numeric", which is converted to a string by the pg driver.
// we convert this explicitly to a floating point number to get the ms since epoch.
// Note: JavaScript's new Date(...) has no hesitation converting from a float.
export function timeInSeconds(field: string, asField?: string): string {
return ` EXTRACT(EPOCH FROM ${field})*1000 as ${asField ?? field} `;
return ` (EXTRACT(EPOCH FROM ${field})*1000)::FLOAT as ${asField ?? field} `;
}

// Given number of seconds **in the future**.
Expand Down
10 changes: 1 addition & 9 deletions src/packages/database/postgres-blobs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -804,20 +804,12 @@ exports.extend_PostgreSQL = (ext) -> class PostgreSQL extends ext
string_id : required
cb : required # cb(err, array)
@_query
query : "SELECT extract(epoch from time)*1000 as epoch, * FROM patches"
query : "SELECT * FROM patches"
where : {"string_id = $::CHAR(40)" : opts.string_id}
cb : all_results (err, patches) =>
if err
opts.cb(err)
else
for p in patches
# TODO why using epoch and then converting to Date, why not just taking time?
# Besides that: @hsy noticed in development that p.epoch could be a string, resulting in an invalid date.
if typeof p.epoch == 'string'
p.time = new Date(parseInt(p.epoch))
else
p.time = new Date(p.epoch)
delete p.epoch
opts.cb(undefined, patches)

import_patches: (opts) =>
Expand Down
4 changes: 2 additions & 2 deletions src/packages/next/pages/share/public_paths/page/[page].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ such as Google, and only exists for that purpose.

import Link from "next/link";
import SiteName from "components/share/site-name";
import getPool from "@cocalc/database/pool";
import getPool, { timeInSeconds } from "@cocalc/database/pool";
import PublicPaths from "components/share/public-paths";
import { Layout } from "components/share/layout";
import withCustomize from "lib/with-customize";
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function getServerSideProps(context) {
const page = getPage(context.params);
const pool = getPool("medium");
const { rows } = await pool.query(
`SELECT id, path, description, EXTRACT(EPOCH FROM last_edited)*1000 AS last_edited
`SELECT id, path, description, ${timeInSeconds("last_edited")}
FROM public_paths
WHERE vhost IS NULL AND disabled IS NOT TRUE AND unlisted IS NOT TRUE AND
((authenticated IS TRUE AND $1 IS TRUE) OR (authenticated IS NOT TRUE))
Expand Down
6 changes: 4 additions & 2 deletions src/packages/server/projects/control/stop-idle-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ async function stopIdleProjects(stopProject: (string) => Promise<void>) {
logger.debug("query database for all running projects");
const runningProjects = (
await callback2(db()._query, {
query:
"SELECT project_id, EXTRACT(EPOCH FROM NOW() - last_edited) as idle_time, settings, run_quota FROM projects WHERE state ->> 'state' = 'running'",
// ::float necessary for Postgres 14, see @cocalc/database/pool/util.ts timeInSeconds for more info
query: `SELECT project_id, (EXTRACT(EPOCH FROM NOW() - last_edited))::FLOAT as idle_time, settings, run_quota
FROM projects
WHERE state ->> 'state' = 'running'`,
})
).rows;
logger.debug("got ", runningProjects);
Expand Down

0 comments on commit 7c751ca

Please sign in to comment.