Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for custom seed sources (#4842)
Co-authored-by: maximelkin <maxelkin@list.ru>
- Loading branch information
1 parent
6347f1c
commit bd1c31a
Showing
11 changed files
with
210 additions
and
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const path = require('path'); | ||
const DEFAULT_LOAD_EXTENSIONS = Object.freeze([ | ||
'.co', | ||
'.coffee', | ||
'.eg', | ||
'.iced', | ||
'.js', | ||
'.cjs', | ||
'.litcoffee', | ||
'.ls', | ||
'.ts', | ||
]); | ||
|
||
class AbstractMigrationsLoader { | ||
constructor(migrationDirectories, sortDirsSeparately, loadExtensions) { | ||
this.sortDirsSeparately = sortDirsSeparately; | ||
|
||
if (!Array.isArray(migrationDirectories)) { | ||
migrationDirectories = [migrationDirectories]; | ||
} | ||
this.migrationsPaths = migrationDirectories; | ||
this.loadExtensions = loadExtensions || DEFAULT_LOAD_EXTENSIONS; | ||
} | ||
|
||
getFile(migrationsInfo) { | ||
const absoluteDir = path.resolve(process.cwd(), migrationsInfo.directory); | ||
const _path = path.join(absoluteDir, migrationsInfo.file); | ||
const importFile = require('../util/import-file'); // late import | ||
return importFile(_path); | ||
} | ||
} | ||
|
||
module.exports = { | ||
DEFAULT_LOAD_EXTENSIONS, | ||
AbstractMigrationsLoader, | ||
}; |
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
6 changes: 2 additions & 4 deletions
6
...igrations/migrate/configuration-merger.js → .../migrate/migrator-configuration-merger.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { FsSeeds } = require('./sources/fs-seeds'); | ||
const Logger = require('../../logger'); | ||
const { DEFAULT_LOAD_EXTENSIONS } = require('../common/MigrationsLoader'); | ||
const defaultLogger = new Logger(); | ||
|
||
const CONFIG_DEFAULT = Object.freeze({ | ||
extension: 'js', | ||
directory: './seeds', | ||
loadExtensions: DEFAULT_LOAD_EXTENSIONS, | ||
specific: null, | ||
timestampFilenamePrefix: false, | ||
recursive: false, | ||
sortDirsSeparately: false, | ||
}); | ||
|
||
function getMergedConfig(config, currentConfig, logger = defaultLogger) { | ||
// config is the user specified config, mergedConfig has defaults and current config | ||
// applied to it. | ||
const mergedConfig = Object.assign( | ||
{}, | ||
CONFIG_DEFAULT, | ||
currentConfig || {}, | ||
config, | ||
{ | ||
logger, | ||
} | ||
); | ||
|
||
if ( | ||
config && | ||
// If user specifies any FS related config, | ||
// clear specified migrationSource to avoid ambiguity | ||
(config.directory || | ||
config.sortDirsSeparately !== undefined || | ||
config.loadExtensions) | ||
) { | ||
if (config.seedSource) { | ||
logger.warn( | ||
'FS-related option specified for seed configuration. This resets seedSource to default FsMigrations' | ||
); | ||
} | ||
mergedConfig.seedSource = null; | ||
} | ||
|
||
// If the user has not specified any configs, we need to | ||
// default to fs migrations to maintain compatibility | ||
if (!mergedConfig.seedSource) { | ||
mergedConfig.seedSource = new FsSeeds( | ||
mergedConfig.directory, | ||
mergedConfig.sortDirsSeparately, | ||
mergedConfig.loadExtensions | ||
); | ||
} | ||
|
||
return mergedConfig; | ||
} | ||
|
||
module.exports = { | ||
getMergedConfig, | ||
}; |
Oops, something went wrong.