Skip to content

Commit 5f421d2

Browse files
authored
Merge pull request #94 from nwerter/master
Remove id column
2 parents f70476b + e35e5c0 commit 5f421d2

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ dmypy.json
152152
# Cython debug symbols
153153
cython_debug/
154154

155+
#MacOS
156+
.DS_Store
157+
155158
# PyCharm
156159
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157160
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ configuration.yaml
103103
## Details
104104
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:
105105

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

112112
### Only available with TimescaleDB:
113113
[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).

custom_components/ltss/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"domain": "ltss",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"name": "Long Time State Storage (LTSS)",
55
"documentation": "https://github.com/freol35241/ltss",
66
"requirements": [

custom_components/ltss/migrations.py

+26
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def index_exists(index_name):
4646
_LOGGER.warning("Index on entity_id no longer needed, dropping...")
4747
drop_entityid_index(engine)
4848

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

5057
def migrate_attributes_text_to_jsonb(engine):
5158
with engine.connect() as con:
@@ -75,3 +82,22 @@ def drop_entityid_index(engine):
7582
autocommit=True
7683
)
7784
)
85+
86+
87+
def remove_id_column(engine):
88+
with engine.begin() as con:
89+
con.execute(
90+
text(
91+
f"""ALTER TABLE {LTSS.__tablename__}
92+
DROP CONSTRAINT {LTSS.__tablename__}_pkey CASCADE,
93+
ADD PRIMARY KEY(time,entity_id);"""
94+
)
95+
)
96+
con.execute(
97+
text(
98+
f"""ALTER TABLE {LTSS.__tablename__}
99+
DROP COLUMN id"""
100+
)
101+
)
102+
con.commit()
103+
_LOGGER.info("Migration completed successfully!")

custom_components/ltss/models.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ class LTSS(Base): # type: ignore
2727
"""State change history."""
2828

2929
__tablename__ = "ltss"
30-
id = Column(BigInteger, primary_key=True, autoincrement=True)
3130
time = Column(DateTime(timezone=True), default=datetime.utcnow, primary_key=True)
32-
entity_id = Column(String(255))
31+
entity_id = Column(String(255), primary_key=True)
3332
state = Column(String(255), index=True)
3433
attributes = Column(JSONB)
3534
location = None # when not activated, no location column will be added to the table/database

tests/pytest/test_databases.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _is_hypertable(con):
128128
@staticmethod
129129
def _has_columns(con):
130130
return (
131-
5
131+
4
132132
<= con.execute(
133133
text(
134134
f"SELECT COLUMN_NAME\

0 commit comments

Comments
 (0)