Database
The Database connector reads and writes tables in a SQL database. Mention it as @database. A workflow can look up records, insert and update rows, and work through a table one row per run as a pipeline.
Connect
The credential is a Connection String in SQLAlchemy form, such as postgresql://user:pass@host:5432/dbname. Flow supports these databases:
| Database | Connection string starts with |
|---|---|
| PostgreSQL | postgresql:// |
| MySQL | mysql+pymysql:// |
| Microsoft SQL Server | mssql+pymssql:// |
| Oracle | oracle+oracledb:// |
| Snowflake | snowflake:// |
| BigQuery | bigquery://<project>?credentials_base64=<key>, with your service account key base64-encoded |
Test Connection refuses a SQLite connection string, because a SQLite database is a file on Flow's own servers. It also refuses a BigQuery connection string with no credentials_base64, which would sign in as Flow's own servers, and one with credentials_path, which names a key file on them. A connection string that connects through a socket on Flow's own servers is refused too: a PostgreSQL host that starts with /, a PostgreSQL connection string with no host, or MySQL's unix_socket. So is a parameter that can read a file on Flow's own servers: Snowflake's private_key_file or any other parameter that ends in file or path, PostgreSQL's sslcert, sslkey, sslrootcert, sslcrl, sslcrldir and service, MySQL's ssl_ca, ssl_cert, ssl_key, ssl_capath, local_infile and client_flag, and Oracle's wallet_location, config_dir and tns_admin. PostgreSQL's sslrootcert=system, which uses Flow's standard list of trusted certificate authorities, still runs. Runs use every parameter.
In select, count, insert, update, delete and claim_next_row, steps name tables and columns as describe_table shows them. On Oracle and Snowflake it shows an ordinary upper-case name in lower case. Names with spaces and names that are SQL keywords, such as order, work too. On BigQuery a name can't contain a backtick.
Give the database user the rights the workflow needs and no more. The settings below guard what steps ask for; they don't make the connection read-only, and only the database user's rights do that. The database must be reachable from the internet, and Test Connection refuses a host on a private network. Connectors, instances and credentials covers the rest of the setup.
| Setting | Default | What it does |
|---|---|---|
| Allow Write Operations | Off | Lets steps insert, update and delete rows. |
| Allow DDL Operations | Off | Lets steps run any CREATE, ALTER, DROP or TRUNCATE statement, together with Allow Write Operations, including dropping a whole schema. Truncate empties a table. |
| Allow Raw SQL Queries | On | Lets steps run their own read-only SELECT queries, for joins and aggregations. |
| Max Rows Per Query | 1,000 | From 1 to 50,000. The most rows select and query return; rows past it are left out. On SQL Server and Oracle, and when a query has its own LIMIT, query isn't capped. |
| Default schema | Empty | The schema steps use when they don't name one, such as public. A step that names another schema, or a query step, can still reach it. |
| Query Timeout (seconds) | 30 | From 1 to 300. Keep it at 180 or less. A step gives up after three minutes, and a read is tried up to five times, but on PostgreSQL and MySQL each abandoned query keeps running on the database until this timeout. Applies to PostgreSQL, and to MySQL reads. |
What a step can do
| Action | What it does |
|---|---|
list_tables, describe_table | Reads the tables and their columns, keys and indexes. |
select, count | Reads rows that match conditions. |
query | Runs one read-only query that the step writes, for joins and aggregations. Needs Allow Raw SQL Queries. Nothing it does is committed, and on PostgreSQL, MySQL, MariaDB and Oracle it runs in a read-only transaction. Queries that query refuses lists what it turns away. |
insert, update, delete | Changes rows. Needs Allow Write Operations. update and delete always need a condition. An insert of several rows writes them one at a time; if one fails, the rows before it stay in the table. |
execute_ddl | Changes the schema. Needs Allow Write Operations and Allow DDL Operations. |
claim_next_row | Claims the next unprocessed row of a table for this run. |
mark_row_processed | Marks a claimed row Success or Failed. |
Take the next unprocessed row from the
payment_requeststable in @database, keyed onrequest_id. Check the vendor against thevendorstable. Mark the row processed when the payment has been posted.
A condition in select, count, update and delete names the column in the key and the value beside it: {"status": "new"} matches rows where status is new. For any other comparison, put it in the key after the column name, such as {"age >": 25} or {"status IN": ["new", "open"]}. The comparisons are =, !=, <>, <, <=, >, >=, LIKE, ILIKE, IN, NOT IN, IS and IS NOT. A step that writes the comparison as an object, such as {"age": {"$gt": 25}}, fails before it reaches the database. The error shows the key form and lists the comparisons. In update, a value for a JSON column is sent to the database as JSON, so {"meta": {"$schema": "v1"}} is a value to compare, not a comparison.
Queries that query refuses
Before a query step runs, Flow checks the text of its query. It reads the text the way your kind of database writes SQL, using its own model of that SQL, not your database server's parser. For any database outside the table under Connect, it reads the query as several kinds of SQL, and one of them must read it. Flow refuses:
- A query the parser can't read, even when the database would run it.
- More than one statement, or a statement that isn't a query. A query is a
SELECT, aUNIONof them, or aWITHclause ending in one. - A statement that writes, such as
SELECT ... INTOor aWITHclause holding aDELETE. - A locking clause, such as
FOR UPDATE. - A semicolon before the end of the query, even inside a value.
- On SQL Server, a word that starts another statement, such as
EXEC,DECLARE,SET,WAITFORorCOMMIT, even at the end of the query, where it would otherwise read as a column name. - On SQL Server, a table hint that takes a lock, such as
TABLOCKX,UPDLOCKorHOLDLOCK.NOLOCKandREADPASTare accepted. - A
:nameparameter inside quotes or a comment. Write\:for a colon that isn't a parameter. - A quote or comment that isn't closed.
- A control character other than a tab or a line break.
- A MySQL or MariaDB executable comment (
/*! ... */). - A Unicode-escaped name or string (
U&"..."). - A function that acts outside the query, such as PostgreSQL's
dblink_execorpg_terminate_backend, MySQL'sLOAD_FILE, SQL Server'sOPENROWSET, or a call to one of Oracle's network or job packages, such asUTL_HTTP.REQUESTorDBMS_SCHEDULER.
These checks read the text of the query, and they can miss a statement that your server reads differently. The database user's rights are what stop a query that gets past them, on every database and on SQL Server in particular, where a query has no read-only transaction around it. For query steps, use an instance whose database user can only read. On MySQL, a read-only transaction doesn't stop SELECT ... INTO OUTFILE from writing a file on the database server, so leave the FILE privilege off that user. On Oracle, a read-only transaction doesn't stop UTL_HTTP from reaching the network: revoke EXECUTE on UTL_HTTP, UTL_TCP, UTL_SMTP, UTL_INADDR and HTTPURITYPE from PUBLIC, and grant the user no network access control list.
Work through a table
To process a table row by row, the table needs a primary key, and the spec names it. Unstract Flow keeps track of which rows each run has claimed and finished on its side, so the table itself needs no status column and can stay read-only. The connector's pipeline tab in the editor, under Database, shows each row a run has reached, as Available, In Progress, Success or Failed. A Failed row stays failed; to run it again, select it and Reset Status. A row a run is still working on shows In use, and Reset Status and Remove are off for it until that run ends. If a run ends with a row still in progress, the row is marked Failed once eight hours have passed since the claim. Flow checks for such rows every five minutes. To free it sooner, select Release claims in the details of a run that ended Partial, Failed or Cancelled.
Each claim reads the rows that match the spec's condition in key order, 100 at a time unless the step asks for another number up to Max Rows Per Query, until it finds a row that is still available. It stops once it has read about 10,000 rows. On a larger table, write a condition that leaves out finished rows, such as "requests created in the last day": the claim is quicker, and rows past the first 10,000 are reached.