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

View File

@@ -10,9 +10,19 @@ export async function getMigrations({
collection: 'payload-migrations', collection: 'payload-migrations',
sort: '-name', sort: '-name',
where: { where: {
batch: { and: [
not_equals: '-1', {
}, batch: {
not_equals: '-1',
},
},
{
batch: {
not_equals: -1,
},
},
],
}, },
}); });
@@ -22,7 +32,13 @@ export async function getMigrations({
const latestBatch = Number(existingMigrations?.[0]?.batch) || 0; const latestBatch = Number(existingMigrations?.[0]?.batch) || 0;
return { return {
existingMigrations, existingMigrations: existingMigrations.map((m) => {
latestBatch, 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(); transactionID = await this.beginTransaction();
await migrationFile.down({ payload }); await migrationFile.down({ payload });
payload.logger.info({ msg: `Migrated: ${migrationFile.name} (${Date.now() - start}ms)` }); payload.logger.info({ msg: `Migrated: ${migrationFile.name} (${Date.now() - start}ms)` });
// Waiting for implementation here
await payload.delete({ await payload.delete({
collection: 'payload-migrations', collection: 'payload-migrations',
id: migration.id, id: migration.id,

View File

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