diff options
Diffstat (limited to 'migrations')
-rw-r--r-- | migrations/20220719122322_peer.sql | 11 | ||||
-rw-r--r-- | migrations/20220719152547_users.sql | 8 | ||||
-rw-r--r-- | migrations/README | 1 | ||||
-rw-r--r-- | migrations/alembic.ini | 50 | ||||
-rw-r--r-- | migrations/env.py | 91 | ||||
-rw-r--r-- | migrations/script.py.mako | 24 | ||||
-rw-r--r-- | migrations/versions/81173c1d4f0a_initial_migration.py | 90 | ||||
-rw-r--r-- | migrations/versions/946e687d2d42_roles_users.py | 35 |
8 files changed, 291 insertions, 19 deletions
diff --git a/migrations/20220719122322_peer.sql b/migrations/20220719122322_peer.sql deleted file mode 100644 index c06c574..0000000 --- a/migrations/20220719122322_peer.sql +++ /dev/null @@ -1,11 +0,0 @@ --- Add migration script here -CREATE TABLE peers ( - id TEXT PRIMARY KEY, - addr CIDR NOT NULL, - public_key TEXT NOT NULL -); -CREATE TABLE networks ( - id TEXT PRIMARY KEY, - subnet CIDR NOT NULL, - description TEXT -) diff --git a/migrations/20220719152547_users.sql b/migrations/20220719152547_users.sql deleted file mode 100644 index 4407bcc..0000000 --- a/migrations/20220719152547_users.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Add migration script here -CREATE TABLE users ( - id TEXT PRIMARY KEY, - email TEXT NOT NULL, - pref_name TEXT NOT NULL, - pw_hash TEXT NOT NULL, - last_login TIMESTAMP -); diff --git a/migrations/README b/migrations/README new file mode 100644 index 0000000..0e04844 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Single-database configuration for Flask. diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 0000000..ec9d45c --- /dev/null +++ b/migrations/alembic.ini @@ -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 diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 0000000..68feded --- /dev/null +++ b/migrations/env.py @@ -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() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/migrations/script.py.mako @@ -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"} diff --git a/migrations/versions/81173c1d4f0a_initial_migration.py b/migrations/versions/81173c1d4f0a_initial_migration.py new file mode 100644 index 0000000..f96ff21 --- /dev/null +++ b/migrations/versions/81173c1d4f0a_initial_migration.py @@ -0,0 +1,90 @@ +"""Initial migration + +Revision ID: 81173c1d4f0a +Revises: +Create Date: 2022-07-25 13:13:29.135474 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '81173c1d4f0a' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('role', + sa.Column('id', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('user', + sa.Column('id', sa.String(), nullable=False), + sa.Column('email', sa.String(), nullable=False), + sa.Column('password', sa.String(), nullable=False), + sa.Column('pref_name', sa.String(), nullable=False), + sa.Column('last_login', sa.DateTime(), nullable=False), + sa.Column('fs_uniquifier', sa.String(), nullable=False), + sa.Column('active', sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email'), + sa.UniqueConstraint('fs_uniquifier') + ) + op.drop_table('enroll_requests') + op.drop_table('_sqlx_migrations') + op.drop_table('networks') + op.drop_table('peers') + op.drop_table('users') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('users', + sa.Column('id', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('email', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('pref_name', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('pw_hash', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('last_login', postgresql.TIMESTAMP(), autoincrement=False, nullable=True), + sa.PrimaryKeyConstraint('id', name='users_pkey'), + postgresql_ignore_search_path=False + ) + op.create_table('peers', + sa.Column('id', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('addr', postgresql.CIDR(), autoincrement=False, nullable=False), + sa.Column('public_key', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('owner', sa.TEXT(), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint(['owner'], ['users.id'], name='peers_owner_fkey'), + sa.PrimaryKeyConstraint('id', name='peers_pkey') + ) + op.create_table('networks', + sa.Column('id', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('subnet', postgresql.CIDR(), autoincrement=False, nullable=False), + sa.Column('description', sa.TEXT(), autoincrement=False, nullable=True), + sa.PrimaryKeyConstraint('id', name='networks_pkey') + ) + op.create_table('_sqlx_migrations', + sa.Column('version', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('description', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('installed_on', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False), + sa.Column('success', sa.BOOLEAN(), autoincrement=False, nullable=False), + sa.Column('checksum', postgresql.BYTEA(), autoincrement=False, nullable=False), + sa.Column('execution_time', sa.BIGINT(), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('version', name='_sqlx_migrations_pkey') + ) + op.create_table('enroll_requests', + sa.Column('token', sa.TEXT(), autoincrement=False, nullable=False), + sa.Column('expires', postgresql.TIMESTAMP(), server_default=sa.text("(CURRENT_TIMESTAMP + ((30)::double precision * '00:01:00'::interval))"), autoincrement=False, nullable=False), + sa.Column('confirmed', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('token', name='enroll_requests_pkey') + ) + op.drop_table('user') + op.drop_table('role') + # ### end Alembic commands ### diff --git a/migrations/versions/946e687d2d42_roles_users.py b/migrations/versions/946e687d2d42_roles_users.py new file mode 100644 index 0000000..08ebbe2 --- /dev/null +++ b/migrations/versions/946e687d2d42_roles_users.py @@ -0,0 +1,35 @@ +"""roles_users + +Revision ID: 946e687d2d42 +Revises: 81173c1d4f0a +Create Date: 2022-07-25 14:30:56.122119 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '946e687d2d42' +down_revision = '81173c1d4f0a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('roles_users', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('role_id', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['role_id'], ['role.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('roles_users') + # ### end Alembic commands ### |