From 166ad559a1668cbeb49029b4080eb126138e16b8 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Wed, 8 Dec 2021 18:22:35 +0000 Subject: [PATCH] Create classification_roots table --- ...00001-create-classification-roots-table.js | 57 +++++++++++++++++++ package.json | 1 + scripts/populate_roots.js | 42 ++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 database/migrations/20211208000001-create-classification-roots-table.js create mode 100644 scripts/populate_roots.js diff --git a/database/migrations/20211208000001-create-classification-roots-table.js b/database/migrations/20211208000001-create-classification-roots-table.js new file mode 100644 index 0000000..f408a87 --- /dev/null +++ b/database/migrations/20211208000001-create-classification-roots-table.js @@ -0,0 +1,57 @@ +module.exports = { + up: (migration, Types) => { + return migration.createTable('classification_roots', { + metric_id: { + type: Types.UUID, + allowNull: false, + unique: true, + primaryKey: true, + }, + upload_id: { + type: Types.UUID, + allowNull: false, + references: { + model: 'uploads', + key: 'id', + }, + }, + image_id: { + type: Types.UUID, + allowNull: false, + }, + url: { + type: Types.TEXT, + allowNull: false, + }, + status: { + type: Types.TEXT, + allowNull: false, + defaultValue: 'pending', + }, + meta: { + type: Types.JSONB, + defaultValue: {}, + allowNull: false, + }, + created_at: { + type: Types.DATE, + defaultValue: Types.fn('now'), + allowNull: false, + }, + updated_at: { + type: Types.DATE, + defaultValue: Types.fn('now'), + allowNull: false, + }, + deleted_at: { + type: Types.DATE, + defaultValue: null, + allowNull: true, + }, + }) + }, + + down: (migration, Types) => { + return migration.dropTable('') + }, +} \ No newline at end of file diff --git a/package.json b/package.json index d71f47c..07e7d25 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "exec:env": "docker-compose -p jetenv up", "exec:ngrok": "ngrok http 7124 --hostname trash.4l2.uk", "exec:check_img": "NODE_PATH=src node scripts/exec-boot 'node scripts/check_img.js'", + "exec:populate_roots": "NODE_PATH=src node scripts/exec-boot 'node scripts/populate_roots.js'", "test": "NODE_ENV=testing NODE_PATH=src node scripts/jest.js", "start": "NODE_PATH=src node server", "cmd": "NODE_PATH=src node run", diff --git a/scripts/populate_roots.js b/scripts/populate_roots.js new file mode 100644 index 0000000..4a39d21 --- /dev/null +++ b/scripts/populate_roots.js @@ -0,0 +1,42 @@ +const SelectQuery = `select + metrics.id as metric_id, + uploads.id as upload_id, + (metrics.meta->>'image_id')::uuid as image_id, + (regexp_split_to_array(uploads.upload_url, '\\?'::text))[1] as url +from metrics + join uploads on uploads.upload_url like concat('%', metrics.meta ->> 'image_id', '%') +where + metrics.meta -> 'image_id' IS NOT NULL + and uploads.status = 'success' + and not exists( + select classification_roots.metric_id + from classification_roots + where classification_roots.metric_id = metrics.id + ) +limit 20;` + +const InsertQuery = `insert into + classification_roots(metric_id, upload_id, image_id, url) + ${ SelectQuery } +` + +async function main() { + const { sequelize } = require('database/models') + + let totalCount = 0 + let lastCount = 0 + + do { + const [_, inserted] = await sequelize.query(InsertQuery, { raw: true }) + lastCount = inserted + totalCount += inserted + console.log("Inserted %d entries", lastCount) + await new Promise(r => setTimeout(r, 250)) + } while (lastCount > 0) + + console.log("Created %d New Roots", totalCount) + + process.exit(0) +} + +main() \ No newline at end of file -- GitLab