Skip to content
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

Add an option to terminate all existing connections to the database when resetting a database #239

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ Options:
--shadow Applies migrations to shadow DB. [boolean] [default: false]
--erase This is your double opt-in to make it clear this DELETES
EVERYTHING. [boolean] [default: false]
--force Terminate all existing connections to the database.
[boolean] [default: false]
```


Expand Down
63 changes: 63 additions & 0 deletions __tests__/reset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { reset } from "../src/commands/reset";
import { withClient } from "../src/pgReal";

jest.mock("../src/commands/migrate");

jest.mock("../src/pgReal", () => {
const actual = jest.requireActual("../src/pgReal");
const allAutoMocked = jest.createMockFromModule<any>("../src/pgReal");

return {
...allAutoMocked,
withClient: jest.fn(),
escapeIdentifier: actual.escapeIdentifier,
};
});

let mockPgClient: {
query: jest.Mock<any, any, any>;
};

describe("reset", () => {
beforeEach(async () => {
mockPgClient = {
query: jest.fn(),
};

(withClient as any).mockImplementation(
async (_connString: any, _settings: any, callback: any) => {
await callback(mockPgClient);
},
);
});

it("calls DROP DATABASE without FORCE when force is false", async () => {
await reset(
{
connectionString: "test_db",
},
false,
false,
);

expect(mockPgClient.query).toHaveBeenNthCalledWith(
1,
'DROP DATABASE IF EXISTS "test_db";',
);
});

it("calls DROP DATABASE with FORCE when force is true", async () => {
await reset(
{
connectionString: "test_db",
},
false,
true,
);

expect(mockPgClient.query).toHaveBeenNthCalledWith(
1,
'DROP DATABASE IF EXISTS "test_db" WITH (FORCE);',
);
});
});
2 changes: 1 addition & 1 deletion src/commands/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function _commit(
Message: message ? message : undefined,
...omit(headers, ["Previous", "Hash", "Message"]),
});
await _reset(parsedSettings, true);
await _reset(parsedSettings, true, false);
const newMigrationFilepath = `${committedMigrationsFolder}/${newMigrationFilename}`;
await fsp.writeFile(newMigrationFilepath, finalBody);
await fsp.chmod(newMigrationFilepath, "440");
Expand Down
33 changes: 27 additions & 6 deletions src/commands/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { _migrate } from "./migrate";
interface ResetArgv extends CommonArgv {
shadow: boolean;
erase: boolean;
force: boolean;
}

export async function _reset(
parsedSettings: ParsedSettings,
shadow: boolean,
force: boolean,
): Promise<void> {
const connectionString = shadow
? parsedSettings.shadowConnectionString
Expand All @@ -34,9 +36,15 @@ export async function _reset(
}
const databaseOwner = parsedSettings.databaseOwner;
const logSuffix = shadow ? "[shadow]" : "";
await pgClient.query(
`DROP DATABASE IF EXISTS ${escapeIdentifier(databaseName)};`,
);
if (force) {
await pgClient.query(
`DROP DATABASE IF EXISTS ${escapeIdentifier(databaseName)} WITH (FORCE);`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced in PG13, so this is technically supported by all supported versions of PostgreSQL now. Interesting.

);
} else {
await pgClient.query(
`DROP DATABASE IF EXISTS ${escapeIdentifier(databaseName)};`,
);
}
parsedSettings.logger.info(
`graphile-migrate${logSuffix}: dropped database '${databaseName}'`,
);
Expand All @@ -63,9 +71,13 @@ export async function _reset(
await _migrate(parsedSettings, shadow);
}

export async function reset(settings: Settings, shadow = false): Promise<void> {
export async function reset(
settings: Settings,
shadow = false,
force = false,
): Promise<void> {
const parsedSettings = await parseSettings(settings, shadow);
return _reset(parsedSettings, shadow);
return _reset(parsedSettings, shadow, force);
}

export const resetCommand: CommandModule<Record<string, never>, ResetArgv> = {
Expand All @@ -85,6 +97,11 @@ export const resetCommand: CommandModule<Record<string, never>, ResetArgv> = {
description:
"This is your double opt-in to make it clear this DELETES EVERYTHING.",
},
force: {
type: "boolean",
default: false,
description: "Terminate all existing connections to the database.",
},
},
handler: async (argv) => {
if (!argv.erase) {
Expand All @@ -94,6 +111,10 @@ export const resetCommand: CommandModule<Record<string, never>, ResetArgv> = {
);
process.exit(2);
}
await reset(await getSettings({ configFile: argv.config }), argv.shadow);
await reset(
await getSettings({ configFile: argv.config }),
argv.shadow,
argv.force,
);
},
};
2 changes: 1 addition & 1 deletion src/commands/uncommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function _uncommit(parsedSettings: ParsedSettings): Promise<void> {
);

// Reset shadow
await _reset(parsedSettings, true);
await _reset(parsedSettings, true, false);
await _migrate(parsedSettings, true, true);
}

Expand Down