[Asterisk-code-review] queue log: Add alembic script for generate db table for queu... (asterisk[master])

Rodrigo Ramirez Norambuena asteriskteam at digium.com
Sat Oct 1 10:36:18 CDT 2016


Rodrigo Ramirez Norambuena has uploaded a new change for review.

  https://gerrit.asterisk.org/4012

Change subject: queue_log: Add alembic script for generate db table for queue_log
......................................................................

queue_log: Add alembic script for generate db table for queue_log

Change-Id: I35b928a6251f9da9a1742b2cd14c63a00c3d0f0c
---
M contrib/ast-db-manage/README.md
A contrib/ast-db-manage/queue_log.ini.sample
A contrib/ast-db-manage/queue_log/env.py
A contrib/ast-db-manage/queue_log/script.py.mako
A contrib/ast-db-manage/queue_log/versions/4105ee839f58_create_queue_log_table.py
5 files changed, 188 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/12/4012/1

diff --git a/contrib/ast-db-manage/README.md b/contrib/ast-db-manage/README.md
index 3444dd5..33838aa 100644
--- a/contrib/ast-db-manage/README.md
+++ b/contrib/ast-db-manage/README.md
@@ -10,6 +10,7 @@
 repositories include:
 
  * `config` - Tables used for Asterisk realtime configuration
+ * `queue_log` - Table used for Asterisk to store Queue Log records
  * `voicemail` - Tables used for `ODBC_STOARGE` of voicemail messages
 
 Alembic uses SQLAlchemy, which has support for
diff --git a/contrib/ast-db-manage/queue_log.ini.sample b/contrib/ast-db-manage/queue_log.ini.sample
new file mode 100644
index 0000000..ada14b5
--- /dev/null
+++ b/contrib/ast-db-manage/queue_log.ini.sample
@@ -0,0 +1,58 @@
+# A generic, single database configuration.
+
+[alembic]
+# path to migration scripts
+script_location = queue_log
+
+# 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 = driver://user:pass@localhost/dbname
+
+
+#sqlalchemy.url = mysql://user:pass@localhost/queue_log
+sqlalchemy.url = postgresql://user:pass@localhost/queue_log
+
+
+# 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/contrib/ast-db-manage/queue_log/env.py b/contrib/ast-db-manage/queue_log/env.py
new file mode 100755
index 0000000..280006d
--- /dev/null
+++ b/contrib/ast-db-manage/queue_log/env.py
@@ -0,0 +1,69 @@
+from __future__ import with_statement
+from alembic import context
+from sqlalchemy import engine_from_config, pool
+from logging.config import fileConfig
+
+# 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 basically.
+fileConfig(config.config_file_name)
+
+# add your model's MetaData object here
+# for 'autogenerate' support
+# from myapp import mymodel
+# target_metadata = mymodel.Base.metadata
+target_metadata = None
+
+# 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 = config.get_main_option("sqlalchemy.url")
+    context.configure(url=url, target_metadata=target_metadata)
+
+    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.
+
+    """
+    connectable = engine_from_config(
+        config.get_section(config.config_ini_section),
+        prefix='sqlalchemy.',
+        poolclass=pool.NullPool)
+
+    with connectable.connect() as connection:
+        context.configure(
+            connection=connection,
+            target_metadata=target_metadata
+        )
+
+        with context.begin_transaction():
+            context.run_migrations()
+
+if context.is_offline_mode():
+    run_migrations_offline()
+else:
+    run_migrations_online()
diff --git a/contrib/ast-db-manage/queue_log/script.py.mako b/contrib/ast-db-manage/queue_log/script.py.mako
new file mode 100644
index 0000000..43c0940
--- /dev/null
+++ b/contrib/ast-db-manage/queue_log/script.py.mako
@@ -0,0 +1,24 @@
+"""${message}
+
+Revision ID: ${up_revision}
+Revises: ${down_revision | comma,n}
+Create Date: ${create_date}
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = ${repr(up_revision)}
+down_revision = ${repr(down_revision)}
+branch_labels = ${repr(branch_labels)}
+depends_on = ${repr(depends_on)}
+
+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/contrib/ast-db-manage/queue_log/versions/4105ee839f58_create_queue_log_table.py b/contrib/ast-db-manage/queue_log/versions/4105ee839f58_create_queue_log_table.py
new file mode 100644
index 0000000..e467ec3
--- /dev/null
+++ b/contrib/ast-db-manage/queue_log/versions/4105ee839f58_create_queue_log_table.py
@@ -0,0 +1,36 @@
+"""create queue_log table
+
+Revision ID: 4105ee839f58
+Revises:
+Create Date: 2016-09-30 22:32:45.918340
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '4105ee839f58'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    op.create_table(
+        'queue_log',
+        sa.Column('time', sa.DateTime()),
+        sa.Column('callid', sa.String(80)),
+        sa.Column('queuename', sa.String(256)),
+        sa.Column('agent', sa.String(80)),
+        sa.Column('event', sa.String(32)),
+        sa.Column('data1', sa.String(100)),
+        sa.Column('data2', sa.String(100)),
+        sa.Column('data3', sa.String(100)),
+        sa.Column('data4', sa.String(100)),
+        sa.Column('data5', sa.String(100))
+    )
+
+
+def downgrade():
+    op.drop_table('queue_log')

-- 
To view, visit https://gerrit.asterisk.org/4012
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I35b928a6251f9da9a1742b2cd14c63a00c3d0f0c
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Rodrigo Ramirez Norambuena <a at rodrigoramirez.com>



More information about the asterisk-code-review mailing list