forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-all-data.js
More file actions
156 lines (143 loc) · 3.78 KB
/
Copy pathdelete-all-data.js
File metadata and controls
156 lines (143 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
import { intro, isCancel, log, outro, text } from "@clack/prompts";
import { createTinybirdClient, resolveTinybirdAuth } from "./shared.js";
async function getAllDatasources(client) {
try {
const payload = await client.request(`/v0/datasources`);
const list = Array.isArray(payload?.datasources)
? payload.datasources
: Array.isArray(payload?.data)
? payload.data
: Array.isArray(payload)
? payload
: [];
const names = list
.map((ds) => (typeof ds === "string" ? ds : ds?.name))
.filter(Boolean);
const unique = Array.from(new Set(names));
if (unique.length > 0) return unique;
} catch {
// fall through to fallback list
}
return ["analytics_events", "analytics_pages_mv", "analytics_sessions_mv"];
}
async function doubleConfirm({
workspaceName,
workspaceId,
host,
datasources,
}) {
const workspaceLabel =
workspaceName || workspaceId || new URL(host).host || "unknown-workspace";
intro("Delete ALL analytics data from Tinybird");
log.warn(
`This will TRUNCATE all datasources in the Tinybird workspace:\n` +
`- Workspace: ${workspaceLabel}\n` +
`- Host: ${host}\n` +
`- Datasources (${datasources.length}): ${datasources.join(", ")}`,
);
const first = await text({
message: `Type the workspace name or ID to confirm (${workspaceLabel})`,
placeholder: workspaceLabel,
defaultValue: "",
validate: (value) => {
if (!value) return "Required";
if (
value !== workspaceName &&
value !== workspaceId &&
value !== workspaceLabel
) {
return "Value does not match the workspace name or ID";
}
},
});
if (isCancel(first)) {
outro("Cancelled.");
process.exit(0);
}
const second = await text({
message: 'Final confirmation: type "DELETE ALL" to proceed',
placeholder: "DELETE ALL",
defaultValue: "",
validate: (value) =>
value === "DELETE ALL" ? undefined : 'You must type "DELETE ALL"',
});
if (isCancel(second)) {
outro("Cancelled.");
process.exit(0);
}
}
async function deleteAllData() {
const auth = resolveTinybirdAuth();
const client = createTinybirdClient(auth);
const datasources = await getAllDatasources(client);
await doubleConfirm({
workspaceName: client.workspaceName,
workspaceId: client.workspaceId,
host: client.host,
datasources,
});
console.log("\nDeleting all data from Tinybird datasources...\n");
const successes = [];
const failures = [];
for (const datasource of datasources) {
try {
console.log(`Deleting data from ${datasource}...`);
let ok = false;
try {
await client.request(
`/v0/datasources/${encodeURIComponent(datasource)}/truncate`,
{
method: "POST",
},
);
ok = true;
} catch (_e1) {
try {
await client.request(
`/v0/datasources/${encodeURIComponent(datasource)}/data`,
{
method: "DELETE",
},
);
ok = true;
} catch (_e2) {
await client.request(
`/v0/datasources/${encodeURIComponent(datasource)}`,
{
method: "DELETE",
},
);
ok = true;
}
}
if (ok) {
console.log(`✅ Deleted data from ${datasource}`);
successes.push(datasource);
} else {
failures.push(datasource);
}
} catch (error) {
console.error(
`❌ Failed to delete data from ${datasource}:`,
error.message,
);
if (error.payload) {
console.error(" Details:", JSON.stringify(error.payload, null, 2));
}
failures.push(datasource);
}
}
if (failures.length === 0) {
console.log("\n✅ Finished deleting all data");
} else {
console.log(
`\n⚠️ Finished with errors. Deleted: ${successes.length}. Failed: ${failures.length} -> ${failures.join(", ")}`,
);
process.exitCode = 1;
}
}
deleteAllData().catch((error) => {
console.error("❌ Failed to delete data:", error.message);
process.exit(1);
});