Skip to content

Commit

Permalink
fix: migrations can run on pristine database
Browse files Browse the repository at this point in the history
  • Loading branch information
neopostmodern committed Jan 5, 2024
1 parent ea15985 commit 2b82999
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
8 changes: 6 additions & 2 deletions server/lib/migrationSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import migrations from './migrations.js'
import { Meta } from './mongo.js'

const migrationStorage = {
async initializeStorage() {
async initialize() {
await migrations.get(0).up()
return (await Meta.findOne({ _id: 'database-version' })).value
},
async getVersion() {
return (await Meta.findOne({ _id: 'database-version' })).value
const databaseVersion = await Meta.findOne({ _id: 'database-version' })
if (!databaseVersion) {
return null;
}
return databaseVersion.value
},
async setVersion(version) {
const databaseVersion = await Meta.findOne({ _id: 'database-version' })
Expand Down
26 changes: 8 additions & 18 deletions server/lib/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,15 @@ migrations.set(2, {
migrations.set(3, {
name: 'note-types',
async up() {
return new Promise(async (resolve, reject) => {
mongoose.connection.db
.listCollections({ name: 'links' })
.toArray((error, collectionNames) => {
if (error) {
reject(error)
}
const collectionNames = await mongoose.connection.db
.listCollections({ name: 'links' })
.toArray()

if (collectionNames.length) {
const linkCollection = mongoose.connection.db.collection('links')
linkCollection
.updateMany({}, { $set: { type: 'Link' } })
.then(() => linkCollection.rename('notes'))
.then(() => resolve())
} else {
resolve()
}
})
})
if (collectionNames.length) {
const linkCollection = mongoose.connection.db.collection('links')
await linkCollection.updateMany({}, { $set: { type: 'Link' } })
await linkCollection.rename('notes')
}
},
async down() {
return Text.find()
Expand Down

0 comments on commit 2b82999

Please sign in to comment.