Skip to content
Snippets Groups Projects
Commit 4267df92 authored by Alexandros Georgantas's avatar Alexandros Georgantas
Browse files

fix(models): improvments in file migration script

parent 2fb8ec87
No related branches found
No related tags found
3 merge requests!52Docx,!43fix(models): improvments in file migration script,!17Graphql api
......@@ -110,7 +110,8 @@ class File extends BaseModel {
const { trx } = options
return useTransaction(
async tr => {
return File.query(tr).where({ objectId })
const { result: files } = await File.find({ objectId }, options)
return files
},
{ trx, passedTrxOnly: true },
)
......
......@@ -3,23 +3,106 @@ const logger = require('@pubsweet/logger')
exports.up = async knex => {
try {
return knex.schema.createTable('files', table => {
table.uuid('id').primary()
table
.timestamp('created', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now())
table.timestamp('updated', { useTz: true })
table.text('type').notNullable()
table.text('name').notNullable()
table.jsonb('storedObjects').notNullable()
table.jsonb('tags').defaultTo([])
table.uuid('referenceId').nullable()
table.uuid('objectId').nullable()
table.text('alt').nullable()
table.text('uploadStatus').nullable()
table.text('caption').nullable()
const tableExists = await knex.schema.hasTable('files')
if (!tableExists) {
await knex.schema.createTable('files', table => {
table.uuid('id').primary()
table
.timestamp('created', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now())
table.timestamp('updated', { useTz: true })
table.text('type').notNullable()
table.text('name').notNullable()
table.jsonb('storedObjects').notNullable()
table.jsonb('tags').defaultTo([])
table.uuid('referenceId').nullable()
table.uuid('objectId').nullable()
table.text('alt').nullable()
table.text('uploadStatus').nullable()
table.text('caption').nullable()
})
return true
}
const hasId = await knex.schema.hasColumn('files', 'id')
const hasCreated = await knex.schema.hasColumn('files', 'created')
const hasUpdated = await knex.schema.hasColumn('files', 'updated')
const hasType = await knex.schema.hasColumn('files', 'type')
const hasName = await knex.schema.hasColumn('files', 'name')
const hasStoredObjects = await knex.schema.hasColumn(
'files',
'stored_objects',
)
const hasTags = await knex.schema.hasColumn('files', 'tags')
const hasReferenceId = await knex.schema.hasColumn('files', 'reference_id')
const hasObjectId = await knex.schema.hasColumn('files', 'object_id')
const hasAlt = await knex.schema.hasColumn('files', 'alt')
const hasUploadStatus = await knex.schema.hasColumn(
'files',
'upload_status',
)
const hasCaption = await knex.schema.hasColumn('files', 'caption')
await knex.schema.alterTable('files', table => {
if (!hasId) {
table.dropPrimary('files_pkey')
table.uuid('id').primary()
}
if (!hasCreated) {
table
.timestamp('created', { useTz: true })
.notNullable()
.defaultTo(knex.fn.now())
}
if (!hasUpdated) {
table.timestamp('updated', { useTz: true })
}
if (!hasType) {
table.text('type').notNullable()
}
if (!hasName) {
table.text('name').notNullable()
}
if (!hasStoredObjects) {
table.jsonb('storedObjects').notNullable()
}
if (!hasTags) {
table.jsonb('tags').defaultTo([])
}
if (!hasReferenceId) {
table.uuid('referenceId').nullable()
}
if (!hasObjectId) {
table.uuid('objectId').nullable()
}
if (!hasAlt) {
table.text('alt').nullable()
}
if (!hasUploadStatus) {
table.text('uploadStatus').nullable()
}
if (!hasCaption) {
table.text('caption').nullable()
}
})
return true
} catch (e) {
logger.error('File: Initial: Migration failed!')
throw new Error(e)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment