From 768a8884ef314fc45f6aba7ca905f62b3533b447 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Fri, 17 Feb 2023 10:35:15 -0100 Subject: [PATCH] Glue:get_table_version() - Add VersionId/UpdateTime fields (#5940) --- moto/glue/models.py | 19 ++++++++++++++----- moto/glue/responses.py | 3 +-- tests/test_glue/test_datacatalog.py | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/moto/glue/models.py b/moto/glue/models.py index 31f3bd1be328..dcec934ba668 100644 --- a/moto/glue/models.py +++ b/moto/glue/models.py @@ -130,7 +130,7 @@ def create_table(self, database_name, table_name, table_input): database.tables[table_name] = table return table - def get_table(self, database_name, table_name): + def get_table(self, database_name: str, table_name: str) -> "FakeTable": database = self.get_database(database_name) try: return database.tables[table_name] @@ -165,6 +165,10 @@ def delete_table(self, database_name, table_name): raise TableNotFoundException(table_name) return {} + def update_table(self, database_name, table_name: str, table_input) -> None: + table = self.get_table(database_name, table_name) + table.update(table_input) + def get_partitions(self, database_name, table_name, expression): """ See https://docs.aws.amazon.com/glue/latest/webapi/API_GetPartitions.html @@ -769,16 +773,17 @@ def as_dict(self): class FakeTable(BaseModel): - def __init__(self, database_name, table_name, table_input): + def __init__(self, database_name: str, table_name: str, table_input): self.database_name = database_name self.name = table_name self.partitions = OrderedDict() self.created_time = datetime.utcnow() - self.versions = [] - self.update(table_input) + self.updated_time = None + self.versions = [table_input] def update(self, table_input): self.versions.append(table_input) + self.updated_time = datetime.utcnow() def get_version(self, ver): try: @@ -798,8 +803,12 @@ def as_dict(self, version=-1): "DatabaseName": self.database_name, "Name": self.name, "CreateTime": unix_time(self.created_time), + **self.get_version(version), + # Add VersionId after we get the version-details, just to make sure that it's a valid version (int) + "VersionId": str(int(version) + 1), } - obj.update(self.get_version(version)) + if self.updated_time is not None: + obj["UpdateTime"] = unix_time(self.updated_time) return obj def create_partition(self, partiton_input): diff --git a/moto/glue/responses.py b/moto/glue/responses.py index 361848a3b752..3c6aa365b869 100644 --- a/moto/glue/responses.py +++ b/moto/glue/responses.py @@ -66,8 +66,7 @@ def update_table(self): database_name = self.parameters.get("DatabaseName") table_input = self.parameters.get("TableInput") table_name = table_input.get("Name") - table = self.glue_backend.get_table(database_name, table_name) - table.update(table_input) + self.glue_backend.update_table(database_name, table_name, table_input) return "" def get_table_versions(self): diff --git a/tests/test_glue/test_datacatalog.py b/tests/test_glue/test_datacatalog.py index 8dc0ecf82a20..56956213aad4 100644 --- a/tests/test_glue/test_datacatalog.py +++ b/tests/test_glue/test_datacatalog.py @@ -339,11 +339,13 @@ def test_get_table_versions(): for n, ver in enumerate(vers): n = str(n + 1) ver["VersionId"].should.equal(n) + ver["Table"]["VersionId"].should.equal(n) ver["Table"]["Name"].should.equal(table_name) ver["Table"]["StorageDescriptor"].should.equal( version_inputs[n]["StorageDescriptor"] ) ver["Table"]["PartitionKeys"].should.equal(version_inputs[n]["PartitionKeys"]) + ver["Table"].should.have.key("UpdateTime") response = helpers.get_table_version(client, database_name, table_name, "3") ver = response["TableVersion"]