Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(dashboard,serverless): replace assets table with json column #915

Merged
merged 7 commits into from
Jun 2, 2023
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
6 changes: 6 additions & 0 deletions .changeset/violet-panthers-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/serverless': patch
'@lagon/dashboard': patch
---

Replace assets table with a json column
30 changes: 14 additions & 16 deletions crates/serverless/src/deployments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lagon_runtime_utils::{Deployment, DEPLOYMENTS_DIR};
use lagon_serverless_downloader::Downloader;
use log::{error, info, warn};
use mysql::{prelude::Queryable, PooledConn};
use serde::Deserialize;
use std::{
collections::{HashMap, HashSet},
fs,
Expand Down Expand Up @@ -59,17 +60,20 @@ where
}
}

#[derive(Deserialize)]
struct AssetObj(Vec<String>);

type QueryResult = (
String,
bool,
String,
String,
String,
usize,
usize,
usize,
Option<String>,
Option<String>,
Option<String>,
);

pub async fn get_deployments<D>(
Expand All @@ -90,22 +94,20 @@ where
SELECT
Deployment.id,
Deployment.isProduction,
Deployment.assets,
Function.id,
Function.name,
Function.memory,
Function.tickTimeout,
Function.totalTimeout,
Function.cron,
Domain.domain,
Asset.name
Domain.domain
FROM
Deployment
INNER JOIN Function
ON Deployment.functionId = Function.id
LEFT JOIN Domain
ON Function.id = Domain.functionId
LEFT JOIN Asset
ON Deployment.id = Asset.deploymentId
WHERE
Function.cron IS NULL
OR
Expand All @@ -116,25 +118,27 @@ OR
|(
id,
is_production,
assets,
function_id,
function_name,
memory,
tick_timeout,
total_timeout,
cron,
domain,
asset,
): QueryResult| {
let assets = serde_json::from_str::<AssetObj>(&assets)
.map(|asset_obj| asset_obj.0)
.unwrap_or_default();

deployments_list
.entry(id.clone())
.and_modify(|deployment| {
if let Some(domain) = domain.clone() {
deployment.domains.insert(domain);
}

if let Some(asset) = asset.clone() {
deployment.assets.insert(asset);
}
deployment.assets.extend(assets.clone());
})
.or_insert(Deployment {
id,
Expand All @@ -147,13 +151,7 @@ OR
domains
})
.unwrap_or_default(),
assets: asset
.map(|asset| {
let mut assets = HashSet::new();
assets.insert(asset);
assets
})
.unwrap_or_default(),
assets: HashSet::from_iter(assets.iter().cloned()),
environment_variables: HashMap::new(),
memory,
tick_timeout,
Expand Down
37 changes: 6 additions & 31 deletions packages/dashboard/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ export async function createDeployment(
return prisma.deployment.create({
data: {
isProduction: false,
assets: {
createMany: {
data: assets.map(name => ({
name,
})),
},
},
assets,
functionId: func.id,
triggerer,
},
Expand All @@ -39,11 +33,6 @@ export async function createDeployment(
createdAt: true,
updatedAt: true,
isProduction: true,
assets: {
select: {
name: true,
},
},
functionId: true,
},
});
Expand All @@ -63,12 +52,6 @@ export async function removeDeployment(
},
deploymentId: string,
) {
await prisma.asset.deleteMany({
where: {
deploymentId,
},
});

const deployment = await prisma.deployment.delete({
where: {
id: deploymentId,
Expand All @@ -79,11 +62,7 @@ export async function removeDeployment(
updatedAt: true,
functionId: true,
isProduction: true,
assets: {
select: {
name: true,
},
},
assets: true,
},
});

Expand All @@ -96,7 +75,7 @@ export async function removeDeployment(
),
];

if (deployment.assets.length > 0) {
if (Array.isArray(deployment.assets) && deployment.assets.length > 0) {
deletePromises.push(
s3.send(
new DeleteObjectsCommand({
Expand Down Expand Up @@ -127,7 +106,7 @@ export async function removeDeployment(
cronRegion: func.cronRegion,
env: envStringToObject(func.env),
isProduction: deployment.isProduction,
assets: deployment.assets.map(({ name }) => name),
assets: deployment.assets,
}),
);
}
Expand Down Expand Up @@ -208,11 +187,7 @@ export async function promoteProductionDeployment(functionId: string, newDeploym
createdAt: true,
updatedAt: true,
isProduction: true,
assets: {
select: {
name: true,
},
},
assets: true,
},
});

Expand All @@ -231,7 +206,7 @@ export async function promoteProductionDeployment(functionId: string, newDeploym
cronRegion: func.cronRegion,
env: envStringToObject(func.env),
isProduction: true,
assets: deployment.assets.map(({ name }) => name),
assets: deployment.assets,
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dashboard/lib/trpc/deploymentsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const deploymentsRouter = (t: T) =>
cronRegion: func.cronRegion,
env: envStringToObject(func.env),
isProduction: deployment.isProduction,
assets: deployment.assets.map(({ name }) => name),
assets: deployment.assets,
}),
);

Expand Down
8 changes: 1 addition & 7 deletions packages/dashboard/lib/trpc/functionsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ LIMIT 100`,
},
{
...deployment,
assets: deployment.assets.map(({ name }) => name),
assets: deployment.assets as string[],
},
oldDomains,
);
Expand Down Expand Up @@ -490,12 +490,6 @@ LIMIT 100`,
deployments: {
select: {
id: true,
triggerer: true,
commit: true,
isProduction: true,
assets: true,
createdAt: true,
updatedAt: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Warnings:

- You are about to drop the `Asset` table. If the table is not empty, all the data it contains will be lost.

*/
-- AlterTable
ALTER TABLE `Deployment` ADD COLUMN `assets` JSON NOT NULL;

-- Migrate Asset rows to Deployment's assets column
UPDATE
Deployment AS deployment
INNER JOIN (
SELECT
JSON_ARRAYAGG(name) AS assets,
deploymentId
FROM
Asset
GROUP BY
deploymentId) AS res SET deployment.assets = res.assets
WHERE
deployment.id = res.deploymentId

-- DropTable
DROP TABLE `Asset`;
13 changes: 1 addition & 12 deletions packages/dashboard/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,11 @@ model Deployment {
commit String?
isProduction Boolean @default(false)
function Function @relation(fields: [functionId], references: [id])
assets Asset[]
assets Json @default("[]")

@@index([functionId])
}

model Asset {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
deploymentId String
deployment Deployment @relation(fields: [deploymentId], references: [id])

@@index([deploymentId])
}

model Token {
id String @id @default(cuid())
createdAt DateTime @default(now())
Expand Down