Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
out/
liquibase/liquibase.local.properties
liquibase/lib/
devel/pg/src
devel/pg/1*
devel/pg/data*
31 changes: 31 additions & 0 deletions cli/scripts/sql/get_party_description.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Read-only PL/pgSQL helper for party.description.
--
-- Expected table (adjust names/types to match your schema):
-- CREATE TABLE party (
-- party_id bigint PRIMARY KEY,
-- description text
-- );

CREATE OR REPLACE FUNCTION get_party_description(p_party_id bigint)
RETURNS text
LANGUAGE plpgsql
STABLE
STRICT
PARALLEL SAFE
SECURITY INVOKER
SET search_path = ''
AS $function$
DECLARE
v_description text;
BEGIN
SELECT p.description
INTO v_description
FROM public.party AS p
WHERE p.party_id = p_party_id;

RETURN v_description;
END;
$function$;

COMMENT ON FUNCTION get_party_description(bigint) IS
'Returns description for the given party_id; NULL if the row does not exist.';
59 changes: 59 additions & 0 deletions liquibase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Liquibase

Database migrations for PostgreSQL. **SQL files are plain PostgreSQL** — no Liquibase directives inside them. Liquibase metadata lives only in the small XML changelogs that reference those files via `sqlFile`.

## Layout

```
liquibase/
liquibase.properties
changelog/
db.changelog-master.xml
changes/
001-get-party-description.xml # changeset (sqlFile refs only)
001-get-party-description.sql # native SQL
001-get-party-description-rollback.sql # native rollback SQL
```

## Prerequisites

- [Liquibase CLI](https://docs.liquibase.com/start/install/home.html)
- PostgreSQL JDBC driver on the classpath (or Liquibase 4.29+ with bundled drivers)

## Configure

```bash
cp liquibase.properties.example liquibase.local.properties
# edit liquibase.local.properties with your JDBC URL and credentials
```

`liquibase.local.properties` is gitignored.

## Run

```bash
cd liquibase
liquibase update
```

Preview SQL:

```bash
liquibase update-sql
```

Rollback:

```bash
liquibase rollback-count 1
```

You can also run the native `.sql` files directly with `psql` if you are not using Liquibase.

## Changesets

| Id | Apply SQL | Rollback SQL |
|----|-----------|--------------|
| `001-get-party-description` | `changes/001-get-party-description.sql` | `changes/001-get-party-description-rollback.sql` |

`splitStatements="false"` is set on `sqlFile` so PostgreSQL `$function$` delimiters are not split.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP FUNCTION IF EXISTS public.get_party_description(bigint);
23 changes: 23 additions & 0 deletions liquibase/changelog/changes/001-get-party-description.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE OR REPLACE FUNCTION public.get_party_description(p_party_id bigint)
RETURNS text
LANGUAGE plpgsql
STABLE
STRICT
PARALLEL SAFE
SECURITY INVOKER
SET search_path = ''
AS $function$
DECLARE
v_description text;
BEGIN
SELECT p.description
INTO v_description
FROM public.party AS p
WHERE p.party_id = p_party_id;

RETURN v_description;
END;
$function$;

COMMENT ON FUNCTION public.get_party_description(bigint) IS
'Returns description for the given party_id; NULL if the row does not exist.';
24 changes: 24 additions & 0 deletions liquibase/changelog/changes/001-get-party-description.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="001-get-party-description" author="ctl">
<comment>Read-only PL/pgSQL function get_party_description(party_id)</comment>

<sqlFile
path="001-get-party-description.sql"
relativeToChangelogFile="true"
splitStatements="false"/>

<rollback>
<sqlFile
path="001-get-party-description-rollback.sql"
relativeToChangelogFile="true"
splitStatements="false"/>
</rollback>
</changeSet>

</databaseChangeLog>
10 changes: 10 additions & 0 deletions liquibase/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<include file="changes/001-get-party-description.xml" relativeToChangelogFile="true"/>

</databaseChangeLog>
13 changes: 13 additions & 0 deletions liquibase/liquibase.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copy to liquibase.local.properties and override for your environment.
# Liquibase loads liquibase.local.properties automatically when present.

changeLogFile=changelog/db.changelog-master.xml

url=jdbc:postgresql://localhost:5432/ctl
username=postgres
password=

driver=org.postgresql.Driver
classpath=lib/postgresql.jar

logLevel=info
7 changes: 7 additions & 0 deletions liquibase/liquibase.properties.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
changeLogFile=changelog/db.changelog-master.xml

url=jdbc:postgresql://localhost:5432/your_database
username=your_user
password=your_password

driver=org.postgresql.Driver