Skip to content

Commit

Permalink
feat: add DB models to support build task feature (#4184)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbo committed Sep 6, 2023
1 parent 5a7fcec commit c52bfc0
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
33 changes: 33 additions & 0 deletions api/models/build-task-type.js
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;
};
56 changes: 56 additions & 0 deletions api/models/build-task.js
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;
};
4 changes: 4 additions & 0 deletions api/models/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const States = buildEnum([
const associate = ({
Build,
BuildLog,
BuildTask,
Organization,
Site,
User,
Expand All @@ -37,6 +38,9 @@ const associate = ({
foreignKey: 'user',
allowNull: false,
});
Build.hasMany(BuildTask, {
foreignKey: 'buildId',
});

// Scopes
Build.addScope('byOrg', id => ({
Expand Down
2 changes: 2 additions & 0 deletions api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const sequelize = new Sequelize(database, username, password, {

require('./build')(sequelize, DataTypes);
require('./build-log')(sequelize, DataTypes);
require('./build-task-type')(sequelize, DataTypes);
require('./build-task')(sequelize, DataTypes);
require('./site')(sequelize, DataTypes);
require('./site-branch-config')(sequelize, DataTypes);
require('./site-user')(sequelize, DataTypes);
Expand Down
60 changes: 60 additions & 0 deletions migrations/20230829172947-build-tasks.js
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);
};

16 changes: 16 additions & 0 deletions scripts/create-dev-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const cleanDatabase = require('../api/utils/cleanDatabase');
const {
ActionType,
Build,
BuildTaskType,
BuildTask,
BuildLog,
Domain,
Event,
Expand Down Expand Up @@ -434,6 +436,20 @@ async function createData() {
})),
]);

const taskType = await BuildTaskType.create({
name: 'test',
description: 'test description',
metadata: {
foo: 'bar',
},
});
await BuildTask.create({
buildId: nodeSiteBuilds[0].id,
buildTaskTypeId: taskType.id,
name: 'type',
status: 'processing',
});

const goSiteBuilds = await Promise.all([
Build.create({
branch: goSite.defaultBranch,
Expand Down
4 changes: 2 additions & 2 deletions test/api/unit/services/NightlyBuildsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('NightlyBuildsHelper', () => {

beforeEach(async () => {
await Promise.all([
Build.truncate(),
Build.truncate({ force: true, cascade: true }),
Site.truncate(),
SiteBranchConfig.truncate(),
]);
Expand All @@ -29,7 +29,7 @@ describe('NightlyBuildsHelper', () => {
afterEach(async () => {
sinon.restore();
await Promise.all([
Build.truncate(),
Build.truncate({ force: true, cascade: true }),
Site.truncate(),
SiteBranchConfig.truncate(),
]);
Expand Down

0 comments on commit c52bfc0

Please sign in to comment.