Skip to content

Commit

Permalink
Use psycopg3 everywhere for PostgreSQL database
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Feb 7, 2024
1 parent 409aa79 commit dd6086d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
11 changes: 6 additions & 5 deletions bitcoinlib/data/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
# Default directory for database files. Relative paths will be based in user or bitcoinlib installation directory. Only used for sqlite files.
;database_dir=database

# Default database file for wallets, keys and transactions. Relative paths will be based in 'database_dir'
# Default database file for wallets, keys and transactions. Relative paths for sqlite will be based in 'database_dir'.
# You can use SQLite, PostgreSQL and MariaDB (MySQL) databases
;default_databasefile=bitcoinlib.sqlite
;default_databasefile_cache=bitcoinlib_cache.sqlite
;default_databasefile=postgresql+psycopg://postgres:bitcoinlib@localhost:5432/bitcoinlib

# You can also use PostgreSQL or MySQL databases, for instance:
;default_databasefile=postgresql://postgres:bitcoinlib@localhost:5432/bitcoinlib
;default_databasefile_cache==postgresql://postgres:bitcoinlib@localhost:5432/bitcoinlib_cache
# For caching SQLite or PostgreSQL databases can be used.
;default_databasefile_cache=bitcoinlib_cache.sqlite
;default_databasefile_cache==postgresql+psycopg://postgres:bitcoinlib@localhost:5432/bitcoinlib_cache

[common]
# Allow database threads in SQLite databases
Expand Down
2 changes: 2 additions & 0 deletions bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def __init__(self, db_uri=None, password=None):
if self.o.scheme == 'mysql':
db_uri += "&" if "?" in db_uri else "?"
db_uri += 'binary_prefix=true'
if self.o.scheme == 'postgresql':
db_uri = self.o._replace(scheme="postgresql+psycopg").geturl()
self.engine = create_engine(db_uri, isolation_level='READ UNCOMMITTED')

Session = sessionmaker(bind=self.engine)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@

CACHE_DBNAME1 = 'bitcoinlib_cache_unittest1'
CACHE_DBNAME2 = 'bitcoinlib_cache_unittest2'
# FIXME: Allow mariadb for cache database
# FIXME: Mariadb for cache database does not work due to problem with BLOB indexing
# if os.getenv('UNITTEST_DATABASE') == 'mysql' or os.getenv('UNITTEST_DATABASE') == 'mariadb':
# DATABASE_CACHE_UNITTESTS = 'mariadb://user:password@localhost:3306/%s' % CACHE_DBNAME1
# DATABASE_CACHE_UNITTESTS2 = 'mariadb://user:password@localhost:3306/%s' % CACHE_DBNAME2
if os.getenv('UNITTEST_DATABASE') == 'postgresql':
DATABASE_CACHE_UNITTESTS = 'postgresql://postgres:postgres@localhost:5432/%s' % CACHE_DBNAME1
DATABASE_CACHE_UNITTESTS2 = 'postgresql://postgres:postgres@localhost:5432/%s' % CACHE_DBNAME2
DATABASE_CACHE_UNITTESTS = 'postgresql+psycopg://postgres:postgres@localhost:5432/%s' % CACHE_DBNAME1
DATABASE_CACHE_UNITTESTS2 = 'postgresql+psycopg://postgres:postgres@localhost:5432/%s' % CACHE_DBNAME2
else:
DATABASE_CACHE_UNITTESTS = os.path.join(str(BCL_DATABASE_DIR), CACHE_DBNAME1) + '.sqlite'
DATABASE_CACHE_UNITTESTS2 = os.path.join(str(BCL_DATABASE_DIR), CACHE_DBNAME2) + '.sqlite'
Expand Down

0 comments on commit dd6086d

Please sign in to comment.