This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from mxmeinhold/reporting-and-hiding
- Loading branch information
Showing
30 changed files
with
1,136 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
app.run(host=app.config['IP'], port=app.config['PORT'], threaded = False) | ||
|
||
application = app | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## What are Alembic and migrations? | ||
Migrations give a versioned history of changes to the SQL Schema for this project. | ||
A `revision` or `migration` defines changes to the structure of the database (such as adding/dropping tables or columns), as well as procedures for migrating existing data to the new structure. Migrations allow for safe(ish) rollbacks, and make it easier to make incremental changes to the database. | ||
|
||
## Applying migrations | ||
Once you have your db URI in `config.py`, run `flask db upgrade` to bring the db up to the latest revision. | ||
|
||
## Creating new revisions | ||
When you change `models.py`, you'll need to make a new migration. | ||
Run `flask db revision -m "<descriptive title>"` to autogenerate one. | ||
You should go manually inspect the created revision to make sure it does what you expect. | ||
Once you've got your changes all set, be sure to commit the migration file in the same commit as your changes to `models.py` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = migrations | ||
|
||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# timezone to use when rendering the date | ||
# within the migration file as well as the filename. | ||
# string value is passed to dateutil.tz.gettz() | ||
# leave blank for localtime | ||
# timezone = | ||
|
||
# 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 | ||
|
||
# set to 'true' to allow .pyc and .pyo files without | ||
# a source .py file to be detected as revisions in the | ||
# versions/ directory | ||
# sourceless = false | ||
|
||
# version location specification; this defaults | ||
# to migrations/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path | ||
# version_locations = %(here)s/bar %(here)s/bat migrations/versions | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
sqlalchemy.url = driver://user:pass@localhost/dbname | ||
|
||
|
||
# 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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.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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""${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"} | ||
|
25 changes: 25 additions & 0 deletions
25
migrations/versions/3b8a4c7fbcc2_report_reviewed_column_added.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Report Reviewed Column Added | ||
Revision ID: 3b8a4c7fbcc2 | ||
Revises: 76898f8ac346 | ||
Create Date: 2021-11-07 22:31:04.835460 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '3b8a4c7fbcc2' | ||
down_revision = '76898f8ac346' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column('report', sa.Column('reviewed', sa.Boolean(), nullable=False, default=False)) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('report', 'reviewed') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Initial schema | ||
Revision ID: 4f95c173f1d9 | ||
Revises: | ||
Create Date: 2021-04-23 01:02:40.157049 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '4f95c173f1d9' | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('api_key', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('hash', sa.String(length=64), nullable=True), | ||
sa.Column('owner', sa.String(length=80), nullable=True), | ||
sa.Column('reason', sa.String(length=120), nullable=True), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('hash'), | ||
sa.UniqueConstraint('owner', 'reason', name='unique_key') | ||
) | ||
op.create_table('quote', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('submitter', sa.String(length=80), nullable=False), | ||
sa.Column('quote', sa.String(length=200), nullable=False), | ||
sa.Column('speaker', sa.String(length=50), nullable=False), | ||
sa.Column('quote_time', sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('quote') | ||
) | ||
op.create_table('vote', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('quote_id', sa.Integer(), nullable=True), | ||
sa.Column('voter', sa.String(length=200), nullable=False), | ||
sa.Column('direction', sa.Integer(), nullable=False), | ||
sa.Column('updated_time', sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint(['quote_id'], ['quote.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('vote') | ||
op.drop_table('quote') | ||
op.drop_table('api_key') | ||
# ### end Alembic commands ### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Add reports | ||
Revision ID: 76898f8ac346 | ||
Revises: 4f95c173f1d9 | ||
Create Date: 2021-04-23 01:18:03.934757 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '76898f8ac346' | ||
down_revision = '4f95c173f1d9' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table('report', | ||
sa.Column('id', sa.Integer(), nullable=False, autoincrement=True), | ||
sa.Column('quote_id', sa.Integer(), nullable=False), | ||
sa.Column('reporter', sa.Text(), nullable=False), | ||
sa.Column('reason', sa.Text(), nullable=False), | ||
sa.ForeignKeyConstraint(['quote_id'], ['quote.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.add_column('quote', sa.Column('hidden', sa.Boolean(), nullable=False, default=False)) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('quote', 'hidden') | ||
op.drop_table('report') | ||
|
Oops, something went wrong.