Skip to content

Commit

Permalink
Make CLI backwards compatible with non pgroll branches (#1494)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Rico <[email protected]>
Co-authored-by: Alexis Rico <[email protected]>
Co-authored-by: Andrew Farries <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2024
1 parent e68dc03 commit cc673c9
Show file tree
Hide file tree
Showing 36 changed files with 5,020 additions and 3,548 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-buckets-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xata.io/client': minor
---

[Breaking] Update `XataApiClient` to use unified parameters
3 changes: 3 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"enquirer": "^2.4.1",
"env-editor": "^1.1.0",
"ini": "^4.1.3",
"lodash.keyby": "^4.6.0",
"lodash.compact": "^3.0.1",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
Expand All @@ -51,6 +52,7 @@
"text-table": "^0.2.0",
"tslib": "^2.6.3",
"tmp": "^0.2.3",
"type-fest": "^4.18.1",
"which": "^4.0.0",
"zod": "^3.23.8"
},
Expand All @@ -59,6 +61,7 @@
"@types/babel__core": "^7.20.5",
"@types/lodash.compact": "^3.0.9",
"@types/lodash.get": "^4.4.9",
"@types/lodash.keyby": "^4.6.9",
"@types/lodash.set": "^4.3.9",
"@types/relaxed-json": "^1.0.4",
"@types/text-table": "^0.2.5",
Expand Down
36 changes: 23 additions & 13 deletions cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
message: 'New workspace name'
});
if (!name) return this.error('No workspace name provided');
const workspace = await xata.api.workspaces.createWorkspace({ data: { name } });
const workspace = await xata.api.workspaces.createWorkspace({ body: { name } });
return workspace.id;
} else if (workspaces.workspaces.length === 1) {
const workspace = workspaces.workspaces[0].id;
Expand Down Expand Up @@ -309,7 +309,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
options: { allowCreate?: boolean } = {}
): Promise<{ name: string; region: string }> {
const xata = await this.getXataClient();
const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace });
const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({
pathParams: { workspaceId: workspace }
});

if (dbs.length > 0) {
const choices = dbs.map((db) => ({
Expand Down Expand Up @@ -355,7 +357,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
} = {}
): Promise<string> {
const xata = await this.getXataClient();
const { branches = [] } = await xata.api.branches.getBranchList({ workspace, region, database });
const { branches = [] } = await xata.api.branch.getBranchList({
pathParams: { workspace, region, dbName: database }
});

const EMPTY_CHOICE = '$empty';
const CREATE_CHOICE = '$create';
Expand Down Expand Up @@ -421,7 +425,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
);
if (!name) return this.error('No database name provided');

const { regions } = await xata.api.database.listRegions({ workspace });
const { regions } = await xata.api.databases.listRegions({ pathParams: { workspaceId: workspace } });
const { region } = await this.prompt(
{
type: 'select',
Expand All @@ -434,7 +438,10 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
);
if (!region) return this.error('No region selected');

const result = await xata.api.database.createDatabase({ workspace, database: name, data: { region } });
const result = await xata.api.databases.createDatabase({
pathParams: { workspaceId: workspace, dbName: name },
body: { region }
});

return { name: result.databaseName, region };
}
Expand All @@ -455,9 +462,12 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
});

if (!from) {
await xata.api.branches.createBranch({ workspace, region, database, branch: name });
await xata.api.branch.createBranch({ pathParams: { workspace, region, dbBranchName: `${database}:${name}` } });
} else {
await xata.api.branches.createBranch({ workspace, region, database, branch: name, from });
await xata.api.branch.createBranch({
pathParams: { workspace, region, dbBranchName: `${database}:${name}` },
body: { from }
});
}

return name;
Expand Down Expand Up @@ -566,11 +576,8 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
async deploySchema(workspace: string, region: string, database: string, branch: string, schema: Schemas.Schema) {
const xata = await this.getXataClient();
const compare = await xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
schema
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { schema }
});

if (compare.edits.operations.length === 0) {
Expand All @@ -587,7 +594,10 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
});
if (!confirm) return this.exit(1);

await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits: compare.edits });
await xata.api.migrations.applyBranchSchemaEdit({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { edits: compare.edits }
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion cli/src/commands/branch/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default class BranchCreate extends BaseCommand<typeof BranchCreate> {
const { from } = flags;

try {
const result = await xata.api.branches.createBranch({ workspace, region, database, branch, from });
const result = await xata.api.branch.createBranch({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { from }
});

if (this.jsonEnabled()) return result;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/branch/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class BranchDelete extends BaseCommand<typeof BranchDelete> {
if (!confirm) return this.exit(1);
if (confirm !== branch) return this.error('The branch name did not match');

await xata.api.branches.deleteBranch({ workspace, region, database, branch });
await xata.api.branch.deleteBranch({ pathParams: { workspace, region, dbBranchName: `${database}:${branch}` } });

if (this.jsonEnabled()) return {};

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/branch/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class BranchList extends BaseCommand<typeof BranchList> {
const { workspace, region, database } = await this.getParsedDatabaseURL(flags.db);

const xata = await this.getXataClient();
const { branches } = await xata.api.branches.getBranchList({ workspace, region, database });
const { branches } = await xata.api.branch.getBranchList({ pathParams: { workspace, region, dbName: database } });

if (this.jsonEnabled()) return branches;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/dbs/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class DatabasesDelete extends BaseCommand<typeof DatabasesDelete>
if (!confirm) return this.exit(1);
if (confirm !== database) return this.error('The database name did not match');

await xata.api.database.deleteDatabase({ workspace, database });
await xata.api.databases.deleteDatabase({ pathParams: { workspaceId: workspace, dbName: database } });

if (this.jsonEnabled()) return {};

Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/dbs/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class DatabasesList extends BaseCommand<typeof DatabasesList> {
(await this.getWorkspace());

const xata = await this.getXataClient();
const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace });
const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({
pathParams: { workspaceId: workspace }
});

if (this.jsonEnabled()) return dbs;

Expand Down
5 changes: 4 additions & 1 deletion cli/src/commands/dbs/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export default class DatabasesRename extends BaseCommand<typeof DatabasesRename>
if (!confirm) return this.exit(1);
if (confirm !== database) return this.error('The database name did not match');

await xata.api.database.renameDatabase({ workspace, database, newName });
await xata.api.databases.renameDatabase({
pathParams: { workspaceId: workspace, dbName: database },
body: { newName }
});

if (this.jsonEnabled()) return {};

Expand Down
18 changes: 6 additions & 12 deletions cli/src/commands/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Args } from '@oclif/core';
import { BaseCommand } from '../../base.js';
import { getLocalMigrationFiles } from '../../migrations/files.js';
import { buildMigrationDiff } from '../../utils/diff.js';
import compact from 'lodash.compact';

export default class Diff extends BaseCommand<typeof Diff> {
static description = 'Compare two local or remote branches';
Expand Down Expand Up @@ -34,24 +35,17 @@ export default class Diff extends BaseCommand<typeof Diff> {
this.info(`Diff command is experimental, use with caution`);

const localMigrationFiles = await getLocalMigrationFiles();
const schemaOperations = localMigrationFiles.flatMap((migrationFile) => migrationFile.operations);
const schemaOperations = compact(localMigrationFiles.flatMap((migrationFile) => migrationFile.operations));

const apiRequest =
args.branch && args.base
? xata.api.migrations.compareBranchSchemas({
workspace,
region,
database,
branch: args.branch,
compare: args.base
pathParams: { workspace, region, dbBranchName: `${database}:${args.branch}`, branchName: args.base },
body: {}
})
: xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
schema: { tables: [] },
schemaOperations: schemaOperations as any
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { schema: { tables: [] }, schemaOperations }
});

const {
Expand Down
Loading

0 comments on commit cc673c9

Please sign in to comment.