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 0000000000000000000000000000000000000000..f408a876cc72df7f25dddd3528bfc7ff2c486084 --- /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 d71f47cf4f24e3ca9171028d71d0f5f28f8adc80..07e7d2566b3d78886342fddcfafd1d8ec4c4b291 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 0000000000000000000000000000000000000000..4a39d212dcd8a12832ff571165b818b9e121cdeb --- /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