Skip to content

Commit

Permalink
Use connection from Migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
lelinhtinh committed Jan 12, 2025
1 parent b83510f commit d7534fe
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import getModels from '../models'
import { UserSchema } from '../models/User'
import { Connection } from 'mongoose'

export async function up() {
const { User } = await getModels()
// Write migration here
export async function up(connection: Connection) {
const User = connection.model('User', UserSchema)
await User.create([
{
firstName: 'John',
Expand All @@ -15,8 +15,7 @@ export async function up() {
])
}

export async function down() {
const { User } = await getModels()
// Write migration here
export async function down(connection: Connection) {
const User = connection.model('User', UserSchema)
await User.deleteMany({ firstName: { $in: ['Jane', 'John'] } }).exec()
}
2 changes: 1 addition & 1 deletion examples/config-file-usage/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface IUser {
lastName?: string
}

const UserSchema = new Schema<IUser>({
export const UserSchema = new Schema<IUser>({
firstName: {
type: String,
required: true,
Expand Down
21 changes: 0 additions & 21 deletions examples/config-file-usage/src/models/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ class Migrator {
}

try {
await migrationFunction()
await migrationFunction(this.connection)

this.logMigrationStatus(direction, migration.filename)

Expand Down
7 changes: 4 additions & 3 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export default `// Import your models here
export default `// Import your schemas here
import { Connection } from 'mongoose'
export async function up (): Promise<void> {
export async function up (connection: Connection): Promise<void> {
// Write migration here
}
export async function down (): Promise<void> {
export async function down (connection: Connection): Promise<void> {
// Write migration here
}
`
54 changes: 24 additions & 30 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export interface IExample {
type: Date
}
export let ExampleSchema = new Schema({
export const ExampleSchema = new Schema({
name: {
type: Schema.Types.String,
required: true,
Expand All @@ -174,40 +174,34 @@ export let ExampleSchema = new Schema({
export default models.User ?? model<IExample>('Example', ExampleSchema)`

const testTemplate = defaultTemplate
.replace(
'// Import your models here',
`
import Example from './Example.ts'
`,
)
.replace('// Import your schemas here', `import { ExampleSchema } from './Example.ts'`)
.replace(
'// Write migration here',
`
await Example.insertMany([
{
name: 'test',
type: 1,
},
{
name: 'test2',
type: 1,
},
{
name: 'test3',
type: 2,
},
{
name: 'test4',
type: 2,
}
])`,
`const Example = connection.model('Example', ExampleSchema)
await Example.insertMany([
{
name: 'test',
type: 1,
},
{
name: 'test2',
type: 1,
},
{
name: 'test3',
type: 2,
},
{
name: 'test4',
type: 2,
}
])`,
)
.replace(
'// Write migration here',
`
await Example.deleteMany({ propertyIsNotDefinedInSchema: 'some-value' })
await Example.deleteMany({ type: 1 })
`,
`const Example = connection.model('Example', ExampleSchema)
await Example.deleteMany({ propertyIsNotDefinedInSchema: 'some-value' })
await Example.deleteMany({ type: 1 })`,
)

console.log(testModel)
Expand Down
27 changes: 24 additions & 3 deletions tests/migrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ import { getConfig } from '../src/commander'

describe('Tests for Migrator class - Programmatic approach', () => {
const uri = `${globalThis.__MONGO_URI__}${globalThis.__MONGO_DB_NAME__}`
const migrateUri = process.env.MIGRATE_MONGO_URI // use in getModels in examples/config-file-usage/src/models/index.ts
let connection: Connection

beforeEach(async () => {
process.env.MIGRATE_MONGO_URI = uri
clearDirectory('migrations')
connection = await mongoose.createConnection(uri).asPromise()
await connection.collection('migrations').deleteMany({})
})

afterEach(async () => {
process.env.MIGRATE_MONGO_URI = migrateUri
if (connection.readyState !== 0) {
await connection.close()
}
Expand Down Expand Up @@ -468,6 +465,10 @@ describe('Tests for Migrator class - Programmatic approach', () => {

const { migrationsInFs } = await migrator.getMigrations()
expect(migrationsInFs).toHaveLength(1)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})

it('should get migration .js files', async () => {
Expand All @@ -476,6 +477,10 @@ describe('Tests for Migrator class - Programmatic approach', () => {

const { migrationsInFs } = await migrator.getMigrations()
expect(migrationsInFs).toHaveLength(1)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})

it('should same filename when migrate using .ts files', async () => {
Expand All @@ -491,6 +496,10 @@ describe('Tests for Migrator class - Programmatic approach', () => {
const migrations = await migrator.sync()

expect(migrationsInFs[0].filename).toBe(migrations[0].filename)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})

it('should same filename when migrate using .js files', async () => {
Expand All @@ -506,6 +515,10 @@ describe('Tests for Migrator class - Programmatic approach', () => {
const migrations = await migrator.sync()

expect(migrationsInFs[0].filename).toBe(migrations[0].filename)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})

it('should run up/down when migrate using .ts files', async () => {
Expand Down Expand Up @@ -558,6 +571,10 @@ describe('Tests for Migrator class - Programmatic approach', () => {
.find({ firstName: { $in: ['Jane', 'John'] } })
.toArray()
expect(deletedUsers).toHaveLength(0)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})

it('should run up/down when migrate using .js files', async () => {
Expand Down Expand Up @@ -610,5 +627,9 @@ describe('Tests for Migrator class - Programmatic approach', () => {
.find({ firstName: { $in: ['Jane', 'John'] } })
.toArray()
expect(deletedUsers).toHaveLength(0)

expect(migrator.connection.readyState).toBe(1)
await migrator.close()
expect(migrator.connection.readyState).toBe(0)
})
})

0 comments on commit d7534fe

Please sign in to comment.