Skip to content

Commit aeb1f95

Browse files
committed
remove id column + 7 day default
1 parent 2f72a82 commit aeb1f95

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
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

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ configuration.yaml
4545

4646
ltss:
4747
db_url: postgresql://USER:PASSWORD@HOST_ADRESS/DB_NAME
48-
chunk_time_interval: 2592000000000
48+
chunk_time_interval: 604800000000
4949
include:
5050
domains:
5151
- sensor
@@ -66,7 +66,7 @@ configuration.yaml
6666

6767
chunk_time_interval
6868
(int)(Optional)
69-
The time interval to be used for chunking in TimescaleDB in microseconds. Defaults to 2592000000000 (30 days). Ignored for databases without TimescaleDB extension.
69+
The time interval to be used for chunking in TimescaleDB in microseconds. Defaults to 604800000000 (7 days). Ignored for databases without TimescaleDB extension.
7070

7171
exclude
7272
(map)(Optional)
@@ -103,14 +103,14 @@ 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:
113-
[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).
113+
[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 604800000000 microseconds (7 days).
114114

115115
### Only available with PosttGIS:
116116
The location column is populated for those states where ```latitude``` and ```longitude``` is part of the state attributes.

custom_components/ltss/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
{
5858
vol.Required(CONF_DB_URL): cv.string,
5959
vol.Optional(
60-
CONF_CHUNK_TIME_INTERVAL, default=2592000000000
60+
CONF_CHUNK_TIME_INTERVAL, default=604800000000
6161
): cv.positive_int, # 30 days
6262
}
6363
)

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ 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+
with engine.begin() as con:
55+
con.execute(text(
56+
f"""ALTER TABLE {LTSS.__tablename__}
57+
DROP CONSTRAINT {LTSS.__tablename__}_pkey CASCADE,
58+
ADD PRIMARY KEY(time,entity_id);"""
59+
)
60+
)
61+
con.execute(text(
62+
f"""ALTER TABLE {LTSS.__tablename__}
63+
DROP COLUMN id"""
64+
)
65+
)
66+
con.commit()
67+
_LOGGER.info('Migration completed successfully!')
4968

5069
def migrate_attributes_text_to_jsonb(engine):
5170
with engine.connect() as con:
@@ -74,4 +93,4 @@ def drop_entityid_index(engine):
7493
text(f"""DROP INDEX ix_ltss_entity_id;""").execution_options(
7594
autocommit=True
7695
)
77-
)
96+
)

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

0 commit comments

Comments
 (0)