Note
Go to the end to download the full example code.
Migrate (update) databases#
In this example we show how to restore a model from two different sources:
From model data in storage (csv) format, which is the standard form to maintain Polaris models
Directly from version control (not necessarily available for all users and models)
icon credits: https://c2.staticflickr.com/2/1049/5142119589_1aaf74ded4_b.jpg
sphinx_gallery_thumbnail_path = ‘../../examples/working_with_models/migration.jpg’
import shutil
from pathlib import Path
from polaris import Polaris
# Let's open the model
project = Polaris.from_dir(Path("/tmp/Grid"))
# 1. Migration Manager
# --------------------
# The easiest way is to just point the migration manager at each of your supply and demand databases and let it do
# what it can to bring them up to speed with the latest requirements from the polaris executable.
#
# If the database is older, you will get a warning and it may fail. If so try method #2 below
project.upgrade()
2. Dump and Reload#
Finally, if your database is too old to work with migrations (<2022), you can always try to dump and reload the database to csv to re-populate it with all the columns, tables and default values that are coded into polaris.
from polaris.utils.database.db_utils import read_and_close
from polaris.utils.database.database_dumper import dump_database_to_csv
from polaris.project.project_restorer import create_db_from_csv
from polaris.utils.database.standard_database import DatabaseType
from tempfile import mkdtemp
# We export the supply database to text
export_dir = Path(mkdtemp(prefix="my_new_db"))
with read_and_close(project.supply_file, spatial=True) as conn:
dump_database_to_csv(conn, export_dir / "supply", None)
# We export the demand database to text
with read_and_close(project.demand_file, spatial=True) as conn:
dump_database_to_csv(conn, export_dir / "demand", None)
# Rebuild the files
# Supply
create_db_from_csv(
export_dir / "Grid-Supply_new.sqlite",
export_dir / "supply",
DatabaseType.Supply,
signal=None,
overwrite=False,
jumpstart=True,
)
# Demand
create_db_from_csv(
export_dir / "Grid-demand_new.sqlite", export_dir / "demand", DatabaseType.Demand, signal=None, overwrite=False
)
# replace files into the model folder
shutil.copy(export_dir / "Grid-Supply_new.sqlite", project.supply_file)
shutil.copy(export_dir / "Grid-demand_new.sqlite", project.demand_file)