Skip to content

Commit

Permalink
feat(migrations): add migrationHistoryCollection in config for a cust…
Browse files Browse the repository at this point in the history
…om migration history collection
  • Loading branch information
TimMikeladze committed Jun 10, 2022
1 parent ed096f7 commit edf304c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ import 'dotenv/config'

export default {
dbConfig: {
databaseName: process.env.ARANGO_NAME,
url: process.env.ARANGO_URL,
auth: {
username: process.env.ARANGO_USERNAME,
password: process.env.ARANGO_PASSWORD || ''
databaseName: process.env.ARANGO_NAME,
url: process.env.ARANGO_URL,
auth: {
username: process.env.ARANGO_USERNAME,
password: process.env.ARANGO_PASSWORD || ''
}
},
autoCreateNewCollections: true, // defaults to true if not specified
migrationHistoryCollection: 'migration_history', // defaults to 'migration_history' if not specified
migrationsPath: './migrations'
}
```
Expand Down
24 changes: 23 additions & 1 deletion __tests__/ArangoMigrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('getMigrationHistoryCollection', () => {
await tu.destroy()
})
it('returns collection', async () => {
expect((await tu.context.am.getMigrationHistoryCollection('migration_history')).name).toEqual('migration_history')
expect((await tu.context.am.getMigrationHistoryCollection()).name).toEqual('migration_history')
})
})

Expand Down Expand Up @@ -516,3 +516,25 @@ describe('runUpMigrations - noHistory', () => {
expect(await tu.context.am.getMigrationHistory()).toHaveLength(0)
})
})

describe('custom migrationHistoryCollection', () => {
let tu: TestUtil

beforeAll(async () => {
tu = await createTestUtil({
...defaultConfig,
migrationHistoryCollection: 'custom_migration_history'
})
await tu.context.am.initialize()
})
afterAll(async () => {
await tu.destroy()
})
it('saves migration history in the configured collection', async () => {
await tu.context.am.runUpMigrations()
expect(await tu.context.am.getMigrationHistory()).toHaveLength(3)
await expect(tu.context.am.getMigrationHistoryCollection()).resolves.toMatchObject({
name: 'custom_migration_history'
})
})
})
20 changes: 13 additions & 7 deletions src/ArangoMigrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export interface ArangoMigrateOptions {
* ArangoDB connection options.
*/
dbConfig: Config,
/**
* The collection in which the migration history will be stored. Defaults to `migration_history`.
*/
migrationHistoryCollection?: string,
/**
* Path to the directory containing the migration files.
*/
Expand Down Expand Up @@ -140,17 +144,19 @@ export default migration

export const DEFAULT_CONFIG_PATH = './config.migrate.js'
export const DEFAULT_MIGRATIONS_PATH = './migrations'
export const DEFAULT_MIGRATION_HISTORY_COLLECTION = 'migration_history'

export class ArangoMigrate {
private readonly options: ArangoMigrateOptions
private readonly migrationHistoryCollectionName: string = 'migration_history'
private readonly migrationHistoryCollection: string
private db: Database
private readonly migrationPaths: string[]
private readonly migrationsPath: string

constructor (options: ArangoMigrateOptions) {
this.options = options
this.migrationsPath = this.options.migrationsPath || DEFAULT_MIGRATIONS_PATH
this.migrationHistoryCollection = this.options.migrationHistoryCollection || DEFAULT_MIGRATION_HISTORY_COLLECTION
this.migrationPaths = this.loadMigrationPaths(this.migrationsPath)
}

Expand Down Expand Up @@ -214,18 +220,18 @@ export class ArangoMigrate {
return importedMigration.default
}

public async getMigrationHistoryCollection (collectionName: string) {
public async getMigrationHistoryCollection () {
let collection: DocumentCollection
try {
collection = await this.db.createCollection(collectionName)
collection = await this.db.createCollection(this.migrationHistoryCollection)
} catch {
collection = this.db.collection(collectionName)
collection = this.db.collection(this.migrationHistoryCollection)
}
return collection
}

public async getMigrationHistory (): Promise<MigrationHistory[]> {
const collection = await this.getMigrationHistoryCollection(this.migrationHistoryCollectionName)
const collection = await this.getMigrationHistoryCollection()

return await (await this.db.query(aql`
FOR x IN ${collection}
Expand All @@ -234,7 +240,7 @@ export class ArangoMigrate {
}

public async getLatestMigration (): Promise<MigrationHistory | null> {
const collection = await this.getMigrationHistoryCollection(this.migrationHistoryCollectionName)
const collection = await this.getMigrationHistoryCollection()

return await (await this.db.query(aql`
FOR x IN ${collection}
Expand All @@ -244,7 +250,7 @@ export class ArangoMigrate {
}

public async writeMigrationHistory (direction: 'up' | 'down', name: string, description: string, version: number) {
const collection = await this.getMigrationHistoryCollection(this.migrationHistoryCollectionName)
const collection = await this.getMigrationHistoryCollection()

const latest = await this.getLatestMigration()

Expand Down

0 comments on commit edf304c

Please sign in to comment.