From 8a2636e77066e9d7bccd8e3d9d005d2574d39c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wo=C5=9B?= Date: Mon, 11 Sep 2023 16:19:06 +0200 Subject: [PATCH] chore: remove indexer asset versioning logic (#1340) remove indexer asset version --- .../database-types/src/lib.rs | 3 - .../20230911123600_remove_versions.down.sql | 4 ++ .../20230911123600_remove_versions.up.sql | 3 + .../fuel-indexer-database/postgres/src/lib.rs | 62 +++++-------------- packages/fuel-indexer-database/src/queries.rs | 24 ++----- packages/fuel-indexer-schema/src/db/tables.rs | 9 +-- packages/fuel-indexer/src/service.rs | 6 +- 7 files changed, 32 insertions(+), 79 deletions(-) create mode 100644 packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.down.sql create mode 100644 packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.up.sql diff --git a/packages/fuel-indexer-database/database-types/src/lib.rs b/packages/fuel-indexer-database/database-types/src/lib.rs index 405f9e57c..512d18639 100644 --- a/packages/fuel-indexer-database/database-types/src/lib.rs +++ b/packages/fuel-indexer-database/database-types/src/lib.rs @@ -566,9 +566,6 @@ pub struct IndexerAsset { /// Database ID of the indexer. pub index_id: i64, - /// Version associated with this indexer asset. - pub version: i32, - /// Digest of the asset's bytes. pub digest: String, diff --git a/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.down.sql b/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.down.sql new file mode 100644 index 000000000..63d324d38 --- /dev/null +++ b/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.down.sql @@ -0,0 +1,4 @@ +-- Active: 1693382282533@@127.0.0.1@5432@postgres +ALTER TABLE index_asset_registry_manifest ADD COLUMN version integer not null; +ALTER TABLE index_asset_registry_schema ADD COLUMN version integer not null; +ALTER TABLE index_asset_registry_wasm ADD COLUMN version integer not null; \ No newline at end of file diff --git a/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.up.sql b/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.up.sql new file mode 100644 index 000000000..598b5ac72 --- /dev/null +++ b/packages/fuel-indexer-database/postgres/migrations/20230911123600_remove_versions.up.sql @@ -0,0 +1,3 @@ +ALTER TABLE index_asset_registry_manifest DROP COLUMN version; +ALTER TABLE index_asset_registry_schema DROP COLUMN version; +ALTER TABLE index_asset_registry_wasm DROP COLUMN version; \ No newline at end of file diff --git a/packages/fuel-indexer-database/postgres/src/lib.rs b/packages/fuel-indexer-database/postgres/src/lib.rs index 99fde57d3..e02701c68 100644 --- a/packages/fuel-indexer-database/postgres/src/lib.rs +++ b/packages/fuel-indexer-database/postgres/src/lib.rs @@ -526,27 +526,6 @@ pub async fn all_registered_indexers( .collect::>()) } -#[cfg_attr(feature = "metrics", metrics)] -pub async fn indexer_asset_version( - conn: &mut PoolConnection, - index_id: &i64, - asset_type: &IndexerAssetType, -) -> sqlx::Result { - match sqlx::query(&format!( - "SELECT COUNT(*) - FROM index_asset_registry_{} - WHERE index_id = {}", - asset_type.as_ref(), - index_id, - )) - .fetch_one(conn) - .await - { - Ok(row) => Ok(row.try_get::(0).unwrap_or(0)), - Err(_e) => Ok(0), - } -} - /// Register a single indexer asset. #[cfg_attr(feature = "metrics", metrics)] pub async fn register_indexer_asset( @@ -577,15 +556,10 @@ pub async fn register_indexer_asset( return Ok(asset); } - let current_version = indexer_asset_version(conn, &index.id, &asset_type) - .await - .expect("Failed to get asset version."); - let query = format!( - "INSERT INTO index_asset_registry_{} (index_id, bytes, version, digest) VALUES ({}, $1, {}, '{digest}') RETURNING *", + "INSERT INTO index_asset_registry_{} (index_id, bytes, digest) VALUES ({}, $1, '{digest}') RETURNING *", asset_type.as_ref(), index.id, - current_version + 1, ); let row = sqlx::QueryBuilder::new(query) @@ -603,22 +577,20 @@ pub async fn register_indexer_asset( let id = row.get(0); let index_id = row.get(1); - let version = row.get(2); - let digest = row.get(3); - let bytes = row.get(4); + let digest = row.get(2); + let bytes = row.get(3); Ok(IndexerAsset { id, index_id, - version, digest, bytes, }) } -/// Return the latest version for a given indexer asset type. +/// Returns the requested asset for an indexer with the given id. #[cfg_attr(feature = "metrics", metrics)] -pub async fn latest_asset_for_indexer( +pub async fn indexer_asset( conn: &mut PoolConnection, index_id: &i64, asset_type: IndexerAssetType, @@ -633,30 +605,26 @@ pub async fn latest_asset_for_indexer( let id = row.get(0); let index_id = row.get(1); - let version = row.get(2); - let digest = row.get(3); - let bytes = row.get(4); + let digest = row.get(2); + let bytes = row.get(3); Ok(IndexerAsset { id, index_id, - version, digest, bytes, }) } -/// Return the latest version for every indexer asset type. +/// Return every indexer asset type for an indexer with the give id. #[cfg_attr(feature = "metrics", metrics)] -pub async fn latest_assets_for_indexer( +pub async fn indexer_assets( conn: &mut PoolConnection, indexer_id: &i64, ) -> sqlx::Result { - let wasm = latest_asset_for_indexer(conn, indexer_id, IndexerAssetType::Wasm).await?; - let schema = - latest_asset_for_indexer(conn, indexer_id, IndexerAssetType::Schema).await?; - let manifest = - latest_asset_for_indexer(conn, indexer_id, IndexerAssetType::Manifest).await?; + let wasm = indexer_asset(conn, indexer_id, IndexerAssetType::Wasm).await?; + let schema = indexer_asset(conn, indexer_id, IndexerAssetType::Schema).await?; + let manifest = indexer_asset(conn, indexer_id, IndexerAssetType::Manifest).await?; Ok(IndexerAssetBundle { wasm, @@ -705,14 +673,12 @@ pub async fn asset_already_exists( Ok(row) => { let id = row.get(0); let index_id = row.get(1); - let version = row.get(2); - let digest = row.get(3); - let bytes = row.get(4); + let digest = row.get(2); + let bytes = row.get(3); Ok(Some(IndexerAsset { id, index_id, - version, digest, bytes, })) diff --git a/packages/fuel-indexer-database/src/queries.rs b/packages/fuel-indexer-database/src/queries.rs index 1fe6d8f58..39fae7644 100644 --- a/packages/fuel-indexer-database/src/queries.rs +++ b/packages/fuel-indexer-database/src/queries.rs @@ -230,18 +230,6 @@ pub async fn all_registered_indexers( } } -pub async fn indexer_asset_version( - conn: &mut IndexerConnection, - index_id: &i64, - asset_type: &IndexerAssetType, -) -> sqlx::Result { - match conn { - IndexerConnection::Postgres(ref mut c) => { - postgres::indexer_asset_version(c, index_id, asset_type).await - } - } -} - /// Register a single indexer asset. pub async fn register_indexer_asset( conn: &mut IndexerConnection, @@ -261,27 +249,27 @@ pub async fn register_indexer_asset( } } -/// Return the latest version for a given indexer asset type. -pub async fn latest_asset_for_indexer( +/// Returns the requested asset for an indexer with the given id. +pub async fn indexer_asset( conn: &mut IndexerConnection, index_id: &i64, asset_type: IndexerAssetType, ) -> sqlx::Result { match conn { IndexerConnection::Postgres(ref mut c) => { - postgres::latest_asset_for_indexer(c, index_id, asset_type).await + postgres::indexer_asset(c, index_id, asset_type).await } } } -/// Return the latest version for every indexer asset type. -pub async fn latest_assets_for_indexer( +/// Return every indexer asset type for an indexer with the give id. +pub async fn indexer_assets( conn: &mut IndexerConnection, index_id: &i64, ) -> sqlx::Result { match conn { IndexerConnection::Postgres(ref mut c) => { - postgres::latest_assets_for_indexer(c, index_id).await + postgres::indexer_assets(c, index_id).await } } } diff --git a/packages/fuel-indexer-schema/src/db/tables.rs b/packages/fuel-indexer-schema/src/db/tables.rs index b9cfd3e80..b43cbe186 100644 --- a/packages/fuel-indexer-schema/src/db/tables.rs +++ b/packages/fuel-indexer-schema/src/db/tables.rs @@ -203,12 +203,9 @@ impl IndexerSchema { let indexer_id = queries::get_indexer_id(&mut conn, namespace, identifier).await?; - let IndexerAsset { bytes, .. } = queries::latest_asset_for_indexer( - &mut conn, - &indexer_id, - IndexerAssetType::Manifest, - ) - .await?; + let IndexerAsset { bytes, .. } = + queries::indexer_asset(&mut conn, &indexer_id, IndexerAssetType::Manifest) + .await?; let manifest = Manifest::try_from(&bytes)?; let schema = GraphQLSchema::new(root.schema.clone()); diff --git a/packages/fuel-indexer/src/service.rs b/packages/fuel-indexer/src/service.rs index 85cc5f29d..f92f0148a 100644 --- a/packages/fuel-indexer/src/service.rs +++ b/packages/fuel-indexer/src/service.rs @@ -177,7 +177,7 @@ impl IndexerService { let mut conn = self.pool.acquire().await?; let indices = queries::all_registered_indexers(&mut conn).await?; for index in indices { - let assets = queries::latest_assets_for_indexer(&mut conn, &index.id).await?; + let assets = queries::indexer_assets(&mut conn, &index.id).await?; let mut manifest = Manifest::try_from(&assets.manifest.bytes)?; let start_block = get_start_block(&mut conn, &manifest).await.unwrap_or(1); @@ -306,9 +306,7 @@ async fn create_service_task( .await { Ok(id) => { - let assets = - queries::latest_assets_for_indexer(&mut conn, &id) - .await?; + let assets = queries::indexer_assets(&mut conn, &id).await?; let mut manifest = Manifest::try_from(&assets.manifest.bytes)?;