Skip to content

Commit

Permalink
Merge pull request #94 from nwerter/master
Browse files Browse the repository at this point in the history
Remove id column
  • Loading branch information
freol35241 authored Jun 11, 2023
2 parents f70476b + e35e5c0 commit 5f421d2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ dmypy.json
# Cython debug symbols
cython_debug/

#MacOS
.DS_Store

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ configuration.yaml
## Details
The states are stored in a single table ([hypertable](https://docs.timescale.com/latest/using-timescaledb/hypertables), when TimescaleDB is available) with the following layout:

| Column name: | id | time | entity_id | state | attributes | location [PostGIS-only] |
|:---|:---:|:---:|:---:|:---:|:---:|:-----------------------:|
| Type: | bigint | timestamp with timezone | string | string | JSONB | POINT(4326) |
| Column name: | time | entity_id | state | attributes | location [PostGIS-only] |
|:---:|:---:|:---:|:---:|:---:|:-----------------------:|
| Type: | timestamp with timezone | string | string | JSONB | POINT(4326) |
| Primary key: | x | x | | | |
| Index: | x | x | x | x | x | |
| Index: | x | x | x | x | |

### Only available with TimescaleDB:
[Chunk size](https://docs.timescale.com/latest/using-timescaledb/hypertables#best-practices) of the hypertable is configurable using the `chunk_time_interval` config option. It defaults to 2592000000000 microseconds (30 days).
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ltss/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "ltss",
"version": "2.0.1",
"version": "2.1.0",
"name": "Long Time State Storage (LTSS)",
"documentation": "https://github.com/freol35241/ltss",
"requirements": [
Expand Down
26 changes: 26 additions & 0 deletions custom_components/ltss/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def index_exists(index_name):
_LOGGER.warning("Index on entity_id no longer needed, dropping...")
drop_entityid_index(engine)

# id column?
if any(col["name"] == "id" for col in columns):
_LOGGER.warning(
"Migrating you LTSS table to the latest schema, this might take a couple of minutes!"
)
remove_id_column(engine)


def migrate_attributes_text_to_jsonb(engine):
with engine.connect() as con:
Expand Down Expand Up @@ -75,3 +82,22 @@ def drop_entityid_index(engine):
autocommit=True
)
)


def remove_id_column(engine):
with engine.begin() as con:
con.execute(
text(
f"""ALTER TABLE {LTSS.__tablename__}
DROP CONSTRAINT {LTSS.__tablename__}_pkey CASCADE,
ADD PRIMARY KEY(time,entity_id);"""
)
)
con.execute(
text(
f"""ALTER TABLE {LTSS.__tablename__}
DROP COLUMN id"""
)
)
con.commit()
_LOGGER.info("Migration completed successfully!")
3 changes: 1 addition & 2 deletions custom_components/ltss/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class LTSS(Base): # type: ignore
"""State change history."""

__tablename__ = "ltss"
id = Column(BigInteger, primary_key=True, autoincrement=True)
time = Column(DateTime(timezone=True), default=datetime.utcnow, primary_key=True)
entity_id = Column(String(255))
entity_id = Column(String(255), primary_key=True)
state = Column(String(255), index=True)
attributes = Column(JSONB)
location = None # when not activated, no location column will be added to the table/database
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _is_hypertable(con):
@staticmethod
def _has_columns(con):
return (
5
4
<= con.execute(
text(
f"SELECT COLUMN_NAME\
Expand Down

0 comments on commit 5f421d2

Please sign in to comment.