CREATE TABLE IF NOT EXISTS blocks(
    sqlId INT PRIMARY KEY AUTO_INCREMENT,
    blockId VARCHAR(32) UNIQUE NOT NULL,
    blockHash VARCHAR(64) UNIQUE NOT NULL,
    previousHash VARCHAR(64) UNIQUE NOT NULL,
    timestamp DATETIME NOT NULL,
    height INT UNIQUE NOT NULL,
    nonce INT NOT NULL
);
CREATE TABLE IF NOT EXISTS cards(
    sqlId INT PRIMARY KEY AUTO_INCREMENT,
    blockId VARCHAR(32) UNIQUE NOT NULL,
    cardId VARCHAR(32) UNIQUE NOT NULL,
    pageId INT NOT NULL,
    FOREIGN KEY (blockId) REFERENCES blocks(blockId)
);
CREATE TABLE IF NOT EXISTS transactions(
    sqlId INT PRIMARY KEY AUTO_INCREMENT,
    blockId VARCHAR(32) NOT NULL,
    cardId VARCHAR(32) UNIQUE NOT NULL,
    transactionId VARCHAR(32) UNIQUE NOT NULL,
    timestamp DATETIME NOT NULL,
    sender VARCHAR(128) NOT NULL,
    receiver VARCHAR(128) NOT NULL,
    signature VARCHAR(128) NOT NULL,
    isPending BOOLEAN NOT NULL DEFAULT true,
    isAbandoned BOOLEAN NOT NULL DEFAULT false,
    FOREIGN KEY (blockId) REFERENCES blocks(blockId),
    FOREIGN KEY (cardId) REFERENCES cards(cardId)
);
CREATE TABLE IF NOT EXISTS peers(
    sqlId INT PRIMARY KEY AUTO_INCREMENT,
    peerId VARCHAR(32) UNIQUE NOT NULL,
    baseUrl VARCHAR(128) UNIQUE NOT NULL,
    isUp BOOLEAN NOT NULL,
    downCount INT DEFAULT 0,
    lastTry TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
