diff --git a/.gitattributes b/.gitattributes index 4f45c74e3d3af8676977c35df9751374478a2e30..156df2c620906490a2263d9cbc7b19d2e10300f6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4234,6 +4234,7 @@ SAS/Feedback_Service/src/fb_data_44883.txt -text SAS/LSMR/CMakeLists.txt -text SAS/LSMR/bin/CMakeLists.txt -text SAS/LSMR/bin/lsmr -text +SAS/LSMR/bin/lsmr_makemigrations -text SAS/LSMR/bin/lsmr_testdatabase -text SAS/LSMR/doc/LSMR.md -text SAS/LSMR/doc/package.dox -text @@ -4245,7 +4246,8 @@ SAS/LSMR/src/lsmr/lsmrapp/__init__.py -text SAS/LSMR/src/lsmr/lsmrapp/admin.py -text SAS/LSMR/src/lsmr/lsmrapp/apps.py -text SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py -text -SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180801_1619.py -text +SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180802_1547.py -text +SAS/LSMR/src/lsmr/lsmrapp/migrations/0003_populate.py -text SAS/LSMR/src/lsmr/lsmrapp/migrations/CMakeLists.txt -text SAS/LSMR/src/lsmr/lsmrapp/migrations/__init__.py -text SAS/LSMR/src/lsmr/lsmrapp/models/CMakeLists.txt -text @@ -4265,6 +4267,7 @@ SAS/LSMR/src/lsmr/settings.py -text SAS/LSMR/src/lsmr/urls.py -text SAS/LSMR/src/lsmr/wsgi.py -text SAS/LSMR/src/manage.py -text +SAS/LSMR/src/remakemigration.py -text SAS/LSMR/test/CMakeLists.txt -text SAS/LSMR/test/__init__.py -text SAS/LSMR/test/postgres_testrunner.py -text diff --git a/SAS/LSMR/bin/lsmr_makemigrations b/SAS/LSMR/bin/lsmr_makemigrations new file mode 100755 index 0000000000000000000000000000000000000000..75e3704360360291abc3326e71f9eb3f66b72bc7 --- /dev/null +++ b/SAS/LSMR/bin/lsmr_makemigrations @@ -0,0 +1,32 @@ +#!/bin/bash + +# Copyright (C) 2012-2015 ASTRON (Netherlands Institute for Radio Astronomy) +# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands +# +# This file is part of the LOFAR software suite. +# The LOFAR software suite is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# The LOFAR software suite is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. + + +# Utility script to make migrations from scratch. This should not be used once a production database has been created +# since from that moment on we want to have incremental migrations that alter the existing database! + +# This script will remove existing migrations, then run makemigrations to create fresh migrations, and then recreate +# the migration for populating the database with the valid choices. + +# Parse args: + +echo "Removing existing migrations" + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +/usr/bin/env python3 $DIR/../lib*/python*/site-packages/lofar/sas/lsmr/remakemigrations.py \ No newline at end of file diff --git a/SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180801_1619.py b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180802_1547.py similarity index 99% rename from SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180801_1619.py rename to SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180802_1547.py index c998951f8ccfc1e3bc31bc57129d08912a10313e..3c41b0dabdea5f56744a3713edcb92cb65012be2 100644 --- a/SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180801_1619.py +++ b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_auto_20180802_1547.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.6 on 2018-08-01 16:19 +# Generated by Django 2.0.6 on 2018-08-02 15:47 import django.contrib.postgres.fields import django.contrib.postgres.fields.jsonb diff --git a/SAS/LSMR/src/lsmr/lsmrapp/migrations/0003_populate.py b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0003_populate.py new file mode 100644 index 0000000000000000000000000000000000000000..a68502041c0053609c9a5fc5ee25cb906d6a7f4c --- /dev/null +++ b/SAS/LSMR/src/lsmr/lsmrapp/migrations/0003_populate.py @@ -0,0 +1,12 @@ + +from django.db import migrations + +from ..populate import * + +class Migration(migrations.Migration): + + dependencies = [ + ('lsmrapp', '0002_auto_20180802_1547'), + ] + + operations = [ migrations.RunPython(populate_choices) ] diff --git a/SAS/LSMR/src/remakemigration.py b/SAS/LSMR/src/remakemigration.py new file mode 100755 index 0000000000000000000000000000000000000000..221d2e677eaa328a79bec481473745a62690d439 --- /dev/null +++ b/SAS/LSMR/src/remakemigration.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +import os +import sys +from glob import glob +import subprocess as sp +import logging + +logger = logging.getLogger(__file__) +here = os.path.dirname(__file__) +relapath = '/lsmr/lsmrapp/migrations/' +template = """ +from django.db import migrations + +from ..populate import * + +class Migration(migrations.Migration): + + dependencies = [ + ('lsmrapp', '%s'), + ] + + operations = [ migrations.RunPython(populate_choices) ] +""" + + +def execute_and_log(cmd): + + logger.info('Executing: %s' % cmd) + p = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE) + out, err = p.communicate() + if out is not None: + logger.info("STDOUT: %s" % out) + if err is not None: + logger.info("STDERR: %s" % err) + +def delete_old_migrations(): + + logger.info('Removing old migrations...') + + files = glob_migrations() + for f in [path for path in files if ("auto" in path or "populate" in path)]: + logger.info('...deleting %s' % f) + os.remove(f) + execute_and_log('svn rm %s' % f) + +def make_django_migrations(): + + logger.info('Making Django migrations...') + execute_and_log('/usr/bin/env python3 %s/manage.py makemigrations' % here) + + +def make_populate_migration(): + + logger.info('Making migration for populating database...') + last_migration = determine_last_migration() + migration = template % last_migration + + path = here + relapath + '%s_populate.py' % str(int(last_migration.split('_')[0])+1).zfill(4) + logger.info('...writing to %s' % path) + with open(path,'w') as f: + f.write(migration) + + +def glob_migrations(): + paths = glob(here + '/' + relapath + '0*_*') + return paths + + +def determine_last_migration(): + logger.info('Determining last migration...') + files = glob_migrations() + files = [os.path.basename(path) for path in files] + f = max(files) + last_migration = f.split('.py')[0] + logger.info('...determined last migration: %s' % last_migration) + return last_migration + + +def put_migrations_under_version_control(): + logger.info('Putting migrations under version control...') + files = glob_migrations() + for f in files: + execute_and_log('svn add %s' % f) + + +def remake_migrations(): + delete_old_migrations() + make_django_migrations() + make_populate_migration() + put_migrations_under_version_control() + + +if __name__ == "__main__": + + logger.setLevel(logging.DEBUG) + + handler = logging.StreamHandler() + handler.setLevel(logging.INFO) + logger.addHandler(handler) + + remake_migrations() \ No newline at end of file