Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.0.78] - 2026-06-15

### Changed

- Updated Tinybird CLI behavior to match the `4.6.1` branch-management changes.
- Branch data config handling now uses `branch_data_mode`; legacy `branch_data_on_create` now triggers an explicit migration error.
- `branch_data_mode` now only accepts `last_partition` as a user-facing value.
- In local development mode, branch data mode warnings are now shown only when `branch_data_mode` is explicitly set in `tinybird.config.json`.
- `tinybird branch create` and `tinybird branch clear` now show a deprecation warning (instead of failing) when `--ignore-datasource` is passed, then continue by ignoring that flag.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tinybirdco/sdk",
"version": "0.0.77",
"version": "0.0.78",
"description": "TypeScript SDK for Tinybird Forward - define datasources and pipes as TypeScript",
"type": "module",
"main": "./dist/index.js",
Expand Down
39 changes: 38 additions & 1 deletion src/api/branches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,40 @@ describe("Branch API client", () => {
);
});

it("uses data=last_partition wire format when branch_data_mode is set", async () => {
const mockBranch = {
id: "branch-123",
name: "my-feature",
token: "p.branch-token",
created_at: "2024-01-01T00:00:00Z",
};

mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
job: { id: "job-123", status: "waiting" },
workspace: { id: "ws-123" },
}),
});
mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ id: "job-123", status: "done" }),
});
mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockBranch),
});

await createBranch(config, "my-feature", {
branch_data_mode: "last_partition",
});

const [createUrl] = mockFetch.mock.calls[0];
const createParsed = expectFromParam(createUrl);
expect(createParsed.searchParams.get("name")).toBe("my-feature");
expect(createParsed.searchParams.get("data")).toBe("last_partition");
});

it("uses custom fetch when provided", async () => {
const customFetch = vi
.fn()
Expand Down Expand Up @@ -494,7 +528,9 @@ describe("Branch API client", () => {
json: () => Promise.resolve(newBranch),
});

const result = await clearBranch(config, "my-feature");
const result = await clearBranch(config, "my-feature", {
branch_data_mode: "last_partition",
});

expect(mockFetch).toHaveBeenCalledTimes(5);

Expand All @@ -515,6 +551,7 @@ describe("Branch API client", () => {
const createParsed = expectFromParam(createUrl);
expect(createParsed.pathname).toBe("/v1/environments");
expect(createParsed.searchParams.get("name")).toBe("my-feature");
expect(createParsed.searchParams.get("data")).toBe("last_partition");
expect(createInit.method).toBe("POST");

expect(result).toEqual(newBranch);
Expand Down
14 changes: 8 additions & 6 deletions src/api/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Uses the /v1/environments endpoints (Forward API)
*/

import type { BranchDataMode } from "../cli/config-types.js";
import { createTinybirdFetcher } from "./fetcher.js";

/**
Expand Down Expand Up @@ -152,8 +153,8 @@ async function pollJob(
* @returns The created branch with token
*/
export interface CreateBranchOptions {
/** Copy the last partition of production data into the branch */
lastPartition?: boolean;
/** Data mode applied when creating the branch */
branch_data_mode?: BranchDataMode;
}

export async function createBranch(
Expand All @@ -164,8 +165,8 @@ export async function createBranch(
const fetchFn = getFetch(config);
const url = new URL("/v1/environments", config.baseUrl);
url.searchParams.set("name", name);
if (options?.lastPartition) {
url.searchParams.set("last_partition", "1");
if (options?.branch_data_mode) {
url.searchParams.set("data", options.branch_data_mode);
}

const debug = !!process.env.TINYBIRD_DEBUG;
Expand Down Expand Up @@ -377,13 +378,14 @@ export async function getOrCreateBranch(
*/
export async function clearBranch(
config: BranchApiConfig,
name: string
name: string,
options?: CreateBranchOptions
): Promise<TinybirdBranch> {
// Delete the branch
await deleteBranch(config, name);

// Recreate the branch
const branch = await createBranch(config, name);
const branch = await createBranch(config, name, options);

return branch;
}
176 changes: 176 additions & 0 deletions src/cli/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("Build Command", () => {
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: null,
});

vi.mocked(buildFromInclude).mockResolvedValue({
Expand Down Expand Up @@ -156,6 +157,7 @@ describe("Build Command", () => {
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: null,
});

vi.mocked(buildFromInclude).mockResolvedValue({
Expand Down Expand Up @@ -242,6 +244,7 @@ describe("Build Command", () => {
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: null,
});

vi.mocked(buildFromInclude).mockResolvedValue({
Expand Down Expand Up @@ -307,4 +310,177 @@ describe("Build Command", () => {
expect(getLocalTokens).toHaveBeenCalled();
});
});

describe("branch_data_mode wiring", () => {
it("uses config-only last_partition when flag is absent", async () => {
const { loadConfigAsync } = await import("../config.js");
const { buildFromInclude } = await import("../../generator/index.js");
const { getOrCreateBranch } = await import("../../api/branches.js");
const { buildToTinybird } = await import("../../api/build.js");
const { getWorkspace } = await import("../../api/workspaces.js");
const { getBranchDashboardUrl } = await import("../../api/dashboard.js");

vi.mocked(loadConfigAsync).mockResolvedValue({
include: ["test.ts"],
token: "p.test-token",
baseUrl: "https://api.tinybird.co",
configPath: "/test/tinybird.config.json",
devMode: "branch",
cwd: "/test",
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: "last_partition",
});
vi.mocked(buildFromInclude).mockResolvedValue({
resources: { datasources: [], pipes: [], connections: [] },
entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] },
stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 },
});
vi.mocked(getOrCreateBranch).mockResolvedValue({
id: "branch-id",
name: "feature_test",
token: "branch-token",
wasCreated: false,
created_at: "2024-01-01",
});
vi.mocked(getWorkspace).mockResolvedValue({
id: "ws-id",
name: "test-workspace",
user_id: "user-id",
user_email: "user@test.com",
scope: "USER",
main: null,
});
vi.mocked(getBranchDashboardUrl).mockReturnValue("https://app.tinybird.co/dashboard");
vi.mocked(buildToTinybird).mockResolvedValue({
success: true,
result: "success",
datasourceCount: 0,
pipeCount: 0,
connectionCount: 0,
});

await runBuild();
expect(getOrCreateBranch).toHaveBeenCalledWith(
expect.any(Object),
"feature_test",
{ branch_data_mode: "last_partition" }
);
});

it("keeps CLI --last-partition precedence over config", async () => {
const { loadConfigAsync } = await import("../config.js");
const { buildFromInclude } = await import("../../generator/index.js");
const { getOrCreateBranch } = await import("../../api/branches.js");
const { buildToTinybird } = await import("../../api/build.js");
const { getWorkspace } = await import("../../api/workspaces.js");
const { getBranchDashboardUrl } = await import("../../api/dashboard.js");

vi.mocked(loadConfigAsync).mockResolvedValue({
include: ["test.ts"],
token: "p.test-token",
baseUrl: "https://api.tinybird.co",
configPath: "/test/tinybird.config.json",
devMode: "branch",
cwd: "/test",
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: null,
});
vi.mocked(buildFromInclude).mockResolvedValue({
resources: { datasources: [], pipes: [], connections: [] },
entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] },
stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 },
});
vi.mocked(getOrCreateBranch).mockResolvedValue({
id: "branch-id",
name: "feature_test",
token: "branch-token",
wasCreated: false,
created_at: "2024-01-01",
});
vi.mocked(getWorkspace).mockResolvedValue({
id: "ws-id",
name: "test-workspace",
user_id: "user-id",
user_email: "user@test.com",
scope: "USER",
main: null,
});
vi.mocked(getBranchDashboardUrl).mockReturnValue("https://app.tinybird.co/dashboard");
vi.mocked(buildToTinybird).mockResolvedValue({
success: true,
result: "success",
datasourceCount: 0,
pipeCount: 0,
connectionCount: 0,
});

await runBuild({ lastPartition: true });
expect(getOrCreateBranch).toHaveBeenCalledWith(
expect.any(Object),
"feature_test",
{ branch_data_mode: "last_partition" }
);
});

it("ignores config branch_data_mode in local mode", async () => {
const { loadConfigAsync } = await import("../config.js");
const { buildFromInclude } = await import("../../generator/index.js");
const { getOrCreateBranch } = await import("../../api/branches.js");
const { getLocalTokens, getOrCreateLocalWorkspace, getLocalWorkspaceName } = await import("../../api/local.js");
const { buildToTinybird } = await import("../../api/build.js");
const { getWorkspace } = await import("../../api/workspaces.js");
const { getLocalDashboardUrl } = await import("../../api/dashboard.js");

vi.mocked(loadConfigAsync).mockResolvedValue({
include: ["test.ts"],
token: "p.test-token",
baseUrl: "https://api.tinybird.co",
configPath: "/test/tinybird.config.json",
devMode: "local",
cwd: "/test",
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: "last_partition",
});
vi.mocked(buildFromInclude).mockResolvedValue({
resources: { datasources: [], pipes: [], connections: [] },
entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] },
stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 },
});
vi.mocked(getLocalTokens).mockResolvedValue({
admin_token: "admin-token",
user_token: "user-token",
workspace_admin_token: "workspace-admin-token",
});
vi.mocked(getWorkspace).mockResolvedValue({
id: "ws-id",
name: "test-workspace",
user_id: "user-id",
user_email: "user@test.com",
scope: "USER",
main: null,
});
vi.mocked(getLocalWorkspaceName).mockReturnValue("feature_test_workspace");
vi.mocked(getOrCreateLocalWorkspace).mockResolvedValue({
workspace: { id: "local-ws-id", name: "feature_test_workspace", token: "local-token" },
wasCreated: false,
});
vi.mocked(getLocalDashboardUrl).mockReturnValue("http://localhost:7181/dashboard");
vi.mocked(buildToTinybird).mockResolvedValue({
success: true,
result: "success",
datasourceCount: 0,
pipeCount: 0,
connectionCount: 0,
});

await runBuild();
expect(getOrCreateBranch).not.toHaveBeenCalled();
});
});
});
11 changes: 9 additions & 2 deletions src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Build command - generates and pushes resources to Tinybird branches
*/

import { loadConfigAsync, LOCAL_BASE_URL, type ResolvedConfig, type DevMode } from "../config.js";
import { loadConfigAsync, LOCAL_BASE_URL, type ResolvedConfig, type DevMode, type BranchDataMode } from "../config.js";
import { buildFromInclude, type BuildFromIncludeResult } from "../../generator/index.js";
import { buildToTinybird, type BuildApiResult } from "../../api/build.js";
import { getOrCreateBranch } from "../../api/branches.js";
Expand Down Expand Up @@ -225,13 +225,20 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
console.log(`[debug] Getting/creating Tinybird branch: ${config.tinybirdBranch}`);
}
try {
const branchDataMode: BranchDataMode | undefined =
options.lastPartition || config.branchDataMode === "last_partition"
? "last_partition"
: undefined;
const branchOptions = branchDataMode
? { branch_data_mode: branchDataMode }
: undefined;
const tinybirdBranch = await getOrCreateBranch(
{
baseUrl: config.baseUrl,
token: config.token,
},
config.tinybirdBranch!,
{ lastPartition: options.lastPartition }
branchOptions
);

if (!tinybirdBranch.token) {
Expand Down
9 changes: 8 additions & 1 deletion src/cli/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
clearBranch,
BranchApiError,
type CreateBranchOptions,
} from "../../api/branches.js";
import {
setBranchToken,
Expand Down Expand Up @@ -147,12 +148,18 @@ async function clearCloudBranch(config: ResolvedConfig): Promise<ClearResult> {
});

// Clear the branch (delete and recreate)
const branchOptions: CreateBranchOptions | undefined =
config.devMode !== "local" && config.branchDataMode === "last_partition"
? { branch_data_mode: "last_partition" }
: undefined;

const newBranch = await clearBranch(
{
baseUrl: config.baseUrl,
token: config.token,
},
branchName
branchName,
branchOptions
);

// Update the cached token with the new branch token
Expand Down
Loading
Loading