Skip to content

Commit

Permalink
feat: separate migration for development and staging environtment (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-nguyen-20032023 authored Dec 7, 2023
1 parent 069fe80 commit 17201a6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
6 changes: 6 additions & 0 deletions migrations/20231018090129_refactor_transaction_index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
Expand All @@ -12,6 +16,8 @@ export async function up(knex: Knex): Promise<void> {
}

export async function down(knex: Knex): Promise<void> {
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');
Expand Down
6 changes: 6 additions & 0 deletions migrations/20231018092805_refactor_event_index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
Expand All @@ -12,6 +16,8 @@ export async function up(knex: Knex): Promise<void> {
}

export async function down(knex: Knex): Promise<void> {
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');
Expand Down
5 changes: 5 additions & 0 deletions migrations/20231019031350_drop_old_btree_index_event.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Knex } from 'knex';
import { environmentDeploy } from '../src/common';
const envDeploy = process.env.NODE_ENV;

export async function up(knex: Knex): Promise<void> {
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');
});
}

export async function down(knex: Knex): Promise<void> {
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');
Expand Down
6 changes: 6 additions & 0 deletions migrations/20231019031400_drop_old_btree_index_transaction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Knex } from 'knex';
import { environmentDeploy } from '../src/common';
const envDeploy = process.env.NODE_ENV;

export async function up(knex: Knex): Promise<void> {
if (envDeploy !== environmentDeploy.development) return;

await knex.schema.alterTable('transaction', (table) => {
table.dropIndex('height', 'transaction_height_index');
table.dropIndex('timestamp', 'transaction_timestamp_index');
});
}

export async function down(knex: Knex): Promise<void> {
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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
Expand All @@ -10,6 +15,8 @@ export async function up(knex: Knex): Promise<void> {
});
}
export async function down(knex: Knex): Promise<void> {
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}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
if (envDeploy !== environmentDeploy.development) return;

await knex.schema.alterTable('event_attribute', (table) => {
table.dropIndex(
'block_height',
Expand All @@ -9,6 +14,8 @@ export async function up(knex: Knex): Promise<void> {
});
}
export async function down(knex: Knex): Promise<void> {
if (envDeploy !== environmentDeploy.development) return;

await knex.schema.alterTable('event_attribute', (table) => {
table.index(
'block_height',
Expand Down
31 changes: 25 additions & 6 deletions migrations/20231121021951_create_table_event_partition.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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
*/
Expand All @@ -19,12 +41,7 @@ export async function up(knex: Knex): Promise<void> {
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);`
`
);

/**
Expand Down Expand Up @@ -116,6 +133,8 @@ export async function up(knex: Knex): Promise<void> {
`
)
.transacting(trx);
await knex.raw(createTxIdIndex).transacting(trx);
await knex.raw(createBlockHeightIndex).transacting(trx);
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const environmentDeploy = {
development: 'development',
staging: 'staging',
};

export const REDIS_KEY = {
IBC_DENOM: 'ibc_denom',
DASHBOARD_STATISTICS: 'dashboard_statistics',
Expand Down

0 comments on commit 17201a6

Please sign in to comment.