Skip to content

Commit 4ab0f51

Browse files
authored
add spi plugin (#290)
1 parent 199e53b commit 4ab0f51

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ options:
215215
default: false
216216
type: boolean
217217
description: Enable uuid_ossp extension
218+
plugin_spi_enable:
219+
default: false
220+
type: boolean
221+
description: Enable spi extension
218222
profile:
219223
description: |
220224
Profile representing the scope of deployment, and used to tune resource allocation.

src/charm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ def enable_disable_extensions(self, database: str = None) -> None:
947947
Args:
948948
database: optional database where to enable/disable the extension.
949949
"""
950+
spi_module = ["refint", "autoinc", "insert_username", "moddatetime"]
950951
original_status = self.unit.status
951952
plugins_exception = {"uuid_ossp": '"uuid-ossp"'}
952953
extensions = {}
@@ -956,6 +957,10 @@ def enable_disable_extensions(self, database: str = None) -> None:
956957

957958
# Enable or disable the plugin/extension.
958959
extension = "_".join(plugin.split("_")[1:-1])
960+
if extension == "spi":
961+
for ext in spi_module:
962+
extensions[ext] = enable
963+
continue
959964
if extension in plugins_exception:
960965
extension = plugins_exception[extension]
961966
extensions[extension] = enable

src/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CharmConfig(BaseConfigModel):
6363
plugin_tsm_system_rows_enable: bool
6464
plugin_tsm_system_time_enable: bool
6565
plugin_uuid_ossp_enable: bool
66+
plugin_spi_enable: bool
6667
request_date_style: Optional[str]
6768
request_standard_conforming_strings: Optional[bool]
6869
request_time_zone: Optional[str]

src/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# Snap constants.
3333
PGBACKREST_EXECUTABLE = "charmed-postgresql.pgbackrest"
3434
POSTGRESQL_SNAP_NAME = "charmed-postgresql"
35-
SNAP_PACKAGES = [(POSTGRESQL_SNAP_NAME, {"revision": "86"})]
35+
SNAP_PACKAGES = [(POSTGRESQL_SNAP_NAME, {"revision": "87"})]
3636

3737
SNAP_COMMON_PATH = "/var/snap/charmed-postgresql/common"
3838
SNAP_CURRENT_PATH = "/var/snap/charmed-postgresql/current"

tests/integration/test_plugins.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
TSM_SYSTEM_ROWS_EXTENSION_STATEMENT = "CREATE TABLE tsm_system_rows_test (i int);SELECT * FROM tsm_system_rows_test TABLESAMPLE SYSTEM_ROWS(100);"
5454
TSM_SYSTEM_TIME_EXTENSION_STATEMENT = "CREATE TABLE tsm_system_time_test (i int);SELECT * FROM tsm_system_time_test TABLESAMPLE SYSTEM_TIME(1000);"
5555
UUID_OSSP_EXTENSION_STATEMENT = "SELECT uuid_nil();"
56+
REFINT_EXTENSION_STATEMENT = "CREATE TABLE A (ID int4 not null); CREATE UNIQUE INDEX AI ON A (ID);CREATE TABLE B (REFB int4);CREATE INDEX BI ON B (REFB);CREATE TRIGGER BT BEFORE INSERT OR UPDATE ON B FOR EACH ROW EXECUTE PROCEDURE check_primary_key ('REFB', 'A', 'ID');"
57+
AUTOINC_EXTENSION_STATEMENT = "CREATE TABLE ids (id int4, idesc text);CREATE TRIGGER ids_nextid BEFORE INSERT OR UPDATE ON ids FOR EACH ROW EXECUTE PROCEDURE autoinc (id, next_id);"
58+
INSERT_USERNAME_EXTENSION_STATEMENT = "CREATE TABLE username_test (name text, username text not null);CREATE TRIGGER insert_usernames BEFORE INSERT OR UPDATE ON username_test FOR EACH ROW EXECUTE PROCEDURE insert_username (username);"
59+
MODDATETIME_EXTENSION_STATEMENT = "CREATE TABLE mdt (moddate timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL);CREATE TRIGGER mdt_moddatetime BEFORE UPDATE ON mdt FOR EACH ROW EXECUTE PROCEDURE moddatetime (moddate);"
5660

5761

5862
@pytest.mark.abort_on_fail
@@ -99,6 +103,12 @@ async def test_plugins(ops_test: OpsTest) -> None:
99103
"plugin_tsm_system_rows_enable": TSM_SYSTEM_ROWS_EXTENSION_STATEMENT,
100104
"plugin_tsm_system_time_enable": TSM_SYSTEM_TIME_EXTENSION_STATEMENT,
101105
"plugin_uuid_ossp_enable": UUID_OSSP_EXTENSION_STATEMENT,
106+
"plugin_spi_enable": [
107+
REFINT_EXTENSION_STATEMENT,
108+
AUTOINC_EXTENSION_STATEMENT,
109+
INSERT_USERNAME_EXTENSION_STATEMENT,
110+
MODDATETIME_EXTENSION_STATEMENT,
111+
],
102112
}
103113

104114
def enable_disable_config(enabled: False):
@@ -120,8 +130,13 @@ def enable_disable_config(enabled: False):
120130
with db_connect(host=address, password=password) as connection:
121131
connection.autocommit = True
122132
for query in sql_tests.values():
123-
with pytest.raises(psycopg2.Error):
124-
connection.cursor().execute(query)
133+
if isinstance(query, list):
134+
for test in query:
135+
with pytest.raises(psycopg2.Error):
136+
connection.cursor().execute(test)
137+
else:
138+
with pytest.raises(psycopg2.Error):
139+
connection.cursor().execute(query)
125140
connection.close()
126141

127142
# Enable the plugins.
@@ -136,5 +151,9 @@ def enable_disable_config(enabled: False):
136151
with db_connect(host=address, password=password) as connection:
137152
connection.autocommit = True
138153
for query in sql_tests.values():
139-
connection.cursor().execute(query)
154+
if isinstance(query, list):
155+
for test in query:
156+
connection.cursor().execute(test)
157+
else:
158+
connection.cursor().execute(query)
140159
connection.close()

tests/unit/test_charm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ def test_enable_disable_extensions(self, _):
393393
plugin_uuid_ossp_enable:
394394
default: false
395395
type: boolean
396+
plugin_spi_enable:
397+
default: false
398+
type: boolean
396399
profile:
397400
default: production
398401
type: string"""

0 commit comments

Comments
 (0)