diff --git a/package.json b/package.json index efa750e..11c6799 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tinybirdco/sdk", - "version": "0.0.62", + "version": "0.0.63", "description": "TypeScript SDK for Tinybird Forward - define datasources and pipes as TypeScript", "type": "module", "main": "./dist/index.js", diff --git a/src/api/branches.ts b/src/api/branches.ts index 9c6368c..774d12c 100644 --- a/src/api/branches.ts +++ b/src/api/branches.ts @@ -143,12 +143,26 @@ async function pollJob( * @param name - Branch name to create * @returns The created branch with token */ +export interface CreateBranchOptions { + /** Copy the last partition of production data into the branch */ + lastPartition?: boolean; +} + export async function createBranch( config: BranchApiConfig, - name: string + name: string, + options?: CreateBranchOptions ): Promise { const url = new URL("/v1/environments", config.baseUrl); url.searchParams.set("name", name); + if (options?.lastPartition) { + url.searchParams.set("last_partition", "1"); + } + + const debug = !!process.env.TINYBIRD_DEBUG; + if (debug) { + console.log(`[debug] POST ${url.toString()}`); + } const response = await tinybirdFetch(url.toString(), { method: "POST", @@ -325,7 +339,8 @@ export async function branchExists( */ export async function getOrCreateBranch( config: BranchApiConfig, - name: string + name: string, + options?: CreateBranchOptions ): Promise { // First try to get the existing branch try { @@ -334,7 +349,7 @@ export async function getOrCreateBranch( } catch (error) { // If it's a 404, create the branch if (error instanceof BranchApiError && error.status === 404) { - const branch = await createBranch(config, name); + const branch = await createBranch(config, name, options); return { ...branch, wasCreated: true }; } throw error; diff --git a/src/cli/commands/build.ts b/src/cli/commands/build.ts index 0a9d3ee..24a364b 100644 --- a/src/cli/commands/build.ts +++ b/src/cli/commands/build.ts @@ -27,6 +27,8 @@ export interface BuildCommandOptions { tokenOverride?: string; /** Override the devMode from config */ devModeOverride?: DevMode; + /** Copy the last partition of production data when creating a branch */ + lastPartition?: boolean; } /** @@ -225,7 +227,8 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise void; /** Override the devMode from config */ devModeOverride?: DevMode; + /** Copy the last partition of production data when creating a branch */ + lastPartition?: boolean; } /** @@ -241,7 +243,8 @@ export async function runDev( baseUrl: config.baseUrl, token: config.token, }, - branchName + branchName, + { lastPartition: options.lastPartition } ); if (!tinybirdBranch.token) { diff --git a/src/cli/index.ts b/src/cli/index.ts index 191b444..fa5a24a 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -270,6 +270,7 @@ function createCli(): Command { .option("--debug", "Show debug output including API requests/responses") .option("--local", "Use local Tinybird container") .option("--branch", "Use Tinybird cloud with branches") + .option("--last-partition", "Copy the last partition of production data when creating a branch") .action(async (options) => { if (options.debug) { process.env.TINYBIRD_DEBUG = "1"; @@ -286,6 +287,7 @@ function createCli(): Command { const result = await runBuild({ dryRun: options.dryRun, devModeOverride, + lastPartition: options.lastPartition, }); const { build, deploy, branchInfo } = result; @@ -677,6 +679,7 @@ function createCli(): Command { .description("Watch for changes and sync with Tinybird") .option("--local", "Use local Tinybird container") .option("--branch", "Use Tinybird cloud with branches") + .option("--last-partition", "Copy the last partition of production data when creating a branch") .action(async (options) => { // Determine devMode override let devModeOverride: DevMode | undefined; @@ -689,6 +692,7 @@ function createCli(): Command { try { const controller = await runDev({ devModeOverride, + lastPartition: options.lastPartition, onLoginComplete: (info) => { console.log("\nAuthentication successful!"); if (info.workspaceName) {