Skip to content

Commit

Permalink
v1: handle conflicts in insert_trunk_project
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Dec 21, 2023
1 parent b97e8e2 commit e2e62bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
90 changes: 45 additions & 45 deletions registry/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,21 @@
},
"query": "SELECT * FROM extensions WHERE name = $1"
},
"4ba793c5e681f2bec3e50c82ab9ec580a7f76660cee45f585a368758266b781d": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Bool",
"Text",
"Text"
]
}
},
"query": "INSERT INTO v1.extension_configurations (extension_version_id, is_required, configuration_name, recommended_default_value)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (extension_version_id, configuration_name)\n DO UPDATE SET \n is_required = EXCLUDED.is_required,\n recommended_default_value = EXCLUDED.recommended_default_value"
},
"4db2be5140f4d7c0edebcf62616e8ce97c5479134580a654727fbe4a83435319": {
"describe": {
"columns": [
Expand Down Expand Up @@ -669,21 +684,6 @@
},
"query": "SELECT id FROM extensions WHERE name = $1"
},
"580c2476466e3831640ab78b7f314d258f6f26b305ebd798a2bcdf06fbd0b01c": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Bool",
"Text",
"Text"
]
}
},
"query": "INSERT INTO v1.extension_configurations (extension_version_id, is_required, configuration_name, recommended_default_value)\n VALUES ($1, $2, $3, $4)"
},
"65eec92a739b8837322c565e5b85b2c82c4bfc29fd4153c79bf45a2ac3320678": {
"describe": {
"columns": [
Expand All @@ -702,7 +702,7 @@
},
"query": "\n SELECT\n json_build_object(\n 'name', tp.name,\n 'description', latest_tpvs.description,\n 'documentation_link', latest_tpvs.documentation_link,\n 'repository_link', latest_tpvs.repository_link,\n 'version', latest_tpvs.version,\n 'postgres_versions', (\n SELECT json_agg(pg.major)\n FROM v1.trunk_project_postgres_support tpps\n JOIN v1.postgres_version pg ON tpps.postgres_version_id = pg.id\n WHERE tpps.trunk_project_version_id = latest_tpvs.id\n ),\n 'extensions', (\n SELECT json_agg(json_build_object(\n 'extension_name', ev.extension_name,\n 'version', ev.version,\n 'trunk_project_name', tp.name,\n 'dependencies_extension_names', (\n SELECT json_agg(ed.depends_on_extension_name)\n FROM v1.extension_dependency ed\n WHERE ed.extension_version_id = ev.id\n ),\n 'loadable_libraries', (\n SELECT json_agg(json_build_object(\n 'library_name', ell.library_name,\n 'requires_restart', ell.requires_restart,\n 'priority', ell.priority\n ))\n FROM v1.extensions_loadable_libraries ell\n WHERE ell.extension_version_id = ev.id\n ),\n 'configurations', (\n SELECT json_agg(json_build_object(\n 'name', ec.configuration_name,\n 'is_required', ec.is_required,\n 'default', ec.recommended_default_value\n ))\n FROM v1.extension_configurations ec\n WHERE ec.extension_version_id = ev.id\n ),\n 'control_file', (\n SELECT json_build_object(\n 'absent', cf.absent,\n 'content', cf.content\n )\n FROM v1.control_file cf\n WHERE cf.extension_version_id = ev.id\n )\n ))\n FROM v1.extension_versions ev\n WHERE ev.trunk_project_version_id = latest_tpvs.id\n )\n ) AS result\n FROM\n v1.trunk_project tp\n JOIN (\n SELECT tpv.*\n FROM v1.trunk_project_versions tpv\n JOIN (\n SELECT trunk_project_name, MAX(string_to_array(version, '.')::int[]) as max_version\n FROM v1.trunk_project_versions\n GROUP BY trunk_project_name\n ) sub_tpv\n ON tpv.trunk_project_name = sub_tpv.trunk_project_name\n AND string_to_array(tpv.version, '.')::int[] = sub_tpv.max_version\n ) latest_tpvs\n ON tp.name = latest_tpvs.trunk_project_name\n ORDER BY tp.name"
},
"6722d3fb3cbd849aad563fe00ab77363b17cb7a63a14752a88d6c162011dc652": {
"68d793399afe7fff28bd42fa91c4d25389eb5e90a451694d779ef8a8f1125de0": {
"describe": {
"columns": [],
"nullable": [],
Expand All @@ -714,7 +714,7 @@
]
}
},
"query": "INSERT INTO v1.control_file (extension_version_id, absent, content)\n VALUES ($1, $2, $3)"
"query": "INSERT INTO v1.control_file (extension_version_id, absent, content)\n VALUES ($1, $2, $3)\n ON CONFLICT (extension_version_id) \n DO UPDATE SET \n absent = EXCLUDED.absent,\n content = EXCLUDED.content"
},
"6991d9aae9d130b3d209ea5816847f661979e07c07f754c15e9b05eee3d61cb6": {
"describe": {
Expand Down Expand Up @@ -766,6 +766,19 @@
},
"query": "\n INSERT INTO extension_owners(extension_id, owner_id, user_name, created_at, created_by)\n VALUES ($1, $2, $3, (now() at time zone 'utc'), $2)\n "
},
"72250de71fdc4e1f66965a25bd6ebf5f4ea5566013706b61c0ba028367102a98": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Text"
]
}
},
"query": "INSERT INTO v1.extension_dependency (extension_version_id, depends_on_extension_name)\n VALUES ($1, $2)\n ON CONFLICT (extension_version_id, depends_on_extension_name) \n DO NOTHING"
},
"756737edb5733514dcb510266ab577703d9cf4cb7f61702d7a4a15962fc8ae87": {
"describe": {
"columns": [
Expand Down Expand Up @@ -1033,19 +1046,6 @@
},
"query": "\n INSERT INTO api_tokens(user_id, user_name, token, created_at)\n VALUES ($1, $2, $3, (now() at time zone 'utc'))\n "
},
"8937136b4deb49610728a789a022f48c42064d13df598b8c38a90662443f02ae": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Text"
]
}
},
"query": "INSERT INTO v1.extension_dependency (extension_version_id, depends_on_extension_name)\n VALUES ($1, $2)"
},
"8a3643fa646f1768ce6b3abdc949b6daade8f796a8f2b9a388f436e486425299": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -1448,6 +1448,21 @@
},
"query": "SELECT\n json_build_object(\n 'name', tpv.trunk_project_name,\n 'description', tpv.description,\n 'version', tpv.version,\n 'documentation_link', tpv.documentation_link,\n 'repository_link', tpv.repository_link,\n 'postgres_versions', (\n SELECT json_agg(pg.major)\n FROM v1.trunk_project_postgres_support tpps\n JOIN v1.postgres_version pg ON tpps.postgres_version_id = pg.id\n WHERE tpps.trunk_project_version_id = tpv.id\n ),\n 'extensions', (\n SELECT json_agg(json_build_object(\n 'extension_name', ev.extension_name,\n 'version', ev.version,\n 'trunk_project_name', tpv.trunk_project_name,\n 'dependencies_extension_names', (\n SELECT json_agg(ed.depends_on_extension_name)\n FROM v1.extension_dependency ed\n WHERE ed.extension_version_id = ev.id\n ),\n 'loadable_libraries', (\n SELECT json_agg(json_build_object(\n 'library_name', ell.library_name,\n 'requires_restart', ell.requires_restart,\n 'priority', ell.priority\n ))\n FROM v1.extensions_loadable_libraries ell\n WHERE ell.extension_version_id = ev.id\n ),\n 'configurations', (\n SELECT json_agg(json_build_object(\n 'name', ec.configuration_name,\n 'is_required', ec.is_required,\n 'default', ec.recommended_default_value\n ))\n FROM v1.extension_configurations ec\n WHERE ec.extension_version_id = ev.id\n ),\n 'control_file', (\n SELECT json_build_object(\n 'absent', cf.absent,\n 'content', cf.content\n )\n FROM v1.control_file cf\n WHERE cf.extension_version_id = ev.id\n )\n ))\n FROM v1.extension_versions ev\n WHERE ev.trunk_project_version_id = tpv.id\n )\n ) AS result\n FROM\n v1.trunk_project_versions tpv\n WHERE\n tpv.trunk_project_name = $1"
},
"da8d71c18c746ac07a83c970bffec92a6ea2380bf8e480122fdd2ada4a3ac616": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Text",
"Bool",
"Int4"
]
}
},
"query": "INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (extension_version_id, library_name)\n DO UPDATE SET \n requires_restart = EXCLUDED.requires_restart,\n priority = EXCLUDED.priority"
},
"e41ccd0b4b07c1761d78d4de2efdba33979114f1b055db3261af653afca8be56": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -1479,21 +1494,6 @@
},
"query": "\n UPDATE categories\n SET extension_count = extension_count + 1\n WHERE id = $1\n "
},
"f5c4444586051427aaf351b423d8dba82b213eef6e707a1cf5b54854769ce1b9": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Text",
"Bool",
"Int4"
]
}
},
"query": "INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)\n VALUES ($1, $2, $3, $4)"
},
"f77d6c607d830e7ea1d564c34ec42b998e0c2657f4cdb8a89dd211a29b4a7d65": {
"describe": {
"columns": [],
Expand Down
24 changes: 20 additions & 4 deletions registry/src/v1/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ impl Registry {
/// 6. insert shared preload libraries
/// 7. Insert control file metadata
pub async fn insert_trunk_project(&self, trunk_project: TrunkProjectView) -> Result<()> {
// 0. insert Postgres version

// 1. insert trunk project name
sqlx::query!(
"INSERT INTO v1.trunk_project (name)
Expand Down Expand Up @@ -401,7 +403,9 @@ impl Registry {
for dependency_name in dependencies {
sqlx::query!(
"INSERT INTO v1.extension_dependency (extension_version_id, depends_on_extension_name)
VALUES ($1, $2)",
VALUES ($1, $2)
ON CONFLICT (extension_version_id, depends_on_extension_name)
DO NOTHING",
extension_version_id,
dependency_name,
).execute(&self.pool).await?;
Expand Down Expand Up @@ -432,7 +436,11 @@ impl Registry {
for config in configurations {
sqlx::query!(
"INSERT INTO v1.extension_configurations (extension_version_id, is_required, configuration_name, recommended_default_value)
VALUES ($1, $2, $3, $4)",
VALUES ($1, $2, $3, $4)
ON CONFLICT (extension_version_id, configuration_name)
DO UPDATE SET
is_required = EXCLUDED.is_required,
recommended_default_value = EXCLUDED.recommended_default_value",
extension_version_id,
config.is_required,
config.configuration_name,
Expand All @@ -450,7 +458,11 @@ impl Registry {
for library in loadable_libraries {
sqlx::query!(
"INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)
VALUES ($1, $2, $3, $4)",
VALUES ($1, $2, $3, $4)
ON CONFLICT (extension_version_id, library_name)
DO UPDATE SET
requires_restart = EXCLUDED.requires_restart,
priority = EXCLUDED.priority",
extension_version_id,
library.library_name,
library.requires_restart,
Expand All @@ -468,7 +480,11 @@ impl Registry {
) -> Result {
sqlx::query!(
"INSERT INTO v1.control_file (extension_version_id, absent, content)
VALUES ($1, $2, $3)",
VALUES ($1, $2, $3)
ON CONFLICT (extension_version_id)
DO UPDATE SET
absent = EXCLUDED.absent,
content = EXCLUDED.content",
extension_version_id,
control_file.absent,
control_file.content,
Expand Down

0 comments on commit e2e62bd

Please sign in to comment.