move everything to backend folder for migration to dashboard repository
This commit is contained in:
parent
af6b006409
commit
92ec7c653d
89 changed files with 0 additions and 0 deletions
1
backend/migrations/README
Normal file
1
backend/migrations/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Single-database configuration for Flask.
|
||||
50
backend/migrations/alembic.ini
Normal file
50
backend/migrations/alembic.ini
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# 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,flask_migrate
|
||||
|
||||
[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
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[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
|
||||
91
backend/migrations/env.py
Normal file
91
backend/migrations/env.py
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
from __future__ import with_statement
|
||||
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
# 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
|
||||
config.set_main_option(
|
||||
'sqlalchemy.url',
|
||||
str(current_app.extensions['migrate'].db.get_engine().url).replace(
|
||||
'%', '%%'))
|
||||
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, target_metadata=target_metadata, literal_binds=True
|
||||
)
|
||||
|
||||
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.zzzcomputing.com/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.')
|
||||
|
||||
connectable = current_app.extensions['migrate'].db.get_engine()
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
**current_app.extensions['migrate'].configure_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
24
backend/migrations/script.py.mako
Normal file
24
backend/migrations/script.py.mako
Normal file
|
|
@ -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"}
|
||||
46
backend/migrations/versions/27761560bbcb_.py
Normal file
46
backend/migrations/versions/27761560bbcb_.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
"""empty message
|
||||
|
||||
Revision ID: 27761560bbcb
|
||||
Revises:
|
||||
Create Date: 2021-12-21 06:07:14.857940
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "27761560bbcb"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"app",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(length=64), nullable=True),
|
||||
sa.Column("slug", sa.String(length=64), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("slug"),
|
||||
)
|
||||
op.create_table(
|
||||
"app_role",
|
||||
sa.Column("user_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("app_id", sa.Integer(), nullable=False),
|
||||
sa.Column("role", sa.String(length=64), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["app_id"],
|
||||
["app.id"],
|
||||
),
|
||||
sa.PrimaryKeyConstraint("user_id", "app_id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("app_role")
|
||||
op.drop_table("app")
|
||||
# ### end Alembic commands ###
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
"""convert role column to table
|
||||
|
||||
Revision ID: 5f462d2d9d25
|
||||
Revises: 27761560bbcb
|
||||
Create Date: 2022-04-13 15:00:27.182898
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "5f462d2d9d25"
|
||||
down_revision = "27761560bbcb"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
role_table = op.create_table(
|
||||
"role",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(length=64), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.add_column("app_role", sa.Column("role_id", sa.Integer(), nullable=True))
|
||||
op.create_foreign_key(None, "app_role", "role", ["role_id"], ["id"])
|
||||
# ### end Alembic commands ###
|
||||
|
||||
# Insert default role "admin" as ID 1
|
||||
op.execute(sa.insert(role_table).values(id=1,name="admin"))
|
||||
# Set role_id 1 to all current "admin" users
|
||||
op.execute("UPDATE app_role SET role_id = 1 WHERE role = 'admin'")
|
||||
|
||||
# Drop old column
|
||||
op.drop_column("app_role", "role")
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column(
|
||||
"app_role", sa.Column("role", mysql.VARCHAR(length=64), nullable=True)
|
||||
)
|
||||
op.drop_constraint(None, "app_role", type_="foreignkey")
|
||||
op.drop_column("app_role", "role_id")
|
||||
op.drop_table("role")
|
||||
# ### end Alembic commands ###
|
||||
76
backend/migrations/versions/b514cca2d47b_add_user_role.py
Normal file
76
backend/migrations/versions/b514cca2d47b_add_user_role.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
"""update apps and add 'user' and 'no access' role
|
||||
|
||||
Revision ID: b514cca2d47b
|
||||
Revises: 5f462d2d9d25
|
||||
Create Date: 2022-06-08 17:24:51.305129
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'b514cca2d47b'
|
||||
down_revision = '5f462d2d9d25'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### end Alembic commands ###
|
||||
|
||||
# Check and update app table in DB
|
||||
apps = {
|
||||
"dashboard": "Dashboard",
|
||||
"wekan": "Wekan",
|
||||
"wordpress": "WordPress",
|
||||
"nextcloud": "Nextcloud",
|
||||
"zulip": "Zulip"
|
||||
}
|
||||
# app table
|
||||
app_table = sa.table('app', sa.column('id', sa.Integer), sa.column(
|
||||
'name', sa.String), sa.column('slug', sa.String))
|
||||
|
||||
existing_apps = op.get_bind().execute(app_table.select()).fetchall()
|
||||
existing_app_slugs = [app['slug'] for app in existing_apps]
|
||||
for app_slug in apps.keys():
|
||||
if app_slug in existing_app_slugs:
|
||||
op.execute(f'UPDATE app SET `name` = "{apps.get(app_slug)}" WHERE slug = "{app_slug}"')
|
||||
else:
|
||||
op.execute(f'INSERT INTO app (`name`, slug) VALUES ("{apps.get(app_slug)}","{app_slug}")')
|
||||
|
||||
# Fetch all apps including newly created
|
||||
existing_apps = op.get_bind().execute(app_table.select()).fetchall()
|
||||
# Insert role "user" as ID 2
|
||||
op.execute("INSERT INTO `role` (id, `name`) VALUES (2, 'user')")
|
||||
# Insert role "no access" as ID 3
|
||||
op.execute("INSERT INTO `role` (id, `name`) VALUES (3, 'no access')")
|
||||
# Set role_id 2 to all current "user" users which by have NULL role ID
|
||||
op.execute("UPDATE app_role SET role_id = 2 WHERE role_id IS NULL")
|
||||
|
||||
# Add 'no access' role for all users that don't have any roles for specific apps
|
||||
app_roles_table = sa.table('app_role', sa.column('user_id', sa.String), sa.column(
|
||||
'app_id', sa.Integer), sa.column('role_id', sa.Integer))
|
||||
|
||||
app_ids = [app['id'] for app in existing_apps]
|
||||
app_roles = op.get_bind().execute(app_roles_table.select()).fetchall()
|
||||
user_ids = set([app_role['user_id'] for app_role in app_roles])
|
||||
|
||||
for user_id in user_ids:
|
||||
existing_user_app_ids = [x['app_id'] for x in list(filter(lambda role: role['user_id'] == user_id, app_roles))]
|
||||
missing_user_app_ids = [x for x in app_ids if x not in existing_user_app_ids]
|
||||
|
||||
if len(missing_user_app_ids) > 0:
|
||||
values = [{'user_id': user_id, 'app_id': app_id, 'role_id': 3} for app_id in missing_user_app_ids]
|
||||
op.bulk_insert(app_roles_table, values)
|
||||
|
||||
|
||||
def downgrade():
|
||||
# Revert all users role_id to NULL where role is 'user'
|
||||
op.execute("UPDATE app_role SET role_id = NULL WHERE role_id = 2")
|
||||
# Delete role 'user' from roles
|
||||
op.execute("DELETE FROM `role` WHERE id = 2")
|
||||
|
||||
# Delete all user app roles where role is 'no access' with role_id 3
|
||||
op.execute("DELETE FROM app_role WHERE role_id = 3")
|
||||
# Delete role 'no access' from roles
|
||||
op.execute("DELETE FROM `role` WHERE id = 3")
|
||||
33
backend/migrations/versions/e08df0bef76f_.py
Normal file
33
backend/migrations/versions/e08df0bef76f_.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""Add fields for external apps
|
||||
|
||||
Revision ID: e08df0bef76f
|
||||
Revises: b514cca2d47b
|
||||
Create Date: 2022-09-23 16:38:06.557307
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'e08df0bef76f'
|
||||
down_revision = 'b514cca2d47b'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('app', sa.Column('external', sa.Boolean(), server_default='0', nullable=False))
|
||||
op.add_column('app', sa.Column('url', sa.String(length=128), nullable=True))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
# Add monitoring app
|
||||
op.execute(f'INSERT IGNORE INTO app (`name`, `slug`) VALUES ("Monitoring","monitoring")')
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('app', 'url')
|
||||
op.drop_column('app', 'external')
|
||||
# ### end Alembic commands ###
|
||||
Reference in a new issue