Commit ea186fa5 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Implemented copy user migration.

parent 197899d4
......@@ -143,18 +143,30 @@ def setup_repository_db():
global repository_db
global repository_db_conn
repository_db_conn, repository_db = sqlalchemy_repository_db(readonly=False)
logger.info('setup repository db')
url = 'postgresql://%s:%s@%s:%d/%s' % (
config.repository_db.user,
config.repository_db.password,
config.repository_db.host,
config.repository_db.port,
config.repository_db.dbname)
def sqlalchemy_repository_db(readonly=True, **kwargs):
"""
Returns SQLAlchemy connection and session for the given db parameters.
It uses the regular `config.repository_db` parameters updated with the given
`**kwargs`.
"""
def no_flush():
pass
params = config.repository_db._asdict()
params.update(**kwargs)
url = 'postgresql://%s:%s@%s:%d/%s' % utils.to_tuple(params, 'user', 'password', 'host', 'port', 'dbname')
engine = create_engine(url, echo=False)
repository_db_conn = engine.connect()
repository_db = Session(bind=repository_db_conn, autocommit=True)
logger.info('setup repository db')
if readonly:
repository_db.flush = no_flush
return repository_db_conn, repository_db
def reset():
......@@ -261,7 +273,7 @@ def repository_db_connection(dbname=None, with_trans=True):
logger.error('Unhandled exception within repository db connection.', exc_info=e)
conn.rollback()
conn.close()
return
raise e
conn.commit()
conn.close()
......@@ -276,7 +288,7 @@ def reset_repository_db():
repository_db.expunge_all()
repository_db.invalidate()
if repository_db_conn is not None:
repository_db_conn.close()
repository_db_conn.close_all()
# perform the reset
with repository_db_connection(with_trans=False) as conn:
......
......@@ -346,8 +346,6 @@ class Upload(Chord, datamodel.Upload):
with utils.timer(
logger, 'staged upload deleted', step='delete',
upload_size=self.upload_files.size):
import time
time.sleep(10)
self.upload_files.delete()
self.delete()
......
......@@ -253,3 +253,7 @@ class archive:
@staticmethod
def upload_id(archive_id: str) -> str:
return archive.item(archive_id, 0)
def to_tuple(self, *args):
return tuple(self[arg] for arg in args)
......@@ -114,7 +114,9 @@ def repository_db(monkeysession):
monkeysession.setattr('nomad.infrastructure.repository_db', session)
yield infrastructure.repository_db
trans.rollback()
session.close()
session.expunge_all()
session.invalidate()
session.close_all()
@pytest.fixture(scope='function')
......@@ -142,38 +144,6 @@ def admin_user(repository_db):
return coe_repo.admin_user()
# @pytest.fixture(scope='function')
# def mocksearch(monkeypatch):
# uploads_by_id = {}
# by_archive_id = {}
# def persist(calc):
# uploads_by_id.setdefault(calc.upload_id, []).append(calc)
# by_archive_id[calc.calc_id] = calc
# def upload_exists(self):
# return self.upload_id in uploads_by_id
# def upload_delete(self):
# upload_id = self.upload_id
# if upload_id in uploads_by_id:
# for calc in uploads_by_id[upload_id]:
# del(by_archive_id[calc.calc_id])
# del(uploads_by_id[upload_id])
# @property
# def upload_calcs(self):
# return uploads_by_id.get(self.upload_id, [])
# monkeypatch.setattr('nomad.repo.RepoCalc.persist', persist)
# monkeypatch.setattr('nomad.repo.RepoUpload.exists', upload_exists)
# monkeypatch.setattr('nomad.repo.RepoUpload.delete', upload_delete)
# monkeypatch.setattr('nomad.repo.RepoUpload.calcs', upload_calcs)
# monkeypatch.setattr('nomad.repo.RepoUpload.commit', lambda *args, **kwargs: None)
# return by_archive_id
@pytest.fixture(scope='function')
def no_warn(caplog):
yield caplog
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment