├── project ├── poll │ ├── __init__.py │ ├── tests │ │ ├── __init__.py │ │ └── test_views.py │ ├── migrations │ │ ├── __init__.py │ │ └── 0001_initial.py │ ├── admin.py │ ├── apps.py │ ├── models.py │ ├── fixtures │ │ └── poll.json │ ├── urls.py │ └── views.py ├── project │ ├── __init__.py │ ├── wsgi.py │ ├── urls.py │ └── settings.py └── manage.py ├── requirements.txt ├── .gitattributes ├── README.md ├── .travis.yml ├── Makefile └── .gitignore /project/poll/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /project/project/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /project/poll/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /project/poll/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==2.0 2 | mysqlclient==1.3.12 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /project/poll/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /project/poll/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class PollConfig(AppConfig): 5 | name = 'poll' 6 | -------------------------------------------------------------------------------- /project/poll/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Poll(models.Model): 5 | """A poll.""" 6 | 7 | question = models.CharField(max_length=100) 8 | -------------------------------------------------------------------------------- /project/poll/fixtures/poll.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "model": "poll.poll", 3 | "pk": 1, 4 | "fields": { 5 | "id": 1, 6 | "question": "Is this a test?" 7 | } 8 | }] 9 | -------------------------------------------------------------------------------- /project/poll/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from .views import poll_list 4 | 5 | app_name = 'polls' 6 | urlpatterns = [ 7 | path('', poll_list, name='list'), 8 | ] 9 | -------------------------------------------------------------------------------- /project/poll/views.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | 3 | from .models import Poll 4 | 5 | 6 | def poll_list(request): 7 | """Return a list of polls.""" 8 | data = [] 9 | for poll in Poll.objects.all(): 10 | data.append({'id': poll.id, 'question': poll.question}) 11 | return JsonResponse(data, safe=False) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # travis-ci-persistent-db 2 | 3 | Maintain a persistent database per branch on Travis CI builds. 4 | Use `[reset mysql]` in a commit message to reset database. 5 | 6 | ### Pull Request Showing Difference 7 | 8 | https://github.com/michaelhelmick/travis-ci-persistent-db/pull/1 9 | 10 | ### Blog Post 11 | 12 | https://medium.com/@mikehelmick/persistent-databases-on-travis-ci-19ca6f968d50 13 | -------------------------------------------------------------------------------- /project/project/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for project project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /project/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError as exc: 10 | raise ImportError( 11 | "Couldn't import Django. Are you sure it's installed and " 12 | "available on your PYTHONPATH environment variable? Did you " 13 | "forget to activate a virtual environment?" 14 | ) from exc 15 | execute_from_command_line(sys.argv) 16 | -------------------------------------------------------------------------------- /project/poll/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.0 on 2018-02-05 19:17 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Poll', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('question', models.CharField(max_length=100)), 19 | ], 20 | ), 21 | ] 22 | -------------------------------------------------------------------------------- /project/poll/tests/test_views.py: -------------------------------------------------------------------------------- 1 | from django.test import Client, TestCase 2 | from django.urls import reverse 3 | 4 | from poll.models import Poll 5 | 6 | 7 | class PollViewTestCase(TestCase): 8 | """Test poll views.""" 9 | 10 | fixtures = ['poll.json'] 11 | 12 | def setUp(self): 13 | """Set up.""" 14 | self.client = Client() 15 | 16 | def test_poll_list(self): 17 | """Test poll_list returns correct results.""" 18 | response = self.client.get( 19 | reverse('polls:list') 20 | ) 21 | 22 | self.assertEqual(response.status_code, 200) 23 | self.assertEqual(len(response.json()), 1) 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: python 3 | cache: 4 | directories: 5 | - pip 6 | before_install: 7 | - make travis-before-install-$TEST_SUITE 8 | install: 9 | - make travis-install-$TEST_SUITE 10 | before_script: 11 | - make travis-before-script-$TEST_SUITE 12 | script: 13 | - make travis-$SCRIPT-$TEST_SUITE 14 | jobs: 15 | include: 16 | - stage: test 17 | python: 3.6 18 | env: 19 | - TEST_SUITE=backend 20 | - SCRIPT=test 21 | services: 22 | - mysql 23 | after_success: 24 | - mysqldump --user='root' --password='' test_project > ~/$TRAVIS_BRANCH/databases/db.sql 25 | - aws s3 sync ~/$TRAVIS_BRANCH s3://org-travis-ci/$TRAVIS_BRANCH 26 | notifications: 27 | email: false 28 | -------------------------------------------------------------------------------- /project/project/urls.py: -------------------------------------------------------------------------------- 1 | """project URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import include, path 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('polls/', include('poll.urls', namespace='polls')) 22 | ] 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | travis-before-install-backend: 2 | sudo rm -f /etc/boto.cfg 3 | 4 | travis-install-backend: 5 | pip install awscli 6 | pip install -r ./requirements.txt 7 | 8 | travis-before-script-backend: 9 | mkdir -p ~/$(TRAVIS_BRANCH)/databases/ 10 | touch ~/$(TRAVIS_BRANCH)/databases/db.sql 11 | aws s3 sync s3://org-travis-ci/$(TRAVIS_BRANCH) ~/$(TRAVIS_BRANCH) 12 | mysql --user='root' --password='' -e 'CREATE DATABASE IF NOT EXISTS test_project;' 13 | @case $$TRAVIS_COMMIT_MESSAGE in *"[reset mysql]"*) \ 14 | echo 'Resetting MySQL database...'; \ 15 | aws s3 rm s3://org-travis-ci/$(TRAVIS_BRANCH)/databases/db.sql; \ 16 | ;; *) \ 17 | echo 'Loading previous MySQL database...'; \ 18 | mysql --user='root' --password='' test_project < ~/$(TRAVIS_BRANCH)/databases/db.sql; \ 19 | esac 20 | 21 | travis-test-backend: 22 | cd project; python manage.py test --keepdb --failfast -v 3 project 23 | 24 | travis-noop: 25 | @echo "Move along..." 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *.cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # Jupyter Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # SageMath parsed files 79 | *.sage.py 80 | 81 | # Environments 82 | .env 83 | .venv 84 | env/ 85 | venv/ 86 | ENV/ 87 | 88 | # Spyder project settings 89 | .spyderproject 90 | .spyproject 91 | 92 | # Rope project settings 93 | .ropeproject 94 | 95 | # mkdocs documentation 96 | /site 97 | 98 | # mypy 99 | .mypy_cache/ 100 | -------------------------------------------------------------------------------- /project/project/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for project project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.0. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.0/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'xi&*bi!_5a$v!$cua=3sj4go%1emv_&=md4c2rz#jw(85y7lky' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'poll', 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'project.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'project.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/2.0/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.mysql', 80 | 'NAME': 'project', 81 | 'USER': 'root', 82 | 'PASSWORD': '', 83 | 'HOST': '127.0.0.1', 84 | 'PORT': 3306, 85 | } 86 | } 87 | 88 | 89 | # Password validation 90 | # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators 91 | 92 | AUTH_PASSWORD_VALIDATORS = [ 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 101 | }, 102 | { 103 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 104 | }, 105 | ] 106 | 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/2.0/topics/i18n/ 110 | 111 | LANGUAGE_CODE = 'en-us' 112 | 113 | TIME_ZONE = 'UTC' 114 | 115 | USE_I18N = True 116 | 117 | USE_L10N = True 118 | 119 | USE_TZ = True 120 | 121 | 122 | # Static files (CSS, JavaScript, Images) 123 | # https://docs.djangoproject.com/en/2.0/howto/static-files/ 124 | 125 | STATIC_URL = '/static/' 126 | 127 | TEST_RUNNER = 'django.test.runner.DiscoverRunner' 128 | --------------------------------------------------------------------------------