Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove id column #94

Merged
merged 7 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions custom_components/ltss/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ 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!"
)
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!")
freol35241 marked this conversation as resolved.
Show resolved Hide resolved


def migrate_attributes_text_to_jsonb(engine):
with engine.connect() as con:
Expand Down
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)
freol35241 marked this conversation as resolved.
Show resolved Hide resolved
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