Skip to content

Commit

Permalink
feat: add database (postgres, mysql, mariadb) action
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgrittner committed Oct 11, 2024
1 parent 8a365cb commit 90a0e8a
Show file tree
Hide file tree
Showing 12 changed files with 896 additions and 581 deletions.
2 changes: 2 additions & 0 deletions admyral/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
aws_s3_restrict_public_read_access,
aws_s3_default_encryption_enabled,
)
from admyral.actions.integrations.database import run_sql_query


__all__ = [
Expand Down Expand Up @@ -139,4 +140,5 @@
"get_time_interval_of_last_n_days",
"get_time_interval_of_last_n_hours",
"format_json_to_list_view_string",
"run_sql_query",
]
2 changes: 1 addition & 1 deletion admyral/actions/integrations/cloud/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@action(
display_name="AWS Steampipe Query",
display_name="Steampipe Query",
display_namespace="AWS",
description="Query AWS using Steampipe",
secrets_placeholders=["AWS_SECRET"],
Expand Down
3 changes: 3 additions & 0 deletions admyral/actions/integrations/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from admyral.actions.integrations.database.db import run_sql_query

__all__ = ["run_sql_query"]
34 changes: 34 additions & 0 deletions admyral/actions/integrations/database/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Annotated
from sqlalchemy import create_engine, text

from admyral.action import action, ArgumentMetadata
from admyral.typings import JsonValue
from admyral.context import ctx


@action(
display_name="Run SQL Query",
display_namespace="Database",
description="Run a SQL query on a database. Supported databases: PostgreSQL, MySQL, and MariaDB.",
secrets_placeholders=["DB_URI"],
)
def run_sql_query(
sql_query: Annotated[
str,
ArgumentMetadata(
display_name="SQL Query", description="The query to run on the database"
),
],
) -> JsonValue:
secret = ctx.get().secrets.get("DB_URI")
db_uri = secret["uri"]

if db_uri.startswith("mysql://"):
db_uri = db_uri.replace("mysql://", "mysql+pymysql://")

with create_engine(db_uri).connect() as connection:
result = connection.execute(text(sql_query))
if not result.returns_rows:
return
columns = result.keys()
return [dict(zip(columns, row)) for row in result.fetchall()]
Empty file.
10 changes: 0 additions & 10 deletions admyral/actions/integrations/destinations/db.py

This file was deleted.

1 change: 1 addition & 0 deletions docs/pages/integrations/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"anthropic": "Anthropic",
"aws": "AWS",
"azure_openai": "Azure OpenAI",
"database": "Database",
"github": "GitHub",
"greynoise": "GreyNoise",
"jira": "Jira",
Expand Down
66 changes: 66 additions & 0 deletions docs/pages/integrations/database/database.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Database

Connect to an external database and run SQL queries.

The following databases are supported:

- PostgreSQL
- MySQL
- MariaDB

**SDK Import:**

```python
from admyral.actions import run_sql_query
```

## Connect your Database

In order to connect your database, you need to store the database URI as a secret.

Go to your **Settings** page in **Admyral** and click on **Add New Secret** in the **Secrets** section. Give your credential a name. The following secret structure is expected:

| Key | Value |
| ----- | ------------------------------------------ |
| `uri` | Paste your database connection string here |

Then, click on **Save**.

Alternatively, you can use the following CLI command structure:

```bash
admyral secret set database_connection --value uri=postgresql://postgres:your-super-secret-and-long-postgres-password@localhost:5432/admyral
```

Example connection strings:

- PostgreSQL: `postgresql://postgres:your-super-secret-and-long-postgres-password@localhost:5432/admyral`
- MySQL: `mysql://testuser:testpassword@localhost:3306/testdb`
- MariaDB: `mysql://testuser:testpassword@localhost:3307/testdb`

## Arguments:

| Argument Name | Description | Required |
| ------------------------- | ------------------------------------- | :------: |
| **SQL Query** `sql_query` | The SQL query to run on the database. | Yes |

## Returns

A list of rows.

## Required Secrets

| Secret Placeholder | Description |
| ------------------ | -------------------------------- |
| `DB_URI` | Database URI stored as a secret. |

## SDK Example

```python
response = run_sql_query(
sql_query="select count(*) from students;",
secrets={
"DB_URI": "my_database_connection"
}
)
```
1,295 changes: 725 additions & 570 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ psycopg2-binary = "^2.9.9"
setuptools-scm = "^8.1.0"
docker = "^7.1.0"
fastapi-nextauth-jwt = "^2.0.0"
pymysql = "^1.1.1"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"
Expand Down
62 changes: 62 additions & 0 deletions web/public/database_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions web/src/components/workflow-editor/namespace-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const NAMESPACE_ICON_MAPPING: Record<string, string> = {
GitHub: "/github_logo.svg",
AbuseIPDB: "/abuseipdb-logo.svg",
AWS: "/aws_logo.svg",
Database: "/database_icon.svg",
};

export default function NamespaceIcon({ namespace }: { namespace: string }) {
Expand Down

0 comments on commit 90a0e8a

Please sign in to comment.