Skip to content

Commit

Permalink
Glue:get_table_version() - Add VersionId/UpdateTime fields (#5940)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Feb 17, 2023
1 parent 418c69f commit 768a888
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
19 changes: 14 additions & 5 deletions moto/glue/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions moto/glue/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_glue/test_datacatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 768a888

Please sign in to comment.