Skip to content

Commit 399eccd

Browse files
authored
Merge pull request #218 from open-data/feature/ds-purge-options
DS Purge Options & Delete State Handling
2 parents 5ebed74 + 919e022 commit 399eccd

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

changes/218.canada.changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `--list` and `--yes` options to the `datastore purge` command. The command now handled soft-deleted Resources and Datasets.

ckanext/datastore/cli.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,17 @@ def _parse_db_config(config_key: str = u'sqlalchemy.url'):
129129

130130
@datastore.command(
131131
'purge',
132-
short_help='purge orphaned resources from the datastore.'
132+
short_help='purge orphaned or deleted resources from the datastore.'
133133
)
134-
def purge():
135-
'''Purge orphaned resources from the datastore using the datastore_delete
134+
# (canada fork only): more options and state=deleted handling
135+
# TODO: upstream contrib!!
136+
@click.option('-l', '--list', is_flag=True,
137+
type=click.BOOL,
138+
help='Only output the list of oprhaned or deleted Resource IDs.')
139+
@click.option('-y', '--yes', is_flag=True,
140+
type=click.BOOL, help='Purge without asking for confirmation.')
141+
def purge(list: bool = False, yes: bool = False):
142+
'''Purge orphaned or deleted resources from the datastore using the datastore_delete
136143
action, which drops tables when called without filters.'''
137144

138145
site_user = logic.get_action('get_site_user')({'ignore_auth': True}, {})
@@ -150,24 +157,53 @@ def purge():
150157
if record['alias_of']:
151158
continue
152159

153-
logic.get_action('resource_show')(
160+
# (canada fork only): more options and state=deleted handling
161+
# TODO: upstream contrib!!
162+
res = logic.get_action('resource_show')(
154163
{'user': site_user['name']},
155164
{'id': record['name']}
156165
)
166+
pkg = logic.get_action('package_show')(
167+
{'user': site_user['name']},
168+
{'id': res['package_id']}
169+
)
157170
except logic.NotFound:
158171
resource_id_list.append(record['name'])
159-
click.echo("Resource '%s' orphaned - queued for drop" %
160-
record[u'name'])
172+
if not list:
173+
click.echo("Resource '%s' orphaned - queued for drop" %
174+
record['name'])
175+
continue
161176
except KeyError:
162177
continue
178+
# (canada fork only): more options and state=deleted handling
179+
# TODO: upstream contrib!!
180+
if res['state'] == 'deleted':
181+
resource_id_list.append(record['name'])
182+
if not list:
183+
click.echo("Resource '%s' deleted - queued for drop" %
184+
record['name'])
185+
if pkg['state'] == 'deleted':
186+
resource_id_list.append(record['name'])
187+
if not list:
188+
click.echo("Package '%s' deleted - queued for drop" %
189+
pkg['id'])
190+
191+
# (canada fork only): more options and state=deleted handling
192+
# TODO: upstream contrib!!
193+
if list:
194+
click.echo('\n'.join(resource_id_list))
195+
return
163196

164197
orphaned_table_count = len(resource_id_list)
165198
click.echo('%d orphaned tables found.' % orphaned_table_count)
166199

167200
if not orphaned_table_count:
168201
return
169202

170-
click.confirm('Proceed with purge?', abort=True)
203+
# (canada fork only): more options and state=deleted handling
204+
# TODO: upstream contrib!!
205+
if not yes:
206+
click.confirm('Proceed with purge?', abort=True)
171207

172208
# Drop the orphaned datastore tables. When datastore_delete is called
173209
# without filters, it does a drop table cascade

0 commit comments

Comments
 (0)