Skip to content
Open
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
40 changes: 25 additions & 15 deletions deploy/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,10 @@ const databasesLinkCommand = new Command<DatabaseContext>()
"Test connection without linking",
"link --dry-run my-db --hostname db.example.com",
)
.option("--hostname <string>", "The hostname to use for the database", {
required: true,
conflicts: ["connectionString"],
})
.option("--username <string>", "The username to use for the database", {
conflicts: ["connectionString"],
})
.option("--password <string>", "The password to use for the database", {
conflicts: ["connectionString"],
})
.option("--port <number>", "The port to use for the database", {
conflicts: ["connectionString"],
})
.option("--hostname <string>", "The hostname to use for the database")
.option("--username <string>", "The username to use for the database")
.option("--password <string>", "The password to use for the database")
.option("--port <number>", "The port to use for the database")
.option("--cert <string>", "The SSL certificate to use for the database")
.option(
"--dry-run",
Expand All @@ -111,6 +102,23 @@ const databasesLinkCommand = new Command<DatabaseContext>()
.action(actionHandler(async (config, options, name, connectionString) => {
config.noCreate();

// `connectionString` is a positional argument, so its mutual exclusion
// with --hostname/--port/--username/--password can't be expressed via
// cliffy's `conflicts` (which only accepts option names). Validate manually.
const hasIndividualFlags = options.hostname || options.port !== undefined ||
options.username || options.password;
if (connectionString && hasIndividualFlags) {
throw new TypeError(
"Provide either a connection string or the individual " +
"--hostname/--port/--username/--password flags, not both.",
);
}
if (!connectionString && !options.hostname) {
throw new TypeError(
"A connection string or --hostname is required.",
);
}

const org = await getOrg(options, config, options.org);
const trpcClient = createTrpcClient(options);

Expand Down Expand Up @@ -152,7 +160,9 @@ const databasesLinkCommand = new Command<DatabaseContext>()

const connectionConfig = {
hostname: hostname,
port: port || null,
// The backend expects a number; `--port <number>` and the parsed
// connection-string port both arrive as strings, so coerce here.
port: port != null ? Number(port) : null,
username: username || null,
password: password || null,
certificate: options.cert || null,
Expand All @@ -170,7 +180,7 @@ const databasesLinkCommand = new Command<DatabaseContext>()
org: org,
slug: name,
engine,
connectionConfig,
connection_config: connectionConfig,
});
console.log(`${green("✔")} Successfully linked database '${name}'.`);
}
Expand Down
Loading