Skip to content

Commit e982a7d

Browse files
authored
refactor(l2): improve SQL store initialization (#5479)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> Migrations of SQL schema are tricky. If new tables were added, it has to be migrated manually. **Description** <!-- A clear and concise general description of the changes this PR introduces --> Make SQL schema setup sentences idempotent so they can be executed on every start up. <!-- Link to issues: Resolves #111, Resolves #222 -->
1 parent 86dd773 commit e982a7d

File tree

1 file changed

+25
-31
lines changed
  • crates/l2/storage/src/store_db

1 file changed

+25
-31
lines changed

crates/l2/storage/src/store_db/sql.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ impl Debug for SQLStore {
2929
}
3030

3131
const DB_SCHEMA: [&str; 17] = [
32-
"CREATE TABLE blocks (block_number INT PRIMARY KEY, batch INT)",
33-
"CREATE TABLE messages (batch INT, idx INT, message_hash BLOB, PRIMARY KEY (batch, idx))",
34-
"CREATE TABLE privileged_transactions (batch INT PRIMARY KEY, transactions_hash BLOB)",
35-
"CREATE TABLE state_roots (batch INT PRIMARY KEY, state_root BLOB)",
36-
"CREATE TABLE blob_bundles (batch INT, idx INT, blob_bundle BLOB, PRIMARY KEY (batch, idx))",
37-
"CREATE TABLE account_updates (block_number INT PRIMARY KEY, updates BLOB)",
38-
"CREATE TABLE commit_txs (batch INT PRIMARY KEY, commit_tx BLOB)",
39-
"CREATE TABLE verify_txs (batch INT PRIMARY KEY, verify_tx BLOB)",
40-
"CREATE TABLE operation_count (_id INT PRIMARY KEY, transactions INT, privileged_transactions INT, messages INT)",
41-
"INSERT INTO operation_count VALUES (0, 0, 0, 0)",
42-
"CREATE TABLE latest_sent (_id INT PRIMARY KEY, batch INT)",
43-
"INSERT INTO latest_sent VALUES (0, 0)",
44-
"CREATE TABLE batch_proofs (batch INT, prover_type INT, proof BLOB, PRIMARY KEY (batch, prover_type))",
45-
"CREATE TABLE block_signatures (block_hash BLOB PRIMARY KEY, signature BLOB)",
46-
"CREATE TABLE batch_signatures (batch INT PRIMARY KEY, signature BLOB)",
47-
"CREATE TABLE batch_prover_input (batch INT, prover_version TEXT, prover_input BLOB, PRIMARY KEY (batch, prover_version))",
48-
"CREATE TABLE fee_config (block_number INT PRIMARY KEY, fee_config BLOB)",
32+
"CREATE TABLE IF NOT EXISTS blocks (block_number INT PRIMARY KEY, batch INT)",
33+
"CREATE TABLE IF NOT EXISTS messages (batch INT, idx INT, message_hash BLOB, PRIMARY KEY (batch, idx))",
34+
"CREATE TABLE IF NOT EXISTS privileged_transactions (batch INT PRIMARY KEY, transactions_hash BLOB)",
35+
"CREATE TABLE IF NOT EXISTS state_roots (batch INT PRIMARY KEY, state_root BLOB)",
36+
"CREATE TABLE IF NOT EXISTS blob_bundles (batch INT, idx INT, blob_bundle BLOB, PRIMARY KEY (batch, idx))",
37+
"CREATE TABLE IF NOT EXISTS account_updates (block_number INT PRIMARY KEY, updates BLOB)",
38+
"CREATE TABLE IF NOT EXISTS commit_txs (batch INT PRIMARY KEY, commit_tx BLOB)",
39+
"CREATE TABLE IF NOT EXISTS verify_txs (batch INT PRIMARY KEY, verify_tx BLOB)",
40+
"CREATE TABLE IF NOT EXISTS operation_count (_id INT PRIMARY KEY, transactions INT, privileged_transactions INT, messages INT)",
41+
"INSERT INTO operation_count VALUES (0, 0, 0, 0) ON CONFLICT(_id) DO NOTHING",
42+
"CREATE TABLE IF NOT EXISTS latest_sent (_id INT PRIMARY KEY, batch INT)",
43+
"INSERT INTO latest_sent VALUES (0, 0) ON CONFLICT(_id) DO NOTHING",
44+
"CREATE TABLE IF NOT EXISTS batch_proofs (batch INT, prover_type INT, proof BLOB, PRIMARY KEY (batch, prover_type))",
45+
"CREATE TABLE IF NOT EXISTS block_signatures (block_hash BLOB PRIMARY KEY, signature BLOB)",
46+
"CREATE TABLE IF NOT EXISTS batch_signatures (batch INT PRIMARY KEY, signature BLOB)",
47+
"CREATE TABLE IF NOT EXISTS batch_prover_input (batch INT, prover_version TEXT, prover_input BLOB, PRIMARY KEY (batch, prover_version))",
48+
"CREATE TABLE IF NOT EXISTS fee_config (block_number INT PRIMARY KEY, fee_config BLOB)",
4949
];
5050

5151
impl SQLStore {
@@ -82,20 +82,14 @@ impl SQLStore {
8282
// https://sqlite.org/wal.html#concurrency
8383
// still a limit of only 1 writer is imposed by sqlite databases
8484
self.query("PRAGMA journal_mode=WAL;", ()).await?;
85-
let mut rows = self
86-
.query(
87-
"SELECT name FROM sqlite_schema WHERE type='table' AND name='blocks'",
88-
(),
89-
)
90-
.await?;
91-
if rows.next().await?.is_none() {
92-
let empty_param = ().into_params()?;
93-
let queries = DB_SCHEMA
94-
.iter()
95-
.map(|v| (*v, empty_param.clone()))
96-
.collect();
97-
self.execute_in_tx(queries, None).await?;
98-
}
85+
86+
// Create DB schema if not exists
87+
let empty_param = ().into_params()?;
88+
let queries = DB_SCHEMA
89+
.iter()
90+
.map(|v| (*v, empty_param.clone()))
91+
.collect();
92+
self.execute_in_tx(queries, None).await?;
9993
Ok(())
10094
}
10195

0 commit comments

Comments
 (0)