Skip to content

Commit

Permalink
chore: db party model (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr authored Jul 25, 2024
1 parent fe9c883 commit a4008b6
Show file tree
Hide file tree
Showing 13 changed files with 636 additions and 17 deletions.
6 changes: 1 addition & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,5 @@
"BaseArtifactsPath",
"ArtifactsPath",
"VersionFilePath"
],
"sqltools.format": {
"language": "sql",
"linesBetweenQueries": 2
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ private static void AddDatabase(IHostApplicationBuilder builder)
var descriptor = builder.GetAltinnServiceDescriptor();
var yuniqlSchema = builder.Configuration.GetValue<string>($"Altinn:Npgsql:{descriptor.Name}:Yuniql:MigrationsTable:Schema");
var migrationsFs = new ManifestEmbeddedFileProvider(typeof(RegisterPersistenceExtensions).Assembly, "Migration");
////var seedDataFs = new ManifestEmbeddedFileProvider(typeof(RegisterPersistenceExtensions).Assembly, "TestData");
var seedDataFs = new ManifestEmbeddedFileProvider(typeof(RegisterPersistenceExtensions).Assembly, "TestData");
builder.AddAltinnPostgresDataSource()
////.SeedFromFileProvider(seedDataFs)
.SeedFromFileProvider(seedDataFs)
.AddYuniqlMigrations(y =>
{
y.WorkspaceFileProvider = migrationsFs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CREATE SCHEMA IF NOT EXISTS register;

Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
-- grant on tables
ALTER DEFAULT PRIVILEGES FOR USER "${YUNIQL-USER}" IN SCHEMA register
GRANT SELECT,
INSERT,
UPDATE,
REFERENCES,
DELETE,
TRUNCATE,
TRIGGER ON TABLES TO "${APP-USER}";
ALTER DEFAULT PRIVILEGES FOR USER "${YUNIQL-USER}" IN SCHEMA register GRANT
SELECT
, INSERT, UPDATE, REFERENCES, DELETE, TRUNCATE, TRIGGER ON TABLES TO "${APP-USER}";

-- grant on sequences
ALTER DEFAULT PRIVILEGES FOR USER "${YUNIQL-USER}" IN SCHEMA register
GRANT ALL ON SEQUENCES TO "${APP-USER}";
ALTER DEFAULT PRIVILEGES FOR USER "${YUNIQL-USER}" IN SCHEMA register GRANT ALL ON SEQUENCES TO "${APP-USER}";

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Enum: register.party_type
CREATE TYPE register.party_type AS ENUM(
'person',
'organization'
);

-- Enum: register.party_source
CREATE TYPE register.party_source AS ENUM(
'folkeregistered',
'enhetsregisteret'
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Domain: register.person_identifier
CREATE DOMAIN register.person_identifier AS text CONSTRAINT fmtchk CHECK (value ~ '^[0-9]{11}$');

-- Domain: register.organization_identifier
CREATE DOMAIN register.organization_identifier AS text CONSTRAINT fmtchk CHECK (value ~ '^[0-9]{9}$');

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Composite: register._address (private - implementation detail)
CREATE TYPE register._address AS (
municipal_number text,
municipal_name text,
street_name text,
house_number text,
house_letter text,
apartment_number text,
postal_code text,
city text
);

-- Domain: register.address
-- No checks at this time, but create a domain so they can be added later
CREATE DOMAIN register.address AS register._address;

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Table: register.party
CREATE TABLE register.party(
uuid uuid PRIMARY KEY NOT NULL,
id bigint NOT NULL,
party_type register.party_type NOT NULL,
name text NOT NULL,
person_identifier register.person_identifier,
organization_identifier register.organization_identifier,
created timestamp with time zone NOT NULL,
updated timestamp with time zone NOT NULL,
CONSTRAINT type_identifier_check CHECK ((party_type = 'person'::register.party_type AND person_identifier IS NOT NULL AND organization_identifier IS NULL) OR (party_type = 'organization'::register.party_type AND person_identifier IS NULL AND organization_identifier IS NOT NULL))
)
TABLESPACE pg_default;

CREATE UNIQUE INDEX uq_party_uuid ON register.party(uuid) INCLUDE (uuid, id, party_type, name, person_identifier, organization_identifier) TABLESPACE pg_default;

CREATE UNIQUE INDEX uq_party_id ON register.party(id) INCLUDE (uuid, id, party_type, name, person_identifier, organization_identifier) TABLESPACE pg_default;

CREATE UNIQUE INDEX uq_person_identifier ON register.party(person_identifier) INCLUDE (uuid, id, party_type, name, person_identifier, organization_identifier) TABLESPACE pg_default;

CREATE UNIQUE INDEX uq_organization_identifier ON register.party(organization_identifier) INCLUDE (uuid, id, party_type, name, person_identifier, organization_identifier) TABLESPACE pg_default;

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Table: register.party_source_ref
CREATE TABLE register.party_source_ref(
party_uuid uuid NOT NULL REFERENCES register.party(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
source register.party_source NOT NULL,
source_identifier text NOT NULL,
source_created timestamp with time zone,
source_updated timestamp with time zone,
PRIMARY KEY (party_uuid, source, source_identifier)
)
TABLESPACE pg_default;

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Table: register.person
CREATE TABLE register.person(
uuid uuid PRIMARY KEY NOT NULL REFERENCES register.party(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
first_name text NOT NULL,
middle_name text,
last_name text NOT NULL,
address register.address,
mailing_address register.address,
date_of_birth date,
date_of_death date
)
TABLESPACE pg_default;

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Table: register.organization
CREATE TABLE register.organization(
uuid uuid PRIMARY KEY NOT NULL REFERENCES register.party(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
unit_status text,
unit_type text,
telephone_number text,
mobile_number text,
fax_number text,
email_address text,
internet_address text,
mailing_address register.address,
business_address register.address
)
TABLESPACE pg_default;

Loading

0 comments on commit a4008b6

Please sign in to comment.