aboutsummaryrefslogtreecommitdiffstats
path: root/alembic
diff options
context:
space:
mode:
Diffstat (limited to 'alembic')
-rw-r--r--alembic/README1
-rw-r--r--alembic/env.py85
-rw-r--r--alembic/script.py.mako24
-rw-r--r--alembic/versions/38085c8f1099_enable_cascade_deletion_for_messages.py34
-rw-r--r--alembic/versions/4d293b37634c_initial_migration.py28
-rw-r--r--alembic/versions/6b52b1e7680b_add_channel_categories.py34
-rw-r--r--alembic/versions/abc065ff2fb3_add_bot_column_to_user_table.py28
-rw-r--r--alembic/versions/d42a9cc66591_add_channels_users_and_messages_table.py54
8 files changed, 288 insertions, 0 deletions
diff --git a/alembic/README b/alembic/README
new file mode 100644
index 0000000..98e4f9c
--- /dev/null
+++ b/alembic/README
@@ -0,0 +1 @@
+Generic single-database configuration. \ No newline at end of file
diff --git a/alembic/env.py b/alembic/env.py
new file mode 100644
index 0000000..7978da1
--- /dev/null
+++ b/alembic/env.py
@@ -0,0 +1,85 @@
+from logging.config import fileConfig
+
+from sqlalchemy import engine_from_config
+from sqlalchemy import pool
+
+from alembic import context
+
+import sys
+sys.path.append(".")
+
+from metricity.database import build_db_uri
+from metricity.models 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 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 = db
+
+# 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.
+
+config.set_main_option('sqlalchemy.url', build_db_uri())
+
+
+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,
+ dialect_opts={"paramstyle": "named"},
+ )
+
+ 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/alembic/script.py.mako b/alembic/script.py.mako
new file mode 100644
index 0000000..2c01563
--- /dev/null
+++ b/alembic/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/alembic/versions/38085c8f1099_enable_cascade_deletion_for_messages.py b/alembic/versions/38085c8f1099_enable_cascade_deletion_for_messages.py
new file mode 100644
index 0000000..522d96a
--- /dev/null
+++ b/alembic/versions/38085c8f1099_enable_cascade_deletion_for_messages.py
@@ -0,0 +1,34 @@
+"""enable cascade deletion for messages
+
+Revision ID: 38085c8f1099
+Revises: abc065ff2fb3
+Create Date: 2020-08-25 13:00:21.629836
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '38085c8f1099'
+down_revision = 'abc065ff2fb3'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_constraint('messages_author_id_fkey', 'messages', type_='foreignkey')
+ op.drop_constraint('messages_channel_id_fkey', 'messages', type_='foreignkey')
+ op.create_foreign_key(None, 'messages', 'users', ['author_id'], ['id'], ondelete='CASCADE')
+ op.create_foreign_key(None, 'messages', 'channels', ['channel_id'], ['id'], ondelete='CASCADE')
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_constraint(None, 'messages', type_='foreignkey')
+ op.drop_constraint(None, 'messages', type_='foreignkey')
+ op.create_foreign_key('messages_channel_id_fkey', 'messages', 'channels', ['channel_id'], ['id'])
+ op.create_foreign_key('messages_author_id_fkey', 'messages', 'users', ['author_id'], ['id'])
+ # ### end Alembic commands ###
diff --git a/alembic/versions/4d293b37634c_initial_migration.py b/alembic/versions/4d293b37634c_initial_migration.py
new file mode 100644
index 0000000..23e7ea4
--- /dev/null
+++ b/alembic/versions/4d293b37634c_initial_migration.py
@@ -0,0 +1,28 @@
+"""initial migration
+
+Revision ID: 4d293b37634c
+Revises:
+Create Date: 2020-08-25 03:09:13.565389
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '4d293b37634c'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ pass
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ pass
+ # ### end Alembic commands ###
diff --git a/alembic/versions/6b52b1e7680b_add_channel_categories.py b/alembic/versions/6b52b1e7680b_add_channel_categories.py
new file mode 100644
index 0000000..bdbcde1
--- /dev/null
+++ b/alembic/versions/6b52b1e7680b_add_channel_categories.py
@@ -0,0 +1,34 @@
+"""add channel categories
+
+Revision ID: 6b52b1e7680b
+Revises: d42a9cc66591
+Create Date: 2020-08-25 05:10:43.945126
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '6b52b1e7680b'
+down_revision = 'd42a9cc66591'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_table('categories',
+ sa.Column('id', sa.BigInteger(), nullable=False),
+ sa.Column('name', sa.String(), nullable=False),
+ sa.PrimaryKeyConstraint('id')
+ )
+ op.add_column('channels', sa.Column('category_id', sa.BigInteger(), nullable=True))
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_column('channels', 'category_id')
+ op.drop_table('categories')
+ # ### end Alembic commands ###
diff --git a/alembic/versions/abc065ff2fb3_add_bot_column_to_user_table.py b/alembic/versions/abc065ff2fb3_add_bot_column_to_user_table.py
new file mode 100644
index 0000000..9c53999
--- /dev/null
+++ b/alembic/versions/abc065ff2fb3_add_bot_column_to_user_table.py
@@ -0,0 +1,28 @@
+"""add bot column to user table
+
+Revision ID: abc065ff2fb3
+Revises: 6b52b1e7680b
+Create Date: 2020-08-25 05:24:01.730596
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'abc065ff2fb3'
+down_revision = '6b52b1e7680b'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.add_column('users', sa.Column('bot', sa.Boolean(), nullable=True))
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_column('users', 'bot')
+ # ### end Alembic commands ###
diff --git a/alembic/versions/d42a9cc66591_add_channels_users_and_messages_table.py b/alembic/versions/d42a9cc66591_add_channels_users_and_messages_table.py
new file mode 100644
index 0000000..0ed3728
--- /dev/null
+++ b/alembic/versions/d42a9cc66591_add_channels_users_and_messages_table.py
@@ -0,0 +1,54 @@
+"""add channels, users and messages table
+
+Revision ID: d42a9cc66591
+Revises: 4d293b37634c
+Create Date: 2020-08-25 03:10:21.282787
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'd42a9cc66591'
+down_revision = '4d293b37634c'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_table('channels',
+ sa.Column('id', sa.BigInteger(), nullable=False),
+ sa.Column('name', sa.String(), nullable=False),
+ sa.Column('is_staff', sa.Boolean(), nullable=False),
+ sa.PrimaryKeyConstraint('id')
+ )
+ op.create_table('users',
+ sa.Column('id', sa.BigInteger(), nullable=False),
+ sa.Column('name', sa.String(), nullable=False),
+ sa.Column('avatar_hash', sa.String(), nullable=True),
+ sa.Column('joined_at', sa.DateTime(), nullable=False),
+ sa.Column('created_at', sa.DateTime(), nullable=False),
+ sa.Column('is_staff', sa.Boolean(), nullable=False),
+ sa.Column('opt_out', sa.Boolean(), nullable=True),
+ sa.PrimaryKeyConstraint('id')
+ )
+ op.create_table('messages',
+ sa.Column('id', sa.BigInteger(), nullable=False),
+ sa.Column('channel_id', sa.BigInteger(), nullable=True),
+ sa.Column('author_id', sa.BigInteger(), nullable=True),
+ sa.Column('created_at', sa.DateTime(), nullable=True),
+ sa.ForeignKeyConstraint(['author_id'], ['users.id'], ),
+ sa.ForeignKeyConstraint(['channel_id'], ['channels.id'], ),
+ sa.PrimaryKeyConstraint('id')
+ )
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_table('messages')
+ op.drop_table('users')
+ op.drop_table('channels')
+ # ### end Alembic commands ###