Skip to content

Commit

Permalink
Merge pull request #2705 from smilerz/move_sqlite
Browse files Browse the repository at this point in the history
better support for sqlite
  • Loading branch information
smilerz authored Oct 31, 2023
2 parents 4cec643 + 8bee2e3 commit 97fc15d
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mediafiles/
\.env
staticfiles/
postgresql/
data/


/docker-compose.override.yml
Expand Down
2 changes: 1 addition & 1 deletion boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ echo "Waiting for database to be ready..."
attempt=0
max_attempts=20

if [ "${DB_ENGINE}" != 'django.db.backends.sqlite3' ]; then
if [ "${DB_ENGINE}" == 'django.db.backends.postgresql' ]; then

# POSTGRES_PASSWORD (or a valid file at POSTGRES_PASSWORD_FILE) must be set in .env file

Expand Down
2 changes: 1 addition & 1 deletion cookbook/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class RecipeAdmin(admin.ModelAdmin):
def created_by(obj):
return obj.created_by.get_user_display_name()

if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
actions = [rebuild_index]


Expand Down
2 changes: 1 addition & 1 deletion cookbook/helper/recipe_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# TODO consider creating a simpleListRecipe API that only includes minimum of recipe info and minimal filtering
class RecipeSearch():
_postgres = settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']
_postgres = settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql'

def __init__(self, request, **params):
self._request = request
Expand Down
4 changes: 2 additions & 2 deletions cookbook/management/commands/rebuildindex.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.conf import settings
from django.contrib.postgres.search import SearchVector
from django.core.management.base import BaseCommand
from django_scopes import scopes_disabled
from django.utils import translation
from django.utils.translation import gettext_lazy as _
from django_scopes import scopes_disabled

from cookbook.managers import DICTIONARY
from cookbook.models import Recipe, Step
Expand All @@ -14,7 +14,7 @@ class Command(BaseCommand):
help = _('Rebuilds full text search index on Recipe')

def handle(self, *args, **options):
if settings.DATABASES['default']['ENGINE'] not in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
if settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql':
self.stdout.write(self.style.WARNING(_('Only Postgresql databases use full text search, no index to rebuild')))

try:
Expand Down
4 changes: 2 additions & 2 deletions cookbook/migrations/0143_build_full_text_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django_scopes import scopes_disabled

from cookbook.managers import DICTIONARY
from cookbook.models import (Index, PermissionModelMixin, Recipe, Step, SearchFields)
from cookbook.models import Index, PermissionModelMixin, Recipe, SearchFields, Step


def allSearchFields():
Expand All @@ -21,7 +21,7 @@ def nameSearchField():


def set_default_search_vector(apps, schema_editor):
if settings.DATABASES['default']['ENGINE'] not in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
if settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql':
return
language = DICTIONARY.get(translation.get_language(), 'simple')
with scopes_disabled():
Expand Down
3 changes: 1 addition & 2 deletions cookbook/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
Step, Unit, UserPreference)

SQLITE = True
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
SQLITE = False


Expand Down
3 changes: 1 addition & 2 deletions cookbook/tests/api/test_api_shopping_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from cookbook.models import Food, Ingredient
from cookbook.tests.factories import MealPlanFactory, RecipeFactory, StepFactory, UserFactory

if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
from django.db.backends.postgresql.features import DatabaseFeatures
DatabaseFeatures.can_defer_constraint_checks = False

Expand Down
3 changes: 1 addition & 2 deletions cookbook/tests/other/test_recipe_full_text_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
# TODO makenow with above filters
# TODO test search food/keywords including/excluding children
LIST_URL = 'api:recipe-list'
sqlite = settings.DATABASES['default']['ENGINE'] not in [
'django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']
sqlite = settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql'


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ def get_queryset(self):
fuzzy = True

if query is not None and query not in ["''", '']:
if fuzzy and (settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']):
if fuzzy and (settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql'):
if self.request.user.is_authenticated and any(
[self.model.__name__.lower() in x for x in self.request.user.searchpreference.unaccent.values_list('field', flat=True)]):
self.queryset = self.queryset.annotate(trigram=TrigramSimilarity('name__unaccent', query))
Expand Down
29 changes: 13 additions & 16 deletions cookbook/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from django.utils.translation import gettext as _
from django_scopes import scopes_disabled

from cookbook.forms import (CommentForm, Recipe, SearchPreferenceForm, SpaceCreateForm, SpaceJoinForm, User,
UserCreateForm, UserPreference)
from cookbook.helper.permission_helper import group_required, has_group_permission, share_link_valid, switch_user_active_space
from cookbook.models import (Comment, CookLog, InviteLink, SearchFields, SearchPreference, ShareLink,
Space, ViewLog, UserSpace)
from cookbook.tables import (CookLogTable, ViewLogTable)
from cookbook.forms import (CommentForm, Recipe, SearchPreferenceForm, SpaceCreateForm,
SpaceJoinForm, User, UserCreateForm, UserPreference)
from cookbook.helper.permission_helper import (group_required, has_group_permission,
share_link_valid, switch_user_active_space)
from cookbook.models import (Comment, CookLog, InviteLink, SearchFields, SearchPreference,
ShareLink, Space, UserSpace, ViewLog)
from cookbook.tables import CookLogTable, ViewLogTable
from cookbook.version_info import VERSION_INFO
from recipes.settings import PLUGINS

Expand Down Expand Up @@ -219,10 +220,10 @@ def shopping_settings(request):
if not sp:
sp = SearchPreferenceForm(user=request.user)
fields_searched = (
len(search_form.cleaned_data['icontains'])
+ len(search_form.cleaned_data['istartswith'])
+ len(search_form.cleaned_data['trigram'])
+ len(search_form.cleaned_data['fulltext'])
len(search_form.cleaned_data['icontains'])
+ len(search_form.cleaned_data['istartswith'])
+ len(search_form.cleaned_data['trigram'])
+ len(search_form.cleaned_data['fulltext'])
)
if search_form.cleaned_data['preset'] == 'fuzzy':
sp.search = SearchPreference.SIMPLE
Expand Down Expand Up @@ -278,8 +279,7 @@ def shopping_settings(request):
search_form = SearchPreferenceForm()

# these fields require postgresql - just disable them if postgresql isn't available
if not settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
if not settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
sp.search = SearchPreference.SIMPLE
sp.trigram.clear()
sp.fulltext.clear()
Expand Down Expand Up @@ -309,10 +309,7 @@ def system(request):
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))

postgres = False if (
settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2' # noqa: E501
or settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql' # noqa: E501
) else True
postgres = settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql'

secret_key = False if os.getenv('SECRET_KEY') else True

Expand Down
6 changes: 3 additions & 3 deletions docs/install/k8s/50-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
template:
metadata:
annotations:
backup.velero.io/backup-volumes: media,static
backup.velero.io/backup-volumes: media,static
labels:
app: recipes
tier: frontend
Expand All @@ -35,7 +35,7 @@ spec:
name: recipes
key: secret-key
- name: DB_ENGINE
value: django.db.backends.postgresql_psycopg2
value: django.db.backends.postgresql
- name: POSTGRES_HOST
value: recipes-postgresql
- name: POSTGRES_PORT
Expand Down Expand Up @@ -162,7 +162,7 @@ spec:
- name: GUNICORN_MEDIA
value: "0"
- name: DB_ENGINE
value: django.db.backends.postgresql_psycopg2
value: django.db.backends.postgresql
- name: POSTGRES_HOST
value: recipes-postgresql
- name: POSTGRES_PORT
Expand Down
4 changes: 3 additions & 1 deletion recipes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,16 @@
# Load settings from env files
if os.getenv('DATABASE_URL'):
match = re.match(
r'(?P<schema>\w+):\/\/(?P<user>[\w\d_-]+)(:(?P<password>[^@]+))?@(?P<host>[^:/]+)(:(?P<port>\d+))?(\/(?P<database>[\w\d\/\._-]+))?',
r'(?P<schema>\w+):\/\/(?:(?P<user>[\w\d_-]+)(?::(?P<password>[^@]+))?@)?(?P<host>[^:/]+)(?:(?P<port>\d+))?(?:/(?P<database>[\w\d/._-]+))?',
os.getenv('DATABASE_URL')
)
settings = match.groupdict()
schema = settings['schema']
if schema.startswith('postgres'):
engine = 'django.db.backends.postgresql'
elif schema == 'sqlite':
if not os.path.exists(db_path := os.path.dirname(settings['database'])):
os.makedirs(db_path)
engine = 'django.db.backends.sqlite3'
else:
raise Exception("Unsupported database schema: '%s'" % schema)
Expand Down

0 comments on commit 97fc15d

Please sign in to comment.