From fe34ab835487c61837763e9a4f3189c6a44cfdd3 Mon Sep 17 00:00:00 2001 From: depfryer Date: Mon, 4 Nov 2024 06:35:23 +0100 Subject: [PATCH] add support for postgresql --- .../migrations/2023-10-10_21-34-32.py | 6 ++++- .../migrations/2023-10-11_20-19-17.py | 6 ++++- .../migrations/2023-10-11_20-48-19.py | 6 ++++- .../migrations/2023-10-11_23-11-06.py | 6 ++++- .../migrations/2023-10-11_23-33-47.py | 6 ++++- .../migrations/2023-10-14_21-46-16.py | 6 ++++- .../migrations/2023-10-28_19-21-23.py | 6 ++++- .../migrations/2023-10-29_16-35-41.py | 6 ++++- .../migrations/2024-04-19_18-46-37.py | 7 +++-- .../migrations/2024-04-22_19-17-37.py | 7 +++-- .../migrations/2024-04-30_16-44-17.py | 6 ++++- .../migrations/2024-05-03_22-33-10.py | 6 ++++- .../wizarr_backend/app/migrator/template.py | 6 ++++- .../wizarr_backend/app/models/base.py | 26 +++++++++++++++++-- 14 files changed, 89 insertions(+), 17 deletions(-) diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-10_21-34-32.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-10_21-34-32.py index b209e1879..061d19e19 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-10_21-34-32.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-10_21-34-32.py @@ -9,13 +9,17 @@ from datetime import datetime from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add duration column to the invitations table with a default value of null # if the column already exists, do nothing diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-19-17.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-19-17.py index 9733c4a57..55ca43ea8 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-19-17.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-19-17.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Update all rows in the invitations table that have expires columns with textual "None" to actual None diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-48-19.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-48-19.py index 6ef1f3157..8916447d1 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-48-19.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_20-48-19.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Migrate old V2 settings to new V3 settings # Only run this migration if admin_username and admin_password exist in the settings table diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-11-06.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-11-06.py index 2f4947e5b..69dd174cf 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-11-06.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-11-06.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Move libraries from the settings table to the libraries table with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-33-47.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-33-47.py index c9f402848..f087567e0 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-33-47.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-11_23-33-47.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Update all invitations libraries from there old name format to there id format with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-14_21-46-16.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-14_21-46-16.py index 62b64c48c..df675e9f1 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-14_21-46-16.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-14_21-46-16.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add new Column to users table called tutorial, its a boolean field with a default value of False with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-28_19-21-23.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-28_19-21-23.py index 01bfc894d..efbbc3641 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-28_19-21-23.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-28_19-21-23.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add columns name server_id to requests table with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-29_16-35-41.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-29_16-35-41.py index 2b2ca2c71..c72df7342 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-29_16-35-41.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2023-10-29_16-35-41.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add columns auth to users table with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-19_18-46-37.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-19_18-46-37.py index ed788cb5a..7107ed636 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-19_18-46-37.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-19_18-46-37.py @@ -8,14 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time - def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add new Column to users table called tutorial, its a boolean field with a default value of False with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-22_19-17-37.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-22_19-17-37.py index 047e200ce..ec6ea8571 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-22_19-17-37.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-22_19-17-37.py @@ -8,14 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time - def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add new Column to users table called tutorial, its a boolean field with a default value of False with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-30_16-44-17.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-30_16-44-17.py index e4e4ba696..9be664c46 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-30_16-44-17.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-04-30_16-44-17.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Add new Column to users table, its a boolean field with a default value of True with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-05-03_22-33-10.py b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-05-03_22-33-10.py index 174a86c45..a55903bf8 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-05-03_22-33-10.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/migrations/2024-05-03_22-33-10.py @@ -8,13 +8,17 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) # Remove column plex_home from invitations table with db.transaction(): diff --git a/apps/wizarr-backend/wizarr_backend/app/migrator/template.py b/apps/wizarr-backend/wizarr_backend/app/migrator/template.py index 5ff68b626..4b9126f30 100644 --- a/apps/wizarr-backend/wizarr_backend/app/migrator/template.py +++ b/apps/wizarr-backend/wizarr_backend/app/migrator/template.py @@ -8,10 +8,14 @@ from playhouse.migrate import * from app import db +from os import environ # Do not change the name of this file, # migrations are run in order of their filenames date and time def run(): # Use migrator to perform actions on the database - migrator = SqliteMigrator(db) + if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + migrator = PostgresqlMigrator(db) + else: + migrator = SqliteMigrator(db) diff --git a/apps/wizarr-backend/wizarr_backend/app/models/base.py b/apps/wizarr-backend/wizarr_backend/app/models/base.py index 858092d61..8948f8a87 100644 --- a/apps/wizarr-backend/wizarr_backend/app/models/base.py +++ b/apps/wizarr-backend/wizarr_backend/app/models/base.py @@ -1,6 +1,28 @@ -from peewee import Model, SqliteDatabase +# +# CREATED ON VERSION: V{version} +# MIGRATION: {name} +# CREATED: {date} +# -db = SqliteDatabase("./database/database.db") +from peewee import Model, SqliteDatabase, PostgresqlDatabase +from os import environ + +if environ.get('POSTGRES_ENABLED', 'false').lower() == 'true': + required_vars = ['POSTGRES_DATABASE', 'POSTGRES_HOST', 'POSTGRES_PORT', 'POSTGRES_USER', 'POSTGRES_PASS'] + + missing_vars = [var for var in required_vars if not environ.get(var)] + if missing_vars: + raise EnvironmentError(f"The following environment variables are missing : {', '.join(missing_vars)}") + + db = PostgresqlDatabase( + environ['POSTGRES_DATABASE'], + user=environ['POSTGRES_USER'], + password=environ['POSTGRES_PASS'], + host=environ['POSTGRES_HOST'], + port=int(environ['POSTGRES_PORT']) + ) +else: + db = SqliteDatabase("./database/database.db") class BaseModel(Model): class Meta: