-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add database (postgres, mysql, mariadb) action
- Loading branch information
1 parent
8a365cb
commit 90a0e8a
Showing
12 changed files
with
896 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
) | ||
``` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters