Skip to content

Commit a6358cc

Browse files
cli: adds replace option
* Closes #1836. Signed-off-by: Ioannis Tsanaktsidis <[email protected]>
1 parent 196567e commit a6358cc

File tree

2 files changed

+62
-46
lines changed

2 files changed

+62
-46
lines changed

cernopendata/modules/fixtures/cli.py

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ def fixtures():
5858
@click.option('--profile', is_flag=True,
5959
help='Output profiling information.')
6060
@click.option('--verbose', is_flag=True, default=False)
61+
@click.option('--mode', required=True, type=click.Choice(
62+
['insert', 'replace']))
6163
@with_appcontext
62-
def records(skip_files, files, profile, verbose):
64+
def records(skip_files, files, profile, verbose, mode):
6365
"""Load all records."""
6466
if profile:
6567
import cProfile
@@ -77,13 +79,16 @@ def records(skip_files, files, profile, verbose):
7779
from invenio_files_rest.models import \
7880
Bucket, FileInstance, ObjectVersion
7981
from invenio_records_files.models import RecordsBuckets
82+
from invenio_pidstore.models import PersistentIdentifier
83+
from invenio_pidstore.errors import PIDDoesNotExistError
8084

8185
indexer = RecordIndexer()
8286
schema = current_app.extensions['invenio-jsonschemas'].path_to_url(
8387
'records/record-v1.0.0.json'
8488
)
8589
data = pkg_resources.resource_filename('cernopendata',
8690
'modules/fixtures/data/records')
91+
8792
if files:
8893
record_json = files
8994
else:
@@ -103,48 +108,56 @@ def records(skip_files, files, profile, verbose):
103108
format(data.get('recid')))
104109

105110
files = data.get('files', [])
106-
107-
bucket = Bucket.create()
108-
109-
for file in files:
110-
if skip_files:
111-
break
112-
assert 'uri' in file
113-
assert 'size' in file
114-
assert 'checksum' in file
115-
111+
if mode == 'insert':
112+
bucket = Bucket.create()
113+
114+
for file in files:
115+
if skip_files:
116+
break
117+
assert 'uri' in file
118+
assert 'size' in file
119+
assert 'checksum' in file
120+
121+
try:
122+
f = FileInstance.create()
123+
filename = file.get("uri").split('/')[-1:][0]
124+
f.set_uri(file.get("uri"), file.get(
125+
"size"), file.get("checksum"))
126+
obj = ObjectVersion.create(
127+
bucket,
128+
filename,
129+
_file_id=f.id
130+
)
131+
132+
file.update({
133+
'bucket': str(obj.bucket_id),
134+
'checksum': obj.file.checksum,
135+
'key': obj.key,
136+
'version_id': str(obj.version_id),
137+
})
138+
139+
except Exception as e:
140+
click.echo(
141+
'Recid {0} file {1} could not be loaded due '
142+
'to {2}.'.format(data.get('recid'), filename,
143+
str(e)))
144+
continue
145+
146+
id = uuid.uuid4()
147+
cernopendata_recid_minter(id, data)
148+
record = Record.create(data, id_=id)
149+
record['$schema'] = schema
150+
RecordsBuckets.create(
151+
record=record.model, bucket=bucket)
152+
else:
116153
try:
117-
f = FileInstance.create()
118-
filename = file.get("uri").split('/')[-1:][0]
119-
f.set_uri(file.get("uri"), file.get(
120-
"size"), file.get("checksum"))
121-
obj = ObjectVersion.create(
122-
bucket,
123-
filename,
124-
_file_id=f.id
125-
)
126-
127-
file.update({
128-
'bucket': str(obj.bucket_id),
129-
'checksum': obj.file.checksum,
130-
'key': obj.key,
131-
'version_id': str(obj.version_id),
132-
})
133-
134-
except Exception as e:
135-
click.echo(
136-
'Recid {0} file {1} could not be loaded due '
137-
'to {2}.'.format(data.get('recid'), filename,
138-
str(e)))
139-
continue
140-
141-
id = uuid.uuid4()
142-
cernopendata_recid_minter(id, data)
143-
record = Record.create(data, id_=id)
144-
record['$schema'] = schema
145-
RecordsBuckets.create(
146-
record=record.model, bucket=bucket)
147-
154+
pid = PersistentIdentifier.get('recid', data['recid'])
155+
except PIDDoesNotExistError:
156+
return None
157+
record = Record.get_record(pid.object_uuid)
158+
record['$schema'] = schema
159+
record.update(data)
160+
record.commit()
148161
db.session.commit()
149162
indexer.index(record)
150163
db.session.expunge_all()
@@ -180,7 +193,8 @@ def glossary_terms():
180193
with open(filename, 'rb') as source:
181194
for data in json.load(source):
182195
if "collections" not in data and \
183-
not isinstance(data.get("collections", None), basestring):
196+
not isinstance(
197+
data.get("collections", None), basestring):
184198
data["collections"] = []
185199
data["collections"].append({"primary": "Terms"})
186200
id = uuid.uuid4()
@@ -228,7 +242,8 @@ def articles():
228242
with open(content_filename) as body_field:
229243
data["body"]["content"] = body_field.read()
230244
if "collections" not in data and \
231-
not isinstance(data.get("collections", None), basestring):
245+
not isinstance(
246+
data.get("collections", None), basestring):
232247
data["collections"] = []
233248
id = uuid.uuid4()
234249
cernopendata_articleid_minter(id, data)
@@ -254,6 +269,7 @@ def data_policies(skip_files):
254269
Bucket, FileInstance, ObjectVersion
255270
from invenio_records_files.models import RecordsBuckets
256271
from invenio_records_files.api import Record
272+
257273
from invenio_records.models import RecordMetadata
258274

259275
indexer = RecordIndexer()

scripts/populate-instance.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ if [[ "$@" = *"--skip-records"* ]]; then
127127
else
128128
if [[ "$@" = *"--skip-files"* ]]; then
129129
echo "[INFO] Skipping loading of record files."
130-
${INVENIO_WEB_INSTANCE} fixtures records --skip-files
130+
${INVENIO_WEB_INSTANCE} fixtures records --skip-files --mode insert
131131
else
132132
# Prevent memory leak which happens when all fixtures are loaded at once
133133
for recordfile in $(ls -Sr cernopendata/modules/fixtures/data/records/*.json);
134134
do
135-
${INVENIO_WEB_INSTANCE} fixtures records -f "$recordfile" --verbose
135+
${INVENIO_WEB_INSTANCE} fixtures records -f "$recordfile" --verbose --mode insert
136136
done
137137
fi
138138
fi

0 commit comments

Comments
 (0)