Skip to content

Commit 6752a22

Browse files
committed
v2.16.1: Add install action
1 parent b2ecf59 commit 6752a22

File tree

13 files changed

+116
-13
lines changed

13 files changed

+116
-13
lines changed

docs/functions/actionInstall.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[**@clickup/pg-microsharding**](../README.md)
2+
3+
***
4+
5+
[@clickup/pg-microsharding](../globals.md) / actionInstall
6+
7+
# Function: actionInstall()
8+
9+
> **actionInstall**(`args`): `Promise`\<`boolean`\>
10+
11+
Defined in: src/actions/actionInstall.ts:8
12+
13+
Installs the library in the provided databases.
14+
15+
## Parameters
16+
17+
| Parameter | Type |
18+
| ------ | ------ |
19+
| `args` | `Args` |
20+
21+
## Returns
22+
23+
`Promise`\<`boolean`\>

docs/functions/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **cli**(): `void`
1010
11-
Defined in: [src/cli.ts:207](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L207)
11+
Defined in: [src/cli.ts:212](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L212)
1212

1313
A wrapper around main() to call it from a bin script.
1414

docs/functions/install.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[**@clickup/pg-microsharding**](../README.md)
2+
3+
***
4+
5+
[@clickup/pg-microsharding](../globals.md) / install
6+
7+
# Function: install()
8+
9+
> **install**(`__namedParameters`): `Promise`\<`void`\>
10+
11+
Defined in: src/api/install.ts:10
12+
13+
Installs/updates microsharding schema and functions.
14+
15+
## Parameters
16+
17+
| Parameter | Type |
18+
| ------ | ------ |
19+
| `__namedParameters` | \{ `dsn`: `string`; \} |
20+
| `__namedParameters.dsn` | `string` |
21+
22+
## Returns
23+
24+
`Promise`\<`void`\>

docs/functions/main.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **main**(`argsIn`): `Promise`\<`boolean`\>
1010
11-
Defined in: [src/cli.ts:140](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L140)
11+
Defined in: [src/cli.ts:145](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L145)
1212

1313
Tool main function.
1414

docs/globals.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
- [actionAllocate](functions/actionAllocate.md)
1414
- [actionCleanup](functions/actionCleanup.md)
1515
- [actionFactor](functions/actionFactor.md)
16+
- [actionInstall](functions/actionInstall.md)
1617
- [actionList](functions/actionList.md)
1718
- [calcIslandWeights](functions/calcIslandWeights.md)
1819
- [actionMove](functions/actionMove.md)
1920
- [actionRebalance](functions/actionRebalance.md)
2021
- [allocate](functions/allocate.md)
2122
- [cleanup](functions/cleanup.md)
2223
- [factor](functions/factor.md)
24+
- [install](functions/install.md)
2325
- [move](functions/move.md)
2426
- [rebalance](functions/rebalance.md)
2527
- [weights](functions/weights.md)

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@clickup/pg-microsharding",
33
"description": "Microshards support for PostgreSQL",
4-
"version": "2.15.1",
4+
"version": "2.16.1",
55
"license": "MIT",
66
"keywords": [
77
"postgresql",

src/actions/actionInstall.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { install } from "../api/install";
2+
import { normalizeDsns } from "../internal/normalizeDsns";
3+
import type { Args } from "../internal/parseArgs";
4+
5+
/**
6+
* Installs the library in the provided databases.
7+
*/
8+
export async function actionInstall(args: Args): Promise<boolean> {
9+
const dsns = await normalizeDsns(args["dsns"] || args["dsn"]);
10+
if (dsns.length === 0) {
11+
throw "Please provide --dsn or --dsns, DB DSNs to install the library to";
12+
}
13+
14+
for (const dsn of dsns) {
15+
await install({ dsn });
16+
}
17+
18+
return true;
19+
}

src/api/install.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { realpathSync } from "fs";
2+
import { relative } from "path";
3+
import { libSchema } from "../internal/names";
4+
import { join, sql } from "../internal/quote";
5+
import { runSqlProgress } from "../internal/runSqlProgress";
6+
7+
/**
8+
* Installs/updates microsharding schema and functions.
9+
*/
10+
export async function install({ dsn }: { dsn: string }): Promise<void> {
11+
const scriptPath = relative(
12+
".",
13+
realpathSync(`${__dirname}/../../sql/pg-microsharding-up.sql`),
14+
);
15+
await runSqlProgress(
16+
dsn,
17+
join(
18+
[
19+
sql`
20+
CREATE SCHEMA IF NOT EXISTS ${libSchema()};
21+
SET search_path TO ${libSchema()};
22+
`,
23+
`\\i ${scriptPath}`,
24+
],
25+
"\n",
26+
),
27+
`Updating the library at ${dsn}`,
28+
"single-transaction",
29+
);
30+
}

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import pickBy from "lodash/pickBy";
55
import { actionAllocate } from "./actions/actionAllocate";
66
import { actionCleanup } from "./actions/actionCleanup";
77
import { actionFactor } from "./actions/actionFactor";
8+
import { actionInstall } from "./actions/actionInstall";
89
import { actionList } from "./actions/actionList";
910
import { actionMove } from "./actions/actionMove";
1011
import { actionRebalance } from "./actions/actionRebalance";
@@ -26,6 +27,9 @@ export { allocate, cleanup, factor, move, rebalance, weights, shellQuote };
2627
const HELP = unindent(`
2728
USAGE
2829
30+
pg-microsharding install
31+
[--dsn=DSN | --dsns=DNS1,DSN2,...]
32+
2933
pg-microsharding list | ls
3034
[--weight-sql='SELECT returning weight with optional unit']
3135
[--verbose]
@@ -127,11 +131,12 @@ const HELP = unindent(`
127131
const ACTIONS = {
128132
allocate: actionAllocate,
129133
cleanup: actionCleanup,
134+
factor: actionFactor,
135+
install: actionInstall,
130136
list: actionList,
131137
ls: actionList,
132138
move: actionMove,
133139
rebalance: actionRebalance,
134-
factor: actionFactor,
135140
};
136141

137142
/**

0 commit comments

Comments
 (0)