Adds migration engine for peewee ORM 46/7246/2
authorBenjamin Diaz <bdiaz@whitestack.com>
Thu, 21 Feb 2019 19:24:56 +0000 (16:24 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Thu, 21 Feb 2019 19:30:59 +0000 (16:30 -0300)
Change-Id: I8829dd6e3858149fad9e1c37d773c78523d8c05f
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
debian/python3-osm-mon.postinst
osm_mon/cmd/mon_server.py
osm_mon/core/database.py
osm_mon/migrations/001_initial.py [new file with mode: 0644]
osm_mon/migrations/__init__.py [new file with mode: 0644]
osm_mon/migrations/conf.py [new file with mode: 0644]
requirements.txt
setup.py

index 1600da3..f83eef1 100644 (file)
 echo "Installing python dependencies via pip..."
 pip3 install aiokafka==0.4.*
 pip3 install requests==2.18.*
-pip3 install cherrypy==14.0.*
 pip3 install jsmin==2.2.*
 pip3 install jsonschema==2.6.*
 pip3 install python-keystoneclient==3.15.*
 pip3 install six==1.11.*
-pip3 install peewee==3.1.*
+pip3 install peewee==3.8.*
 pip3 install pyyaml==3.*
 pip3 install prometheus_client==0.4.*
 pip3 install gnocchiclient==7.0.*
 pip3 install pyvcloud==19.1.1
 pip3 install python-ceilometerclient==2.9.*
+pip3 install peewee-migrate==1.1.*
 echo "Installation of python dependencies finished"
index b23aa20..6698339 100644 (file)
@@ -46,11 +46,11 @@ def main():
     root.addHandler(ch)
 
     log = logging.getLogger(__name__)
+    log.info("Starting MON Server...")
     log.debug("Config: %s", cfg.conf)
     log.info("Initializing database...")
     db_manager = DatabaseManager(cfg)
     db_manager.create_tables()
-    log.info("Starting MON Server...")
     log.info("Database initialized correctly.")
     loop = asyncio.get_event_loop()
     server = Server(cfg, loop)
index eca08e9..0886c2b 100644 (file)
 ##
 
 import logging
+import os
 import uuid
 
 from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
+from peewee_migrate import Router
 from playhouse.db_url import connect
 
+from osm_mon import migrations
 from osm_mon.core.config import Config
 
 log = logging.getLogger(__name__)
@@ -72,7 +75,8 @@ class DatabaseManager:
 
     def create_tables(self) -> None:
         with db.atomic():
-            db.create_tables([VimCredentials, Alarm])
+            router = Router(db, os.path.dirname(migrations.__file__))
+            router.run()
 
     def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
         with db.atomic():
diff --git a/osm_mon/migrations/001_initial.py b/osm_mon/migrations/001_initial.py
new file mode 100644 (file)
index 0000000..a8b7290
--- /dev/null
@@ -0,0 +1,79 @@
+"""Peewee migrations -- 001_initial.py.
+
+Some examples (model - class or model name)::
+
+    > Model = migrator.orm['model_name']            # Return model in current state by name
+
+    > migrator.sql(sql)                             # Run custom SQL
+    > migrator.python(func, *args, **kwargs)        # Run python code
+    > migrator.create_model(Model)                  # Create a model (could be used as decorator)
+    > migrator.remove_model(model, cascade=True)    # Remove a model
+    > migrator.add_fields(model, **fields)          # Add fields to a model
+    > migrator.change_fields(model, **fields)       # Change fields
+    > migrator.remove_fields(model, *field_names, cascade=True)
+    > migrator.rename_field(model, old_field_name, new_field_name)
+    > migrator.rename_table(model, new_table_name)
+    > migrator.add_index(model, *col_names, unique=False)
+    > migrator.drop_index(model, *col_names)
+    > migrator.add_not_null(model, *field_names)
+    > migrator.drop_not_null(model, *field_names)
+    > migrator.add_default(model, field_name, default)
+
+"""
+
+import peewee as pw
+
+SQL = pw.SQL
+
+
+def migrate(migrator, database, fake=False, **kwargs):
+    """Write your migrations here."""
+
+    @migrator.create_model
+    class Alarm(pw.Model):
+        id = pw.AutoField()
+        uuid = pw.CharField(max_length=255, unique=True)
+        name = pw.CharField(max_length=255)
+        severity = pw.CharField(max_length=255)
+        threshold = pw.FloatField()
+        operation = pw.CharField(max_length=255)
+        statistic = pw.CharField(max_length=255)
+        monitoring_param = pw.CharField(max_length=255)
+        vdur_name = pw.CharField(max_length=255)
+        vnf_member_index = pw.CharField(max_length=255)
+        nsr_id = pw.CharField(max_length=255)
+
+        class Meta:
+            table_name = "alarm"
+
+    @migrator.create_model
+    class BaseModel(pw.Model):
+        id = pw.AutoField()
+
+        class Meta:
+            table_name = "basemodel"
+
+    @migrator.create_model
+    class VimCredentials(pw.Model):
+        id = pw.AutoField()
+        uuid = pw.CharField(max_length=255, unique=True)
+        name = pw.CharField(max_length=255)
+        type = pw.CharField(max_length=255)
+        url = pw.CharField(max_length=255)
+        user = pw.CharField(max_length=255)
+        password = pw.CharField(max_length=255)
+        tenant_name = pw.CharField(max_length=255)
+        config = pw.TextField(constraints=[SQL("DEFAULT '{}'")])
+
+        class Meta:
+            table_name = "vimcredentials"
+
+
+def rollback(migrator, database, fake=False, **kwargs):
+    """Write your rollback migrations here."""
+
+    migrator.remove_model('vimcredentials')
+
+    migrator.remove_model('basemodel')
+
+    migrator.remove_model('alarm')
diff --git a/osm_mon/migrations/__init__.py b/osm_mon/migrations/__init__.py
new file mode 100644 (file)
index 0000000..d81308a
--- /dev/null
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
+#         http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
diff --git a/osm_mon/migrations/conf.py b/osm_mon/migrations/conf.py
new file mode 100644 (file)
index 0000000..c6bda5a
--- /dev/null
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
+#         http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
+import os
+
+DATABASE = os.getenv('OSMMON_SQL_DATABASE_URI', 'sqlite://')
index 7e4c081..f44e08a 100644 (file)
 # contact: prithiv.mohan@intel.com or adrian.hoban@intel.com
 aiokafka==0.4.*
 requests==2.18.*
-cherrypy==14.0.*
 jsmin==2.2.*
 jsonschema==2.6.*
 python-keystoneclient==3.15.*
 six==1.11.*
-peewee==3.1.*
+peewee==3.8.*
 pyyaml==3.*
 prometheus_client==0.4.*
 gnocchiclient==7.0.*
 pymysql==0.9.*
 pyvcloud==19.1.*
 python-ceilometerclient==2.9.*
+peewee-migrate==1.1.*
 git+https://osm.etsi.org/gerrit/osm/common.git@v5.0#egg=osm-common
 git+https://osm.etsi.org/gerrit/osm/N2VC.git@v5.0#egg=n2vc
index ab5f600..ed2bf7d 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -53,17 +53,17 @@ setup(
     install_requires=[
         "aiokafka==0.4.*",
         "requests==2.18.*",
-        "cherrypy==14.0.*",
         "jsmin==2.2.*",
         "jsonschema==2.6.*",
         "python-keystoneclient==3.15.*",
         "six==1.11.*",
-        "peewee==3.1.*",
+        "peewee==3.8.*",
         "pyyaml==3.*",
         "prometheus_client==0.4.*",
         "gnocchiclient==7.0.*",
         "pyvcloud==19.1.1",
         "python-ceilometerclient==2.9.*",
+        "peewee-migrate==1.1.*",
         "osm-common",
         "n2vc"
     ],