fix: Improve support for older PostgreSQL versions

Functions like `JSON_ARRAY` and `JSON_OBJECT` have been introduced in
PostgreSQL 16 [1]. Older PostgreSQL versions were failing to run the
migrations due to the usage of these functions.

This change uses `jsonb_build_array` and `jsonb_build_object` functions
in PostgreSQL, which have been available since PostgreSQL 9.5 [2].

[1]: https://www.postgresql.org/docs/release/16.0/
[2]: https://www.postgresql.org/docs/release/9.5.0/#AEN132010
This commit is contained in:
Michael Manganiello
2025-01-18 19:50:35 -03:00
parent 07c78836d0
commit fb622ff990
5 changed files with 41 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ Create Date: 2023-09-12 18:18:27.158732
import sqlalchemy as sa
from alembic import op
from sqlalchemy.exc import OperationalError
from utils.database import CustomJSON
from utils.database import CustomJSON, is_postgresql
# revision identifiers, used by Alembic.
revision = "0009_models_refactor"
@@ -21,6 +21,10 @@ depends_on = None
def upgrade() -> None:
connection = op.get_bind()
json_array_build_func = (
"jsonb_build_array()" if is_postgresql(connection) else "JSON_ARRAY()"
)
try:
with op.batch_alter_table("platforms", schema=None) as batch_op:
batch_op.alter_column(
@@ -87,13 +91,13 @@ def upgrade() -> None:
"url_screenshots",
existing_type=CustomJSON(),
nullable=True,
existing_server_default=sa.text("(JSON_ARRAY())"),
existing_server_default=sa.text(f"({json_array_build_func})"),
)
batch_op.alter_column(
"path_screenshots",
existing_type=CustomJSON(),
nullable=True,
existing_server_default=sa.text("(JSON_ARRAY())"),
existing_server_default=sa.text(f"({json_array_build_func})"),
)
try:
@@ -108,6 +112,10 @@ def upgrade() -> None:
def downgrade() -> None:
connection = op.get_bind()
json_array_build_func = (
"jsonb_build_array()" if is_postgresql(connection) else "JSON_ARRAY()"
)
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.alter_column(
"igdb_id",
@@ -136,13 +144,13 @@ def downgrade() -> None:
"path_screenshots",
existing_type=CustomJSON(),
nullable=False,
existing_server_default=sa.text("(JSON_ARRAY())"),
existing_server_default=sa.text(f"({json_array_build_func})"),
)
batch_op.alter_column(
"url_screenshots",
existing_type=CustomJSON(),
nullable=False,
existing_server_default=sa.text("(JSON_ARRAY())"),
existing_server_default=sa.text(f"({json_array_build_func})"),
)
batch_op.alter_column(
"file_size_units", existing_type=sa.VARCHAR(length=10), nullable=True

View File

@@ -18,19 +18,22 @@ depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.add_column(sa.Column("regions", CustomJSON(), nullable=True))
batch_op.add_column(sa.Column("languages", CustomJSON(), nullable=True))
with op.batch_alter_table("roms", schema=None) as batch_op:
# Set default values for languages and regions
batch_op.execute("UPDATE roms SET languages = JSON_ARRAY()")
batch_op.execute("UPDATE roms SET regions = JSON_ARRAY(region)")
if is_postgresql(connection):
batch_op.execute("UPDATE roms SET languages = jsonb_build_array()")
batch_op.execute("UPDATE roms SET regions = jsonb_build_array(region)")
else:
batch_op.execute("UPDATE roms SET languages = JSON_ARRAY()")
batch_op.execute("UPDATE roms SET regions = JSON_ARRAY(region)")
batch_op.drop_column("region")
# ### end Alembic commands ###
def downgrade() -> None:
connection = op.get_bind()

View File

@@ -191,7 +191,10 @@ def upgrade() -> None:
# Move data around
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.execute("update roms set igdb_metadata = JSON_OBJECT()")
if is_postgresql(connection):
batch_op.execute("update roms set igdb_metadata = jsonb_build_object()")
else:
batch_op.execute("update roms set igdb_metadata = JSON_OBJECT()")
batch_op.execute(
"update roms set path_cover_s = '', path_cover_l = '', url_cover = '' where url_cover = 'https://images.igdb.com/igdb/image/upload/t_cover_big/nocover.png'"
)

View File

@@ -8,7 +8,7 @@ Create Date: 2024-02-13 17:57:25.936825
import sqlalchemy as sa
from alembic import op
from utils.database import CustomJSON
from utils.database import CustomJSON, is_postgresql
# revision identifiers, used by Alembic.
revision = "0015_mobygames_data"
@@ -18,7 +18,8 @@ depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()
with op.batch_alter_table("platforms", schema=None) as batch_op:
batch_op.add_column(sa.Column("moby_id", sa.Integer(), nullable=True))
@@ -27,9 +28,10 @@ def upgrade() -> None:
batch_op.add_column(sa.Column("moby_metadata", CustomJSON(), nullable=True))
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.execute("update roms set moby_metadata = JSON_OBJECT()")
# ### end Alembic commands ###
if is_postgresql(connection):
batch_op.execute("update roms set moby_metadata = jsonb_build_object()")
else:
batch_op.execute("update roms set moby_metadata = JSON_OBJECT()")
def downgrade() -> None:

View File

@@ -8,7 +8,7 @@ Create Date: 2023-04-17 12:03:19.163501
import sqlalchemy as sa
from alembic import op
from utils.database import CustomJSON
from utils.database import CustomJSON, is_postgresql
# revision identifiers, used by Alembic.
revision = "1.8.1"
@@ -18,14 +18,19 @@ depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()
json_array_build_func = (
"jsonb_build_array()" if is_postgresql(connection) else "JSON_ARRAY()"
)
with op.batch_alter_table("roms") as batch_op:
batch_op.add_column(
sa.Column(
"url_screenshots",
CustomJSON(),
nullable=False,
server_default=sa.text("(JSON_ARRAY())"),
server_default=sa.text(f"({json_array_build_func})"),
)
)
batch_op.add_column(
@@ -33,10 +38,9 @@ def upgrade() -> None:
"path_screenshots",
CustomJSON(),
nullable=False,
server_default=sa.text("(JSON_ARRAY())"),
server_default=sa.text(f"({json_array_build_func})"),
)
)
# ### end Alembic commands ###
def downgrade() -> None: