forked from ankane/pgslice
-
Notifications
You must be signed in to change notification settings - Fork 0
Add status command #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,45 @@ | ||
| import { describe, expect } from "vitest"; | ||
| import { sql } from "slonik"; | ||
|
|
||
| import { commandTest as test } from "../testing/index.js"; | ||
| import { StatusCommand } from "./status.js"; | ||
|
|
||
| describe("StatusCommand", () => { | ||
| test.scoped({ commandClass: ({}, use) => use(StatusCommand) }); | ||
|
|
||
| test.beforeEach(async ({ transaction }) => { | ||
| await transaction.query(sql.unsafe` | ||
| CREATE TABLE posts ( | ||
| id SERIAL PRIMARY KEY, | ||
| created_at DATE NOT NULL | ||
| ) | ||
| `); | ||
| }); | ||
|
|
||
| test("outputs status as JSON", async ({ cli, commandContext }) => { | ||
| const exitCode = await cli.run( | ||
| ["status", "posts", "--json"], | ||
| commandContext, | ||
| ); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| const status = JSON.parse(commandContext.stdout.read()?.toString() ?? ""); | ||
| expect(status).toEqual({ | ||
| intermediateExists: false, | ||
| partitionCount: 0, | ||
| mirrorTriggerExists: false, | ||
| retiredMirrorTriggerExists: false, | ||
| originalIsPartitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| test("outputs human-readable status", async ({ cli, commandContext }) => { | ||
| const exitCode = await cli.run(["status", "posts"], commandContext); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| const output = commandContext.stdout.read()?.toString(); | ||
| expect(output).toContain("Table: posts"); | ||
| expect(output).toContain("Intermediate exists:"); | ||
| expect(output).toContain("Original is partitioned:"); | ||
| }); | ||
| }); |
This file contains hidden or 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,53 @@ | ||
| import { Command, Option } from "clipanion"; | ||
|
|
||
| import { BaseCommand } from "./base.js"; | ||
| import { type Pgslice } from "../pgslice.js"; | ||
|
|
||
| export class StatusCommand extends BaseCommand { | ||
| static override paths = [["status"]]; | ||
|
|
||
| static override usage = Command.Usage({ | ||
| description: "Show status information about a table's partitioning state", | ||
| details: ` | ||
| This command displays the current state of a table's partitioning workflow, | ||
| including: | ||
| - Whether intermediate table exists | ||
| - Number of partitions | ||
| - Whether mirroring triggers are enabled | ||
| - Whether the original table is partitioned | ||
| `, | ||
| examples: [ | ||
| ["Show status for a table", "$0 status posts"], | ||
| ["Show status with explicit schema", "$0 status myschema.posts"], | ||
| ["Output as JSON", "$0 status posts --json"], | ||
| ], | ||
| }); | ||
|
|
||
| table = Option.String({ required: true, name: "table" }); | ||
| json = Option.Boolean("--json", false, { | ||
| description: "Output status as JSON", | ||
| }); | ||
|
|
||
| override async perform(pgslice: Pgslice): Promise<void> { | ||
| const status = await pgslice.status({ table: this.table }); | ||
|
|
||
| if (this.json) { | ||
| this.context.stdout.write(JSON.stringify(status, null, 2) + "\n"); | ||
| } else { | ||
| this.context.stdout.write(`Table: ${this.table}\n`); | ||
| this.context.stdout.write( | ||
| `Intermediate exists: ${status.intermediateExists}\n`, | ||
| ); | ||
| this.context.stdout.write( | ||
| `Original is partitioned: ${status.originalIsPartitioned}\n`, | ||
| ); | ||
| this.context.stdout.write(`Partition count: ${status.partitionCount}\n`); | ||
| this.context.stdout.write( | ||
| `Mirror trigger exists: ${status.mirrorTriggerExists}\n`, | ||
| ); | ||
| this.context.stdout.write( | ||
| `Retired mirror trigger exists: ${status.retiredMirrorTriggerExists}\n`, | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though it adds more code my preference was to delegate the actual logic to Table in order to maintain separation of concerns.