Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default enrichment idea: search and replace #47

Open
simonw opened this issue Apr 27, 2024 · 1 comment
Open

Default enrichment idea: search and replace #47

simonw opened this issue Apr 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Collaborator

simonw commented Apr 27, 2024

The simplest form of exact match search and replace, to work exclusively against text columns.

@simonw simonw added the enhancement New feature or request label Apr 27, 2024
@simonw
Copy link
Collaborator Author

simonw commented Apr 27, 2024

I built a weird half-baked version of this in a unit test for secrets just now:

class SecretReplacePlugin(Enrichment):
name = "Replace string with a secret"
slug = "secretreplace"
description = "Replace a string with a secret"
secret = Secret(
name="STRING_SECRET",
description="The secret to use in the replacement",
)
async def get_config_form(self, db, table):
choices = [(col, col) for col in await db.table_columns(table)]
class ConfigForm(Form):
column = SelectField("Column", choices=choices)
string = StringField("String to be replaced")
return ConfigForm
async def enrich_batch(
self,
datasette,
db: Database,
table: str,
rows: List[dict],
pks: List[str],
config: dict,
job_id: int,
):
secret = await self.get_secret(datasette, config)
for row in rows:
await db.execute_write(
"update [{}] set [{}] = ? where {}".format(
table,
config["column"],
" and ".join('"{}" = ?'.format(pk) for pk in pks),
),
[row[config["column"]].replace(config["string"], secret)]
+ [row[pk] for pk in pks],
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant