-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aad7f63
commit 7c37407
Showing
15 changed files
with
700 additions
and
246 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
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
58 changes: 58 additions & 0 deletions
58
.../database/migrations/versions/postgresql/2024_05_21_101457_94622c1663e8_json_variables.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,58 @@ | ||
"""json_variables | ||
Revision ID: 94622c1663e8 | ||
Revises: b23c83a12cb4 | ||
Create Date: 2024-05-21 10:14:57.246286 | ||
""" | ||
import json | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
from prefect.server.utilities.database import JSON | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "94622c1663e8" | ||
down_revision = "b23c83a12cb4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column("variable", sa.Column("json_value", JSON, nullable=True)) | ||
|
||
conn = op.get_bind() | ||
|
||
result = conn.execute(sa.text("SELECT id, value FROM variable")) | ||
rows = result.fetchall() | ||
|
||
for variable_id, value in rows: | ||
# these values need to be json compatible strings | ||
json_value = json.dumps(value) | ||
conn.execute( | ||
sa.text("UPDATE variable SET json_value = :json_value WHERE id = :id"), | ||
{"json_value": json_value, "id": variable_id}, | ||
) | ||
|
||
op.drop_column("variable", "value") | ||
op.alter_column("variable", "json_value", new_column_name="value") | ||
|
||
|
||
def downgrade(): | ||
op.add_column("variable", sa.Column("string_value", sa.String, nullable=True)) | ||
|
||
conn = op.get_bind() | ||
|
||
result = conn.execute(sa.text("SELECT id, value FROM variable")) | ||
rows = result.fetchall() | ||
|
||
for variable_id, value in rows: | ||
string_value = str(value) | ||
conn.execute( | ||
sa.text("UPDATE variable SET string_value = :string_value WHERE id = :id"), | ||
{"string_value": string_value, "id": variable_id}, | ||
) | ||
|
||
op.drop_column("variable", "value") | ||
op.alter_column("variable", "string_value", new_column_name="value", nullable=False) |
60 changes: 60 additions & 0 deletions
60
...rver/database/migrations/versions/sqlite/2024_05_21_123101_2ac65f1758c2_json_variables.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,60 @@ | ||
"""json_variables | ||
Revision ID: 2ac65f1758c2 | ||
Revises: 20fbd53b3cef | ||
Create Date: 2024-05-21 12:29:43.948758 | ||
""" | ||
import json | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
from prefect.server.utilities.database import JSON | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "2ac65f1758c2" | ||
down_revision = "20fbd53b3cef" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column("variable", sa.Column("json_value", JSON, nullable=True)) | ||
|
||
conn = op.get_bind() | ||
|
||
result = conn.execute(sa.text("SELECT id, value FROM variable")) | ||
rows = result.fetchall() | ||
|
||
for variable_id, value in rows: | ||
# these values need to be json compatible strings | ||
json_value = json.dumps(value) | ||
conn.execute( | ||
sa.text("UPDATE variable SET json_value = :json_value WHERE id = :id"), | ||
{"json_value": json_value, "id": variable_id}, | ||
) | ||
|
||
with op.batch_alter_table("variable") as batch_op: | ||
batch_op.drop_column("value") | ||
batch_op.alter_column("json_value", new_column_name="value") | ||
|
||
|
||
def downgrade(): | ||
op.add_column("variable", sa.Column("string_value", sa.String, nullable=True)) | ||
|
||
conn = op.get_bind() | ||
|
||
result = conn.execute(sa.text("SELECT id, value FROM variable")) | ||
rows = result.fetchall() | ||
|
||
for variable_id, value in rows: | ||
string_value = json.loads(str(value)) | ||
conn.execute( | ||
sa.text("UPDATE variable SET string_value = :string_value WHERE id = :id"), | ||
{"string_value": string_value, "id": variable_id}, | ||
) | ||
|
||
with op.batch_alter_table("variable") as batch_op: | ||
batch_op.drop_column("value") | ||
batch_op.alter_column("string_value", new_column_name="value", nullable=False) |
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
Oops, something went wrong.