Skip to content

Commit 3cdbd79

Browse files
committed
Introduce Deployment Revamping
1 parent 7a99675 commit 3cdbd79

File tree

5 files changed

+141
-9
lines changed

5 files changed

+141
-9
lines changed

src/database/migrations.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { deployment_configs_create } from './migrations/15_deployment_configs_cr
2121
import { auth_key_upgrade } from './migrations/16_auth_key';
2222
import { domains_by_domain } from './migrations/17_domains_by_domain';
2323
import { owner_admin } from './migrations/18_owner_admin';
24+
import { rebuild_deployments } from './migrations/19_rebuild_deployments';
2425

2526
export type MigrationState = {
2627
instance_id: string;
@@ -123,4 +124,6 @@ export const Migrations: Migration<TableScheme>[] = [
123124
domains_by_domain,
124125
// Add admin field to owners
125126
owner_admin,
127+
// Rebuild the entirey of deployments
128+
rebuild_deployments,
126129
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { createTableRaw, ScylloClient } from 'scyllo';
2+
3+
import { DeploymentV4, DeploymentV5 } from '../../types/Deployment.type';
4+
import { log } from '../../util/logging';
5+
import { Migration } from '../migrations';
6+
7+
const createTable = async (
8+
database: ScylloClient<{
9+
deployments: DeploymentV5;
10+
}>,
11+
table_name: string
12+
) => {
13+
const query = createTableRaw<{ deployments: DeploymentV5 }, 'deployments'>(
14+
'signal',
15+
table_name as 'deployments',
16+
true,
17+
{
18+
app_id: {
19+
type: 'bigint',
20+
},
21+
deploy_id: {
22+
type: 'bigint',
23+
},
24+
cid: {
25+
type: 'text',
26+
},
27+
context: {
28+
type: 'text',
29+
},
30+
sid: {
31+
type: 'text',
32+
},
33+
},
34+
'app_id',
35+
['deploy_id']
36+
);
37+
38+
log.debug(query.query);
39+
40+
await database.query({
41+
args: query.args,
42+
query: query.query,
43+
});
44+
45+
// await database.createIndex(
46+
// table_name as 'deployments',
47+
// table_name + '_by_app_id',
48+
// 'app_id'
49+
// );
50+
};
51+
52+
export const rebuild_deployments: Migration<{
53+
deployments: DeploymentV4;
54+
deployments_tmp: DeploymentV5;
55+
}> = async (database) => {
56+
log.debug('creating table');
57+
await createTable(database, 'deployments_tmp');
58+
59+
log.debug('getting ol');
60+
const deployments_from_old = await database.selectFrom('deployments', [
61+
'app_id',
62+
'deploy_id',
63+
]);
64+
65+
for (const deployment of deployments_from_old) {
66+
const deploymentData = await database.selectOneFrom(
67+
'deployments',
68+
'*',
69+
{ app_id: deployment.app_id, deploy_id: deployment.deploy_id }
70+
);
71+
72+
await database.insertInto('deployments_tmp', {
73+
app_id: deploymentData?.app_id,
74+
cid: deploymentData?.cid,
75+
context: '',
76+
deploy_id: deploymentData?.deploy_id,
77+
sid: deploymentData?.sid,
78+
});
79+
}
80+
81+
await database.dropTable('deployments');
82+
83+
await createTable(database, 'deployments');
84+
85+
const deployments_form_intermediary = await database.selectFrom(
86+
'deployments_tmp',
87+
['app_id', 'deploy_id']
88+
);
89+
90+
for (const deployment of deployments_form_intermediary) {
91+
const deploymentData = await database.selectOneFrom(
92+
'deployments_tmp',
93+
'*',
94+
{
95+
app_id: deployment.app_id,
96+
deploy_id: deployment.deploy_id,
97+
}
98+
);
99+
100+
await (
101+
database as ScylloClient<{ deployments: DeploymentV5 }>
102+
).insertInto('deployments', deploymentData as DeploymentV5);
103+
}
104+
};

src/routes/api/apps/ls.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,40 @@ import { generateSunflake } from 'sunflake';
33

44
import { DB } from '../../../database';
55
import { useAuth } from '../../../util/http/useAuth';
6+
import { log } from '../../../util/logging';
67

78
export const generateSnowflake = generateSunflake();
89

910
export const AppLsRoute: FastifyPluginAsync = async (router, _options) => {
1011
router.get('/', async (_request, reply) => {
1112
const { user_id } = await useAuth(_request, reply);
1213

13-
reply.send(
14-
await DB.selectFrom('applications', '*', {
15-
owner_id: user_id,
14+
const applications = await DB.selectFrom('applications', '*', {
15+
owner_id: user_id,
16+
});
17+
18+
log.debug(applications);
19+
20+
const applicationsAndDomains = await Promise.all(
21+
applications.map(async (app) => {
22+
const [last_deploy] = await DB.selectFrom(
23+
'deployments',
24+
['deploy_id'],
25+
{ app_id: app.app_id },
26+
'ORDER BY deploy_id DESC'
27+
);
28+
29+
if (!last_deploy) return app;
30+
31+
return {
32+
...app,
33+
preview_url: '/api/image/deploy/' + last_deploy.deploy_id,
34+
};
1635
})
1736
);
37+
38+
log.debug(applicationsAndDomains);
39+
40+
reply.send(applicationsAndDomains);
1841
});
1942
};

src/routes/protected/create.ts

-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ export const CreateRoute: FastifyPluginAsync = async (router, options) => {
109109
cid: '',
110110
deploy_id,
111111
sid: bucket_name,
112-
comment: request.query.comment,
113-
git_actor: request.query.git_actor,
114-
git_sha: request.query.git_sha,
115-
git_src: request.query.git_src,
116-
git_type: request.query.git_type,
117112
});
118113
const { domain_id } = (await DB.selectOneFrom(
119114
'applications',

src/types/Deployment.type.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ export type DeploymentV3 = DeploymentV2 & {
2222

2323
export type DeploymentV4 = Omit<DeploymentV3, 'timestamp'>;
2424

25-
export type Deployment = DeploymentV4;
25+
export type DeploymentV5 = Omit<
26+
DeploymentV4,
27+
'git_sha' | 'git_src' | 'git_type' | 'git_actor' | 'comment'
28+
> & {
29+
context: string; // CI/CD Context
30+
};
31+
32+
export type Deployment = DeploymentV5;

0 commit comments

Comments
 (0)