-
Notifications
You must be signed in to change notification settings - Fork 77
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
[DO NOT MERGE!!!] [V8] Paranet sync rework (WIP) #3680
Merged
Mihajlo-Pavlovic
merged 20 commits into
OriginTrail:v8/paranet-sync
from
aleksaelezovic:v8/paranet-sync-rework
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6dcabac
paranet sync rework (wip)
brkagithub c24652f
Merge branch 'OriginTrail:v8/paranet-sync-rework' into v8/paranet-syn…
aleksaelezovic a743e45
add more TODO comments
aleksaelezovic 0d15166
fix; add getParanetSyncedAssetRecordByUAL function
aleksaelezovic cb3d0dc
fix todos
aleksaelezovic ee5eb89
fix paranet asset repo & model
aleksaelezovic a3543dc
register getParanetKnowledgeCollectionCount mock, add TODO comments f…
aleksaelezovic d03752e
same for getParanetKnowledgeCollectionsWithPagination
aleksaelezovic 2b93e55
fix
aleksaelezovic 0abacda
deleted unneccessary fields for paranet asset model
aleksaelezovic c9ea16a
more comments, todos
aleksaelezovic 5c9928c
feat: paranet sync with knowledge collections
aleksaelezovic 5b03b61
document the code and add TODO notes
aleksaelezovic f047aa2
fix getSyncBatch in paranet kc repo
aleksaelezovic 3665aa7
fix: remove TODO
aleksaelezovic 954fad6
fix model name
aleksaelezovic 2b260e9
add indexes and triggers in migration
aleksaelezovic 0d9f343
paranet kc migration, triggers - make AFTER instead of BEFORE
aleksaelezovic 6c1d064
fix paranet_kc index, add down migration
aleksaelezovic 77a6fd0
change paranet repository naming for v8
aleksaelezovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
91 changes: 91 additions & 0 deletions
91
...odules/repository/implementation/sequelize/migrations/20241215122200-create-paranet-kc.js
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,91 @@ | ||
export async function up({ context: { queryInterface, Sequelize } }) { | ||
await queryInterface.createTable('paranet_kc', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
blockchain_id: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
ual: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
paranet_ual: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
error_message: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
is_synced: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false, | ||
}, | ||
retries: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
defaultValue: 0, | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('NOW()'), | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('NOW()'), | ||
}, | ||
}); | ||
await queryInterface.addConstraint('paranet_kc', { | ||
fields: ['ual', 'paranet_ual'], | ||
type: 'unique', | ||
}); | ||
await queryInterface.addIndex( | ||
'paranet_kc', | ||
['paranetUal', 'isSynced', 'retries', 'updatedAt'], | ||
{ name: 'idx_paranet_kc_sync_batch' }, | ||
); | ||
|
||
const [[{ triggerInsertExists }]] = await queryInterface.sequelize.query(` | ||
SELECT COUNT(*) AS triggerInsertExists | ||
FROM information_schema.triggers | ||
WHERE trigger_schema = DATABASE() AND trigger_name = 'after_insert_paranet_kc'; | ||
`); | ||
if (triggerInsertExists === 0) { | ||
await queryInterface.sequelize.query(` | ||
CREATE TRIGGER after_insert_paranet_kc | ||
AFTER INSERT ON paranet_kc | ||
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_paranet_kc'; | ||
`); | ||
if (triggerUpdateExists === 0) { | ||
await queryInterface.sequelize.query(` | ||
CREATE TRIGGER after_update_paranet_kc | ||
AFTER UPDATE ON paranet_kc | ||
FOR EACH ROW | ||
BEGIN | ||
SET NEW.updated_at = NOW(); | ||
END; | ||
`); | ||
} | ||
} | ||
|
||
export async function down({ context: { queryInterface } }) { | ||
await queryInterface.removeIndex('paranet_kc', 'idx_paranet_kc_sync_batch'); | ||
await queryInterface.dropTable('paranet_kc'); | ||
} |
2 changes: 2 additions & 0 deletions
2
src/modules/repository/implementation/sequelize/models/missed-paranet-asset.js
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
62 changes: 62 additions & 0 deletions
62
src/modules/repository/implementation/sequelize/models/paranet-kc.js
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,62 @@ | ||
export default (sequelize, DataTypes) => { | ||
const paranetKC = sequelize.define( | ||
'paranet_kc', | ||
{ | ||
id: { | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER, | ||
}, | ||
blockchainId: { | ||
allowNull: false, | ||
type: DataTypes.STRING, | ||
}, | ||
ual: { | ||
allowNull: false, | ||
type: DataTypes.STRING, | ||
}, | ||
paranetUal: { | ||
allowNull: false, | ||
type: DataTypes.STRING, | ||
}, | ||
errorMessage: { | ||
allowNull: true, | ||
type: DataTypes.TEXT, | ||
}, | ||
isSynced: { | ||
allowNull: false, | ||
type: DataTypes.BOOLEAN, | ||
defaultValue: false, | ||
}, | ||
retries: { | ||
allowNull: false, | ||
type: DataTypes.INTEGER, | ||
defaultValue: 0, | ||
}, | ||
createdAt: { | ||
type: DataTypes.DATE, | ||
}, | ||
updatedAt: { | ||
type: DataTypes.DATE, | ||
}, | ||
}, | ||
{ | ||
underscored: true, | ||
indexes: [ | ||
{ | ||
unique: true, | ||
fields: ['ual', 'paranetUal'], // Composite unique constraint on `ual` and `paranetUal` | ||
}, | ||
{ | ||
fields: ['paranetUal', 'isSynced', 'retries', 'updatedAt'], | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
paranetKC.associate = () => { | ||
// Define associations here if needed | ||
}; | ||
|
||
return paranetKC; | ||
}; |
1 change: 1 addition & 0 deletions
1
src/modules/repository/implementation/sequelize/models/paranet-synced-asset.js
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
105 changes: 105 additions & 0 deletions
105
src/modules/repository/implementation/sequelize/repositories/paranet-kc-repository.js
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,105 @@ | ||
import Sequelize from 'sequelize'; | ||
|
||
class ParanetKcRepository { | ||
constructor(models) { | ||
this.sequelize = models.sequelize; | ||
this.model = models.paranet_kc; | ||
} | ||
|
||
async createParanetKcRecords(paranetUal, blockchainId, uals, options = {}) { | ||
return this.model.bulkCreate( | ||
uals.map((ual) => ({ paranetUal, blockchainId, ual, isSynced: false })), | ||
options, | ||
); | ||
} | ||
|
||
async getCount(paranetUal, options = {}) { | ||
return this.model.count({ | ||
where: { | ||
paranetUal, | ||
}, | ||
...options, | ||
}); | ||
} | ||
|
||
async getCountSynced(paranetUal, options = {}) { | ||
return this.model.count({ | ||
where: { | ||
paranetUal, | ||
isSynced: true, | ||
}, | ||
...options, | ||
}); | ||
} | ||
|
||
async getCountUnsynced(paranetUal, options = {}) { | ||
return this.model.count({ | ||
where: { | ||
paranetUal, | ||
isSynced: false, | ||
}, | ||
...options, | ||
}); | ||
} | ||
|
||
async getSyncBatch(paranetUal, maxRetries, delayInMs, limit = null, options = {}) { | ||
const queryOptions = { | ||
where: { | ||
paranetUal, | ||
isSynced: false, | ||
[Sequelize.Op.and]: [ | ||
{ retries: { [Sequelize.Op.lt]: maxRetries } }, | ||
{ | ||
[Sequelize.Op.or]: [ | ||
{ retries: 0 }, | ||
{ | ||
updatedAt: { | ||
[Sequelize.Op.lte]: new Date(Date.now() - delayInMs), | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
order: [['retries', 'DESC']], | ||
...options, | ||
}; | ||
|
||
if (limit !== null) { | ||
queryOptions.limit = limit; | ||
} | ||
|
||
return this.model.findAll(queryOptions); | ||
} | ||
|
||
async incrementRetries(paranetUal, ual, errorMessage = null, options = {}) { | ||
const [affectedRows] = await this.model.increment('retries', { | ||
aleksaelezovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
by: 1, | ||
where: { | ||
ual, | ||
paranetUal, | ||
errorMessage, | ||
}, | ||
...options, | ||
}); | ||
|
||
return affectedRows; | ||
} | ||
|
||
async markAsSynced(paranetUal, ual, options = {}) { | ||
const [affectedRows] = await this.model.update( | ||
{ isSynced: true }, | ||
{ | ||
where: { | ||
ual, | ||
paranetUal, | ||
}, | ||
...options, | ||
}, | ||
); | ||
|
||
return affectedRows; | ||
} | ||
} | ||
|
||
export default ParanetKcRepository; |
1 change: 1 addition & 0 deletions
1
...dules/repository/implementation/sequelize/repositories/paranet-synced-asset-repository.js
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// DEPRECATED | ||
class ParanetSyncedAssetRepository { | ||
constructor(models) { | ||
this.sequelize = models.sequelize; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should create migration to drop tables that aren't used anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what is our plan regarding ot-node migration. I can add it when we decide, sure.