feat: handle postgres numeric field for batch number comparison

This commit is contained in:
Elliot DeNolf
2023-08-14 14:02:56 -04:00
parent 791ed3be50
commit 24aa1f27c1
4 changed files with 43 additions and 37 deletions

52
.vscode/launch.json vendored
View File

@@ -40,7 +40,7 @@
"env": {
"PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts",
"PAYLOAD_DATABASE": "postgres",
"PAYLOAD_DROP_DATABASE": "true",
// "PAYLOAD_DROP_DATABASE": "true",
},
"outputCapture": "std",
},
@@ -52,7 +52,7 @@
"env": {
"PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts",
"PAYLOAD_DATABASE": "postgres",
"PAYLOAD_DROP_DATABASE": "true",
// "PAYLOAD_DROP_DATABASE": "true",
},
"outputCapture": "std",
},
@@ -70,53 +70,41 @@
"outputCapture": "std",
},
{
"type": "node",
"type": "node-terminal",
"command": "ts-node src/bin/migrate.ts migrate:down",
"request": "launch",
"name": "Migrate CLI - down",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"src/bin/migrate.ts",
"migrate:down",
],
"env": {
"PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts"
// "PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts",
"PAYLOAD_CONFIG_PATH": "test/postgres/config.ts",
"PAYLOAD_DATABASE": "postgres",
// "PAYLOAD_DROP_DATABASE": "true",
},
"outputCapture": "std",
},
{
"type": "node",
"type": "node-terminal",
"command": "ts-node src/bin/migrate.ts migrate:reset",
"request": "launch",
"name": "Migrate CLI - reset",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"src/bin/migrate.ts",
"migrate:reset",
],
"env": {
"PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts"
// "PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts",
"PAYLOAD_CONFIG_PATH": "test/postgres/config.ts",
"PAYLOAD_DATABASE": "postgres",
// "PAYLOAD_DROP_DATABASE": "true",
},
"outputCapture": "std",
},
{
"type": "node",
"type": "node-terminal",
"command": "ts-node src/bin/migrate.ts migrate:refresh",
"request": "launch",
"name": "Migrate CLI - refresh",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"src/bin/migrate.ts",
"migrate:refresh",
],
"env": {
"PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts"
// "PAYLOAD_CONFIG_PATH": "test/migrations-cli/config.ts",
"PAYLOAD_CONFIG_PATH": "test/postgres/config.ts",
"PAYLOAD_DATABASE": "postgres",
// "PAYLOAD_DROP_DATABASE": "true",
},
"outputCapture": "std",
},

View File

@@ -10,9 +10,19 @@ export async function getMigrations({
collection: 'payload-migrations',
sort: '-name',
where: {
batch: {
not_equals: '-1',
},
and: [
{
batch: {
not_equals: '-1',
},
},
{
batch: {
not_equals: -1,
},
},
],
},
});
@@ -22,7 +32,13 @@ export async function getMigrations({
const latestBatch = Number(existingMigrations?.[0]?.batch) || 0;
return {
existingMigrations,
latestBatch,
existingMigrations: existingMigrations.map((m) => {
return {
...m,
// Cast to number to accomodate postgres numeric field type. Stores as string.
batch: Number(m.batch),
};
}),
latestBatch: Number(latestBatch),
};
}

View File

@@ -37,6 +37,7 @@ export async function migrateDown(this: DatabaseAdapter): Promise<void> {
transactionID = await this.beginTransaction();
await migrationFile.down({ payload });
payload.logger.info({ msg: `Migrated: ${migrationFile.name} (${Date.now() - start}ms)` });
// Waiting for implementation here
await payload.delete({
collection: 'payload-migrations',
id: migration.id,

View File

@@ -20,6 +20,7 @@ export const readMigrationFiles = async ({
.map((file) => {
return path.resolve(payload.db.migrationDir, file);
});
return files.map((filePath) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires,import/no-dynamic-require
const migration = require(filePath) as Migration;