- Create a file named "Main.sql" containing something like this:
BEGIN TRANSACTION;
--@inline {filepath0}
--@inline {filepath1}
--[...]
COMMIT;- In any of the imported files, use
--@inline {path}to include more files. This fits a lot of workflows, but my personal folder structure looks like this:
Schemas/
- AppName/
- - Tables
- - Views
- REFS/
- - Tables
- - Data
Main.sql
- imports Permissions
- imports Schemas
Permissions.sql
Schemas.sql
- imports Schemas/REFS/Tables
- imports Schemas/Refs/Data
- imports Schemas/AppName/Tables
- imports Schemas/AppName/Views
Since the import statement is prefaced by --, it will register as a comment to sql linters and won't cause any trouble.
Conditionals can be specified in the CLI and supports alphanumeric defines.
You can use unary conditions, i.e.
--@if abcdefg
-- something here
--@else
-- something else
--@endby specifying abcdefg=true, abcdefg=false, or even just abcdefg to check if it's defined.
Currently there is no else-if block, but nested conditions function:
--@if something
-- dead block
--@else
--@if something2
-- something 2
--@else
-- something 3
--@end
--@endNumerics, text, booleans, and plain defines are all supported:
./build.py letters=abc numbers=123 bool=false define--@if letters < def
-- this would go to the output
--@end
--@if bool == false
-- this would go to the output
--@end
--@if numbers < 200
-- this would go to the output
--@end
--@if define
-- this would go to the output
--@endThese will function recursively, so
--@if someflag
--@inline file1
--@else
--@inline file2
--@endis fully functional and intended.
At any point in a file, use @$NAME_HERE to insert a value defined in CLI args (see Conditionals for how to do that) Both keys and values can be any string of capital and/or lower case letters and underscores. Technically you can use bools/ints/anything python can cast to a string in this too, but I wouldn't exactly call that "intended behavior."
Example:
CREATE TABLE @$Schema_Name.@$TaBlE___NAME (
value text not null
);Called with
./sqlbuild.py Schema_Name=SchemaNameHere TaBlE___NAME=tableName__Herewill bundle to
CREATE TABLE SchemaNameHere.tableName__Here (
value text not null
);In any included sql file, insert the line --@csv_insert TABLENAME CSVPATH to insert values from a specified csv file inline.
Example:
Row_A,Row_B,Row_C,WhateverRowTitle
val1,NULL,15,22.96
NULL,15,22.96,val1
--@csv_insert TABLENAME_HERE example.csvThis will generate the following sql:
--example.csv
INSERT INTO TABLENAME_HERE (Row_A,Row_B,Row_C,WhateverRowTitle) VALUES
('val1',NULL,15,22.96),
(NULL,15,22.96,'val1');
--end example.csvNote that filenames are expressly and exclusively compatible with dynamic values! You can use --@csv_insert TABLENAME @$csv_argument and run with sqlbuild.py csv_argument=path/to/csv/file.csv to dynamically insert a csv table.
IF comparisons:
- These will not automatically converge types, and come with the jank of python's
<>=string compares. - In conditional blocks, comparisons are NOT REVERSIBLE!
true == boolwill attempt to compare a string variable named true to the stringbool.
Added support for conditional blocks using the cli. I didn't test it particularly well. This also required me to import some modules, but it's still fully python core.
This script is hosted on my quick links. You can access it from anywhere with wget/curl/your favorite REST client at https://sh.crinfarr.zip/sqlbuild.py