Skip to content

Commit

Permalink
Merge pull request #1504 from rommapp/fix/improve-support-older-postg…
Browse files Browse the repository at this point in the history
…res-versions

fix: Improve support for older PostgreSQL versions
  • Loading branch information
adamantike authored Jan 19, 2025
2 parents 07c7883 + fb622ff commit 19c4c61
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
18 changes: 13 additions & 5 deletions backend/alembic/versions/0009_models_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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"
Expand All @@ -21,6 +21,10 @@
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(
Expand Down Expand Up @@ -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:
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions backend/alembic/versions/0012_add_regions_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@


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()
Expand Down
5 changes: 4 additions & 1 deletion backend/alembic/versions/0014_asset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
)
Expand Down
12 changes: 7 additions & 5 deletions backend/alembic/versions/0015_mobygames_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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"
Expand All @@ -18,7 +18,8 @@


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))

Expand All @@ -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:
Expand Down
14 changes: 9 additions & 5 deletions backend/alembic/versions/1.8.1_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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"
Expand All @@ -18,25 +18,29 @@


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(
sa.Column(
"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:
Expand Down

0 comments on commit 19c4c61

Please sign in to comment.