Skip to content

Commit

Permalink
feat!: optimize transfer replay capability (#129)
Browse files Browse the repository at this point in the history
* chore: first migration

* chore: first pass

* refactor: consolidate migrations

* test: replay capability

* fix: warn when inserting location with missing prev_output

* chore: tweak transfer gap query
  • Loading branch information
rafaelcr committed Jul 5, 2023
1 parent 3e272f8 commit 97874cc
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 391 deletions.
24 changes: 21 additions & 3 deletions migrations/1676395230930_inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ export const shorthands: ColumnDefinitions | undefined = undefined;
export function up(pgm: MigrationBuilder): void {
pgm.createTable('inscriptions', {
id: {
type: 'serial',
type: 'bigserial',
primaryKey: true,
},
genesis_id: {
type: 'text',
notNull: true,
},
number: {
type: 'int',
type: 'bigint',
notNull: true,
},
sat_ordinal: {
type: 'numeric',
notNull: true,
},
sat_rarity: {
type: 'text',
notNull: true,
},
sat_coinbase_height: {
type: 'bigint',
notNull: true,
},
mime_type: {
Expand All @@ -26,7 +38,7 @@ export function up(pgm: MigrationBuilder): void {
notNull: true,
},
content_length: {
type: 'int',
type: 'bigint',
notNull: true,
},
content: {
Expand All @@ -37,8 +49,14 @@ export function up(pgm: MigrationBuilder): void {
type: 'numeric',
notNull: true,
},
curse_type: {
type: 'text',
},
});
pgm.createConstraint('inscriptions', 'inscriptions_number_unique', 'UNIQUE(number)');
pgm.createIndex('inscriptions', ['genesis_id']);
pgm.createIndex('inscriptions', ['mime_type']);
pgm.createIndex('inscriptions', ['sat_ordinal']);
pgm.createIndex('inscriptions', ['sat_rarity']);
pgm.createIndex('inscriptions', ['sat_coinbase_height']);
}
34 changes: 15 additions & 19 deletions migrations/1677284495299_locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ export const shorthands: ColumnDefinitions | undefined = undefined;
export function up(pgm: MigrationBuilder): void {
pgm.createTable('locations', {
id: {
type: 'serial',
type: 'bigserial',
primaryKey: true,
},
inscription_id: {
type: 'int',
type: 'bigint',
},
genesis_id: {
type: 'text',
notNull: true,
},
block_height: {
type: 'int',
type: 'bigint',
notNull: true,
},
block_hash: {
Expand All @@ -35,20 +38,14 @@ export function up(pgm: MigrationBuilder): void {
offset: {
type: 'numeric',
},
value: {
type: 'numeric',
prev_output: {
type: 'text',
},
sat_ordinal: {
prev_offset: {
type: 'numeric',
notNull: true,
},
sat_rarity: {
type: 'text',
notNull: true,
},
sat_coinbase_height: {
type: 'int',
notNull: true,
value: {
type: 'numeric',
},
timestamp: {
type: 'timestamptz',
Expand All @@ -72,15 +69,14 @@ export function up(pgm: MigrationBuilder): void {
);
pgm.createConstraint(
'locations',
'locations_inscription_id_block_height_unique',
'UNIQUE(inscription_id, block_height)'
'locations_genesis_id_block_height_unique',
'UNIQUE(genesis_id, block_height)'
);
pgm.createIndex('locations', ['genesis_id']);
pgm.createIndex('locations', ['block_height']);
pgm.createIndex('locations', ['block_hash']);
pgm.createIndex('locations', ['address']);
pgm.createIndex('locations', ['output']);
pgm.createIndex('locations', ['sat_ordinal']);
pgm.createIndex('locations', ['sat_rarity']);
pgm.createIndex('locations', ['sat_coinbase_height']);
pgm.createIndex('locations', ['timestamp']);
pgm.createIndex('locations', ['prev_output']);
}
27 changes: 7 additions & 20 deletions migrations/1677360299810_chain-tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@ import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('chain_tip', {
id: {
type: 'bool',
primaryKey: true,
default: true,
},
block_height: {
type: 'int',
notNull: true,
default: 767430, // First inscription block height
},
});
// Ensure only a single row can exist
pgm.addConstraint('chain_tip', 'chain_tip_one_row', 'CHECK(id)');
// Create the single row
pgm.sql('INSERT INTO chain_tip VALUES(DEFAULT)');
}

export function down(pgm: MigrationBuilder): void {
pgm.dropTable('chain_tip');
pgm.createMaterializedView(
'chain_tip',
{ data: true },
// Set block height 767430 (inscription #0 genesis) as default.
`SELECT GREATEST(MAX(block_height), 767430) AS block_height FROM locations`
);
pgm.createIndex('chain_tip', ['block_height'], { unique: true });
}
4 changes: 2 additions & 2 deletions migrations/1679686636818_json-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const shorthands: ColumnDefinitions | undefined = undefined;
export function up(pgm: MigrationBuilder): void {
pgm.createTable('json_contents', {
id: {
type: 'serial',
type: 'bigserial',
primaryKey: true,
},
inscription_id: {
type: 'int',
type: 'bigint',
notNull: true,
},
p: {
Expand Down
14 changes: 0 additions & 14 deletions migrations/1682654536767_chain-tip-inscription-count.ts

This file was deleted.

32 changes: 0 additions & 32 deletions migrations/1683042840697_expand-int-sizes.ts

This file was deleted.

1 change: 1 addition & 0 deletions migrations/1683047918926_mime-type-counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export function up(pgm: MigrationBuilder): void {
{ data: true },
`SELECT mime_type, COUNT(*) AS count FROM inscriptions GROUP BY mime_type`
);
pgm.createIndex('mime_type_counts', ['mime_type'], { unique: true });
}
3 changes: 1 addition & 2 deletions migrations/1683061444855_sat-rarity-counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export function up(pgm: MigrationBuilder): void {
`
SELECT sat_rarity, COUNT(*) AS count
FROM inscriptions AS i
INNER JOIN locations AS loc ON loc.inscription_id = i.id
WHERE loc.current = TRUE
GROUP BY sat_rarity
`
);
pgm.createIndex('sat_rarity_counts', ['sat_rarity'], { unique: true });
}
1 change: 1 addition & 0 deletions migrations/1683130423352_inscription-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export function up(pgm: MigrationBuilder): void {
{ data: true },
`SELECT COUNT(*) AS count FROM inscriptions`
);
pgm.createIndex('inscription_count', ['count'], { unique: true });
}
39 changes: 0 additions & 39 deletions migrations/1683130430977_chain-tip-view.ts

This file was deleted.

16 changes: 0 additions & 16 deletions migrations/1685378650151_prev-location.ts

This file was deleted.

12 changes: 0 additions & 12 deletions migrations/1686170300438_curse-type.ts

This file was deleted.

11 changes: 0 additions & 11 deletions migrations/1688153654886_concurrent-view-indexes.ts

This file was deleted.

Loading

0 comments on commit 97874cc

Please sign in to comment.