Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Chore/db logs #54

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- image: circleci/postgres:12
environment:
POSTGRES_USER: circleci
POSTGRES_DB: circle_test
POSTGRES_DB: "circle_test"
POSTGRES_HOST_AUTH_METHOD: trust

working_directory: ~/repo
Expand Down
9 changes: 5 additions & 4 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ djangocms-admin-style = "==1.4.0"
inflection = "==0.3.1"
ipython = "==7.6.1"
pypugjs = "==5.8.1"
pytz = "==2020.1"
pytz = ">=2020.1"
python-memcached = "==1.59"
gunicorn = "==19.9.0"
djangorestframework = "==3.9.1"
"psycopg2-binary" = "==2.8.6"
djangorestframework = "~=3.11.0"
djangorestframework-camel-case = "==1.1.2"
django-webpack-loader = "==0.7.0"
psycopg2-binary = "==2.8.5"
ipdb = "==0.13.3"
ipdb = "==0.13.4"
django-db-logger = "==0.1.10"

[dev-packages]
flake8 = "*"
Expand Down
121 changes: 66 additions & 55 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 28 additions & 5 deletions ansible/playbooks/roles/quickstart/tasks/db.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
- name: set database name in local_settings
- name: set databases name in local_settings
loop:
- { placeholder: 'database-name', database: "{{ project_name }}" }
- { placeholder: 'logs-database-name', database: "{{ database_for_logs }}" }
replace:
path: "{{ server_root_dir }}/project/local_settings.py"
regexp: " 'NAME': 'database-name',"
replace: " 'NAME': '{{ project_name }}',"
regexp: " 'NAME': '{{ item.placeholder }}',"
replace: " 'NAME': '{{ item.database }}',"
validate: bash -c "cd {{ server_root_dir }} && pipenv run python %s" # It returns 1 on "invalid syntax"

- name: install and configure Postgres and create DB
Expand Down Expand Up @@ -64,9 +67,12 @@
- dev_psql_existence.rc == 0
- ansible_distribution == 'Ubuntu'

- name: create database
- name: create databases
loop:
- "{{ project_name }}"
- "{{ database_for_logs }}"
postgresql_db:
name: "{{ project_name }}"
name: "{{ item }}"
state: present
login_user: "{{ ansible_user_id }}"

Expand All @@ -82,9 +88,26 @@
changed_when: "' already exists' not in (osx_createdb.stderr | default(''))"
when: ansible_distribution == 'MacOSX'

name: create logs database with a hack on OSX
command: createdb {{ database_for_logs }}
register: osx_createdb
failed_when:
- osx_createdb is failed
- "' already exists' not in (osx_createdb.stderr | default(''))"
changed_when: "' already exists' not in (osx_createdb.stderr | default(''))"
when: ansible_distribution == 'MacOSX'

- name: run migrations
django_manage:
command: migrate
app_path: "{{ server_root_dir }}"
virtualenv: "{{ venv.stdout }}"
tags: update

- name: run logs migrations
django_manage:
command: migrate
database: logs
app_path: "{{ server_root_dir }}"
virtualenv: "{{ venv.stdout }}"
tags: update
1 change: 1 addition & 0 deletions ansible/playbooks/roles/quickstart/vars/main.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
path_for_node: "{{ interactive_path | default(omit) }}:{{ ansible_env.PATH }}" # messy paths...
no_venv_message: No virtualenv has been created for this project
database_for_logs: "{{ project_name }}-logs"
58 changes: 58 additions & 0 deletions base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
from base.utils import random_string
from base.mockups import Mockup

from project.routers import DatabaseByAppRouter


class BaseTestCase(TestCase, Mockup):
multi_db = True

def setUp(self):
super(BaseTestCase, self).setUp()
Expand Down Expand Up @@ -232,3 +235,58 @@ def test_cron_classes_to_run(self):
cron_class_names = getattr(settings, 'CRON_CLASSES', [])
for cron_class_name in cron_class_names:
assert get_class(cron_class_name)


class DatabaseByAppRouterTest(BaseTestCase):

def test_validate_database_by_app_router(self):
database_by_app = DatabaseByAppRouter()

# custom methods
self.assertEqual(
database_by_app.get_database_name(
app_label=self.user._meta.app_label,
model_name=self.user._meta.model_name
),
database_by_app.DEFAULT_DATABASE
)
self.assertEqual(
database_by_app.get_database_name_from_model(
model=self.user._meta.model
),
database_by_app.DEFAULT_DATABASE
)

# django methods
self.assertEqual(
database_by_app.db_for_read(
model=self.user._meta.model
),
database_by_app.DEFAULT_DATABASE
)
self.assertEqual(
database_by_app.db_for_write(
model=self.user._meta.model
),
database_by_app.DEFAULT_DATABASE
)
self.assertTrue(
database_by_app.allow_relation(
obj1=self.user,
obj2=self.user,
)
)
self.assertTrue(
database_by_app.allow_migrate(
db=database_by_app.DEFAULT_DATABASE,
app_label=self.user._meta.app_label,
model_name=self.user._meta.model_name
)
)
self.assertFalse(
database_by_app.allow_migrate(
db='logs',
app_label=self.user._meta.app_label,
model_name=self.user._meta.model_name
)
)
8 changes: 8 additions & 0 deletions project/circleci_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
},
'logs': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'circle_test_logs',
'USER': 'circleci',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}

Expand Down
Loading