Permalink
Browse files

Add Flask-Migrate to the project

Also moved old migrations to old_migrations folder (before deleting them entirely).
  • Loading branch information...
1 parent 74e6ef5 commit 70d545410dc68da0f98cf395795634265004ada3 @arikfr arikfr committed Dec 7, 2016
Showing with 160 additions and 0 deletions.
  1. +1 −0 migrations/README
  2. +45 −0 migrations/alembic.ini
  3. +87 −0 migrations/env.py
  4. +24 −0 migrations/script.py.mako
  5. 0 {migrations → old_migrations}/0001_allow_delete_query.py
  6. 0 {migrations → old_migrations}/0002_fix_timestamp_fields.py
  7. 0 {migrations → old_migrations}/0003_update_data_source_config.py
  8. 0 {migrations → old_migrations}/0004_allow_null_in_event_user.py
  9. 0 {migrations → old_migrations}/0005_add_updated_at.py
  10. 0 {migrations → old_migrations}/0006_queries_last_edit_by.py
  11. 0 {migrations → old_migrations}/0007_add_schedule_to_queries.py
  12. 0 {migrations → old_migrations}/0008_make_ds_name_unique.py
  13. 0 {migrations → old_migrations}/0009_add_api_key_to_user.py
  14. 0 {migrations → old_migrations}/0010_allow_deleting_datasources.py
  15. 0 {migrations → old_migrations}/0010_create_alerts.py
  16. 0 {migrations → old_migrations}/0011_migrate_bigquery_to_json.py
  17. 0 {migrations → old_migrations}/0012_add_list_users_permission.py
  18. 0 {migrations → old_migrations}/0013_update_counter_options.py
  19. 0 {migrations → old_migrations}/0014_add_alert_rearm_seconds.py
  20. 0 {migrations → old_migrations}/0014_migrate_existing_es_to_kibana.py
  21. 0 {migrations → old_migrations}/0015_add_schedule_query_permission.py
  22. 0 {migrations → old_migrations}/0016_add_alert_subscriber.py
  23. 0 {migrations → old_migrations}/0016_drop_tables_from_group.py
  24. 0 {migrations → old_migrations}/0017_add_organization.py
  25. 0 {migrations → old_migrations}/0018_add_groups_refs.py
  26. 0 {migrations → old_migrations}/0019_add_super_admin_permission.py
  27. 0 {migrations → old_migrations}/0020_change_ds_name_to_non_uniqe.py
  28. 0 {migrations → old_migrations}/0021_create_api_keys_table.py
  29. 0 {migrations → old_migrations}/0022_add_new_permissions.py
  30. 0 {migrations → old_migrations}/0023_add_notification_destination.py
  31. 0 {migrations → old_migrations}/0023_make_sure_correct_group_type.py
  32. 0 {migrations → old_migrations}/0024_add_options_to_query.py
  33. 0 {migrations → old_migrations}/0025_add_query_snippets_table.py
  34. 0 {migrations → old_migrations}/0026_add_access_control_tables.py
  35. 0 {migrations → old_migrations}/0026_remove_query_trackers_redis_key.py
  36. 0 {migrations → old_migrations}/0027_add_draft_toggle.py
  37. +3 −0 redash/__init__.py
View
@@ -0,0 +1 @@
+Generic single-database configuration.
@@ -0,0 +1,45 @@
+# A generic, single database configuration.
+
+[alembic]
+# template used to generate migration files
+# file_template = %%(rev)s_%%(slug)s
+
+# set to 'true' to run the environment during
+# the 'revision' command, regardless of autogenerate
+# revision_environment = false
+
+
+# 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
View
@@ -0,0 +1,87 @@
+from __future__ import with_statement
+from alembic import context
+from sqlalchemy import engine_from_config, pool
+from logging.config import fileConfig
+import logging
+
+# 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)
+logger = logging.getLogger('alembic.env')
+
+# add your model's MetaData object here
+# for 'autogenerate' support
+# from myapp import mymodel
+# target_metadata = mymodel.Base.metadata
+from flask import current_app
+config.set_main_option('sqlalchemy.url',
+ current_app.config.get('SQLALCHEMY_DATABASE_URI'))
+target_metadata = current_app.extensions['migrate'].db.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 = config.get_main_option("sqlalchemy.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.
+
+ """
+
+ # this callback is used to prevent an auto-migration from being generated
+ # when there are no changes to the schema
+ # reference: http://alembic.readthedocs.org/en/latest/cookbook.html
+ def process_revision_directives(context, revision, directives):
+ if getattr(config.cmd_opts, 'autogenerate', False):
+ script = directives[0]
+ if script.upgrade_ops.is_empty():
+ directives[:] = []
+ logger.info('No changes in schema detected.')
+
+ engine = engine_from_config(config.get_section(config.config_ini_section),
+ prefix='sqlalchemy.',
+ poolclass=pool.NullPool)
+
+ connection = engine.connect()
+ context.configure(connection=connection,
+ target_metadata=target_metadata,
+ process_revision_directives=process_revision_directives,
+ **current_app.extensions['migrate'].configure_args)
+
+ try:
+ with context.begin_transaction():
+ context.run_migrations()
+ finally:
+ connection.close()
+
+if context.is_offline_mode():
+ run_migrations_offline()
+else:
+ run_migrations_online()
@@ -0,0 +1,24 @@
+"""${message}
+
+Revision ID: ${up_revision}
+Revises: ${down_revision | comma,n}
+Create Date: ${create_date}
+
+"""
+from alembic import op
+import sqlalchemy as sa
+${imports if imports else ""}
+
+# revision identifiers, used by Alembic.
+revision = ${repr(up_revision)}
+down_revision = ${repr(down_revision)}
+branch_labels = ${repr(branch_labels)}
+depends_on = ${repr(depends_on)}
+
+
+def upgrade():
+ ${upgrades if upgrades else "pass"}
+
+
+def downgrade():
+ ${downgrades if downgrades else "pass"}
View
@@ -9,6 +9,7 @@
from flask_mail import Mail
from flask_limiter import Limiter
from flask_limiter.util import get_ipaddr
+from flask_migrate import Migrate
from redash import settings
from redash.query_runner import import_query_runners
@@ -52,6 +53,7 @@ def create_redis_connection():
setup_logging()
redis_connection = create_redis_connection()
mail = Mail()
+migrate = Migrate()
mail.init_mail(settings.all_settings())
statsd_client = StatsClient(host=settings.STATSD_HOST, port=settings.STATSD_PORT, prefix=settings.STATSD_PREFIX)
limiter = Limiter(key_func=get_ipaddr, storage_uri=settings.REDIS_URL)
@@ -110,6 +112,7 @@ def create_app():
provision_app(app)
db.init_app(app)
+ migrate.init_app(app, db)
init_admin(app)
mail.init_app(app)
setup_authentication(app)

0 comments on commit 70d5454

Please sign in to comment.