Milimetric has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/111220

Change subject: Adds alembic migrations
......................................................................

Adds alembic migrations

Instructions for using alembic can be found in:
database_migrations/README
Card: analytics 1431

Change-Id: Ibd7671094502159b52adb597935123014073aeca
---
A alembic.ini
A database_migrations/README
A database_migrations/env.py
A database_migrations/script.py.mako
M requirements.txt
M wikimetrics/database.py
6 files changed, 174 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics 
refs/changes/20/111220/1

diff --git a/alembic.ini b/alembic.ini
new file mode 100644
index 0000000..d63c1b5
--- /dev/null
+++ b/alembic.ini
@@ -0,0 +1,54 @@
+# A generic, single database configuration.
+
+[alembic]
+# path to migration scripts
+script_location = database_migrations
+
+# template used to generate migration files
+# file_template = %%(rev)s_%%(slug)s
+
+# max length of characters to apply to the
+# "slug" field
+#truncate_slug_length = 40
+
+# set to 'true' to run the environment during
+# the 'revision' command, regardless of autogenerate
+# revision_environment = false
+
+sqlalchemy.url = not-used
+
+
+# Logging configuration
+[loggers]
+keys = root,sqlalchemy,alembic
+
+[handlers]
+keys = console
+
+[formatters]
+keys = generic
+
+[logger_root]
+level = WARN
+handlers = console
+qualname =
+
+[logger_sqlalchemy]
+level = WARN
+handlers =
+qualname = sqlalchemy.engine
+
+[logger_alembic]
+level = INFO
+handlers =
+qualname = alembic
+
+[handler_console]
+class = StreamHandler
+args = (sys.stderr,)
+level = NOTSET
+formatter = generic
+
+[formatter_generic]
+format = %(levelname)-5.5s [%(name)s] %(message)s
+datefmt = %H:%M:%S
diff --git a/database_migrations/README b/database_migrations/README
new file mode 100644
index 0000000..27b6158
--- /dev/null
+++ b/database_migrations/README
@@ -0,0 +1,12 @@
+Generic single-database configuration.
+
+Alembic is configured to handle database migrations for the wikimetrics 
database.  Autogeneration of migrations is supported.  The  general workflow is:
+
+````
+$ # make some changes to the models in wikimetrics.models
+$ alembic revision --autogenerate -m "Short description of your change"
+$ # confirm the file added to database_migrations/versions/ does what you want
+$ alembic upgrade head
+````
+
+At this point your local database is up to date with your models and the 
proper alembic scripts have been added.  From a database point of view, you're 
ready to submit your change.
diff --git a/database_migrations/env.py b/database_migrations/env.py
new file mode 100644
index 0000000..a6613f7
--- /dev/null
+++ b/database_migrations/env.py
@@ -0,0 +1,68 @@
+from __future__ import with_statement
+from alembic import context
+from sqlalchemy import engine_from_config, pool
+from logging.config import fileConfig
+
+from wikimetrics.configurables import db
+
+# this is the Alembic Config object, which provides
+# access to the values within the .ini file in use.
+config = context.config
+
+# Interpret the config file for Python logging.
+# This line sets up loggers
+fileConfig(config.config_file_name)
+
+# add your model's MetaData object here for 'autogenerate' support
+# initialize metadata
+db.get_session()
+# set metadata
+target_metadata = db.WikimetricsBase.metadata
+
+# other values from the config, defined by the needs of env.py,
+# can be acquired:
+# my_important_option = config.get_main_option("my_important_option")
+# ... etc.
+
+
+def run_migrations_offline():
+    """Run migrations in 'offline' mode.
+
+    This configures the context with just a URL
+    and not an Engine, though an Engine is acceptable
+    here as well.  By skipping the Engine creation
+    we don't even need a DBAPI to be available.
+
+    Calls to context.execute() here emit the given string to the
+    script output.
+
+    """
+    url = db.get_engine().url
+    context.configure(url=url)
+    
+    with context.begin_transaction():
+        context.run_migrations()
+
+
+def run_migrations_online():
+    """Run migrations in 'online' mode.
+
+    In this scenario we need to create an Engine
+    and associate a connection with the context.
+
+    """
+    engine = db.get_engine()
+    
+    connection = engine.connect()
+    context.configure(connection=connection, target_metadata=target_metadata)
+    
+    try:
+        with context.begin_transaction():
+            context.run_migrations()
+    finally:
+        connection.close()
+
+if context.is_offline_mode():
+    run_migrations_offline()
+else:
+    run_migrations_online()
diff --git a/database_migrations/script.py.mako 
b/database_migrations/script.py.mako
new file mode 100644
index 0000000..9570201
--- /dev/null
+++ b/database_migrations/script.py.mako
@@ -0,0 +1,22 @@
+"""${message}
+
+Revision ID: ${up_revision}
+Revises: ${down_revision}
+Create Date: ${create_date}
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = ${repr(up_revision)}
+down_revision = ${repr(down_revision)}
+
+from alembic import op
+import sqlalchemy as sa
+${imports if imports else ""}
+
+def upgrade():
+    ${upgrades if upgrades else "pass"}
+
+
+def downgrade():
+    ${downgrades if downgrades else "pass"}
diff --git a/requirements.txt b/requirements.txt
index a0a83b6..40bd80d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -14,3 +14,4 @@
 PyYAML==3.10
 PyJWT==0.1.6
 python-dateutil==2.2
+alembic==0.6.3
diff --git a/wikimetrics/database.py b/wikimetrics/database.py
index ff11bf7..72586ed 100644
--- a/wikimetrics/database.py
+++ b/wikimetrics/database.py
@@ -80,6 +80,21 @@
         self.mediawiki_sessionmakers = {}
         self.project_host_map = self.get_project_host_map(usecache=True)
     
+    def get_engine(self):
+        """
+        Create a sqlalchemy engine for the wikimetrics database.
+        
+        Returns:
+            new or cached sqlalchemy engine connected to the wikimetrics 
database.
+        """
+        if self.wikimetrics_engine is None:
+            self.wikimetrics_engine = create_engine(
+                self.config['WIKIMETRICS_ENGINE_URL'],
+                echo=self.config['SQL_ECHO'],
+            )
+        
+        return self.wikimetrics_engine
+    
     def get_session(self):
         """
         On the first run, instantiates the Wikimetrics session maker
@@ -89,11 +104,8 @@
         Returns:
             new sqlalchemy session open to the wikimetrics database
         """
-        if not self.wikimetrics_sessionmaker:
-            self.wikimetrics_engine = create_engine(
-                self.config['WIKIMETRICS_ENGINE_URL'],
-                echo=self.config['SQL_ECHO'],
-            )
+        if self.wikimetrics_sessionmaker is None:
+            self.get_engine()
             # This import is necessary here so that
             # WikimetricsBase knows about all its children.
             import wikimetrics.models

-- 
To view, visit https://gerrit.wikimedia.org/r/111220
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibd7671094502159b52adb597935123014073aeca
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to