Skip to content

Commit

Permalink
Prepare simplified PostgreSQL deployment (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
swaldmann authored Oct 6, 2023
1 parent bc23131 commit 9011671
Showing 1 changed file with 23 additions and 112 deletions.
135 changes: 23 additions & 112 deletions guides/databases-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,141 +279,52 @@ cds deploy --profile pg

### With a Deployer App

When deploying to Cloud Foundry, this can be accomplished by providing a simple deployer app, which you can construct as follows:
When deploying to Cloud Foundry, this can be accomplished by providing a simple deployer app. Similar to SAP HANA deployer apps, it is auto-generated for PostgreSQL-enabled projects by running

1. Create a new folder named `gen/pg/db`:
```sh
mkdir -p gen/pg/db
```

2. Generate a precompiled cds model:
```sh
cds compile '*' > gen/pg/db/csn.json
```

3. Add required `.csv` files, for example:
```sh
cp -r db/data gen/pg/db/data
```
```sh
cds build
```

4. Add a *package.json* to `gen/pg` with this content:
::: code-group
```json [gen/pg/package.json]
::: details What `cds build` does…
1. Compiles the model into _gen/pg/db/csn.json_.
2. Copies required `.csv` files into _gen/pg/db/data_.
3. Adds a _gen/pg/package.json_ with this content:
```json
{
"dependencies": {
"@sap/cds": "*",
"@cap-js/postgres": "*"
"@sap/cds": "^7",
"@cap-js/postgres": "^1"
},
"scripts": {
"start": "cds-deploy"
}
}
```
:::
> **Note the dash in `cds-deploy`**, which is required as we don't use `@cds-dk` for deployment and runtime, so the `cds` CLI executable isn't available.
:::

5. Finally, package and deploy that, for example using [MTA-based deployment](deployment/to-cf#build-mta).
> **Note the dash in `cds-deploy`**, which is required as we don't use `@cds-dk` for deployment and runtime, so the `cds` CLI executable isn't available.

Finally, package and deploy the application, for example using [MTA-based deployment](deployment/to-cf#build-mta).

## Step-by-Step Instructions

Here's a step by step guide to add PostgreSQL to an existing project and deploy to SAP BTP. We assume that the following prerequisites are fulfilled:
Here's a step-by-step guide to add PostgreSQL to an existing project and deploy it to SAP BTP. We assume there are service definitions and data models in _/srv_ and _/db_.

1. An existing instance of PostgreSQL running. For this example the instance name `my-postgres-db` is used.
2. Service definition(s) and data model are in place (content in _/srv_ and _/db_ folder)
### Add Postgres configuration

### Add Postgres dependencies
```sh
npm install @cap-js/postgres
cds add postgres
```
This automatically hooks itself into the production profile of CAP. Once the CAP service is deployed in the BTP and the production profile is active, the Postgres adapter is used.

### Add Standard CAP Dependencies
```sh
cds add xsuaa,mta --for production
```

### Modify the mta.yaml

1. Add the Postgres instance as existing service to the `resource` section:
::: code-group
```yaml [mta.yaml]
- name: my-postgres-db
type: org.cloudfoundry.existing-service
```
:::

2. Add a deployer task/module, to deploy the data model to the Postgres instance as part of the standard deployment.
::: code-group
```yaml [mta.yaml]
- name: pg-db-deployer
type: hdb
path: gen/pg
parameters:
buildpack: nodejs_buildpack
requires:
- name: my-postgres-db
```
:::

- Make sure to use the type `hdb` and NOT `nodejs` as the nodejs type will try to restart the service over and over again.
- The deployer path points to a _gen/pg_ directory we need to create as part of the deployment process. See next step.
- The deployer also defines the dependency/binding to the postgres instance to have the credentials available at deploy time.

3. Add dependencies to your CAP service module
::: code-group
```yaml [mta.yaml]
requires:
- name: my-postgres-db
- name: pg-db-deployer
```
:::

This configuration creates a binding to the Postgres instance and waits for the deployer to finish before deploying the service.

4. To generate the content into the `gen/pg` folder, we reference a shell script in the `custom` builder section. The complete section should look like this:
::: code-group
```yaml [mta.yaml]
build-parameters:
before-all:
- builder: custom
commands:
- npx cds build --production
- ./scripts/pgbuild.sh
```
:::

### Create the Shell Script
The shell script specified in the previous step is a simple combination of all the commands outlined in the CAP documentation. It creates the necessary artifacts in the _gen/pg_ directory. Here are the simple steps:

1. Create a directory _/scripts_ in the root of the project
2. Create a file _pgbuild.sh_ in the _/scripts_ directory and change the permissions to make it executable:
```sh
chmod +x pgbuild.sh
```
3. Add the following content to the _pgbuild.sh_ file:
```bash
#!/usr/bin/env bash
echo ** Starting Postgres build **
echo - creating dir gen/pg/db -
mkdir -p gen/pg/db
echo - compiling model -
cds compile '*' > gen/pg/db/csn.json
echo - copy .csv files -
cp -r db/data gen/pg/db/data
echo '{"dependencies": { "@sap/cds": "*", "@cap-js/postgres": "*"}, "scripts": { "start": "cds-deploy"}}' > gen/pg/package.json
```
::: details See what this does…
1. Adds `@cap-js/postgres` dependency to your _package.json_ `dependencies`.
2. Sets up deployment descriptors such as _mta.yaml_ to use a PostgreSQL instance deployer application.
3. Wires up the PostgreSQL service to your deployer app and CAP backend.
:::

### Deploy

Package and deploy your project, for example using [MTA-based deployment](deployment/to-cf#build-mta).

You can package and deploy that application, for example using [MTA-based deployment](deployment/to-cf#build-mta).

## Automatic Schema Evolution { #schema-evolution }

Expand Down

0 comments on commit 9011671

Please sign in to comment.