Skip to content

Commit 9d33828

Browse files
authored
Merge pull request #104 from supabase/extension_structure_docs
Extension structure docs
2 parents 7f8dc66 + e5e5229 commit 9d33828

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

docs/extension_structure.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
A Postgres [trusted lanuguage extension](https://github.com/aws/pg_tle) (TLE) consists of the following files:
2+
3+
1. Script files. These files contain the SQL commands to create the extension's objects.
4+
2. Control files. These files contain basic properties of the extension itself.
5+
6+
For an extension to be valid, one file of each type must be present in an extension. For example, if you want to create an extension named `my-extension`, create the following folder structure:
7+
8+
- my-extension
9+
- my-extension.control
10+
- my-extension--0.0.1.sql
11+
- README.md
12+
13+
In the above example, the `my-extension` folder contains the extension files. Names of the files are important. The control file should be named `<extension_name>.control` and the script file should be named `<extension_name>--<extension_version>.sql`. The readme file should be named `README.md` (case sensitive).
14+
15+
## Control Files
16+
17+
A control file contains metadata about the extension in key-value pairs. The most common keys that you should consider setting are the following:
18+
19+
- default_version (string). The version to use if the user doesn't provide one in the `create extension` command.
20+
- comment (string). A comment added to the extension object created in the database.
21+
- requires (string). A comma separated list of extensions that this extension depends on.
22+
- relocatable (boolean). Set to true if the extension's objects can be moved to a different schema after they are created.
23+
- superuser (boolean). Set to true if only superusers should be able to create this extension.
24+
25+
For example, the [pgjwt extension's control file](https://github.com/michelp/pgjwt/blob/master/pgjwt.control) looks like this:
26+
27+
```control
28+
# pgjwt extension
29+
comment = 'JSON Web Token API for Postgresql'
30+
default_version = '0.2.0'
31+
relocatable = false
32+
requires = pgcrypto
33+
superuser = false
34+
```
35+
36+
For a complete list of keys available in a control file, refer to [Postgres documentation](https://www.postgresql.org/docs/current/extend-extensions.html#EXTEND-EXTENSIONS-FILES).
37+
38+
### Syntax
39+
40+
A control file contains key-value pairs. Each pair should be on a separate line. Empty lines are ignored. Text after a `#` is a comment and is also ignored. Keys should start with a letter and contain only letters or digits. The `=` sign following a key is optional, but there must be at least one whitespace after a key if `=` is omitted. Values can be either a boolean or a string. Valid values for a boolean are `true` or `false`. Strings are anything between a pair of single quotes. If you want to include a single quote in the middle of a string, use `''` to escape it. A complete list of escape sequences is:
41+
42+
1. `\b` - backspace
43+
2. `f` - formfeed
44+
3. `\n` - newline
45+
4. `\r` - carriage return
46+
5. `\t` - tab
47+
6. `''` - single quote
48+
49+
Strings which do not contain whitespace or escape sequences can be written without the surrounding single quotes.
50+
51+
Some examples of valid control file syntax follow:
52+
53+
```
54+
# A boolean value
55+
relocatable = true
56+
57+
# `=` is optional
58+
superuser false
59+
60+
# A string
61+
comment = 'a comment for the extension'
62+
63+
# A string without quotes should not have whitespace or escape sequences
64+
comment = ACommentForTheExtension
65+
66+
# A string with escapse sequences
67+
comment = 'A double quote '' and a newline \n'
68+
```
69+
70+
71+
## Script Files
72+
73+
Script files contain the SQL commands to create or modify database objects. These database objects can be, but are not limited to, tables, views, functions, types, operators etc. For example, the [pgjwt's `pgjwt--0.1.1.sql` file](https://github.com/michelp/pgjwt/blob/master/pgjwt--0.1.1.sql) contains definitions for functions which the extension adds to the database. One exception to the SQL commands which can exist in a script file are transaction control commands like `BEGIN`, `COMMIT`, `ROLLBACK` etc.
74+
75+
You might have noticed a strange line at the beginning of the [`pgjwt-0.1.1.sql` file](https://github.com/michelp/pgjwt/blob/master/pgjwt--0.1.1.sql) starting with `\echo`. This line prevents the script file from being run accidentally in `psql`. Lines starting with `\echo` are run only in `psql` but are ignored when the script file is executed by the [`CREATE EXTENSION` command](https://www.postgresql.org/docs/current/sql-createextension.html). It is recommended that you include such a line at the beginning of your script file.
76+
77+
### Update Scripts
78+
79+
Update scripts are used to update an installed extension. An update script should be named `<extension_name>--<old_version>--<new_version>.sql`. For example if version `1.0` of `my-extension` was published already and you want to publish a new version `1.1` you need to create `my-extension--1.0--1.1.sql`. Update scripts can create new database object or modify/delete existing objects created by the previous version of the extension.
80+
81+
## README.md File
82+
83+
README.md file contains documentation for the extension in the [markdown](https://en.wikipedia.org/wiki/Markdown) format. The documentation typically contains installation and usage instructions about the extension.

docs/install.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ The dbdev CLI is required to publish your TLE to [database.dev](https://database
22

33
## Installation
44

5-
Binaries for dbdev CLI are available for Linux, Windows and macOS platforms. Visit the [dbdev releases page](https://github.com/supabase/dbdev/releases) to download a binary for your OS. The downloaded binary should be placed in a folder which is in your [PATH](https://en.wikipedia.org/wiki/PATH_(variable))
5+
Binaries for dbdev CLI are available for Linux, Windows and macOS platforms. Visit the [dbdev releases page](https://github.com/supabase/dbdev/releases) to download a binary for your OS. The downloaded binary should be placed in a folder which is in your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)).
66

77
Alternatively, you can build the binary from source. You will need to have [Rust installed](https://www.rust-lang.org/tools/install) on your system. To build from source:
88

99
1. Clone the repo: ```git clone https://github.com/supabase/dbdev.git```.
1010
2. Change directory to `dbdev`: ```cd dbdev```.
1111
3. Build: ```cargo install --release```.
12-
4. Copy the `dbdev` binary in `target/release` to a folder in you PATH.
12+
4. Copy the `dbdev` binary in `target/release` to a folder in your PATH.
1313

1414
If you have [cargo-install](https://doc.rust-lang.org/cargo/commands/cargo-install.html), you can perform all the above steps with a single command: ```cargo install --git https://github.com/supabase/dbdev.git dbdev```.
1515

docs/publish.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Before you can publish your TLE, you need to authenticate with [database.dev](https://database.dev/).
1+
Before you can publish your extension, you need to authenticate with [database.dev](https://database.dev/).
22

33
### Login to database.dev
44

@@ -14,12 +14,42 @@ If you don't have an account, [sign-up for one](https://database.dev/sign-up) on
1414

1515
You are now logged into `database.dev`.
1616

17-
### Publish TLE
17+
### Publish Your First Extension
1818

19-
To publish a TLE, run the `dbdev publish` command. For example, to publish a TLE in the `/all_tles/my_tle` folder run the following:
19+
Let's create your first extension. Create a folder which will contain the extension:
2020

2121
```
22-
dbdev publish --path /all_tles/my_tle
22+
mkdir my_first_tle
23+
cd my_first_tle
2324
```
2425

25-
Your TLE is now published to `database.dev` and visible at `https://database.dev/<handle>/<package_name>`. Users can install it using the [dbdev in-database client](https://database.dev/installer).
26+
Next create a `hello_world--0.0.1.sql` file, which will contain your extension's SQL objects. Add the following function definition to this file:
27+
28+
```sql
29+
create function greet(name text default 'world')
30+
returns text language sql
31+
as $$ select 'hello, ' || name; $$;
32+
```
33+
34+
Let's also add some docs about this extension. Create a `README.md` file and add the following content to it:
35+
36+
```
37+
The `hello_world` extension provides a `greet` function, which returns a greeting.
38+
```
39+
40+
Lastly, add a `hello_world.control` file with the following key-value pairs:
41+
42+
```
43+
default_version = 0.0.1
44+
comment = 'An extension to generate greetings'
45+
```
46+
47+
Your extension is ready to publish. Its name is `hello_world` and version is `0.0.1`. For details about what constitutes a valid extension, read about the [Structure of an Extension](extension_structure.md).
48+
49+
Now run the `dbdev publish` command to publish it.
50+
51+
```
52+
dbdev publish --path /path/to/my_first_tle
53+
```
54+
55+
Your extension is now published to `database.dev` and visible under your account profile. You can visit your account profile from the account drop-down at the top right. Users can now install your extension using the [dbdev in-database client](https://database.dev/installer).

mkdocs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ nav:
99
- Welcome: 'index.md'
1010
- Install CLI: 'install.md'
1111
- Publish a Package: 'publish.md'
12+
- Structure of a Postgres Extension: 'extension_structure.md'
1213

1314
theme:
1415
name: 'material'

0 commit comments

Comments
 (0)