Skip to content

Commit e9af96c

Browse files
gnzjgoampcode-com
andcommitted
feat: add --last-partition flag to branch creation (#135)
Amp-Thread-ID: https://ampcode.com/threads/T-019d1b0d-6dca-7013-a8f8-af75363aae7d Co-authored-by: Amp <amp@ampcode.com>
1 parent 3c0cca0 commit e9af96c

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/api/branches.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,21 @@ 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+
}
152161

153162
const response = await tinybirdFetch(url.toString(), {
154163
method: "POST",
@@ -325,7 +334,8 @@ export async function branchExists(
325334
*/
326335
export async function getOrCreateBranch(
327336
config: BranchApiConfig,
328-
name: string
337+
name: string,
338+
options?: CreateBranchOptions
329339
): Promise<GetOrCreateBranchResult> {
330340
// First try to get the existing branch
331341
try {
@@ -334,7 +344,7 @@ export async function getOrCreateBranch(
334344
} catch (error) {
335345
// If it's a 404, create the branch
336346
if (error instanceof BranchApiError && error.status === 404) {
337-
const branch = await createBranch(config, name);
347+
const branch = await createBranch(config, name, options);
338348
return { ...branch, wasCreated: true };
339349
}
340350
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)