Skip to content

Commit

Permalink
add support for postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
depfryer committed Nov 4, 2024
1 parent 4a7b473 commit fe34ab8
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 5 additions & 1 deletion apps/wizarr-backend/wizarr_backend/app/migrator/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 24 additions & 2 deletions apps/wizarr-backend/wizarr_backend/app/models/base.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit fe34ab8

Please sign in to comment.