Skip to content

Commit

Permalink
Updating codebase for breaking changes introduced by bumping the sqla…
Browse files Browse the repository at this point in the history
…lchemy version
  • Loading branch information
freol35241 committed Feb 25, 2023
1 parent 78c4a6a commit 301e86c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Long time state storage (LTSS) custom component for Home Assistant
========================================

**NOTE:** From version 2.0 LTSS requires at least Home Assistant 2023.3

**NOTE:** Starting 2020-09-13 attributes are stored with type JSONB instead of as a plain string, in addition a GIN index is created on this column by default. At first startup after updating of LTSS, migration of your DB happens automatically. Note that this can take a couple of minutes and HASS will not finish starting (i.e. frontend will not be available) until migration is done.

**WARNING:** I take no responsibility for any data loss that may happen as a result of this. Please make sure to backup your data before upgrading!
Expand Down
11 changes: 6 additions & 5 deletions custom_components/ltss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ def _setup_connection(self):
inspector = inspect(self.engine)

with self.engine.connect() as con:
available_extensions = {row['name']: row['installed_version'] for row in
con = con.execution_options(isolation_level="AUTOCOMMIT")
available_extensions = {row.name: row.installed_version for row in
con.execute(text("SELECT name, installed_version FROM pg_available_extensions"))}

# create table if necessary
Expand All @@ -291,7 +292,6 @@ def _setup_connection(self):
try:
con.execute(
text(f"SELECT set_chunk_time_interval('{LTSS.__tablename__}', {self.chunk_time_interval})")
.execution_options(autocommit=True)
)
except exc.ProgrammingError as exception:
if isinstance(exception.orig, psycopg2.errors.UndefinedTable):
Expand All @@ -314,11 +314,12 @@ def _setup_connection(self):
def _create_table(self, available_extensions):
_LOGGER.info("Creating LTSS table")
with self.engine.connect() as con:
con = con.execution_options(isolation_level="AUTOCOMMIT")
if 'postgis' in available_extensions:
_LOGGER.info("PostGIS extension is available, activating location extraction...")
con.execute(
text("CREATE EXTENSION IF NOT EXISTS postgis CASCADE"
).execution_options(autocommit=True))
))

# activate location extraction in model/ORM to add necessary column when calling create_all()
LTSS.activate_location_extraction()
Expand All @@ -329,13 +330,13 @@ def _create_table(self, available_extensions):
_LOGGER.info("TimescaleDB extension is available, creating hypertable...")
con.execute(
text("CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE"
).execution_options(autocommit=True))
))

# Create hypertable
con.execute(text(f"""SELECT create_hypertable(
'{LTSS.__tablename__}',
'time',
if_not_exists => TRUE);""").execution_options(autocommit=True))
if_not_exists => TRUE);"""))

def _close_connection(self):
"""Close the connection."""
Expand Down
28 changes: 14 additions & 14 deletions custom_components/ltss/manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"domain": "ltss",
"version": "1.1.0",
"name": "Long Time State Storage (LTSS)",
"documentation": "https://github.com/freol35241/ltss",
"requirements": [
"sqlalchemy>=1.0,<2.0",
"psycopg2>=2.8,<3.0",
"geoalchemy2>=0.8,<0.9"
],
"dependencies": [],
"codeowners": [
"@freol35241"
]
}
"domain": "ltss",
"version": "2.0.0",
"name": "Long Time State Storage (LTSS)",
"documentation": "https://github.com/freol35241/ltss",
"requirements": [
"sqlalchemy>=2.0,<3.0",
"psycopg2>=2.8,<3.0",
"geoalchemy2>=0.8,<1.0"
],
"dependencies": [],
"codeowners": [
"@freol35241"
]
}
3 changes: 1 addition & 2 deletions custom_components/ltss/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from sqlalchemy.schema import Index
from sqlalchemy.dialects.postgresql import JSONB
from geoalchemy2 import Geometry
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import column_property
from sqlalchemy.orm import column_property, declarative_base

# SQLAlchemy Schema
# pylint: disable=invalid-name
Expand Down
2 changes: 2 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**NOTE:** From version 2.0 LTSS requires at least Home Assistant 2023.3

**NOTE:** Starting 2020-09-13 attributes are stored with type JSONB instead of as a plain string, in addition a GIN index is created on this column by default. At first startup after updating of LTSS, migration of your DB happens automatically. Note that this can take a couple of minutes and HASS will not finish starting (i.e. frontend will not be available) until migration is done.

**WARNING:** I take no responsibility for any data loss that may happen as a result of this. Please make sure to backup your data before upgrading!
Expand Down
5 changes: 3 additions & 2 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
docker==5.0.3
GeoAlchemy2==0.11.1
homeassistant==2021.9.7
psycopg2==2.9.3
SQLAlchemy==2.0.4
homeassistant==2021.9.7

pytest-docker==0.11.0
pytest==7.1.1
SQLAlchemy==1.4.32
6 changes: 3 additions & 3 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def test_default_db(self):

@staticmethod
def _is_hypertable(con):
timescaledb_version = con.execute("SELECT installed_version "
timescaledb_version = con.execute(text("SELECT installed_version "
"FROM pg_available_extensions "
"WHERE name = 'timescaledb'").scalar()
"WHERE name = 'timescaledb'")).scalar()

# timescaledb's table/column name changed with v2
if int(timescaledb_version.split('.')[0]) >= 2:
Expand All @@ -100,7 +100,7 @@ def _is_hypertable(con):
query = f"SELECT 1 FROM timescaledb_information.hypertable "
f"WHERE table_name = '{LTSS.__tablename__}'"

return 1 == con.execute(query).rowcount
return 1 == con.execute(text(query)).rowcount

@staticmethod
def _has_columns(con):
Expand Down

0 comments on commit 301e86c

Please sign in to comment.