Skip to content

Commit

Permalink
H-3595: Create new migrations from old migration (#5692)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann authored Nov 25, 2024
1 parent c7d70be commit 3b86be5
Show file tree
Hide file tree
Showing 30 changed files with 517 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- We cannot remove the extensions again as extensions are global
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct InitialSetup;
pub struct Extensions;

impl Migration for InitialSetup {
impl Migration for Extensions {
type Context = Client;
type Error = tokio_postgres::Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION IF NOT EXISTS vector;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE accounts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct Accounts;

impl Migration for Accounts {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE accounts (
account_id UUID PRIMARY KEY
);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE account_groups;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct AccountGroups;

impl Migration for AccountGroups {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE account_groups (
account_group_id UUID PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE webs;
26 changes: 26 additions & 0 deletions libs/@local/graph/migrations/graph-migrations/v004__webs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct Webs;

impl Migration for Webs {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE webs (
web_id UUID PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP VIEW ontology_additional_metadata;
DROP TABLE ontology_temporal_metadata;
DROP TABLE ontology_owned_metadata;
DROP TABLE ontology_external_metadata;
DROP TABLE ontology_ids;
DROP TABLE base_urls;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct CommonOntology;

impl Migration for CommonOntology {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE TABLE base_urls (
base_url TEXT PRIMARY KEY
);

CREATE TABLE ontology_ids (
ontology_id UUID PRIMARY KEY,
base_url TEXT NOT NULL REFERENCES base_urls,
version BIGINT NOT NULL,
UNIQUE (base_url, version)
);

CREATE TABLE ontology_owned_metadata (
ontology_id UUID PRIMARY KEY REFERENCES ontology_ids,
web_id UUID NOT NULL REFERENCES webs
);

CREATE TABLE ontology_external_metadata (
ontology_id UUID PRIMARY KEY REFERENCES ontology_ids,
fetched_at TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE TABLE ontology_temporal_metadata (
ontology_id UUID NOT NULL REFERENCES ontology_ids,
transaction_time TSTZRANGE NOT NULL,
provenance JSONB NOT NULL,
EXCLUDE USING gist (
ontology_id WITH =,
transaction_time WITH &&
)
);

CREATE VIEW ontology_additional_metadata AS
SELECT
ontology_owned_metadata.ontology_id,
jsonb_build_object(
'web_id',
ontology_owned_metadata.web_id
) AS additional_metadata
FROM ontology_owned_metadata
UNION ALL
SELECT
ontology_external_metadata.ontology_id,
jsonb_build_object(
'fetched_at',
ontology_external_metadata.fetched_at
) AS additional_metadata
FROM ontology_external_metadata;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DROP VIEW data_type_conversion_aggregation;
DROP TABLE data_type_conversions;
DROP TABLE data_type_embeddings;
DROP TABLE data_type_constrains_values_on;
DROP VIEW data_type_inherits_from_aggregation;
DROP TABLE data_type_inherits_from;
DROP TABLE data_types;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct DataTypes;

impl Migration for DataTypes {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
CREATE TABLE data_types (
ontology_id UUID PRIMARY KEY REFERENCES ontology_ids,
schema JSONB NOT NULL,
closed_schema JSONB NOT NULL
);

CREATE TABLE data_type_inherits_from (
source_data_type_ontology_id UUID REFERENCES data_types,
target_data_type_ontology_id UUID REFERENCES data_types,
depth INT NOT NULL,
UNIQUE (source_data_type_ontology_id, target_data_type_ontology_id)
);

CREATE VIEW data_type_inherits_from_aggregation AS
SELECT
data_type_inherits_from.source_data_type_ontology_id,
array_agg(
data_type_inherits_from.target_data_type_ontology_id
) AS target_data_type_ontology_ids,
array_agg(data_type_inherits_from.depth) AS depths
FROM data_type_inherits_from
GROUP BY data_type_inherits_from.source_data_type_ontology_id;


CREATE TABLE data_type_constrains_values_on (
source_data_type_ontology_id UUID NOT NULL REFERENCES data_types,
target_data_type_ontology_id UUID NOT NULL REFERENCES data_types
);

CREATE TABLE data_type_embeddings (
ontology_id UUID PRIMARY KEY REFERENCES data_types,
embedding VECTOR(3072) NOT NULL,
updated_at_transaction_time TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE TABLE data_type_conversions (
source_data_type_ontology_id UUID PRIMARY KEY REFERENCES data_types,
target_data_type_base_url TEXT NOT NULL REFERENCES base_urls,
"into" JSONB NOT NULL,
"from" JSONB NOT NULL
);

CREATE VIEW data_type_conversion_aggregation AS
SELECT
source_data_type_ontology_id,
array_agg(target_data_type_base_url) AS target_data_type_base_urls,
array_agg("into") AS intos,
array_agg("from") AS froms
FROM data_type_conversions
GROUP BY source_data_type_ontology_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP TABLE property_type_embeddings;
DROP TABLE property_type_constrains_properties_on;
DROP TABLE property_type_constrains_values_on;
DROP TABLE property_types;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use error_stack::Report;
use hash_graph_migrations::{Context, Migration};
use tokio_postgres::Client;

pub struct PropertyTypes;

impl Migration for PropertyTypes {
type Context = Client;
type Error = tokio_postgres::Error;

async fn up(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("up.sql")).await?;
Ok(())
}

async fn down(
self,
context: &mut <Self::Context as Context>::Transaction<'_>,
) -> Result<(), Report<Self::Error>> {
context.simple_query(include_str!("down.sql")).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE property_types (
ontology_id UUID PRIMARY KEY REFERENCES ontology_ids,
schema JSONB NOT NULL
);

CREATE TABLE property_type_constrains_values_on (
source_property_type_ontology_id UUID NOT NULL REFERENCES property_types,
target_data_type_ontology_id UUID NOT NULL REFERENCES data_types
);

CREATE TABLE property_type_constrains_properties_on (
source_property_type_ontology_id UUID NOT NULL REFERENCES property_types,
target_property_type_ontology_id UUID NOT NULL REFERENCES property_types
);

CREATE TABLE property_type_embeddings (
ontology_id UUID PRIMARY KEY REFERENCES property_types,
embedding VECTOR(3072) NOT NULL,
updated_at_transaction_time TIMESTAMP WITH TIME ZONE NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE entity_type_embeddings;
DROP TABLE entity_type_constrains_link_destinations_on;
DROP TABLE entity_type_constrains_links_on;
DROP TABLE entity_type_constrains_properties_on;
DROP TABLE entity_type_inherits_from;
DROP TABLE entity_types;
Loading

0 comments on commit 3b86be5

Please sign in to comment.