Skip to content

Commit

Permalink
Merge pull request #749 from balena-io/migrationStatus-type-guard
Browse files Browse the repository at this point in the history
Use a type guard for `"migration"."migration status"`
  • Loading branch information
thgreasi authored Apr 2, 2024
2 parents a9c8d7e + cee8cd7 commit 05e2511
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
"test": "npm run lint && npm run build && npm run webpack-build && npm run test:compose",
"test:compose": "trap 'docker-compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' SIGINT; docker-compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 npm run mocha",
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' SIGINT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 npm run mocha",
"mocha": "TS_NODE_FILES=true mocha",
"prettify": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.ts"
},
Expand Down
29 changes: 20 additions & 9 deletions src/migrator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,27 @@ WHERE "migration"."model name" = ${1}`,
return [];
}

/**
* Should be
* if ( Array.isArray(executedMigrations) && executedMigrations.every((migration) => typeof migration === 'string') ) {
* return executedMigrations;
* }
* but typescript does not infer that every element of the array is a string
*/
return sbvrUtils.sbvrTypes.JSON.fetchProcessing(
const executedMigrations = sbvrUtils.sbvrTypes.JSON.fetchProcessing(
data.executed_migrations,
) as string[];
);
if (!Array.isArray(executedMigrations)) {
throw new Error(
`"migration"."executed migrations" is expected to be an Array<string>, but the retrieved value was ${typeof executedMigrations}`,
);
}
if (
!executedMigrations.every(
(migration): migration is string => typeof migration === 'string',
)
) {
const nonStringMigrationValue = executedMigrations.find(
(migration) => typeof migration !== 'string',
);
throw new Error(
`"migration"."executed migrations" is expected to be an Array<string>, but the retrieved array included ${typeof nonStringMigrationValue}`,
);
}
return executedMigrations;
};

export const migrationTablesExist = async (tx: Tx) => {
Expand Down

0 comments on commit 05e2511

Please sign in to comment.