'Create the database schema.'

def initialize(store):

    store.config = {}
    store.configure()
    for stmt in (store._ddl['configvar'], 'CREATE    TABLE    datadir    (\n                datadir_id        NUMERIC(10)    NOT    NULL    PRIMARY    KEY,\n                dirname                    VARCHAR(2000)    NOT    NULL,\n                blkfile_number    NUMERIC(8)    NULL,\n                blkfile_offset    NUMERIC(20)    NULL,\n                chain_id                NUMERIC(10)    NULL\n)', (('CREATE    TABLE    block    (\n                block_id                        NUMERIC(14)    NOT    NULL    PRIMARY    KEY,\n                block_hash                BINARY(32)        UNIQUE    NOT    NULL,\n                block_version    NUMERIC(10),\n                block_hashMerkleRoot    BINARY(32),\n                block_nTime            NUMERIC(20),\n                block_nBits            NUMERIC(10),\n                block_nNonce        NUMERIC(10),\n                block_height        NUMERIC(14)    NULL,\n                prev_block_id    NUMERIC(14)    NULL,\n                search_block_id    NUMERIC(14)    NULL,\n                block_chain_work    BINARY(' + str((WORK_BITS / 8))) + '),\n                block_value_in    NUMERIC(30)    NULL,\n                block_value_out    NUMERIC(30),\n                block_total_satoshis    NUMERIC(26)    NULL,\n                block_total_seconds    NUMERIC(20)    NULL,\n                block_satoshi_seconds    NUMERIC(28)    NULL,\n                block_total_ss    NUMERIC(28)    NULL,\n                block_num_tx        NUMERIC(10)    NOT    NULL,\n                block_ss_destroyed    NUMERIC(28)    NULL,\n                FOREIGN    KEY    (prev_block_id)\n                                REFERENCES    block    (block_id),\n                FOREIGN    KEY    (search_block_id)\n                                REFERENCES    block    (block_id)\n)'), 'CREATE    TABLE    chain    (\n                chain_id                NUMERIC(10)    NOT    NULL    PRIMARY    KEY,\n                chain_name        VARCHAR(100)    UNIQUE    NOT    NULL,\n                chain_code3    VARCHAR(5)        NULL,\n                chain_address_version    VARBINARY(100)    NOT    NULL,\n                chain_script_addr_vers    VARBINARY(100)    NULL,\n                chain_magic    BINARY(4)                    NULL,\n                chain_policy    VARCHAR(255)    NOT    NULL,\n                chain_decimals    NUMERIC(2)    NULL,\n                chain_last_block_id    NUMERIC(14)    NULL,\n                FOREIGN    KEY    (chain_last_block_id)\n                                REFERENCES    block    (block_id)\n)', 'CREATE    TABLE    chain_candidate    (\n                chain_id                        NUMERIC(10)    NOT    NULL,\n                block_id                        NUMERIC(14)    NOT    NULL,\n                in_longest                NUMERIC(1),\n                block_height        NUMERIC(14),\n                PRIMARY    KEY    (chain_id,    block_id),\n                FOREIGN    KEY    (block_id)    REFERENCES    block    (block_id)\n)', 'CREATE    INDEX    x_cc_block    ON    chain_candidate    (block_id)', 'CREATE    INDEX    x_cc_chain_block_height\n                ON    chain_candidate    (chain_id,    block_height)', 'CREATE    INDEX    x_cc_block_height    ON    chain_candidate    (block_height)', 'CREATE    TABLE    orphan_block    (\n                block_id                        NUMERIC(14)    NOT    NULL    PRIMARY    KEY,\n                block_hashPrev    BINARY(32)    NOT    NULL,\n                FOREIGN    KEY    (block_id)    REFERENCES    block    (block_id)\n)', 'CREATE    INDEX    x_orphan_block_hashPrev    ON    orphan_block    (block_hashPrev)', 'CREATE    TABLE    block_next    (\n                block_id                        NUMERIC(14)    NOT    NULL,\n                next_block_id    NUMERIC(14)    NOT    NULL,\n                PRIMARY    KEY    (block_id,    next_block_id),\n                FOREIGN    KEY    (block_id)    REFERENCES    block    (block_id),\n                FOREIGN    KEY    (next_block_id)    REFERENCES    block    (block_id)\n)', 'CREATE    TABLE    tx    (\n                tx_id                                    NUMERIC(26)    NOT    NULL    PRIMARY    KEY,\n                tx_hash                            BINARY(32)        UNIQUE    NOT    NULL,\n                tx_version                NUMERIC(10),\n                tx_lockTime            NUMERIC(10),\n                tx_size                            NUMERIC(10)\n)', 'CREATE    TABLE    unlinked_tx    (\n                tx_id                                NUMERIC(26)    NOT    NULL,\n                PRIMARY    KEY    (tx_id),\n                FOREIGN    KEY    (tx_id)\n                                REFERENCES    tx    (tx_id)\n)', 'CREATE    TABLE    block_tx    (\n                block_id                        NUMERIC(14)    NOT    NULL,\n                tx_id                                    NUMERIC(26)    NOT    NULL,\n                tx_pos                                NUMERIC(10)    NOT    NULL,\n                PRIMARY    KEY    (block_id,    tx_id),\n                UNIQUE    (block_id,    tx_pos),\n                FOREIGN    KEY    (block_id)\n                                REFERENCES    block    (block_id),\n                FOREIGN    KEY    (tx_id)\n                                REFERENCES    tx    (tx_id)\n)', 'CREATE    INDEX    x_block_tx_tx    ON    block_tx    (tx_id)', (('CREATE    TABLE    pubkey    (\n                pubkey_id                    NUMERIC(26)    NOT    NULL    PRIMARY    KEY,\n                pubkey_hash            BINARY(20)        UNIQUE    NOT    NULL,\n                pubkey                                VARBINARY(' + str(MAX_PUBKEY)) + ')    NULL\n)'), 'CREATE    TABLE    multisig_pubkey    (\n                multisig_id            NUMERIC(26)    NOT    NULL,\n                pubkey_id                    NUMERIC(26)    NOT    NULL,\n                PRIMARY    KEY    (multisig_id,    pubkey_id),\n                FOREIGN    KEY    (multisig_id)    REFERENCES    pubkey    (pubkey_id),\n                FOREIGN    KEY    (pubkey_id)    REFERENCES    pubkey    (pubkey_id)\n)', 'CREATE    INDEX    x_multisig_pubkey_pubkey    ON    multisig_pubkey    (pubkey_id)', (('CREATE    TABLE    txout    (\n                txout_id                        NUMERIC(26)    NOT    NULL    PRIMARY    KEY,\n                tx_id                                    NUMERIC(26)    NOT    NULL,\n                txout_pos                    NUMERIC(10)    NOT    NULL,\n                txout_value            NUMERIC(30)    NOT    NULL,\n                txout_scriptPubKey    VARBINARY(' + str(MAX_SCRIPT)) + '),\n                pubkey_id                    NUMERIC(26),\n                UNIQUE    (tx_id,    txout_pos),\n                FOREIGN    KEY    (pubkey_id)\n                                REFERENCES    pubkey    (pubkey_id)\n)'), 'CREATE    INDEX    x_txout_pubkey    ON    txout    (pubkey_id)', (('CREATE    TABLE    txin    (\n                txin_id                            NUMERIC(26)    NOT    NULL    PRIMARY    KEY,\n                tx_id                                    NUMERIC(26)    NOT    NULL,\n                txin_pos                        NUMERIC(10)    NOT    NULL,\n                txout_id                        NUMERIC(26)' + (((',\n                txin_scriptSig    VARBINARY(' + str(MAX_SCRIPT)) + '),\n                txin_sequence    NUMERIC(10)') if store.keep_scriptsig else '')) + ',\n                UNIQUE    (tx_id,    txin_pos),\n                FOREIGN    KEY    (tx_id)\n                                REFERENCES    tx    (tx_id)\n)'), 'CREATE    INDEX    x_txin_txout    ON    txin    (txout_id)', 'CREATE    TABLE    unlinked_txin    (\n                txin_id                            NUMERIC(26)    NOT    NULL    PRIMARY    KEY,\n                txout_tx_hash    BINARY(32)        NOT    NULL,\n                txout_pos                    NUMERIC(10)    NOT    NULL,\n                FOREIGN    KEY    (txin_id)    REFERENCES    txin    (txin_id)\n)', 'CREATE    INDEX    x_unlinked_txin_outpoint\n                ON    unlinked_txin    (txout_tx_hash,    txout_pos)', 'CREATE    TABLE    block_txin    (\n                block_id                        NUMERIC(14)    NOT    NULL,\n                txin_id                            NUMERIC(26)    NOT    NULL,\n                out_block_id        NUMERIC(14)    NOT    NULL,\n                PRIMARY    KEY    (block_id,    txin_id),\n                FOREIGN    KEY    (block_id)    REFERENCES    block    (block_id),\n                FOREIGN    KEY    (txin_id)    REFERENCES    txin    (txin_id),\n                FOREIGN    KEY    (out_block_id)    REFERENCES    block    (block_id)\n)', store._ddl['chain_summary'], store._ddl['txout_detail'], store._ddl['txin_detail'], store._ddl['txout_approx'], 'CREATE    TABLE    abe_lock    (\n                lock_id                            NUMERIC(10)    NOT    NULL    PRIMARY    KEY,\n                pid                                            VARCHAR(255)    NULL\n)'):
        try:
            store.ddl(stmt)
        except Exception:
            store.log.error('Failed:    %s', stmt)
            raise
    for key in ['chain', 'datadir', 'tx', 'txout', 'pubkey', 'txin', 'block']:
        store.create_sequence(key)
    store.sql('INSERT    INTO    abe_lock    (lock_id)    VALUES    (1)')
    for conf in CHAIN_CONFIG:
        conf = conf.copy()
        conf['name'] = conf.pop('chain')
        if ('policy' in conf):
            policy = conf.pop('policy')
        else:
            policy = conf['name']
        chain = Chain.create(policy, **conf)
        store.insert_chain(chain)
    store.sql('\n                                                INSERT    INTO    pubkey    (pubkey_id,    pubkey_hash)    VALUES    (?,    ?)', (NULL_PUBKEY_ID, store.binin(NULL_PUBKEY_HASH)))
    if store.args.use_firstbits:
        store.config['use_firstbits'] = 'true'
        store.ddl('CREATE    TABLE    abe_firstbits    (\n                                                                                pubkey_id                            NUMERIC(26)    NOT    NULL,\n                                                                                block_id                                NUMERIC(14)    NOT    NULL,\n                                                                                address_version    VARBINARY(10)    NOT    NULL,\n                                                                                firstbits                            VARCHAR(50)    NOT    NULL,\n                                                                                PRIMARY    KEY    (address_version,    pubkey_id,    block_id),\n                                                                                FOREIGN    KEY    (pubkey_id)    REFERENCES    pubkey    (pubkey_id),\n                                                                                FOREIGN    KEY    (block_id)    REFERENCES    block    (block_id)\n                                                                )')
        store.ddl('CREATE    INDEX    x_abe_firstbits\n                                                                                ON    abe_firstbits    (address_version,    firstbits)')
    else:
        store.config['use_firstbits'] = 'false'
    store.config['keep_scriptsig'] = ('true' if store.args.keep_scriptsig else 'false')
    store.save_config()
    store.commit()
