Add more invoke commands:

- export-records: Exports all database records to external file
- import-records: Imports database records from external file
- import-fixtures: Fills the database with dummy records
This commit is contained in:
Oliver Walters 2020-11-12 13:31:27 +11:00
parent 4a8170079e
commit ec8d8e5a64
4 changed files with 101 additions and 17 deletions

View File

@ -30,11 +30,23 @@ before_install:
script: script:
- cd InvenTree && python3 manage.py makemigrations && cd .. - cd InvenTree && python3 manage.py makemigrations && cd ..
- python3 ci/check_migration_files.py - python3 ci/check_migration_files.py
# Run unit testing / code coverage tests
- invoke coverage - invoke coverage
# Run unit test for SQL database backend
- cd InvenTree && python3 manage.py test --settings=InvenTree.ci_mysql && cd .. - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_mysql && cd ..
# Run unit test for PostgreSQL database backend
- cd InvenTree && python3 manage.py test --settings=InvenTree.ci_postgresql && cd .. - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_postgresql && cd ..
- invoke translate - invoke translate
- invoke style - invoke style
# Create an empty database and fill it with test data
- rm inventree_db.sqlite3
- invoke migrate
- invoke import-fixtures
# Export database records
- invoke export-records -f data.json
# Create a new empty database and import the saved data
- rm inventree_db.sqlite3
- invoke import-records -f data.json
after_success: after_success:
- coveralls - coveralls

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError, IntegrityError
class CommonConfig(AppConfig): class CommonConfig(AppConfig):

View File

@ -1,16 +0,0 @@
# Test fixtures for Currency objects
- model: common.currency
fields:
symbol: '$'
suffix: 'AUD'
description: 'Australian Dollars'
base: True
- model: common.currency
fields:
symbol: '$'
suffix: 'USD'
description: 'US Dollars'
base: False
value: 1.4

View File

@ -238,6 +238,93 @@ def postgresql(c):
c.run('sudo apt-get install postgresql postgresql-contrib libpq-dev') c.run('sudo apt-get install postgresql postgresql-contrib libpq-dev')
c.run('pip3 install psycopg2') c.run('pip3 install psycopg2')
@task(help={'filename': "Output filename (default = 'data.json')"})
def export_records(c, filename='data.json'):
"""
Export all database records to a file
"""
# Get an absolute path to the file
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
filename = os.path.abspath(filename)
print(f"Exporting database records to file '{filename}'")
if os.path.exists(filename):
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ")
response = str(response).strip().lower()
if response not in ['y', 'yes']:
print("Cancelled export operation")
return 0
cmd = f'dumpdata --exclude contenttypes --exclude auth.permission --indent 2 --output {filename}'
manage(c, cmd, pty=True)
@task(help={'filename': 'Input filename'})
def import_records(c, filename='data.json'):
"""
Import database records from a file
"""
# Get an absolute path to the supplied filename
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
if not os.path.exists(filename):
print(f"Error: File '{filename}' does not exist")
return -1
print(f"Importing database records from '{filename}'")
cmd = f'loaddata {filename}'
manage(c, cmd, pty=True)
@task
def import_fixtures(c):
"""
Import fixture data into the database.
This command imports all existing test fixture data into the database.
Warning:
- Intended for testing / development only!
- Running this command may overwrite existing database data!!
- Don't say you were not warned...
"""
fixtures = [
# Build model
'build',
# Company model
'company',
'price_breaks',
'supplier_part',
# Order model
'order',
# Part model
'bom',
'category',
'params',
'part',
'test_templates',
# Stock model
'location',
'stock_tests',
'stock',
]
command = 'loaddata ' + ' '.join(fixtures)
manage(c, command, pty=True)
@task @task
def backup(c): def backup(c):
""" """