Skip to content

Commit dc09ecd

Browse files
committed
Add SQL scripts for final database setup and migrations
1 parent 882366a commit dc09ecd

5 files changed

Lines changed: 362 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
-- Drop tables in correct order to avoid foreign key constraints
2+
DROP TABLE IF EXISTS blocks;
3+
DROP TABLE IF EXISTS pools;
4+
DROP TABLE IF EXISTS statistics;
5+
DROP TABLE IF EXISTS elements_pegs;
6+
DROP TABLE IF EXISTS prices;
7+
DROP TABLE IF EXISTS state;
8+
9+
-- Create state table first
10+
CREATE TABLE state (
11+
name varchar(25) NOT NULL,
12+
number int NULL,
13+
string varchar(100) NULL,
14+
CONSTRAINT name_unique UNIQUE (name)
15+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
16+
17+
-- Insert initial state values
18+
INSERT INTO state VALUES('schema_version', 83, NULL);
19+
INSERT INTO state VALUES('last_elements_block', 0, NULL);
20+
21+
-- Create pools table with all required columns
22+
CREATE TABLE pools (
23+
id int NOT NULL AUTO_INCREMENT,
24+
unique_id varchar(100) NOT NULL,
25+
name varchar(50) NOT NULL,
26+
link varchar(255) NOT NULL,
27+
addresses json NOT NULL,
28+
regexes json NOT NULL,
29+
slug varchar(100) NOT NULL,
30+
PRIMARY KEY (id),
31+
UNIQUE KEY unique_id_idx (unique_id)
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
33+
34+
-- Insert default pool with all required fields
35+
INSERT INTO pools (id, unique_id, name, link, addresses, regexes, slug)
36+
VALUES (
37+
-1,
38+
'unknown',
39+
'Unknown',
40+
'https://learnmeabitcoin.com/technical/coinbase-transaction',
41+
'[]',
42+
'[]',
43+
'unknown'
44+
);
45+
46+
-- Create blocks table with updated schema
47+
CREATE TABLE blocks (
48+
height int unsigned NOT NULL,
49+
hash varchar(65) NOT NULL,
50+
blockTimestamp timestamp NOT NULL,
51+
size int unsigned NOT NULL,
52+
weight int unsigned NOT NULL,
53+
tx_count smallint unsigned NOT NULL,
54+
coinbase_raw text,
55+
difficulty double NOT NULL,
56+
pool_id int DEFAULT -1,
57+
fees double unsigned NOT NULL,
58+
reward double unsigned NOT NULL DEFAULT 0,
59+
fee_span json NOT NULL,
60+
median_fee double unsigned NOT NULL,
61+
PRIMARY KEY (height),
62+
INDEX pool_idx (pool_id),
63+
CONSTRAINT blocks_pool_fk FOREIGN KEY (pool_id) REFERENCES pools(id)
64+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
65+
66+
-- Create other required tables
67+
CREATE TABLE statistics (
68+
id int AUTO_INCREMENT PRIMARY KEY,
69+
added datetime NOT NULL,
70+
unconfirmed_transactions int unsigned NOT NULL,
71+
tx_per_second float unsigned NOT NULL,
72+
vbytes_per_second int unsigned NOT NULL,
73+
mempool_byte_weight int unsigned NOT NULL,
74+
fee_data longtext NOT NULL,
75+
total_fee double unsigned NOT NULL,
76+
INDEX added_idx (added)
77+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
78+
79+
-- Create prices table
80+
CREATE TABLE prices (
81+
id SERIAL PRIMARY KEY,
82+
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
83+
USD DECIMAL(24,8) NOT NULL DEFAULT 0,
84+
EUR DECIMAL(24,8) NOT NULL DEFAULT 0,
85+
GBP DECIMAL(24,8) NOT NULL DEFAULT 0,
86+
CAD DECIMAL(24,8) NOT NULL DEFAULT 0,
87+
CHF DECIMAL(24,8) NOT NULL DEFAULT 0,
88+
AUD DECIMAL(24,8) NOT NULL DEFAULT 0,
89+
JPY DECIMAL(24,8) NOT NULL DEFAULT 0,
90+
INDEX time_idx (time)
91+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
92+
93+
-- Add initial price data
94+
INSERT INTO prices (time, USD, EUR, GBP, CAD, CHF, AUD, JPY)
95+
VALUES (CURRENT_TIMESTAMP, 0, 0, 0, 0, 0, 0, 0);

backend/scripts/migrations.sql

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
-- First drop foreign keys safely
2+
SET @db_name = DATABASE();
3+
SET @table_name = 'blocks';
4+
5+
SELECT CONCAT('ALTER TABLE ', @table_name, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';')
6+
INTO @drop_fk_sql
7+
FROM information_schema.REFERENTIAL_CONSTRAINTS
8+
WHERE CONSTRAINT_SCHEMA = @db_name
9+
AND TABLE_NAME = @table_name
10+
AND REFERENCED_TABLE_NAME = 'pools'
11+
LIMIT 1;
12+
13+
SET @drop_sql = IFNULL(@drop_fk_sql, 'SELECT 1');
14+
PREPARE stmt FROM @drop_sql;
15+
EXECUTE stmt;
16+
DEALLOCATE PREPARE stmt;
17+
18+
-- Drop and recreate tables
19+
DROP TABLE IF EXISTS blocks;
20+
DROP TABLE IF EXISTS pools;
21+
DROP TABLE IF EXISTS statistics;
22+
DROP TABLE IF EXISTS elements_pegs;
23+
DROP TABLE IF EXISTS prices;
24+
DROP TABLE IF EXISTS state;
25+
26+
-- Create state table first
27+
CREATE TABLE state (
28+
name varchar(25) NOT NULL,
29+
number int(11) NULL,
30+
string varchar(100) NULL,
31+
CONSTRAINT name_unique UNIQUE (name)
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
33+
34+
-- Insert initial state
35+
INSERT INTO state VALUES('schema_version', 0, NULL);
36+
INSERT INTO state VALUES('last_elements_block', 0, NULL);
37+
38+
-- Create pools table
39+
CREATE TABLE pools (
40+
id int(11) NOT NULL AUTO_INCREMENT,
41+
name varchar(50) NOT NULL,
42+
link varchar(255) NOT NULL,
43+
addresses text NOT NULL,
44+
regexes text NOT NULL,
45+
PRIMARY KEY (id)
46+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
47+
48+
-- Create base blocks table
49+
CREATE TABLE blocks (
50+
height int(11) unsigned NOT NULL,
51+
hash varchar(65) NOT NULL,
52+
blockTimestamp timestamp NOT NULL,
53+
size int(11) unsigned NOT NULL,
54+
weight int(11) unsigned NOT NULL,
55+
tx_count int(11) unsigned NOT NULL,
56+
coinbase_raw text,
57+
difficulty bigint(20) unsigned NOT NULL,
58+
pool_id int(11) DEFAULT -1,
59+
fees double unsigned NOT NULL,
60+
fee_span json NOT NULL,
61+
median_fee double unsigned NOT NULL,
62+
reward double unsigned NOT NULL DEFAULT 0,
63+
PRIMARY KEY (height),
64+
INDEX (pool_id),
65+
FOREIGN KEY (pool_id) REFERENCES pools (id)
66+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
67+
68+
-- Create statistics table
69+
CREATE TABLE statistics (
70+
id int(11) NOT NULL AUTO_INCREMENT,
71+
added datetime NOT NULL,
72+
unconfirmed_transactions int(11) UNSIGNED NOT NULL,
73+
tx_per_second float UNSIGNED NOT NULL,
74+
vbytes_per_second int(10) UNSIGNED NOT NULL,
75+
mempool_byte_weight int(10) UNSIGNED NOT NULL,
76+
fee_data longtext NOT NULL,
77+
total_fee double UNSIGNED NOT NULL,
78+
PRIMARY KEY (id),
79+
INDEX added_idx (added)
80+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
81+
82+
-- Insert default pool
83+
INSERT INTO pools (id, name, link, addresses, regexes)
84+
VALUES (-1, 'Unknown', '', '', '');
85+
86+
-- Set schema version to skip migrations
87+
UPDATE state SET number = 83 WHERE name = 'schema_version';
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
-- Drop foreign keys safely using information_schema
2+
SET @table_name = 'blocks';
3+
SET @constraints = (
4+
SELECT GROUP_CONCAT(CONSTRAINT_NAME)
5+
FROM information_schema.TABLE_CONSTRAINTS
6+
WHERE TABLE_NAME = @table_name
7+
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
8+
AND TABLE_SCHEMA = DATABASE()
9+
);
10+
11+
SET @drop_fk_stmt = IF(@constraints IS NOT NULL,
12+
CONCAT('ALTER TABLE ', @table_name, ' DROP FOREIGN KEY ', @constraints),
13+
'SELECT 1');
14+
15+
PREPARE stmt FROM @drop_fk_stmt;
16+
EXECUTE stmt;
17+
DEALLOCATE PREPARE stmt;
18+
19+
-- Drop and recreate tables with proper MySQL 8 syntax
20+
DROP TABLE IF EXISTS blocks;
21+
DROP TABLE IF EXISTS pools;
22+
DROP TABLE IF EXISTS statistics;
23+
DROP TABLE IF EXISTS elements_pegs;
24+
DROP TABLE IF EXISTS state;
25+
26+
-- Create state table
27+
CREATE TABLE state (
28+
name varchar(25) NOT NULL,
29+
number int NULL,
30+
string varchar(100) NULL,
31+
CONSTRAINT name_unique UNIQUE (name)
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
33+
34+
-- Create pools table
35+
CREATE TABLE pools (
36+
id int NOT NULL AUTO_INCREMENT,
37+
name varchar(50) NOT NULL,
38+
link varchar(255) NOT NULL,
39+
addresses text NOT NULL,
40+
regexes text NOT NULL,
41+
PRIMARY KEY (id)
42+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
43+
44+
-- Create blocks table
45+
CREATE TABLE blocks (
46+
height int unsigned NOT NULL DEFAULT 0,
47+
hash varchar(65) NOT NULL,
48+
blockTimestamp timestamp NOT NULL,
49+
size int unsigned NOT NULL DEFAULT 0,
50+
weight int unsigned NOT NULL DEFAULT 0,
51+
tx_count smallint unsigned NOT NULL DEFAULT 0,
52+
coinbase_raw text,
53+
difficulty double NOT NULL DEFAULT 0,
54+
pool_id int DEFAULT -1,
55+
fees double unsigned NOT NULL DEFAULT 0,
56+
reward double unsigned NOT NULL DEFAULT 0,
57+
fee_span json NOT NULL,
58+
median_fee double unsigned NOT NULL DEFAULT 0,
59+
INDEX height_idx (height),
60+
INDEX pool_idx (pool_id),
61+
CONSTRAINT blocks_pool_fk FOREIGN KEY (pool_id) REFERENCES pools(id)
62+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
63+
64+
-- Create prices table
65+
CREATE TABLE prices (
66+
id SERIAL PRIMARY KEY,
67+
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
68+
USD DECIMAL(24,8) NOT NULL DEFAULT 0,
69+
EUR DECIMAL(24,8) NOT NULL DEFAULT 0,
70+
GBP DECIMAL(24,8) NOT NULL DEFAULT 0,
71+
CAD DECIMAL(24,8) NOT NULL DEFAULT 0,
72+
CHF DECIMAL(24,8) NOT NULL DEFAULT 0,
73+
AUD DECIMAL(24,8) NOT NULL DEFAULT 0,
74+
JPY DECIMAL(24,8) NOT NULL DEFAULT 0,
75+
INDEX time_index (time)
76+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
77+
78+
-- Add initial price data to prevent query errors
79+
INSERT INTO prices (time, USD, EUR, GBP, CAD, CHF, AUD, JPY)
80+
VALUES (CURRENT_TIMESTAMP, 0, 0, 0, 0, 0, 0, 0);
81+
82+
-- Initialize state
83+
INSERT INTO state VALUES('schema_version', 83, NULL);
84+
INSERT INTO state VALUES('last_elements_block', 0, NULL);

backend/scripts/safe-init-db.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- Safely drop foreign key constraints
2+
SELECT DISTINCT CONSTRAINT_NAME INTO @fk_name
3+
FROM information_schema.KEY_COLUMN_USAGE
4+
WHERE TABLE_NAME = 'blocks'
5+
AND REFERENCED_TABLE_NAME = 'pools';
6+
7+
SET @drop_fk = CONCAT('ALTER TABLE blocks DROP FOREIGN KEY ', @fk_name);
8+
9+
-- Only execute if foreign key exists
10+
SET @sql = IFNULL(@drop_fk, 'SELECT "No foreign key exists"');
11+
PREPARE stmt FROM @sql;
12+
EXECUTE stmt;
13+
DEALLOCATE PREPARE stmt;
14+
15+
-- Drop all tables in correct order
16+
DROP TABLE IF EXISTS blocks;
17+
DROP TABLE IF EXISTS pools;
18+
DROP TABLE IF EXISTS statistics;
19+
DROP TABLE IF EXISTS elements_pegs;
20+
DROP TABLE IF EXISTS prices;
21+
DROP TABLE IF EXISTS state;
22+
23+
-- Create tables from scratch
24+
-- Create state table with schema version 83
25+
CREATE TABLE state (
26+
name varchar(25) NOT NULL,
27+
number int NULL,
28+
string varchar(100) NULL,
29+
CONSTRAINT name_unique UNIQUE (name)
30+
) ENGINE=InnoDB;
31+
32+
INSERT INTO state VALUES('schema_version', 83, NULL);
33+
INSERT INTO state VALUES('last_elements_block', 0, NULL);
34+
35+
-- Create pools table first (referenced by blocks)
36+
CREATE TABLE pools (
37+
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
38+
name varchar(50) NOT NULL,
39+
link varchar(255) NOT NULL,
40+
addresses text NOT NULL,
41+
regexes text NOT NULL
42+
) ENGINE=InnoDB;
43+
44+
-- Insert default pool
45+
INSERT INTO pools (id, name, link, addresses, regexes) VALUES (-1, 'Unknown', '', '', '');
46+
47+
-- Create blocks table with explicit foreign key name
48+
CREATE TABLE blocks (
49+
height int unsigned NOT NULL,
50+
hash varchar(65) NOT NULL,
51+
blockTimestamp timestamp NOT NULL,
52+
size int unsigned NOT NULL,
53+
weight int unsigned NOT NULL,
54+
tx_count smallint unsigned NOT NULL,
55+
coinbase_raw text,
56+
difficulty double NOT NULL,
57+
pool_id int DEFAULT -1,
58+
fees double unsigned NOT NULL,
59+
reward double unsigned NOT NULL DEFAULT 0,
60+
fee_span json NOT NULL,
61+
median_fee double unsigned NOT NULL,
62+
PRIMARY KEY (height),
63+
INDEX pool_idx (pool_id),
64+
CONSTRAINT blocks_pool_fk FOREIGN KEY (pool_id) REFERENCES pools(id)
65+
) ENGINE=InnoDB;
66+
67+
-- Create statistics and prices tables
68+
-- ...rest of table creation code...

backend/scripts/setup-db.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
$password = "root"
2+
3+
# Drop and create database
4+
Write-Host "Resetting database..."
5+
$dropCreate = @"
6+
DROP DATABASE IF EXISTS mempool;
7+
CREATE DATABASE mempool;
8+
"@
9+
echo $dropCreate | mysql -u root "-p$password"
10+
11+
# Read SQL file content
12+
Write-Host "Reading initialization script..."
13+
$sqlContent = Get-Content -Path "scripts/final-mysql-setup.sql" -Raw
14+
15+
# Execute initialization script
16+
Write-Host "Initializing database..."
17+
echo $sqlContent | mysql -u root "-p$password" mempool
18+
19+
# Verify setup
20+
Write-Host "`nVerifying setup..."
21+
$verify = @"
22+
USE mempool;
23+
SHOW TABLES;
24+
SELECT number FROM state WHERE name='schema_version';
25+
"@
26+
echo $verify | mysql -u root "-p$password"
27+
28+
Write-Host "`nSetup complete!"

0 commit comments

Comments
 (0)