Skip to content

Commit

Permalink
Move responsibility of serialization to the adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleavely committed Apr 26, 2020
1 parent 42bd4f4 commit 5e183e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ exports.getConfig = async () => {
if (!_config.storeState) {
const statePath = path.join(configDir, 'exodus.state.json')
_config.storeState = async (state) => {
return fs.writeFile(statePath, state, 'utf8')
return fs.writeFile(statePath, JSON.stringify(state, null, 2), 'utf8')
}
}
if (!_config.fetchState) {
_config.fetchState = async () => {
const statePath = path.join(configDir, 'exodus.state.json')
try {
return await fs.readFile(statePath, 'utf8')
return JSON.parse(await fs.readFile(statePath, 'utf8'))
} catch (err) {
if (err.code !== 'ENOENT') throw err
return '{}'
return {}
}
}
}
Expand All @@ -50,9 +50,8 @@ const defaultConfig = {
// The folder to store migration scripts in.
migrationsDirectory: './migrations',

// Persists the current migration state. The `state`
// argument will always be a JSON-serialized object.
// Store it to redis, disk, database, ... whatever suits you.
// Persists the current migration state. The `state` argument
// will always be an object. Store it as JSON to redis, disk, etc.
// OPTIONAL: If undefined, Exodus falls back to exodus.state.json
// storeState: async (state, context) => {},

Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.run = async () => {
const files = await listDirectoryFiles(config.migrationsDirectory)

// figure out which ones havent already been ran
const state = JSON.parse(await config.fetchState(context))
const state = await config.fetchState(context)
state.history = state.history || []

// create an ID for our round so we can undo latest batch later
Expand Down Expand Up @@ -87,7 +87,7 @@ exports.run = async () => {
state.history.forEach((job) => {
delete job.path
})
await config.storeState(JSON.stringify(state), context)
await config.storeState(state, context)
return { state, ranMigrations: pendingMigrations }
}

Expand Down
5 changes: 2 additions & 3 deletions src/templates/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module.exports = exports = {
// The folder to store migration scripts in.
// migrationsDirectory: './migrations',

// Persists the current migration state. The `state`
// argument will always be a JSON-serialized object.
// Store it to redis, disk, database, ... whatever suits you.
// Persists the current migration state. The `state` argument
// will always be an object. Store it as JSON to redis, disk, etc.
// OPTIONAL: If undefined, Exodus falls back to exodus.state.json
// storeState: async (state, context) => {},

Expand Down

0 comments on commit 5e183e3

Please sign in to comment.