Merge pull request #3343 from Human-Connection/remove_dangling_image_urls

Remove dangling image urls
This commit is contained in:
mattwr18 2020-03-27 11:44:57 +01:00 committed by GitHub
commit 4347d5efe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 11 deletions

View File

@ -15,12 +15,18 @@ export async function up(next) {
await transaction.commit()
next()
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
const { message } = error
if (message.includes('There already exists an index')) {
// all fine
// eslint-disable-next-line no-console
console.log(message)
next()
} else {
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
}
} finally {
session.close()
}

View File

@ -19,11 +19,18 @@ export async function up(next) {
await transaction.commit()
next()
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
const { message } = error
if (message.includes('There already exists an index')) {
// all fine
// eslint-disable-next-line no-console
console.log(message)
next()
} else {
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
}
} finally {
session.close()
}

View File

@ -0,0 +1,61 @@
import { getDriver } from '../../db/neo4j'
import { existsSync } from 'fs'
export const description = `
In this review:
https://github.com/Human-Connection/Human-Connection/pull/3262#discussion_r398634249
I brought up that we may have image nodes with danling urls (urls that don't
point to local file on disk). I would prefer to remove those urls to avoid
unnecessary 404 errors.
`
export async function up(next) {
const driver = getDriver()
const session = driver.session()
const transaction = session.beginTransaction()
try {
// Implement your migration here.
const { records } = await transaction.run(`
MATCH(image:Image)
WHERE image.url STARTS WITH '/'
RETURN image.url as url
`)
const urls = records.map((record) => record.get('url'))
const danglingUrls = urls.filter((url) => {
const fileLocation = `public${url}`
return !existsSync(fileLocation)
})
await transaction.run(
`
MATCH(image:Image)
WHERE image.url IN $danglingUrls
DETACH DELETE image
`,
{ danglingUrls },
)
await transaction.commit()
if (danglingUrls.length) {
// eslint-disable-next-line no-console
console.log(`
Removed ${danglingUrls.length} dangling urls.\n
===============================================
${danglingUrls.join('\n')}
`)
}
next()
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
} finally {
session.close()
}
}
export async function down() {
throw new Error('Irreversible migration')
}