-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DB models to support build task feature (#4184)
- Loading branch information
Showing
7 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const associate = ({ | ||
BuildTaskType, | ||
BuildTask, | ||
}) => { | ||
// Associations | ||
BuildTaskType.hasMany(BuildTask, { | ||
foreignKey: 'buildTaskTypeId', | ||
}); | ||
}; | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const BuildTaskType = sequelize.define( | ||
'BuildTaskType', | ||
{ | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
description: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
metadata: { | ||
type: DataTypes.JSON, | ||
}, | ||
}, { | ||
tableName: 'build_task_type', | ||
} | ||
); | ||
|
||
BuildTaskType.associate = associate; | ||
return BuildTaskType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { buildEnum } = require('../utils'); | ||
|
||
const Statuses = buildEnum([ | ||
'created', | ||
'queued', | ||
'tasked', | ||
'error', | ||
'processing', | ||
'skipped', // remove? | ||
'success', | ||
]); | ||
|
||
const associate = ({ BuildTask, Build }) => { | ||
BuildTask.belongsTo(Build, { | ||
foreignKey: 'buildId', | ||
allowNull: false, | ||
}); | ||
}; | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const BuildTask = sequelize.define( | ||
'BuildTask', | ||
{ | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
artifact: { | ||
type: DataTypes.STRING, | ||
}, | ||
status: { | ||
type: DataTypes.ENUM, | ||
values: Statuses.values, | ||
defaultValue: Statuses.Created, | ||
allowNull: false, | ||
validate: { | ||
isIn: [Statuses.values], | ||
}, | ||
}, | ||
}, { | ||
tableName: 'build_task', | ||
paranoid: true, | ||
indexes: [ | ||
{ | ||
name: 'build_task_build_id_type_index', | ||
unique: true, | ||
fields: ['buildId'], | ||
}, | ||
], | ||
} | ||
); | ||
|
||
BuildTask.associate = associate; | ||
|
||
return BuildTask; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
const TYPE_TABLE_NAME = "build_task_type"; | ||
const TYPE_TABLE_SCHEMA = { | ||
id: { type: "int", primaryKey: true, autoIncrement: true }, | ||
name: { type: "string", notNull: true }, | ||
description: { type: "string", notNull: true }, | ||
metadata: { type: "jsonb", allowNull: true }, | ||
createdAt: { type: "timestamp", notNull: true }, | ||
updatedAt: { type: "timestamp", notNull: true }, | ||
}; | ||
|
||
const TABLE_NAME = "build_task"; | ||
const TABLE_INDEX_NAME = "build_task_build_id_type_index"; | ||
const TABLE_SCHEMA = { | ||
id: { type: "int", primaryKey: true, autoIncrement: true }, | ||
buildId: { | ||
type: "int", | ||
notNull: true, | ||
foreignKey: { | ||
name: "build_task_build_id_fk", | ||
table: "build", | ||
rules: { | ||
onDelete: "CASCADE", | ||
onUpdate: "RESTRICT" | ||
}, | ||
mapping: "id" | ||
} | ||
}, | ||
buildTaskTypeId: { | ||
type: "int", | ||
notNull: true, | ||
foreignKey: { | ||
name: "build_task_build_task_type_id_fk", | ||
table: "build_task_type", | ||
rules: { | ||
onDelete: "CASCADE", | ||
onUpdate: "RESTRICT" | ||
}, | ||
mapping: "id" | ||
} | ||
}, | ||
name: { type: "string", notNull: true }, | ||
status: { type: "string", notNull: true, default: "created" }, | ||
artifact: { type: "string", allowNull: true }, | ||
createdAt: { type: "timestamp", notNull: true }, | ||
updatedAt: { type: "timestamp", notNull: true }, | ||
deletedAt: { type: "timestamp", allowNull: true }, | ||
}; | ||
|
||
exports.up = async (db) => { | ||
await db.createTable(TYPE_TABLE_NAME, TYPE_TABLE_SCHEMA); | ||
await db.createTable(TABLE_NAME, TABLE_SCHEMA) | ||
await db.addIndex(TABLE_NAME, TABLE_INDEX_NAME, ["buildId", "buildTaskTypeId"], true); | ||
}; | ||
|
||
exports.down = async (db) => { | ||
await db.dropTable(TABLE_NAME); | ||
return db.dropTable(TYPE_TABLE_NAME); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters