Remove domain relation from deployments table

This commit is contained in:
IshaVenikar 2025-02-04 11:59:21 +05:30
parent f6b0f95761
commit 3ea43eab42
10 changed files with 29 additions and 125 deletions

View File

@ -158,10 +158,9 @@ export class Database {
.leftJoinAndSelect( .leftJoinAndSelect(
'project.deployments', 'project.deployments',
'deployments', 'deployments',
'deployments.isCurrent = true AND deployments.isDNS = true' 'deployments.isCurrent = true AND deployments.isCanonical = true'
) )
.leftJoinAndSelect('deployments.createdBy', 'user') .leftJoinAndSelect('deployments.createdBy', 'user')
.leftJoinAndSelect('deployments.domain', 'domain')
.leftJoinAndSelect('deployments.deployer', 'deployer') .leftJoinAndSelect('deployments.deployer', 'deployer')
.leftJoinAndSelect('project.owner', 'owner') .leftJoinAndSelect('project.owner', 'owner')
.leftJoinAndSelect('project.deployers', 'deployers') .leftJoinAndSelect('project.deployers', 'deployers')
@ -203,9 +202,8 @@ export class Database {
.leftJoinAndSelect( .leftJoinAndSelect(
'project.deployments', 'project.deployments',
'deployments', 'deployments',
'deployments.isCurrent = true AND deployments.isDNS = true' 'deployments.isCurrent = true AND deployments.isCanonical = true'
) )
.leftJoinAndSelect('deployments.domain', 'domain')
.leftJoin('project.projectMembers', 'projectMembers') .leftJoin('project.projectMembers', 'projectMembers')
.leftJoin('project.organization', 'organization') .leftJoin('project.organization', 'organization')
.where( .where(
@ -236,7 +234,6 @@ export class Database {
return this.getDeployments({ return this.getDeployments({
relations: { relations: {
project: true, project: true,
domain: true,
createdBy: true, createdBy: true,
deployer: true, deployer: true,
}, },
@ -251,11 +248,10 @@ export class Database {
}); });
} }
async getCommitDeploymentsByProjectId(projectId: string): Promise<Deployment[]> { async getNonCanonicalDeploymentsByProjectId(projectId: string): Promise<Deployment[]> {
return this.getDeployments({ return this.getDeployments({
relations: { relations: {
project: true, project: true,
domain: true,
createdBy: true, createdBy: true,
deployer: true, deployer: true,
}, },
@ -263,7 +259,7 @@ export class Database {
project: { project: {
id: projectId id: projectId
}, },
isDNS: false isCanonical: false
}, },
order: { order: {
createdAt: 'DESC' createdAt: 'DESC'

View File

@ -78,13 +78,6 @@ export class Deployment {
@JoinColumn({ name: 'projectId' }) @JoinColumn({ name: 'projectId' })
project!: Project; project!: Project;
@Column({ nullable: true })
domainId!: string | null;
@OneToOne(() => Domain)
@JoinColumn({ name: 'domainId' })
domain!: Domain | null;
@Column('varchar') @Column('varchar')
branch!: string; branch!: string;
@ -142,9 +135,8 @@ export class Deployment {
@Column('boolean', { default: false }) @Column('boolean', { default: false })
isCurrent!: boolean; isCurrent!: boolean;
// TODO: Rename field
@Column('boolean', { default: false }) @Column('boolean', { default: false })
isDNS!: boolean; isCanonical!: boolean;
@Column({ @Column({
enum: DeploymentStatus enum: DeploymentStatus

View File

@ -38,7 +38,7 @@ export const createResolvers = async (service: Service): Promise<any> => {
}, },
deployments: async (_: any, { projectId }: { projectId: string }) => { deployments: async (_: any, { projectId }: { projectId: string }) => {
return service.getCommitDeploymentsByProjectId(projectId); return service.getNonCanonicalDeploymentsByProjectId(projectId);
}, },
environmentVariables: async ( environmentVariables: async (

View File

@ -100,7 +100,6 @@ type ProjectMember {
type Deployment { type Deployment {
id: String! id: String!
domain: Domain
branch: String! branch: String!
commitHash: String! commitHash: String!
commitMessage: String! commitMessage: String!

View File

@ -228,12 +228,12 @@ export class Service {
deployment.isCurrent = deployment.environment === Environment.Production; deployment.isCurrent = deployment.environment === Environment.Production;
deployment.dnsRecordData = dnsRecordData; deployment.dnsRecordData = dnsRecordData;
if (deployment.isDNS) { if (deployment.isCanonical) {
const oldDNSDeployment = await this.db.getDeployment({ const oldDNSDeployment = await this.db.getDeployment({
where: { where: {
projectId: deployment.project.id, projectId: deployment.project.id,
deployer: deployment.deployer, deployer: deployment.deployer,
isDNS: true, isCanonical: true,
isCurrent: true, isCurrent: true,
}, },
relations: { relations: {
@ -255,14 +255,6 @@ export class Service {
await this.db.deleteDeploymentById(oldDNSDeployment.id); await this.db.deleteDeploymentById(oldDNSDeployment.id);
} }
// Update domain after the previous DNS deployment is deleted due to unique key constraint for domain
// Set the domain for the new current DNS deployment to the custom domain that was added for that project
const customDomain = await this.db.getOldestDomainByProjectId(deployment.project.id);
if (customDomain) {
deployment.domain = customDomain;
}
} }
await this.db.updateDeploymentById(deployment.id, deployment); await this.db.updateDeploymentById(deployment.id, deployment);
@ -301,7 +293,7 @@ export class Service {
projectDeployment.deployer.deployerLrn === projectDeployment.deployer.deployerLrn ===
deployment.deployer.deployerLrn && deployment.deployer.deployerLrn &&
projectDeployment.id !== deployment.id && projectDeployment.id !== deployment.id &&
projectDeployment.isDNS == deployment.isDNS, projectDeployment.isCanonical == deployment.isCanonical,
); );
for (const oldDeployment of oldDeployments) { for (const oldDeployment of oldDeployments) {
await this.db.updateDeployment( await this.db.updateDeployment(
@ -493,14 +485,14 @@ export class Service {
return dbProjects; return dbProjects;
} }
async getCommitDeploymentsByProjectId(projectId: string): Promise<Deployment[]> { async getNonCanonicalDeploymentsByProjectId(projectId: string): Promise<Deployment[]> {
const commitDeployments = await this.db.getCommitDeploymentsByProjectId(projectId); const nonCanonicalDeployments = await this.db.getNonCanonicalDeploymentsByProjectId(projectId);
return commitDeployments; return nonCanonicalDeployments;
} }
async getLatestDNSRecordByProjectId(projectId: string): Promise<DNSRecordAttributes | null> { async getLatestDNSRecordByProjectId(projectId: string): Promise<DNSRecordAttributes | null> {
const dnsDeployments = await this.db.getLatestDNSRecordByProjectId(projectId); const dnsRecord = await this.db.getLatestDNSRecordByProjectId(projectId);
return dnsDeployments; return dnsRecord;
} }
async getEnvironmentVariablesByProjectId( async getEnvironmentVariablesByProjectId(
@ -649,18 +641,12 @@ export class Service {
throw new Error('Deployment does not exist'); throw new Error('Deployment does not exist');
} }
const prodBranchDomains = await this.db.getDomainsByProjectId(
oldDeployment.project.id,
{ branch: oldDeployment.project.prodBranch },
);
const octokit = await this.getOctokit(user.id); const octokit = await this.getOctokit(user.id);
const newDeployment = await this.createDeployment(user.id, octokit, { const newDeployment = await this.createDeployment(user.id, octokit, {
project: oldDeployment.project, project: oldDeployment.project,
branch: oldDeployment.branch, branch: oldDeployment.branch,
environment: Environment.Production, environment: Environment.Production,
domain: prodBranchDomains[0],
commitHash: oldDeployment.commitHash, commitHash: oldDeployment.commitHash,
commitMessage: oldDeployment.commitMessage, commitMessage: oldDeployment.commitMessage,
deployer: oldDeployment.deployer deployer: oldDeployment.deployer
@ -689,19 +675,6 @@ export class Service {
commitHash: data.commitHash!, commitHash: data.commitHash!,
}); });
// Update previous deployment with prod branch domain
// TODO: Fix unique constraint error for domain
if (data.domain) {
await this.db.updateDeployment(
{
domainId: data.domain.id,
},
{
domain: null,
},
);
}
let deployer; let deployer;
if (deployerLrn) { if (deployerLrn) {
deployer = await this.db.getDeployerByLRN(deployerLrn); deployer = await this.db.getDeployerByLRN(deployerLrn);
@ -709,20 +682,19 @@ export class Service {
deployer = data.deployer; deployer = data.deployer;
} }
console.log("DATA>DOMAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",data.domain)
const deployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, false); const deployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, false);
const dnsDeployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, true);
const address = await this.getAddress(); const address = await this.getAddress();
const { repo, repoUrl } = await getRepoDetails(octokit, data.project.repository, data.commitHash); const { repo, repoUrl } = await getRepoDetails(octokit, data.project.repository, data.commitHash);
const environmentVariablesObj = await this.getEnvVariables(data.project!.id!); const environmentVariablesObj = await this.getEnvVariables(data.project!.id!);
// If a custom domain is present then use that as the DNS in the deployment request
const customDomain = await this.db.getOldestDomainByProjectId(data.project!.id!);
// To set project DNS // To set project DNS
if (data.environment === Environment.Production) { if (data.environment === Environment.Production) {
const dnsDeployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, true);
// If a custom domain is present then use that as the DNS in the deployment request
const customDomain = await this.db.getOldestDomainByProjectId(data.project!.id!);
// On deleting deployment later, project DNS deployment is also deleted // On deleting deployment later, project DNS deployment is also deleted
// So publish project DNS deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later // So publish project DNS deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later
const { applicationDeploymentRequestData, applicationDeploymentRequestId } = const { applicationDeploymentRequestData, applicationDeploymentRequestId } =
@ -867,7 +839,7 @@ export class Service {
deployerLrn: string, deployerLrn: string,
applicationRecordId: string, applicationRecordId: string,
applicationRecordData: ApplicationRecord, applicationRecordData: ApplicationRecord,
isDNS: boolean, isCanonical: boolean,
): Promise<Deployment> { ): Promise<Deployment> {
const newDeployment = await this.db.addDeployment({ const newDeployment = await this.db.addDeployment({
project: data.project, project: data.project,
@ -878,14 +850,13 @@ export class Service {
status: DeploymentStatus.Building, status: DeploymentStatus.Building,
applicationRecordId, applicationRecordId,
applicationRecordData, applicationRecordData,
domain: data.domain,
createdBy: Object.assign(new User(), { createdBy: Object.assign(new User(), {
id: userId, id: userId,
}), }),
deployer: Object.assign(new Deployer(), { deployer: Object.assign(new Deployer(), {
deployerLrn, deployerLrn,
}), }),
isDNS isCanonical
}); });
log(`Created deployment ${newDeployment.id}`); log(`Created deployment ${newDeployment.id}`);
@ -1118,9 +1089,6 @@ export class Service {
for await (const project of projects) { for await (const project of projects) {
const octokit = await this.getOctokit(project.ownerId); const octokit = await this.getOctokit(project.ownerId);
const [domain] = await this.db.getDomainsByProjectId(project.id, {
branch,
});
const deployers = project.deployers; const deployers = project.deployers;
if (!deployers) { if (!deployers) {
@ -1138,7 +1106,6 @@ export class Service {
project.prodBranch === branch project.prodBranch === branch
? Environment.Production ? Environment.Production
: Environment.Preview, : Environment.Preview,
domain,
commitHash: headCommit.id, commitHash: headCommit.id,
commitMessage: headCommit.message, commitMessage: headCommit.message,
deployer: deployer deployer: deployer
@ -1180,7 +1147,6 @@ export class Service {
const oldDeployment = await this.db.getDeployment({ const oldDeployment = await this.db.getDeployment({
relations: { relations: {
project: true, project: true,
domain: true,
deployer: true, deployer: true,
createdBy: true, createdBy: true,
}, },
@ -1206,7 +1172,6 @@ export class Service {
// TODO: Put isCurrent field in project // TODO: Put isCurrent field in project
branch: oldDeployment.branch, branch: oldDeployment.branch,
environment: Environment.Production, environment: Environment.Production,
domain: oldDeployment.domain,
commitHash: oldDeployment.commitHash, commitHash: oldDeployment.commitHash,
commitMessage: oldDeployment.commitMessage, commitMessage: oldDeployment.commitMessage,
deployer: oldDeployment.deployer deployer: oldDeployment.deployer
@ -1224,7 +1189,6 @@ export class Service {
// TODO: Implement transactions // TODO: Implement transactions
const oldCurrentDeployment = await this.db.getDeployment({ const oldCurrentDeployment = await this.db.getDeployment({
relations: { relations: {
domain: true,
project: true, project: true,
deployer: true, deployer: true,
}, },
@ -1233,7 +1197,7 @@ export class Service {
id: projectId, id: projectId,
}, },
isCurrent: true, isCurrent: true,
isDNS: false, isCanonical: false,
}, },
}); });
@ -1243,12 +1207,12 @@ export class Service {
const oldCurrentDeploymentUpdate = await this.db.updateDeploymentById( const oldCurrentDeploymentUpdate = await this.db.updateDeploymentById(
oldCurrentDeployment.id, oldCurrentDeployment.id,
{ isCurrent: false, domain: null }, { isCurrent: false },
); );
const newCurrentDeploymentUpdate = await this.db.updateDeploymentById( const newCurrentDeploymentUpdate = await this.db.updateDeploymentById(
deploymentId, deploymentId,
{ isCurrent: true, domain: null }, { isCurrent: true },
); );
const newCurrentDeployment = await this.db.getDeployment({ where: { id: deploymentId }, relations: { project: true, deployer: true } }); const newCurrentDeployment = await this.db.getDeployment({ where: { id: deploymentId }, relations: { project: true, deployer: true } });
@ -1261,19 +1225,7 @@ export class Service {
const customDomain = await this.db.getOldestDomainByProjectId(projectId); const customDomain = await this.db.getOldestDomainByProjectId(projectId);
if (customDomain) { if (customDomain && applicationDeploymentRequestData) {
await this.db.updateDeployment(
{
domainId: customDomain.id,
},
{
domain: null,
},
);
}
if(customDomain && applicationDeploymentRequestData) {
newCurrentDeployment.domain = customDomain
applicationDeploymentRequestData.dns = customDomain.name applicationDeploymentRequestData.dns = customDomain.name
} }
@ -1326,7 +1278,7 @@ export class Service {
where: { where: {
projectId: deployment.project.id, projectId: deployment.project.id,
deployer: deployment.deployer, deployer: deployment.deployer,
isDNS: true isCanonical: true
}, },
relations: { relations: {
project: true, project: true,

View File

@ -243,7 +243,7 @@ export const DeploymentMenu = ({
setRedeployToProduction((preVal) => !preVal); setRedeployToProduction((preVal) => !preVal);
}} }}
deployment={deployment} deployment={deployment}
domains={deployment.domain ? [deployment.domain] : []} domains={[]}
isConfirmButtonLoading={isConfirmButtonLoading} isConfirmButtonLoading={isConfirmButtonLoading}
/> />
{Boolean(currentDeployment) && ( {Boolean(currentDeployment) && (
@ -258,7 +258,7 @@ export const DeploymentMenu = ({
}} }}
deployment={currentDeployment} deployment={currentDeployment}
newDeployment={deployment} newDeployment={deployment}
domains={currentDeployment.domain ? [currentDeployment.domain] : []} domains={[]}
/> />
)} )}
<AssignDomainDialog <AssignDomainDialog

View File

@ -21,15 +21,6 @@ import { ChangeStateToProductionDialog } from 'components/projects/Dialog/Change
const deployment: Deployment = { const deployment: Deployment = {
id: '1', id: '1',
domain: {
id: 'domain1',
branch: 'main',
name: 'example.com',
status: DomainStatus.Live,
redirectTo: null,
createdAt: '1677609600', // 2023-02-25T12:00:00Z
updatedAt: '1677613200', // 2023-02-25T13:00:00Z
},
branch: 'main', branch: 'main',
commitHash: 'a1b2c3d', commitHash: 'a1b2c3d',
commitMessage: commitMessage:
@ -254,7 +245,7 @@ const ModalsPage: React.FC = () => {
setRedeployToProduction((preVal) => !preVal) setRedeployToProduction((preVal) => !preVal)
} }
deployment={deployment} deployment={deployment}
domains={deployment.domain ? [deployment.domain] : []} domains={[]}
/> />
{/* Rollback to this deployment */} {/* Rollback to this deployment */}
<Button onClick={() => setRollbackDeployment(true)}> <Button onClick={() => setRollbackDeployment(true)}>
@ -270,7 +261,7 @@ const ModalsPage: React.FC = () => {
} }
deployment={deployment} deployment={deployment}
newDeployment={deployment} newDeployment={deployment}
domains={deployment.domain ? [deployment.domain] : []} domains={[]}
/> />
</div> </div>
</div> </div>

View File

@ -100,7 +100,6 @@ export const deployment0: Deployment = {
environment: Environment.Development, environment: Environment.Development,
isCurrent: true, isCurrent: true,
commitHash: 'Commit Hash', commitHash: 'Commit Hash',
domain: domain0,
commitMessage: 'Commit Message', commitMessage: 'Commit Message',
createdBy: user, createdBy: user,
deployer: { deployer: {

View File

@ -63,14 +63,6 @@ query ($projectId: String!) {
deployer { deployer {
baseDomain baseDomain
} }
domain {
status
branch
createdAt
updatedAt
id
name
}
createdBy { createdBy {
id id
name name
@ -118,14 +110,6 @@ query ($organizationSlug: String!) {
applicationDeploymentRecordData { applicationDeploymentRecordData {
url url
} }
domain {
status
branch
createdAt
updatedAt
id
name
}
} }
} }
} }
@ -147,14 +131,6 @@ export const getDeployments = gql`
query ($projectId: String!) { query ($projectId: String!) {
deployments(projectId: $projectId) { deployments(projectId: $projectId) {
id id
domain{
branch
createdAt
id
name
status
updatedAt
}
branch branch
commitHash commitHash
commitMessage commitMessage

View File

@ -99,7 +99,6 @@ export type User = {
export type Deployment = { export type Deployment = {
id: string; id: string;
domain: Domain;
branch: string; branch: string;
commitHash: string; commitHash: string;
commitMessage: string; commitMessage: string;