Skip to content

Commit ed46c68

Browse files
authored
Merge pull request #139 from tinybirdco/fix/135-last-partition-branch
2 parents 3c0cca0 + bc64afc commit ed46c68

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tinybirdco/sdk",
3-
"version": "0.0.62",
3+
"version": "0.0.63",
44
"description": "TypeScript SDK for Tinybird Forward - define datasources and pipes as TypeScript",
55
"type": "module",
66
"main": "./dist/index.js",

src/api/branches.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,26 @@ async function pollJob(
143143
* @param name - Branch name to create
144144
* @returns The created branch with token
145145
*/
146+
export interface CreateBranchOptions {
147+
/** Copy the last partition of production data into the branch */
148+
lastPartition?: boolean;
149+
}
150+
146151
export async function createBranch(
147152
config: BranchApiConfig,
148-
name: string
153+
name: string,
154+
options?: CreateBranchOptions
149155
): Promise<TinybirdBranch> {
150156
const url = new URL("/v1/environments", config.baseUrl);
151157
url.searchParams.set("name", name);
158+
if (options?.lastPartition) {
159+
url.searchParams.set("last_partition", "1");
160+
}
161+
162+
const debug = !!process.env.TINYBIRD_DEBUG;
163+
if (debug) {
164+
console.log(`[debug] POST ${url.toString()}`);
165+
}
152166

153167
const response = await tinybirdFetch(url.toString(), {
154168
method: "POST",
@@ -325,7 +339,8 @@ export async function branchExists(
325339
*/
326340
export async function getOrCreateBranch(
327341
config: BranchApiConfig,
328-
name: string
342+
name: string,
343+
options?: CreateBranchOptions
329344
): Promise<GetOrCreateBranchResult> {
330345
// First try to get the existing branch
331346
try {
@@ -334,7 +349,7 @@ export async function getOrCreateBranch(
334349
} catch (error) {
335350
// If it's a 404, create the branch
336351
if (error instanceof BranchApiError && error.status === 404) {
337-
const branch = await createBranch(config, name);
352+
const branch = await createBranch(config, name, options);
338353
return { ...branch, wasCreated: true };
339354
}
340355
throw error;

src/cli/commands/build.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface BuildCommandOptions {
2727
tokenOverride?: string;
2828
/** Override the devMode from config */
2929
devModeOverride?: DevMode;
30+
/** Copy the last partition of production data when creating a branch */
31+
lastPartition?: boolean;
3032
}
3133

3234
/**
@@ -225,7 +227,8 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
225227
baseUrl: config.baseUrl,
226228
token: config.token,
227229
},
228-
config.tinybirdBranch!
230+
config.tinybirdBranch!,
231+
{ lastPartition: options.lastPartition }
229232
);
230233

231234
if (!tinybirdBranch.token) {

src/cli/commands/dev.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export interface DevCommandOptions {
6464
onSchemaValidation?: (result: SchemaValidationResult) => void;
6565
/** Override the devMode from config */
6666
devModeOverride?: DevMode;
67+
/** Copy the last partition of production data when creating a branch */
68+
lastPartition?: boolean;
6769
}
6870

6971
/**
@@ -241,7 +243,8 @@ export async function runDev(
241243
baseUrl: config.baseUrl,
242244
token: config.token,
243245
},
244-
branchName
246+
branchName,
247+
{ lastPartition: options.lastPartition }
245248
);
246249

247250
if (!tinybirdBranch.token) {

src/cli/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ function createCli(): Command {
270270
.option("--debug", "Show debug output including API requests/responses")
271271
.option("--local", "Use local Tinybird container")
272272
.option("--branch", "Use Tinybird cloud with branches")
273+
.option("--last-partition", "Copy the last partition of production data when creating a branch")
273274
.action(async (options) => {
274275
if (options.debug) {
275276
process.env.TINYBIRD_DEBUG = "1";
@@ -286,6 +287,7 @@ function createCli(): Command {
286287
const result = await runBuild({
287288
dryRun: options.dryRun,
288289
devModeOverride,
290+
lastPartition: options.lastPartition,
289291
});
290292

291293
const { build, deploy, branchInfo } = result;
@@ -677,6 +679,7 @@ function createCli(): Command {
677679
.description("Watch for changes and sync with Tinybird")
678680
.option("--local", "Use local Tinybird container")
679681
.option("--branch", "Use Tinybird cloud with branches")
682+
.option("--last-partition", "Copy the last partition of production data when creating a branch")
680683
.action(async (options) => {
681684
// Determine devMode override
682685
let devModeOverride: DevMode | undefined;
@@ -689,6 +692,7 @@ function createCli(): Command {
689692
try {
690693
const controller = await runDev({
691694
devModeOverride,
695+
lastPartition: options.lastPartition,
692696
onLoginComplete: (info) => {
693697
console.log("\nAuthentication successful!");
694698
if (info.workspaceName) {

0 commit comments

Comments
 (0)