-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unique constraint on reviews by author and targetUserId (#1038)
* add mongo migrations to remove unique index * add author_1_targetUserId_1 index migration * fix dotenv config in migrate config * add migration test * refactor migrate scripts * remove migrate:create script * add migrate:create script
- Loading branch information
Showing
8 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require('dotenv').config({ | ||
path: process.env.NODE_ENV === 'test' ? '.env.test.local' : '.env.local' | ||
}) | ||
require('dotenv').config() | ||
|
||
const config = { | ||
mongodb: { | ||
url: process.env.MONGODB_URL, | ||
options: { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
} | ||
}, | ||
|
||
migrationsDir: 'migrations', | ||
changelogCollectionName: 'changelog', | ||
migrationFileExtension: '.js', | ||
useFileHash: false, | ||
moduleSystem: 'commonjs' | ||
} | ||
|
||
module.exports = config |
13 changes: 13 additions & 0 deletions
13
migrations/20241203085442-remove-unique-author-target-user-index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const collectionName = 'reviews' | ||
|
||
const indexName = 'author_1_targetUserId_1' | ||
|
||
module.exports = { | ||
async up(db) { | ||
await db.collection(collectionName).dropIndex(indexName) | ||
}, | ||
|
||
async down(db) { | ||
await db.collection(collectionName).createIndex({ author: 1, targetUserId: 1 }, { unique: true }) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/migrations/20241203085442-remove-unique-author-target-user-index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { MongoClient } = require('mongodb') | ||
|
||
const { up, down } = require('@root/migrations/20241203085442-remove-unique-author-target-user-index') | ||
|
||
require('~/initialization/envSetup') | ||
const { | ||
config: { MONGODB_URL } | ||
} = require('~/configs/config') | ||
|
||
const collectionName = 'reviews' | ||
const indexName = 'author_1_targetUserId_1' | ||
|
||
const url = MONGODB_URL.slice(0, MONGODB_URL.lastIndexOf('/')) | ||
const databaseName = MONGODB_URL.slice(MONGODB_URL.lastIndexOf('/') + 1) | ||
|
||
describe('20241203085442-remove-unique-author-target-user-index', () => { | ||
let client, database | ||
|
||
beforeAll(() => { | ||
client = new MongoClient(url) | ||
database = client.db(databaseName) | ||
}) | ||
|
||
afterAll(() => { | ||
client.close() | ||
}) | ||
|
||
test('should create the unique index on migrate down', async () => { | ||
await down(database) | ||
const indexes = await database.collection(collectionName).indexes() | ||
const indexNames = indexes.map((index) => index.name) | ||
expect(indexNames).toContain(indexName) | ||
}) | ||
|
||
test('should remove the unique index on migrate up', async () => { | ||
await up(database) | ||
const indexes = await database.collection(collectionName).indexes() | ||
const indexNames = indexes.map((index) => index.name) | ||
expect(indexNames).not.toContain(indexName) | ||
}) | ||
}) |