Database migration scripts¶
A migration script changes the structure or the data of your databases : add a column, fill a value, create an index. It is code of the account, kept in Git. API Maker runs each script once per environment, in order.
In short
- After a Git pull or a deployment hook, the active scripts not yet run on this environment run, by
order. - Write scripts that can run twice without harm.
- The REC pill of the panel can write the script for you, as you change tables.
- A name.
- What it does, in words.
- Scripts run in this order.
- How long it may run.
| Page | Database → Database Migration : name, label, order, timeout in minutes, active, and the code. |
| Runs | After a Git pull and after a deployment hook : the active scripts not yet executed on this environment, by order. Or by hand : Execute script, Execute Selected Migration. |
| Remembers | The names of the scripts executed, per environment. Mark as executed skips one without running it ; a script already run shows Script executed. |
| Code | The function main of any language, with the same g as a custom API. Test it on the API testing page. |
A script¶
- Get table meta tells what is there, execute plain query runs any SQL or MongoDB command, create indexes adds indexes the same way on every database. Data moves with
g.sys.db. - Write scripts which can run twice without harm, like the one above : a script which fails half way is run again after the fix.
Order and timeout¶
ordersorts the scripts ; the pull runs them from the lowest. Use it for a script which needs the column another one adds.Timeout in minutesstops a script which runs too long (10when empty) : after a pull, after a deployment hook, with Execute script and on the API testing page alike. A script in another language than TypeScript runs in the sandbox of its language.- A script which throws stops with its error, shown by View error on its row ; the next scripts still run.
Recorded from the panel¶
What you do to tables, collections, indexes and databases in the panel can go into a migration script as you do it : every other environment then makes the same change when it deploys. No SQL to copy, nothing forgotten on staging.
| Where | The pill REC of the table designer (Create, Apply changes, Run), of Delete of a table or a database, of the form of a database, of the index forms. It says where the change goes : a migration script of the account, a New script you name (or the name it suggests, create_orders), or Don't record. The choice stays for the next changes, and a change recorded into a new script makes it the script of the next ones. |
| What | The statements which ran and changed something, as they ran : a SELECT or a find of the editor of scripts is not recorded. A database made, changed or removed is kept as its design and written again for each environment. An index is kept in the words of create indexes. |
| Runs | The script runs like any other : after a pull, after a deployment hook, or by hand. The changes run in their order, and the script is safe to run again and again : what a change makes and is there already is left as it is, what it made before and is missing (a table removed by hand) is made again, and the rest runs once per environment. See Safe to run again. A pull which changes the list marks the script as not executed, so the new changes run on the next deploy. |
| Names | Databases are named as your account names them : the database name masking of each environment finds its own database. |
The script¶
A script the panel makes is TypeScript : the list of the changes between two markers, and one call which makes them. Each statement is written between backticks, as it ran : it reads as SQL, and the editor colours it as the SQL of its database.
| create_orders : recorded in the panel | |
|---|---|
| |
- A change :
id(the same in every environment),operation(CREATE_TABLE,ALTER_TABLE,DROP_TABLE,RUN_SCRIPT,CREATE_DATABASE,ALTER_DATABASE,DROP_DATABASE,CREATE_INDEX,DROP_INDEX),summary,instance(andtenant),database,table,statements,atomic(one transaction, on PostgreSQL and SQL Server),designandoriginalfor a database,indexfor an index. - Edit the list if you need to : it is JSON, read with comments and trailing commas, with each statement between backticks. Remove a change, add one of your own, or write your own code around the call, a backfill of the new column after it for example. The panel only rewrites what is between the markers.
- A change recorded into a TypeScript script you wrote adds the list above its
mainand the call as the first line ofmain. A script in another language takes no recorded change : the pill says so. - Any code can call
applyRecordedChanges, as it calls the other system APIs : the script itself, sent with SEND on the API Testing page, a custom API or a scheduler. Every run is as safe as the first : what is there is left as it is.
A statement between backticks¶
| Escapes | A statement is a text of JavaScript between backticks : write \` for a backtick of the statement (the names of MySQL and MariaDB, \`orders\`), \\ for a backslash and \${ for ${. The panel writes them so ; the editor shows their backslash faint. ${…} with code in it is refused : a statement is text. |
| Indentation | The lines after the first are indented with the code around them. Before a statement runs, API Maker takes away the indentation those lines share, so what runs is what was written in the panel. A statement can also start on the line after its backtick, and end with its backtick on a line of its own. |
| Before | Scripts recorded before the backticks hold a statement over several lines as the list of its lines (["CREATE TABLE a (", " id INT", ")"]) : they read and run as they are, and the panel writes their list between backticks the next time it records into them. |
In the editor¶
The Code tab of a script which holds recorded changes knows the list :
- Each statement is coloured as the SQL of its database (MySQL and MariaDB, PostgreSQL and Oracle, SQL Server), told by how it quotes names (
\`orders\`,"orders",[orders]), and the commands of MongoDB as JavaScript. - In a statement, the tables of the database of its change and the columns of the tables it names are offered as you type, with the words of SQL.
- In the list, type
change: New recorded change writes a change of your own statements with a new id (the instance and the database to choose). In thestatementsof a change, New statement adds one. A change written by hand runs like a recorded one, in its order, and is just as safe to run again. Keep its id as it is once another environment has it. - What became of each change on this server : a bar beside the change, a note after its id (
✓ Made here,◷ Waits to be made here,✗ Failed here), the whole of it (the error of the database) on hover, and the count on the line of// <am-recorded-changes>.
What each environment did¶
applyRecordedChanges answers one result per change, and keeps it in the environment, never in Git :
| Status | |
|---|---|
APPLIED |
Made by this run : new on this environment, or made again because what it made is not there any more (message says what, Made again : the table public.orders was not there.). |
ALREADY_APPLIED |
Nothing to do : made before (recorded here, run before, taken as made) and still there, or found there already on an environment which never ran it (message : Already there on this server : the table public.orders.). |
FAILED |
The database refused it : message says why. The call throws, so the script is not marked as executed and its next run starts again from that change. On MySQL, MariaDB and Oracle, which commit each statement by itself, the statements of the change which ran before the failure are not run again. |
NOT_RUN |
A change before it failed : it runs next time. |
PENDING |
dryRun: true : nothing runs, the change would. |
On the Database Migration page, the red count beside a script opens its Recorded changes : each change with what became of it on this server (made here, waiting, failed with the error of the database, taken as made), its statements coloured as their SQL with a button to copy each one (to run it by hand on a server, then take the change as made there), and :
- Take as made here : this server has it already (made by hand) : a run here does not run its statements again, but still makes what it makes when that is not there. Mark as executed of a script does the same for all of its changes.
- Make again here : the next run makes it on this server again.
- Take out : the servers which did not make it yet never will. What it made here stays.
- Make N here now : runs the script on this server.
Safe to run again¶
Before a change runs, API Maker looks on the server for what it makes, renames or removes, in the catalog of the database (the one get table meta reads) : tables and collections, their columns, their indexes, the schemas and the databases. Then, statement by statement :
| What the statement does | When it runs |
|---|---|
| Makes a table, a collection, a column, an index, a schema, a database | When it is not there, whether the environment made the change before or not. A table removed by hand is made again by the next run, and the changes after it which changed that table run again too, so it comes back as the list made it. What is there already is left as it is. |
| Removes or renames one | Once. A table a change removed here and somebody made again since stays as it is. |
| Anything else : writes rows, changes the type of a column, adds a key | Once per environment (again only for a table the run made again). |
- The list says what the environment ends with : a table which a later change removes, or a column a later change drops, is not made again ; a table a later change renames is looked for under its new name.
- An
ALTER TABLEof several parts (MySQL, MariaDB, PostgreSQL) runs with the parts which are needed only : a column removed by hand comes back without the rest of the statement running again. dryRun: truelooks the same way and says what would run, with why (PENDING,Made again : the column note of orders was not there.).
Good to know
- An Oracle user is made with a password, which would go to Git with the script : it is not recorded, the pill says so.
- A statement of the editor of scripts which names a database itself (
USE,`shop`.`orders`) runs in that database everywhere : name the tables alone, each environment runs them in its own database. - The schema of a table follows it through Git, as every schema does : the script holds the changes of the database.
The Index Maker script¶
am_index_makeris managed by Index Maker : it holds the indexes decided by the automatic mode, the suggestions and the index form as a JSON list, runs on every deploy, and can not be deleted while Index Maker uses it. Edit the code outside the marked region as you like.