Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor migrator * Fix exports * Fix ESLint * Fix migrator * Fix reference to config * Split some more * Fix table builder * Fix argument order * Merge branch 'master' of https://github.com/tgriesser/knex into feature/2690-support-multiple-migration-dirs # Conflicts: # src/migrate/index.js # test/index.js # test/unit/migrate/migrator.js
- Loading branch information
Showing
7 changed files
with
245 additions
and
205 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,54 @@ | ||
import Promise from 'bluebird'; | ||
import { filter, map } from 'lodash'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { getTableName } from './table-resolver'; | ||
import { ensureTable } from './table-creator'; | ||
|
||
export const DEFAULT_LOAD_EXTENSIONS = Object.freeze([ | ||
'.co', | ||
'.coffee', | ||
'.eg', | ||
'.iced', | ||
'.js', | ||
'.litcoffee', | ||
'.ls', | ||
'.ts', | ||
]); | ||
|
||
// Lists all available migration versions, as a sorted array. | ||
export function listAll( | ||
absoluteConfigDir, | ||
loadExtensions = DEFAULT_LOAD_EXTENSIONS | ||
) { | ||
return Promise.promisify(fs.readdir, { context: fs })(absoluteConfigDir).then( | ||
(migrations) => { | ||
return filter(migrations, function(value) { | ||
const extension = path.extname(value); | ||
return loadExtensions.includes(extension); | ||
}).sort(); | ||
} | ||
); | ||
} | ||
|
||
// Lists all migrations that have been completed for the current db, as an | ||
// array. | ||
export function listCompleted(tableName, schemaName, trxOrKnex) { | ||
return ensureTable(tableName, schemaName, trxOrKnex) | ||
.then(() => | ||
trxOrKnex | ||
.from(getTableName(tableName, schemaName)) | ||
.orderBy('id') | ||
.select('name') | ||
) | ||
.then((migrations) => map(migrations, 'name')); | ||
} | ||
|
||
// Gets the migration list from the migration directory specified in config, as well as | ||
// the list of completed migrations to check what should be run. | ||
export function listAllAndCompleted(config, trxOrKnex, absoluteConfigDir) { | ||
return Promise.all([ | ||
listAll(absoluteConfigDir, config.loadExtensions), | ||
listCompleted(config.tableName, config.schemaName, trxOrKnex), | ||
]); | ||
} |
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,57 @@ | ||
import { | ||
getTable, | ||
getLockTableName, | ||
getLockTableNameWithSchema, | ||
getTableName, | ||
} from './table-resolver'; | ||
|
||
export function ensureTable(tableName, schemaName, trxOrKnex) { | ||
const lockTable = getLockTableName(tableName); | ||
const lockTableWithSchema = getLockTableNameWithSchema(tableName, schemaName); | ||
return getSchemaBuilder(trxOrKnex, schemaName) | ||
.hasTable(tableName) | ||
.then( | ||
(exists) => | ||
!exists && _createMigrationTable(tableName, schemaName, trxOrKnex) | ||
) | ||
.then(() => getSchemaBuilder(trxOrKnex, schemaName).hasTable(lockTable)) | ||
.then( | ||
(exists) => | ||
!exists && _createMigrationLockTable(lockTable, schemaName, trxOrKnex) | ||
) | ||
.then(() => getTable(trxOrKnex, lockTable, schemaName).select('*')) | ||
.then( | ||
(data) => | ||
!data.length && | ||
trxOrKnex.into(lockTableWithSchema).insert({ is_locked: 0 }) | ||
); | ||
} | ||
|
||
function _createMigrationTable(tableName, schemaName, trxOrKnex) { | ||
return getSchemaBuilder(trxOrKnex, schemaName).createTable( | ||
getTableName(tableName), | ||
function(t) { | ||
t.increments(); | ||
t.string('name'); | ||
t.integer('batch'); | ||
t.timestamp('migration_time'); | ||
} | ||
); | ||
} | ||
|
||
function _createMigrationLockTable(tableName, schemaName, trxOrKnex) { | ||
return getSchemaBuilder(trxOrKnex, schemaName).createTable( | ||
tableName, | ||
function(t) { | ||
t.increments('index').primary(); | ||
t.integer('is_locked'); | ||
} | ||
); | ||
} | ||
|
||
//Get schema-aware schema builder for a given schema nam | ||
export function getSchemaBuilder(trxOrKnex, schemaName) { | ||
return schemaName | ||
? trxOrKnex.schema.withSchema(schemaName) | ||
: trxOrKnex.schema; | ||
} |
Oops, something went wrong.