mirror of
https://github.com/alexgo-io/stacks-blockchain-api.git
synced 2026-01-12 22:43:34 +08:00
fix: consolidate db migrations (#1314)
This commit is contained in:
@@ -90,6 +90,11 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
|
||||
// `coinbase` tx types
|
||||
coinbase_payload: 'bytea',
|
||||
tx_size: {
|
||||
type: 'integer',
|
||||
notNull: true,
|
||||
expressionGenerated: 'length(raw_tx)'
|
||||
}
|
||||
});
|
||||
|
||||
pgm.createIndex('mempool_txs', 'tx_id', { method: 'hash' });
|
||||
@@ -100,6 +105,9 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.createIndex('mempool_txs', 'sponsor_address', { method: 'hash' });
|
||||
pgm.createIndex('mempool_txs', 'token_transfer_recipient_address', { method: 'hash' });
|
||||
pgm.createIndex('mempool_txs', [{ name: 'receipt_time', sort: 'DESC' }]);
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'receipt_block_height'], { where: 'pruned = false'});
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'fee_rate'], { where: 'pruned = false'});
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'tx_size'], { where: 'pruned = false'});
|
||||
|
||||
pgm.addConstraint('mempool_txs', 'unique_tx_id', `UNIQUE(tx_id)`);
|
||||
|
||||
|
||||
@@ -28,6 +28,11 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
processed: {
|
||||
type: 'boolean',
|
||||
notNull: true,
|
||||
},
|
||||
retry_count: {
|
||||
type: 'integer',
|
||||
notNull: true,
|
||||
default: 0,
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -26,5 +26,6 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
`);
|
||||
|
||||
pgm.createIndex('nft_custody', ['recipient', 'asset_identifier']);
|
||||
pgm.createIndex('nft_custody', 'asset_identifier');
|
||||
pgm.createIndex('nft_custody', ['asset_identifier', 'value'], { unique: true });
|
||||
pgm.createIndex('nft_custody', 'value');
|
||||
}
|
||||
|
||||
@@ -30,5 +30,6 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
`);
|
||||
|
||||
pgm.createIndex('nft_custody_unanchored', ['recipient', 'asset_identifier']);
|
||||
pgm.createIndex('nft_custody_unanchored', 'asset_identifier');
|
||||
pgm.createIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true });
|
||||
pgm.createIndex('nft_custody_unanchored', 'value');
|
||||
}
|
||||
|
||||
@@ -40,5 +40,8 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
LEFT JOIN microblock_count ON TRUE
|
||||
LEFT JOIN tx_count ON TRUE
|
||||
LEFT JOIN tx_count_unanchored ON TRUE
|
||||
LIMIT 1
|
||||
`);
|
||||
|
||||
pgm.createIndex('chain_tip', 'block_height', { unique: true });
|
||||
}
|
||||
|
||||
@@ -31,8 +31,11 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
// extension which might not be possible for some users.
|
||||
pgm.createMaterializedView('mempool_digest', {}, `SELECT NULL AS digest`);
|
||||
}
|
||||
|
||||
pgm.createIndex('mempool_digest', 'digest', { unique: true });
|
||||
}
|
||||
|
||||
export async function down(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.dropIndex('mempool_digest', 'digest', { unique: true, ifExists: true });
|
||||
pgm.dropMaterializedView('mempool_digest');
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
|
||||
|
||||
export const shorthands: ColumnDefinitions | undefined = undefined;
|
||||
|
||||
export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.addColumn('token_metadata_queue', {
|
||||
retry_count: {
|
||||
type: 'integer',
|
||||
notNull: true,
|
||||
default: 0,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
|
||||
|
||||
export const shorthands: ColumnDefinitions | undefined = undefined;
|
||||
|
||||
export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.createIndex('nft_custody', 'value', { method: 'hash' });
|
||||
pgm.createIndex('nft_custody_unanchored', 'value', { method: 'hash' });
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
|
||||
|
||||
export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.addColumn('mempool_txs', {
|
||||
tx_size: {
|
||||
type: 'integer',
|
||||
notNull: true,
|
||||
expressionGenerated: 'length(raw_tx)'
|
||||
}
|
||||
});
|
||||
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'receipt_block_height'], { where: 'pruned = false'});
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'fee_rate'], { where: 'pruned = false'});
|
||||
pgm.createIndex('mempool_txs', ['type_id', 'tx_size'], { where: 'pruned = false'});
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
|
||||
|
||||
export const shorthands: ColumnDefinitions | undefined = undefined;
|
||||
|
||||
export async function up(pgm: MigrationBuilder): Promise<void> {
|
||||
// Add LIMIT 1 to chain_tip view so we can add the uniqueness index for `block_height`.
|
||||
pgm.dropMaterializedView('chain_tip');
|
||||
pgm.createMaterializedView('chain_tip', {}, `
|
||||
WITH block_tip AS (
|
||||
SELECT block_height, block_hash, index_block_hash
|
||||
FROM blocks
|
||||
WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE)
|
||||
),
|
||||
microblock_tip AS (
|
||||
SELECT microblock_hash, microblock_sequence
|
||||
FROM microblocks, block_tip
|
||||
WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash
|
||||
AND microblock_canonical = true AND canonical = true
|
||||
ORDER BY microblock_sequence DESC
|
||||
LIMIT 1
|
||||
),
|
||||
microblock_count AS (
|
||||
SELECT COUNT(*)::INTEGER AS microblock_count
|
||||
FROM microblocks
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
),
|
||||
tx_count AS (
|
||||
SELECT COUNT(*)::INTEGER AS tx_count
|
||||
FROM txs
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE)
|
||||
),
|
||||
tx_count_unanchored AS (
|
||||
SELECT COUNT(*)::INTEGER AS tx_count_unanchored
|
||||
FROM txs
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
)
|
||||
SELECT *, block_tip.block_height AS block_count
|
||||
FROM block_tip
|
||||
LEFT JOIN microblock_tip ON TRUE
|
||||
LEFT JOIN microblock_count ON TRUE
|
||||
LEFT JOIN tx_count ON TRUE
|
||||
LEFT JOIN tx_count_unanchored ON TRUE
|
||||
LIMIT 1
|
||||
`);
|
||||
|
||||
pgm.addIndex('chain_tip', 'block_height', { unique: true });
|
||||
pgm.addIndex('mempool_digest', 'digest', { unique: true });
|
||||
pgm.addIndex('nft_custody', ['asset_identifier', 'value'], { unique: true });
|
||||
pgm.addIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true });
|
||||
}
|
||||
|
||||
export async function down(pgm: MigrationBuilder): Promise<void> {
|
||||
pgm.dropIndex('chain_tip', 'block_height', { unique: true, ifExists: true });
|
||||
pgm.dropIndex('mempool_digest', 'digest', { unique: true, ifExists: true });
|
||||
pgm.dropIndex('nft_custody', ['asset_identifier', 'value'], { unique: true, ifExists: true });
|
||||
pgm.dropIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true, ifExists: true });
|
||||
|
||||
pgm.dropMaterializedView('chain_tip');
|
||||
pgm.createMaterializedView('chain_tip', {}, `
|
||||
WITH block_tip AS (
|
||||
SELECT block_height, block_hash, index_block_hash
|
||||
FROM blocks
|
||||
WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE)
|
||||
),
|
||||
microblock_tip AS (
|
||||
SELECT microblock_hash, microblock_sequence
|
||||
FROM microblocks, block_tip
|
||||
WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash
|
||||
AND microblock_canonical = true AND canonical = true
|
||||
ORDER BY microblock_sequence DESC
|
||||
LIMIT 1
|
||||
),
|
||||
microblock_count AS (
|
||||
SELECT COUNT(*)::INTEGER AS microblock_count
|
||||
FROM microblocks
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
),
|
||||
tx_count AS (
|
||||
SELECT COUNT(*)::INTEGER AS tx_count
|
||||
FROM txs
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE)
|
||||
),
|
||||
tx_count_unanchored AS (
|
||||
SELECT COUNT(*)::INTEGER AS tx_count_unanchored
|
||||
FROM txs
|
||||
WHERE canonical = TRUE AND microblock_canonical = TRUE
|
||||
)
|
||||
SELECT *, block_tip.block_height AS block_count
|
||||
FROM block_tip
|
||||
LEFT JOIN microblock_tip ON TRUE
|
||||
LEFT JOIN microblock_count ON TRUE
|
||||
LEFT JOIN tx_count ON TRUE
|
||||
LEFT JOIN tx_count_unanchored ON TRUE
|
||||
`);
|
||||
}
|
||||
Reference in New Issue
Block a user