Skip to content

Commit

Permalink
db improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kesar committed Jun 21, 2018
1 parent e09ed04 commit e64b72a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
17 changes: 13 additions & 4 deletions plugins/sql_db_plugin/db/actions_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,47 @@ void actions_table::drop()
void actions_table::create()
{
*m_session << "CREATE TABLE actions("
"id INT NOT NULL AUTO_INCREMENT KEY,"
"id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
"account VARCHAR(12),"
"transaction_id VARCHAR(64),"
"seq SMALLINT,"
"parent INT DEFAULT NULL,"
"name VARCHAR(12),"
"created_at DATETIME DEFAULT NOW(),"
"data JSON, FOREIGN KEY (transaction_id) REFERENCES transactions(id) ON DELETE CASCADE,"
"FOREIGN KEY (account) REFERENCES accounts(name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

*m_session << "CREATE INDEX idx_actions_account ON actions (account);";
*m_session << "CREATE INDEX idx_actions_tx_id ON actions (transaction_id);";
*m_session << "CREATE INDEX idx_actions_created ON actions (created_at);";

*m_session << "CREATE TABLE actions_accounts("
"actor VARCHAR(12),"
"permission VARCHAR(12),"
"action_id INT NOT NULL, FOREIGN KEY (action_id) REFERENCES actions(id) ON DELETE CASCADE,"
"FOREIGN KEY (actor) REFERENCES accounts(name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

*m_session << "CREATE INDEX idx_actions_actor ON actions_accounts (actor);";
*m_session << "CREATE INDEX idx_actions_action_id ON actions_accounts (action_id);";

*m_session << "CREATE TABLE tokens("
"account VARCHAR(13),"
"symbol VARCHAR(10),"
"amount REAL(14,4),"
"FOREIGN KEY (account) REFERENCES accounts(name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;"; // TODO: other tokens could have diff format.

*m_session << "CREATE INDEX idx_tokens_account ON tokens (account);";

*m_session << "CREATE TABLE votes("
"account VARCHAR(13) PRIMARY KEY,"
"votes JSON"
", FOREIGN KEY (account) REFERENCES accounts(name), UNIQUE KEY account (account)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

*m_session << "CREATE TABLE stakes("
"account VARCHAR(13),"
"account VARCHAR(13) PRIMARY KEY,"
"cpu REAL(14,4),"
"net REAL(14,4),"
"FOREIGN KEY (account) REFERENCES accounts(name), UNIQUE KEY account (account)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

"FOREIGN KEY (account) REFERENCES accounts(name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";
}

void actions_table::add(chain::action action, chain::transaction_id_type transaction_id, fc::time_point_sec transaction_time, uint8_t seq)
Expand Down
6 changes: 6 additions & 0 deletions plugins/sql_db_plugin/db/blocks_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ void blocks_table::create()
"new_producers JSON DEFAULT NULL,"
"num_transactions INT DEFAULT 0,"
"confirmed INT, FOREIGN KEY (producer) REFERENCES accounts(name), UNIQUE KEY block_number (block_number)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

*m_session << "CREATE INDEX idx_blocks_producer ON blocks (producer);";
*m_session << "CREATE INDEX idx_blocks_number ON blocks (block_number);";

}



void blocks_table::add(chain::signed_block_ptr block)
{
const auto block_id_str = block->id().str();
Expand Down
3 changes: 3 additions & 0 deletions plugins/sql_db_plugin/db/transactions_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void transactions_table::create()
"created_at DATETIME DEFAULT NOW(),"
"num_actions INT DEFAULT 0,"
"updated_at DATETIME DEFAULT NOW(), FOREIGN KEY (block_id) REFERENCES blocks(block_number) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;";

*m_session << "CREATE INDEX transactions_block_id ON transactions (block_id);";

}

void transactions_table::add(uint32_t block_id, chain::transaction transaction)
Expand Down

0 comments on commit e64b72a

Please sign in to comment.