From 17201a630c81b9ff7bbcddc5997431fc13bc5e2c Mon Sep 17 00:00:00 2001 From: matthew-nguyen-20032023 <129312342+matthew-nguyen-20032023@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:00:54 +0700 Subject: [PATCH] feat: separate migration for development and staging environtment (#523) --- ...231018090129_refactor_transaction_index.ts | 6 ++++ .../20231018092805_refactor_event_index.ts | 6 ++++ ...231019031350_drop_old_btree_index_event.ts | 5 +++ ...031400_drop_old_btree_index_transaction.ts | 6 ++++ ...4210_create_brin_index_event_atttribute.ts | 7 +++++ ...34225_drop_btree_index_event_atttribute.ts | 7 +++++ ...1121021951_create_table_event_partition.ts | 31 +++++++++++++++---- src/common/constant.ts | 5 +++ 8 files changed, 67 insertions(+), 6 deletions(-) diff --git a/migrations/20231018090129_refactor_transaction_index.ts b/migrations/20231018090129_refactor_transaction_index.ts index 320d91439..c007fad81 100644 --- a/migrations/20231018090129_refactor_transaction_index.ts +++ b/migrations/20231018090129_refactor_transaction_index.ts @@ -1,6 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.raw(` CREATE INDEX IF NOT EXISTS brin_idx_height_transaction ON transaction USING brin (height) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true) @@ -12,6 +16,8 @@ export async function up(knex: Knex): Promise { } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('transaction', (table) => { table.dropIndex('height', 'brin_idx_height_transaction'); table.dropIndex('timestamp', 'brin_idx_timestamp_transaction'); diff --git a/migrations/20231018092805_refactor_event_index.ts b/migrations/20231018092805_refactor_event_index.ts index 95df668ca..1dc5d38e0 100644 --- a/migrations/20231018092805_refactor_event_index.ts +++ b/migrations/20231018092805_refactor_event_index.ts @@ -1,6 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.raw(` CREATE INDEX IF NOT EXISTS brin_idx_block_height_event ON event USING brin (block_height) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true) @@ -12,6 +16,8 @@ export async function up(knex: Knex): Promise { } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('event', (table) => { table.dropIndex('block_height', 'brin_idx_block_height_event'); table.dropIndex('tx_id', 'brin_idx_tx_id_event'); diff --git a/migrations/20231019031350_drop_old_btree_index_event.ts b/migrations/20231019031350_drop_old_btree_index_event.ts index 8cccb30a1..6ec002b7f 100644 --- a/migrations/20231019031350_drop_old_btree_index_event.ts +++ b/migrations/20231019031350_drop_old_btree_index_event.ts @@ -1,6 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('event', (table) => { table.dropIndex('block_height', 'event_block_height_index'); table.dropIndex('tx_id', 'transaction_event_tx_id_index'); @@ -8,6 +12,7 @@ export async function up(knex: Knex): Promise { } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; await knex.schema.alterTable('event', (table) => { table.index('block_height', 'event_block_height_index', 'btree'); table.index('tx_id', 'transaction_event_tx_id_index', 'btree'); diff --git a/migrations/20231019031400_drop_old_btree_index_transaction.ts b/migrations/20231019031400_drop_old_btree_index_transaction.ts index 425fc836a..a5466d272 100644 --- a/migrations/20231019031400_drop_old_btree_index_transaction.ts +++ b/migrations/20231019031400_drop_old_btree_index_transaction.ts @@ -1,6 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('transaction', (table) => { table.dropIndex('height', 'transaction_height_index'); table.dropIndex('timestamp', 'transaction_timestamp_index'); @@ -8,6 +12,8 @@ export async function up(knex: Knex): Promise { } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('transaction', (table) => { table.index('height', 'transaction_height_index', 'btree'); table.index('timestamp', 'transaction_timestamp_index', 'btree'); diff --git a/migrations/20231019034210_create_brin_index_event_atttribute.ts b/migrations/20231019034210_create_brin_index_event_atttribute.ts index 5cc353ab4..92e7dc6ea 100644 --- a/migrations/20231019034210_create_brin_index_event_atttribute.ts +++ b/migrations/20231019034210_create_brin_index_event_atttribute.ts @@ -1,5 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; + export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.raw(` CREATE INDEX IF NOT EXISTS brin_idx_blh_event_attribute ON event_attribute USING brin (block_height) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true) @@ -10,6 +15,8 @@ export async function up(knex: Knex): Promise { }); } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('event_attribute', (table) => { table.dropIndex('block_height', `brin_idx_blh_event_attribute`); table.dropIndex('tx_id', `brin_idx_tx_id_event_attribute}`); diff --git a/migrations/20231019034225_drop_btree_index_event_atttribute.ts b/migrations/20231019034225_drop_btree_index_event_atttribute.ts index bf3919a62..62674fb1c 100644 --- a/migrations/20231019034225_drop_btree_index_event_atttribute.ts +++ b/migrations/20231019034225_drop_btree_index_event_atttribute.ts @@ -1,5 +1,10 @@ import { Knex } from 'knex'; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; + export async function up(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('event_attribute', (table) => { table.dropIndex( 'block_height', @@ -9,6 +14,8 @@ export async function up(knex: Knex): Promise { }); } export async function down(knex: Knex): Promise { + if (envDeploy !== environmentDeploy.development) return; + await knex.schema.alterTable('event_attribute', (table) => { table.index( 'block_height', diff --git a/migrations/20231121021951_create_table_event_partition.ts b/migrations/20231121021951_create_table_event_partition.ts index 67448bf09..ffb05f3bd 100644 --- a/migrations/20231121021951_create_table_event_partition.ts +++ b/migrations/20231121021951_create_table_event_partition.ts @@ -1,8 +1,30 @@ import { Knex } from 'knex'; import config from '../config.json' assert { type: 'json' }; +import { environmentDeploy } from '../src/common'; +const envDeploy = process.env.NODE_ENV; export async function up(knex: Knex): Promise { await knex.transaction(async (trx) => { + const createTxIdIndex = + envDeploy === environmentDeploy.development + ? ` + CREATE INDEX event_partition_tx_id_brin_idx + ON event USING BRIN (tx_id) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true); + ` + : ` + CREATE INDEX event_partition_tx_id_btree_idx + ON event USING BTREE (tx_id ASC NULLS LAST); + `; + const createBlockHeightIndex = + envDeploy === environmentDeploy.development + ? ` + CREATE INDEX event_partition_block_height_brin_idx + ON event USING BRIN (block_height) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true); + ` + : ` + CREATE INDEX event_partition_block_height_btree_idx + ON event USING BTREE (block_height ASC NULLS LAST); + `; /** * @description: Create event table with config support partition on id column */ @@ -19,12 +41,7 @@ export async function up(knex: Knex): Promise { CREATE INDEX event_partition_type_idx ON event_partition (type); - - CREATE INDEX event_partition_tx_id_brin_idx - ON event_partition USING BRIN (tx_id) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true); - - CREATE INDEX event_partition_block_height_brin_idx - ON event_partition USING BRIN (block_height) WITH (PAGES_PER_RANGE = 10, AUTOSUMMARIZE = true);` + ` ); /** @@ -116,6 +133,8 @@ export async function up(knex: Knex): Promise { ` ) .transacting(trx); + await knex.raw(createTxIdIndex).transacting(trx); + await knex.raw(createBlockHeightIndex).transacting(trx); }); } diff --git a/src/common/constant.ts b/src/common/constant.ts index 0426e98f4..3991063b6 100644 --- a/src/common/constant.ts +++ b/src/common/constant.ts @@ -1,3 +1,8 @@ +export const environmentDeploy = { + development: 'development', + staging: 'staging', +}; + export const REDIS_KEY = { IBC_DENOM: 'ibc_denom', DASHBOARD_STATISTICS: 'dashboard_statistics',