Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use postal code as province parameter for french country #14

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on: [push, pull_request]
jobs:
test:
strategy:
matrix:
pg: [15, 14, 13, 12, 11]
name: 🐘 PostgreSQL ${{ matrix.pg }}
runs-on: ubuntu-latest
container: pgxn/pgxn-tools
steps:
- name: Start PostgreSQL ${{ matrix.pg }}
run: pg-start ${{ matrix.pg }}
- name: Check out the repo
uses: actions/checkout@v3
- name: Build project
run: |
chmod +x ./scripts/create_sql_file.sh
- name: Test on PostgreSQL ${{ matrix.pg }}
run: pg-build-test
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,71 @@ variations and defines a default region.

The above query would also produce the same output.

# Development

For ease of use, instead of installing
[pgxn-client](https://pgxn.github.io/pgxnclient/) locally, you can use the
provided docker container.

* Start the container (it will stay up for 10 days by default):

```sh
docker-compose up -d
```

* Enter the container:

```sh
docker-compose exec pgxn-tools bash
```

* Create the instance and the database:

```sh
sudo pg-start 15
createdb -U postgres contrib_regression
```

* Install the extension

```sh
cd /repo && make install
```

* Then you can enter the PostgreSQL instance:

```sh
psql -U postgres -d contrib_regression
```

* Create the extension:

``` sql
create extension holidays;
\dn
```

* Retrieve some holidays:

``` sql
SELECT * FROM holidays.by_country ('FR'::text, 2022, 2023);
```


* To stop the container:

```sh
docker-compose down
```

* To run the test on PostgreSQL 15:

```sh
docker-compose up -d && \
docker-compose exec pgxn-tools bash -c 'cd /repo && sudo pg-start 15 && pg-build-test' ; \
docker-compose down
```

# To Do

Cross-port the knowledge from the npm / javascript libraries for the same
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3.9'
services:
pgxn-tools:
image: pgxn/pgxn-tools
volumes:
- .:/repo/
command: sleep 10d
5 changes: 3 additions & 2 deletions scripts/create_sql_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ COUNTRIES_FILES=(
)

for sql_file in ${DBSETUP_FILES[*]} ${UTILS_FILES[*]} ${COUNTRIES_FILES[*]}; do
(cat "${sql_file}"; echo; echo) >> holidays.sql
(cat "${sql_file}"; echo; echo) >> holidays_orig.sql
done

sed -e 's#\([ (\t]\)holidays\.#\1@extschema@.#g' -e 's#--\([ \t]*\)@extschema@\.#--\1holidays.#g' -i holidays.sql
sed -e 's#\([ (\t]\)holidays\.#\1@extschema@.#g' -e 's#--\([ \t]*\)@extschema@\.#--\1holidays.#g' holidays_orig.sql > holidays.sql
rm -f holidays_orig.sql
42 changes: 42 additions & 0 deletions sql/by_country.pgsql
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,45 @@ BEGIN
END;

$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION holidays.by_country(
p_country TEXT,
p_start_date date,
p_end_date date,
p_sub_region TEXT DEFAULT NULL
)
RETURNS SETOF holidays.holiday
AS $$
SELECT
datestamp,
reference,
description,
authority,
day_off,
observation_shifted,
start_time,
end_time
FROM holidays.by_country(
p_country,
extract('year' from p_start_date)::int,
extract('year' from p_end_date)::int,
p_sub_region
)
WHERE datestamp >= p_start_date AND datestamp <= p_end_date
$$ LANGUAGE sql;

CREATE OR REPLACE FUNCTION holidays.by_country(
p_country TEXT,
p_daterange daterange,
p_sub_region TEXT DEFAULT NULL
)
RETURNS SETOF holidays.holiday
AS $$
SELECT holidays.by_country(
p_country,
lower(p_daterange),
upper(p_daterange),
p_sub_region
);
$$ LANGUAGE sql;
35 changes: 30 additions & 5 deletions sql/countries/france.pgsql
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ DECLARE
WEEKEND INTEGER[] := ARRAY[0, 6];
-- Provinces
PROVINCES TEXT[] := ARRAY[
'Métropole', 'Alsace-Moselle', 'Guadeloupe', 'Guyane', 'Martinique',
'Mayotte', 'Nouvelle-Calédonie', 'La Réunion', 'Polynésie Française',
'Saint-Barthélémy', 'Saint-Martin', 'Wallis-et-Futuna'
'Métropole', 'Alsace-Moselle', 'Guadeloupe', 'Saint-Barthélémy',
'Saint-Martin', 'Martinique', 'Guyane', 'La Réunion', 'Mayotte',
'Nouvelle-Calédonie', 'Polynésie Française', 'Wallis-et-Futuna'
];
-- Primary Loop
t_years INTEGER[] := (SELECT ARRAY(SELECT generate_series(p_start_year, p_end_year)));
Expand All @@ -61,6 +61,31 @@ DECLARE
t_holiday holidays.holiday%rowtype;

BEGIN
IF p_province ~ '^[0-9]{5}$' THEN
-- set province from postal code
IF LEFT(p_province, 2) IN ('57', '67', '68') THEN
p_province := 'Alsace-Moselle';
ELSE
CASE LEFT(p_province, 3)
WHEN '971' THEN
IF p_province = '97133'
THEN p_province := 'Saint-Barthélémy';
ELSIF p_province = '97150'
THEN p_province := 'Saint-Martin';
ELSE p_province := 'Guadeloupe';
END IF;
WHEN '972' THEN p_province := 'Martinique';
WHEN '973' THEN p_province := 'Guyane';
WHEN '974' THEN p_province := 'La Réunion';
WHEN '976' THEN p_province := 'Mayotte';
WHEN '988' THEN p_province := 'Nouvelle-Calédonie';
WHEN '987' THEN p_province := 'Polynésie Française';
WHEN '986' THEN p_province := 'Wallis-et-Futuna';
ELSE p_province := 'Métropole';
END CASE;
END IF;
END IF;

FOREACH t_year IN ARRAY t_years
LOOP
-- Defaults for additional attributes
Expand Down Expand Up @@ -178,7 +203,7 @@ BEGIN
t_holiday.description := 'Deuxième jour de Noël';
RETURN NEXT t_holiday;
END IF;

-- Citizenship Day
t_holiday.reference := 'Citizenship Day';
IF p_province = 'Nouvelle-Calédonie' THEN
Expand Down Expand Up @@ -245,4 +270,4 @@ BEGIN
END LOOP;
END;

$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;
28 changes: 28 additions & 0 deletions test/expected/base.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,32 @@ SELECT * FROM holidays.by_country ('FR'::text, 2022, 2023);
12-25-2023 | Christmas Day | Noël | national | t | f | 00:00:00 | 24:00:00
(22 rows)

SELECT * FROM holidays.by_country ('FR'::text, '10/01/2022'::date, '05/31/2023'::date);
datestamp | reference | description | authority | day_off | observation_shifted | start_time | end_time
------------+-----------------------+--------------------+-----------+---------+---------------------+------------+----------
11-11-2022 | Remembrance Day | Armistice 1918 | national | t | f | 00:00:00 | 24:00:00
11-01-2022 | All Saints' Day | Toussaint | national | t | f | 00:00:00 | 24:00:00
12-25-2022 | Christmas Day | Noël | national | t | f | 00:00:00 | 24:00:00
01-01-2023 | New Year's Day | Jour de l'an | national | t | f | 00:00:00 | 24:00:00
05-01-2023 | Labour Day | Fête du Travail | national | t | f | 00:00:00 | 24:00:00
05-08-2023 | Victory in Europe Day | Armistice 1945 | national | t | f | 00:00:00 | 24:00:00
04-10-2023 | Easter Monday | Lundi de Pâques | national | t | f | 00:00:00 | 24:00:00
05-29-2023 | Pentecost | Lundi de Pentecôte | national | t | f | 00:00:00 | 24:00:00
05-18-2023 | Ascension | Ascension | national | t | f | 00:00:00 | 24:00:00
(9 rows)

SELECT * FROM holidays.by_country ('FR'::text, daterange('2022/10/01', '2023/05/31'));
datestamp | reference | description | authority | day_off | observation_shifted | start_time | end_time
------------+-----------------------+--------------------+-----------+---------+---------------------+------------+----------
11-11-2022 | Remembrance Day | Armistice 1918 | national | t | f | 00:00:00 | 24:00:00
11-01-2022 | All Saints' Day | Toussaint | national | t | f | 00:00:00 | 24:00:00
12-25-2022 | Christmas Day | Noël | national | t | f | 00:00:00 | 24:00:00
01-01-2023 | New Year's Day | Jour de l'an | national | t | f | 00:00:00 | 24:00:00
05-01-2023 | Labour Day | Fête du Travail | national | t | f | 00:00:00 | 24:00:00
05-08-2023 | Victory in Europe Day | Armistice 1945 | national | t | f | 00:00:00 | 24:00:00
04-10-2023 | Easter Monday | Lundi de Pâques | national | t | f | 00:00:00 | 24:00:00
05-29-2023 | Pentecost | Lundi de Pentecôte | national | t | f | 00:00:00 | 24:00:00
05-18-2023 | Ascension | Ascension | national | t | f | 00:00:00 | 24:00:00
(9 rows)

ROLLBACK;
Loading