Bug in archive content history
Motivation
----------
Traceback
```
2020-03-05 13:38:04,692 11088   CRITICAL        Thread [33/220] :  Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/rucio/daemons/reaper/reaper2.py", line 518, in reaper
    delete_replicas(rse_id=rse_id, files=deleted_files)
  File "/usr/lib/python2.7/site-packages/rucio/db/sqla/session.py", line 374, in new_funct
    raise DatabaseException(str(error))
DatabaseException: Database exception.
Details: (cx_Oracle.IntegrityError) ORA-02292: integrity constraint (ATLAS_RUCIO.DATASET_LOCKS_DID_FK) violated - child record found
```




diff --git a/lib/rucio/core/did.py b/lib/rucio/core/did.py
index 326ccd6a6..cc425a66e 100644
--- a/lib/rucio/core/did.py
+++ b/lib/rucio/core/did.py
@@ -2005,6 +2005,7 @@ def insert_content_history(content_clause, did_created_at, session=None):
     :param did_created_at: Creation date of the did
     :param session: The database session in use.
     """
+    new_did_created_at = did_created_at
     query = session.query(models.DataIdentifierAssociation.scope,
                           models.DataIdentifierAssociation.name,
                           models.DataIdentifierAssociation.child_scope,
@@ -2022,6 +2023,8 @@ def insert_content_history(content_clause, did_created_at, session=None):
         filter(or_(*content_clause))
 
     for cont in query.all():
+        if not did_created_at:
+            new_did_created_at = cont.created_at
         models.DataIdentifierAssociationHistory(
             scope=cont.scope,
             name=cont.name,
@@ -2037,6 +2040,6 @@ def insert_content_history(content_clause, did_created_at, session=None):
             rule_evaluation=cont.rule_evaluation,
             updated_at=cont.updated_at,
             created_at=cont.created_at,
-            did_created_at=did_created_at,
+            did_created_at=new_did_created_at,
             deleted_at=datetime.utcnow()
         ).save(session=session, flush=False)
['lib/rucio/core/did.py']