Files
threshold/internal/db/schema/migrations/postgres/16/02_hostplugin.up.sql
T
Todd KnightandMichael Gaffney db9e1106a9 feat(host): Add host plugin related schemas and tests (#1475)
* feat(host): Add host plugin related schemas and tests

This adds the schema files for host plugins, plugin based catalogs and
plugin based sets.  This also adds the protos and tests which utilize
gorm to ensure the schemas enforce the constraints we expect.

Along with this change was a migration which moved catalog names to the
base type table so we can ensure all catalogs in a single scope,
regardless of subtype, have a unique name.

* Updated plugin schemas to do the following:
- Add plugin_executable table
- plugin table now also has scope_id and name w/ uniqueness constraints
- host_plugin no longer has id_prefix column
- host_plugin separates name and plugin_name
- plugin_version table renamed version to symantic_version column.

* Fix typo in delete_plugin_subtype trigger.  Add update test for host plugins.

* Update id prefixes. Fix public_id typos in protos and plugin_executable name in sql diagrams.

* Created internal/plugin and moved base plugin structs to it and moved host plugin subdirectory to it.

* Created internal/plugin and moved base plugin structs to it and moved host plugin subdirectory to it.

* Add first pass at enums for OS and Arch supported by plugin executables.

* Capitalizing the const enums for OS and Architectures.

* Add comments to sql files.

* Reviewer comments.

Remove unsupported OSes and Archs from SQL and go constants.
Add missing tests.

* Be more permissive in the values the db accepts.

The go logic can restrict which values it will allow getting populated into the db.

* Removing constraints on which values can be placed inside operating_system and architecture columns.

* Updating test helper functions to only create a single catalog or set.

* Move files to new migration version.

* Fixing job/plugin test.

* Update table names, named a number of constraints, remove plugin version and executable tables.

* Add constraints for plugin name and id prefix.

* Rename constraint, remove unhelpful comment block.

* Adding semantic version to plugin_host and set attributes to not null.

* Remove semantic versioning from all locations.

* Remove semantic_version from plugin_host table comment.

* Fix op copy/paste error. Don't use deprecated errors methods. Update constraint names.  Add docs.

* Apply suggestions from code review

Fixing constraint names.

Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com>
2021-09-08 10:06:38 -07:00

90 lines
3.0 KiB
PL/PgSQL

begin;
/*
┌──────────────────┐
│ plugin │
├──────────────────┤
│public_id (pk) │
│scope_id (fk) │
└──────────────────┘
┼
┼
│
┼
┼
┌──────────────────┐
│ plugin_host │
├──────────────────┤
│public_id (pk) │
│scope_id (fk) │
│name │
│description │
│version │
│plugin_name │
│id_prefix │
└──────────────────┘
*/
create table plugin_host (
public_id wt_plugin_id primary key,
scope_id wt_scope_id not null
-- TODO: Allow plugins to be created in different scopes and
-- constrain the host-catalog's plugin reference accordingly.
constraint iam_scope_global_fkey
references iam_scope_global(scope_id)
on delete cascade
on update cascade,
name wt_name,
description text,
create_time wt_timestamp,
update_time wt_timestamp,
version wt_version,
plugin_name text not null
constraint plugin_name_must_be_not_empty
check(length(trim(plugin_name)) > 0)
constraint plugin_name_must_be_lowercase
check(lower(trim(plugin_name)) = plugin_name)
constraint plugin_host_plugin_name_uq
unique,
id_prefix text not null
constraint plugin_id_prefix_must_be_not_empty
check(length(trim(id_prefix)) > 0)
constraint plugin_id_prefix_must_fit_format
check (id_prefix ~ '^[a-z0-9]*$')
constraint plugin_host_id_prefix_uq
unique,
constraint plugin_fkey
foreign key (scope_id, public_id)
references plugin(scope_id, public_id)
on delete cascade
on update cascade,
constraint plugin_host_scope_id_name_uq
unique(scope_id, name)
);
create trigger update_version_column after update on plugin_host
for each row execute procedure update_version_column();
create trigger update_time_column before update on plugin_host
for each row execute procedure update_time_column();
create trigger default_create_time_column before insert on plugin_host
for each row execute procedure default_create_time();
create trigger immutable_columns before update on plugin_host
for each row execute procedure immutable_columns('public_id', 'create_time', 'plugin_name');
create trigger insert_plugin_subtype before insert on plugin_host
for each row execute procedure insert_plugin_subtype();
create trigger update_plugin_subtype before update on plugin_host
for each row execute procedure update_plugin_subtype();
create trigger delete_plugin_subtype after delete on plugin_host
for each row execute procedure delete_plugin_subtype();
insert into oplog_ticket (name, version)
values
('plugin_host', 1);
commit;