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

Feat build task models #4245

Merged
merged 1 commit into from
Sep 6, 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
37 changes: 37 additions & 0 deletions api/models/build-task-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const associate = ({
BuildTaskType,
BuildTask,
SiteBuildTask,
}) => {
// Associations
BuildTaskType.hasMany(BuildTask, {
foreignKey: 'buildTaskTypeId',
});
BuildTaskType.hasMany(SiteBuildTask, {
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', 'buildTaskTypeId'],
},
],
}
);

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
3 changes: 3 additions & 0 deletions api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ 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-build-task')(sequelize, DataTypes);
require('./site-user')(sequelize, DataTypes);
require('./user')(sequelize, DataTypes);
require('./user-action')(sequelize, DataTypes);
Expand Down
33 changes: 33 additions & 0 deletions api/models/site-build-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const associate = ({ BuildTaskType, Site, SiteBuildTask }) => {
SiteBuildTask.belongsTo(BuildTaskType, {
foreignKey: 'buildTaskTypeId',
allowNull: false,
});
SiteBuildTask.belongsTo(Site, {
foreignKey: 'siteId',
});
};

module.exports = (sequelize, DataTypes) => {
const SiteBuildTask = sequelize.define(
'SiteBuildTask',
{

branch: {
type: DataTypes.STRING,
allowNull: true,
},
metadata: {
type: DataTypes.JSON,
allowNull: true,
},
}, {
tableName: 'site_build_task',
paranoid: true,
}
);

SiteBuildTask.associate = associate;

return SiteBuildTask;
};
4 changes: 4 additions & 0 deletions api/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const associate = ({
OrganizationRole,
Site,
SiteBranchConfig,
SiteBuildTask,
SiteUser,
User,
UserAction,
Expand Down Expand Up @@ -62,6 +63,9 @@ const associate = ({
Site.belongsTo(Organization, {
foreignKey: 'organizationId',
});
Site.hasMany(SiteBuildTask, {
foreignKey: 'siteId',
});

// Scopes
Site.addScope('byIdOrText', (search) => {
Expand Down
99 changes: 99 additions & 0 deletions migrations/20230829172947-build-tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

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 },
};

const SITE_TABLE_NAME = "site_build_task";
const SITE_TABLE_SCHEMA = {
id: { type: "int", primaryKey: true, autoIncrement: true },
siteId: {
type: "int",
notNull: true,
foreignKey: {
name: "site_build_task_site_id_fk",
table: "site",
rules: {
onDelete: "CASCADE",
onUpdate: "RESTRICT"
},
mapping: "id"
}
},
buildTaskTypeId: {
type: "int",
notNull: true,
foreignKey: {
name: "site_build_task_build_task_type_id_fk",
table: "build_task_type",
rules: {
onDelete: "CASCADE",
onUpdate: "RESTRICT"
},
mapping: "id"
}
},
branch: { type: "string", allowNull: true },
metadata: { type: "jsonb", 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(SITE_TABLE_NAME, SITE_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);
await db.dropTable(SITE_TABLE_NAME);
return db.dropTable(TYPE_TABLE_NAME);
};

26 changes: 26 additions & 0 deletions scripts/create-dev-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ const cleanDatabase = require('../api/utils/cleanDatabase');
const {
ActionType,
Build,
BuildTaskType,
BuildTask,
BuildLog,
Domain,
Event,
Organization,
Role,
SiteBuildTask,
User,
UserAction,
} = require('../api/models');
Expand Down Expand Up @@ -434,6 +437,29 @@ 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',
});

await SiteBuildTask.create({
siteId: nodeSite.id,
buildTaskTypeId: taskType.id,
branch: 'test',
metadata: {
nightly: true,
},
});

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