Skip to content
Closed
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.76",
"version": "0.0.78",
"description": "TypeScript SDK for Tinybird Forward - define datasources and pipes as TypeScript",
"type": "module",
"main": "./dist/index.js",
Expand Down
35 changes: 34 additions & 1 deletion src/api/branches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,38 @@ describe("Branch API client", () => {
);
});

it("uses last_partition=1 wire format when option is enabled", 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", { lastPartition: true });

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

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

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

expect(mockFetch).toHaveBeenCalledTimes(5);

Expand All @@ -515,6 +547,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("last_partition")).toBe("1");
expect(createInit.method).toBe("POST");

expect(result).toEqual(newBranch);
Expand Down
5 changes: 3 additions & 2 deletions src/api/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,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;
}
177 changes: 177 additions & 0 deletions src/cli/commands/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { runBuild } from "./build.js";
import { BranchDataMode } from "../config-types.js";

// Mock all dependencies
vi.mock("../config.js", () => ({
Expand Down Expand Up @@ -64,6 +65,7 @@ describe("Build Command", () => {
gitBranch: "feature-test",
tinybirdBranch: "feature_test",
isMainBranch: false,
branchDataMode: null,
});

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

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

vi.mocked(buildFromInclude).mockResolvedValue({
Expand Down Expand Up @@ -307,4 +311,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: 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",
{ lastPartition: true }
);
});

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",
{ lastPartition: true }
);
});

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: 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();
});
});
});
6 changes: 5 additions & 1 deletion src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { loadConfigAsync, LOCAL_BASE_URL, type ResolvedConfig, type DevMode } from "../config.js";
import { BranchDataMode } from "../config-types.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 +226,16 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
console.log(`[debug] Getting/creating Tinybird branch: ${config.tinybirdBranch}`);
}
try {
const lastPartitionFromConfig =
config.branchDataMode === BranchDataMode.LAST_PARTITION;
const lastPartitionFromFlag = Boolean(options.lastPartition);
const tinybirdBranch = await getOrCreateBranch(
{
baseUrl: config.baseUrl,
token: config.token,
},
config.tinybirdBranch!,
{ lastPartition: options.lastPartition }
{ lastPartition: lastPartitionFromFlag || lastPartitionFromConfig }
);

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

// Clear the branch (delete and recreate)
const branchOptions: CreateBranchOptions | undefined =
config.devMode !== "local" && config.branchDataMode === BranchDataMode.LAST_PARTITION
? { lastPartition: true }
: 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
1 change: 1 addition & 0 deletions src/cli/commands/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("Deploy command", () => {
tinybirdBranch: "feature_pro_610",
isMainBranch: false,
devMode: "branch",
branchDataMode: null,
});

vi.mocked(buildFromInclude).mockResolvedValue({
Expand Down
6 changes: 5 additions & 1 deletion src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type ResolvedConfig,
type DevMode,
} from "../config.js";
import { BranchDataMode } from "../config-types.js";
import { runBuild, type BuildCommandResult } from "./build.js";
import { getOrCreateBranch, type TinybirdBranch } from "../../api/branches.js";
import { browserLogin } from "../auth.js";
Expand Down Expand Up @@ -239,6 +240,9 @@ export async function runDev(
// Use tinybirdBranch (sanitized name) for Tinybird API, gitBranch for display
if (config.tinybirdBranch) {
const branchName = config.tinybirdBranch; // Sanitized name for Tinybird
const lastPartitionFromConfig =
config.branchDataMode === BranchDataMode.LAST_PARTITION;
const lastPartitionFromFlag = Boolean(options.lastPartition);

// Always fetch fresh from API to avoid stale cache issues
const tinybirdBranch = await getOrCreateBranch(
Expand All @@ -247,7 +251,7 @@ export async function runDev(
token: config.token,
},
branchName,
{ lastPartition: options.lastPartition }
{ lastPartition: lastPartitionFromFlag || lastPartitionFromConfig }
);

if (!tinybirdBranch.token) {
Expand Down
Loading