This repository was archived by the owner on May 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbmigrate.ts
More file actions
191 lines (169 loc) · 5.48 KB
/
dbmigrate.ts
File metadata and controls
191 lines (169 loc) · 5.48 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const command = Deno.args[0];
import { parse } from "https://deno.land/std@0.119.0/flags/mod.ts";
const flags = parse(Deno.args, {
string: ["appId", "workspace", "table"],
});
// read flags passed in
const tableName = flags.table;
let appId = flags.appId;
let workspace = flags.workspace;
const dataFilePath = flags.filePath;
const decoder = new TextDecoder("utf-8");
// Reads and returns app.json
async function readAppsJson() {
console.log("attempting to read .slack/apps.json");
let data;
try {
data = await Deno.readFile(".slack/apps.json");
} catch (e) {
console.error(
"could not find .slack/apps.json. Did you run the export command from your slack app directory?",
);
console.log(e);
Deno.exit(1);
}
return JSON.parse(decoder.decode(data));
}
// Grabs the token from ~/.slack/credentials.json
async function readCredentialsJson(workspaceName: string) {
console.log(
"Reading `.slack/credentails.json from the home directory to get the token",
);
const credentialsData = await Deno.readFile(
Deno.env.get("HOME") + "/.slack/credentials.json",
);
const credentialsJson = JSON.parse(decoder.decode(credentialsData));
const token = credentialsJson[workspaceName].token;
if (!token) {
console.error("Failed getting the xoxp token");
Deno.exit(1);
}
return token;
}
if (command === "export") {
// Start of export command
let jsonData;
if (flags.table === undefined) {
console.error(
"You must provide the table name to export. use the --table flag to set the value.",
);
Deno.exit(1);
}
if (appId & workspace) {
console.log("appId & workspace provided via flags");
} else if (appId && workspace === undefined) {
console.log(
"appId provided via flag, attempting to grab workspace name from apps.json",
);
jsonData = await readAppsJson();
workspace = jsonData["default"];
} else if (workspace && appId === undefined) {
console.log(
"workspace name provided via flag, attempting to grab appId name from apps.json",
);
jsonData = await readAppsJson();
appId = jsonData.apps[workspace].app_id;
} else {
jsonData = await readAppsJson();
workspace = jsonData["default"];
appId = jsonData.apps[workspace].app_id;
}
if (!appId && !workspace && !tableName) {
console.log(
"Script failed because it could not locate one of: appId, workspace name or table name. Please run this script in a slack project directory or pass in the values via flags (--appId, --table, --workspace)",
);
}
const token = await readCredentialsJson(workspace);
const response = await fetch(
"https://slack.com/api/apps.hosted.tables.query",
{
method: "POST", // *GET, POST, PUT, DELETE, etc.
headers: {
"Content-Type": "application/json; charset=utf-8",
"authorization": `Bearer ${token}`,
},
body: JSON.stringify({ "app_id": appId, "table": tableName, "limit": 1000 }),
},
);
const tableData = await response.json();
if (tableData.ok) {
await Deno.writeTextFile(
`${tableName}DataBackup.json`,
JSON.stringify(tableData),
);
console.log(
`backed up data from ${tableName} into ${tableName}DataBackup.json`,
);
} else {
console.error("fetching data from slack failed");
console.error(tableData);
Deno.exit(1);
}
} else if (command === "import") {
// Start of Import command
let jsonData;
if (dataFilePath === undefined) {
console.error(
"You must provide the path to the tables backup file you wish to import. use the --filePath flag to set the value.",
);
Deno.exit(1);
}
if (appId & workspace) {
console.log("appId & workspace provided via flags");
} else if (appId && workspace === undefined) {
console.log(
"appId provided via flag, attempting to grab workspace name from apps.json",
);
jsonData = await readAppsJson();
workspace = jsonData["default"];
} else if (workspace && appId === undefined) {
console.log(
"workspace name provided via flag, attempting to grab appId name from apps.json",
);
jsonData = await readAppsJson();
appId = jsonData.apps[workspace].app_id;
} else {
jsonData = await readAppsJson();
workspace = jsonData["default"];
appId = jsonData.apps[workspace].app_id;
}
if (!appId && !workspace) {
console.log(
"Script failed because it could not locate one of: appId or workspace name. Please run this script in a slack project directory or pass in the values via flags (--appId, --workspace)",
);
}
const token = await readCredentialsJson(workspace);
console.log(`attempting to read ${dataFilePath}`);
let data;
try {
data = await Deno.readFile(dataFilePath);
} catch (e) {
console.error(
`could not read ${dataFilePath}`,
);
console.log(e);
Deno.exit(1);
}
const tableData = JSON.parse(decoder.decode(data));
console.log(tableData);
const tableName = tableData.table;
console.log(`Importing rows from ${tableName}`);
for (const element in tableData.rows) {
const response = await fetch(
"https://slack.com/api/apps.datastore.put",
{
method: "POST", // *GET, POST, PUT, DELETE, etc.
headers: {
"Content-Type": "application/json",
"authorization": `Bearer ${token}`,
},
body: JSON.stringify({
"app_id": appId,
"datastore": tableName,
item: tableData.rows[element],
}),
},
);
console.log(await response.json());
}
}