origintrail-node
Version:
OriginTrail Node - Decentralized Knowledge Graph Node Library
131 lines (125 loc) • 4.37 kB
JavaScript
export const up = async ({ context: { queryInterface, Sequelize } }) => {
await queryInterface.createTable('random_sampling_challenge', {
id: {
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
blockchain_id: {
allowNull: false,
type: Sequelize.STRING,
},
// start_date: {
// allowNull: false,
// type: Sequelize.DATE,
// },
// end_date: {
// allowNull: false,
// type: Sequelize.DATE,
// },
contract_address: {
allowNull: false,
type: Sequelize.STRING,
},
knowledge_collection_id: {
allowNull: false,
type: Sequelize.INTEGER,
},
chunk_number: {
allowNull: false,
type: Sequelize.INTEGER,
},
active_proof_period_start_block: {
allowNull: false,
type: Sequelize.BIGINT,
},
epoch: {
allowNull: false,
type: Sequelize.INTEGER,
},
sent_successfully: {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
},
finalized: {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
},
score: {
allowNull: false,
type: Sequelize.BIGINT,
defaultValue: 0,
},
created_at: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
updated_at: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
});
const [[{ indexExists: randomSamplingBlockchainIdEpochSentSuccessfullyIndexExists }]] =
await queryInterface.sequelize.query(`
SELECT COUNT(*) AS indexExists
FROM information_schema.statistics
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'random_sampling_challenge'
AND INDEX_NAME = 'idx_rs_challenge_status';
`);
if (!randomSamplingBlockchainIdEpochSentSuccessfullyIndexExists) {
await queryInterface.addIndex(
'random_sampling_challenge',
['blockchain_id', 'epoch', 'sent_successfully', 'updated_at'],
{ name: 'idx_rs_challenge_status' },
);
}
const [[{ triggerInsertExists }]] = await queryInterface.sequelize.query(`
SELECT COUNT(*) AS triggerInsertExists
FROM information_schema.triggers
WHERE trigger_schema = DATABASE() AND trigger_name = 'after_insert_random_sampling_challenge';
`);
if (triggerInsertExists === 0) {
await queryInterface.sequelize.query(`
CREATE TRIGGER after_insert_random_sampling_challenge
BEFORE INSERT ON random_sampling_challenge
FOR EACH ROW
BEGIN
SET NEW.created_at = NOW();
END;
`);
}
const [[{ triggerUpdateExists }]] = await queryInterface.sequelize.query(`
SELECT COUNT(*) AS triggerUpdateExists
FROM information_schema.triggers
WHERE trigger_schema = DATABASE() AND trigger_name = 'after_update_random_sampling_challenge';
`);
if (triggerUpdateExists === 0) {
await queryInterface.sequelize.query(`
CREATE TRIGGER after_update_random_sampling_challenge
BEFORE UPDATE ON random_sampling_challenge
FOR EACH ROW
BEGIN
SET NEW.updated_at = NOW();
END;
`);
}
};
export const down = async ({ context: { queryInterface } }) => {
const [[{ indexExists: randomSamplingBlockchainIdEpochSentSuccessfullyIndexExists }]] =
await queryInterface.sequelize.query(`
SELECT COUNT(*) AS indexExists
FROM information_schema.statistics
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'random_sampling_challenge'
AND INDEX_NAME = 'idx_rs_challenge_status';
`);
if (randomSamplingBlockchainIdEpochSentSuccessfullyIndexExists) {
await queryInterface.removeIndex('random_sampling_challenge', 'idx_rs_challenge_status');
}
await queryInterface.dropTable('random_sampling_challenge');
};