-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the withings attributes from the
users
table to the `withings_…
…users` table. In the migration file, add code to migrate the data, in addition to the generated code to migrate the schema. Update code to read from the different objects: `User` and `WithingsUser`.
- Loading branch information
Showing
6 changed files
with
221 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
alembic/versions/2023_05_13_1308-897c349bb45f_move_withings_data_to_new_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
"""move withings data to new table | ||
Revision ID: 897c349bb45f | ||
Revises: d1a96aed325a | ||
Create Date: 2023-05-13 13:08:11.083878 | ||
""" | ||
from datetime import datetime | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "897c349bb45f" | ||
down_revision = "d1a96aed325a" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def _str_to_datetime(input: str) -> datetime: | ||
return datetime.strptime(input, "%Y-%m-%d %H:%M:%S.%f") | ||
|
||
|
||
def _fetch_old_user_data() -> dict: | ||
return [ | ||
{ | ||
**row._asdict(), | ||
"oauth_expiration_date": _str_to_datetime(row.oauth_expiration_date), | ||
} | ||
for row in op.get_bind() | ||
.execute( | ||
sa.select( | ||
sa.table("users"), | ||
sa.text( | ||
",".join( | ||
[ | ||
"id as user_id", | ||
"oauth_userid", | ||
"oauth_access_token", | ||
"oauth_refresh_token", | ||
"oauth_expiration_date", | ||
] | ||
) | ||
), | ||
) | ||
) | ||
.all() | ||
] | ||
|
||
|
||
def _fetch_new_user_data(): | ||
return [ | ||
{ | ||
**row._asdict(), | ||
"oauth_expiration_date": _str_to_datetime(row.oauth_expiration_date), | ||
} | ||
for row in op.get_bind() | ||
.execute( | ||
sa.select( | ||
sa.table("withings_users"), | ||
sa.text( | ||
",".join( | ||
[ | ||
"user_id", | ||
"oauth_userid", | ||
"oauth_access_token", | ||
"oauth_refresh_token", | ||
"oauth_expiration_date", | ||
] | ||
) | ||
), | ||
) | ||
) | ||
.all() | ||
] | ||
|
||
|
||
def upgrade() -> None: | ||
old_user_data = _fetch_old_user_data() | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
withings_users_table = op.create_table( | ||
"withings_users", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("user_id", sa.Integer(), nullable=False), | ||
sa.Column("oauth_access_token", sa.String(length=40), nullable=True), | ||
sa.Column("oauth_refresh_token", sa.String(length=40), nullable=True), | ||
sa.Column("oauth_userid", sa.String(length=40), nullable=False), | ||
sa.Column("oauth_expiration_date", sa.DateTime(), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("user_id"), | ||
) | ||
with op.batch_alter_table("withings_users", schema=None) as batch_op: | ||
batch_op.create_index(batch_op.f("ix_withings_users_id"), ["id"], unique=False) | ||
|
||
with op.batch_alter_table("users", schema=None) as batch_op: | ||
batch_op.drop_column("oauth_userid") | ||
batch_op.drop_column("oauth_access_token") | ||
batch_op.drop_column("oauth_expiration_date") | ||
batch_op.drop_column("oauth_refresh_token") | ||
|
||
# ### end Alembic commands ### | ||
|
||
op.bulk_insert(table=withings_users_table, rows=old_user_data) | ||
|
||
|
||
def downgrade() -> None: | ||
new_user_data = _fetch_new_user_data() | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("users", schema=None) as batch_op: | ||
batch_op.add_column( | ||
sa.Column("oauth_refresh_token", sa.VARCHAR(length=40), nullable=True) | ||
) | ||
batch_op.add_column( | ||
sa.Column("oauth_expiration_date", sa.DATETIME(), nullable=True) | ||
) | ||
batch_op.add_column( | ||
sa.Column("oauth_access_token", sa.VARCHAR(length=40), nullable=True) | ||
) | ||
batch_op.add_column( | ||
sa.Column("oauth_userid", sa.VARCHAR(length=40), nullable=True) | ||
) | ||
|
||
users = sa.table( | ||
"users", | ||
sa.column("id"), | ||
sa.column("oauth_userid"), | ||
sa.column("oauth_access_token"), | ||
sa.column("oauth_refresh_token"), | ||
sa.column("oauth_expiration_date"), | ||
) | ||
for data in new_user_data: | ||
op.get_bind().execute( | ||
sa.update(users) | ||
.where(users.c.id == data["user_id"]) | ||
.values({k: v for k, v in data.items() if k != "user_id"}) | ||
) | ||
|
||
with op.batch_alter_table("users", schema=None) as batch_op: | ||
batch_op.alter_column("oauth_userid", nullable=False) | ||
|
||
with op.batch_alter_table("withings_users", schema=None) as batch_op: | ||
batch_op.drop_index(batch_op.f("ix_withings_users_id")) | ||
|
||
op.drop_table("withings_users") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters