Skip to content

Commit b1a1bd1

Browse files
committed
Document simpler method to update the file version
Text provided by Chris Lambacher.
1 parent 289a81c commit b1a1bd1

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

website/docs/guides/hyper_file/optimize.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,43 @@ To use a specific database file format version, you'll need to use the `default_
1717
Using the latest available database file format version should lessen the need to manually defragment or otherwise optimize your file for size or performance.
1818
However, for extremely fragmented files you might still benefit from manually optimizing your file.
1919

20-
## Rewrite your Hyper file in an optimized format {#rewrite-your-hyper-file-in-an-optimized-format}
21-
22-
If you have a Hyper file that has become fragmented or is still using an older file version, one simple solution is to create a new file and copy all the data into it.
23-
There is a [script that does just that](https://github.com/tableau/hyper-api-samples/tree/main/Community-Supported/convert-hyper-file) available on Github.
24-
25-
The Python script uses the Hyper API to copy all the schemas and tables in an existing `.hyper` file and writes them into a new file in a continuous sequence, eliminating any fragmentation that might have occurred.
26-
By creating the new `.hyper` file with the intended new file format version, you can upgrade / downgrade between the various Hyper file versions.
20+
## Rewrite your Hyper file in an optimized format
21+
22+
If you have a Hyper file that has become fragmented or is still using an older file version where you want to take advantage of new version features, you can
23+
update your existing Hyper databases by checking the version and updating the version prior to performing other operations on them. For instance the Python script
24+
below does this while maintaining a backup of the old Hyper file.
25+
26+
```python
27+
import os
28+
from tableauhyperapi import HyperProcess, Connection, Telemetry, CreateMode
29+
30+
TARGET_DATABASE_VERSION = "2"
31+
32+
with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU,
33+
parameters = {"default_database_version": TARGET_DATABASE_VERSION}) as hyper:
34+
should_update_version = False
35+
with Connection(hyper.endpoint, 'existing.hyper', CreateMode.CREATE_IF_NOT_EXISTS) as connection:
36+
# check the current version of the extract
37+
38+
version = connection.execute_scalar_query("SELECT database_version from pg_catalog.hyper_database")
39+
if version < TARGET_DATABASE_VERSION:
40+
print(f'found version {version}, upgrading to version {TARGET_DATABASE_VERSION}')
41+
should_update_version = True
42+
43+
if should_update_version:
44+
with Connection(hyper.endpoint) as connection:
45+
connection.execute_command(f"""
46+
CREATE DATABASE "updatedversion.hyper" WITH VERSION {TARGET_DATABASE_VERSION} FROM "existing.hyper"
47+
""")
48+
# make a backup of the existing hyper file - will overwrite any existing file
49+
os.replace("existing.hyper", "existing.bak.hyper")
50+
51+
# rename the new file to match old database name
52+
os.replace("updatedversion.hyper", "existing.hyper")
53+
with Connection(hyper.endpoint, 'existing.hyper', CreateMode.CREATE_IF_NOT_EXISTS) as connection:
54+
# perform normal operations on connection
55+
...
56+
```
2757

2858
## Guidelines for avoid fragmentation
2959

0 commit comments

Comments
 (0)