diff --git a/app/models.py b/app/models.py index ffc1959..b0fa6ab 100644 --- a/app/models.py +++ b/app/models.py @@ -139,6 +139,9 @@ class User(UserMixin, db.Model): # collect collected_transaction = db.relationship('Collect', back_populates='collecter', lazy='dynamic', cascade='all, delete-orphan') + # # # # save + # saved_by_post = db.relationship('Save_post', back_populates='saver', lazy='dynamic', cascade='all, delete-orphan') + @staticmethod def add_self_follows(): @@ -398,6 +401,7 @@ class Post(db.Model): comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan', lazy='dynamic') liker = db.relationship('Like', back_populates='liked_post', lazy='dynamic', cascade='all') is_anonymous = db.Column(db.Boolean, default=False) + # saver = db.relationship('Save_post', back_populates='saved_post', lazy='dynamic', cascade='all') def like(self, user): if not self.is_liked_by(user): @@ -461,6 +465,14 @@ class Like(db.Model): liker = db.relationship('User', back_populates='liked_post', lazy='joined') liked_post = db.relationship('Post', back_populates='liker', lazy='joined') +# class Save_post(db.Model): +# __tablename__ = 'save_posts' +# save_post_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) +# save_post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), primary_key=True) +# timestamp = db.Column(db.DateTime, default=datetime.utcnow) +# saver = db.relationship('User', back_populates='saved_poster', lazy='joined') +# saved_post = db.relationship('Post', back_populates='saver', lazy='joined') +# # class Collect(db.Model): diff --git a/data-dev.sqlite b/data-dev.sqlite index fa7fcca..d93b30c 100644 Binary files a/data-dev.sqlite and b/data-dev.sqlite differ diff --git a/migrations/README b/migrations/README index 98e4f9c..0e04844 100644 --- a/migrations/README +++ b/migrations/README @@ -1 +1 @@ -Generic single-database configuration. \ No newline at end of file +Single-database configuration for Flask. diff --git a/migrations/alembic.ini b/migrations/alembic.ini index f8ed480..ec9d45c 100644 --- a/migrations/alembic.ini +++ b/migrations/alembic.ini @@ -11,7 +11,7 @@ # Logging configuration [loggers] -keys = root,sqlalchemy,alembic +keys = root,sqlalchemy,alembic,flask_migrate [handlers] keys = console @@ -34,6 +34,11 @@ level = INFO handlers = qualname = alembic +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + [handler_console] class = StreamHandler args = (sys.stderr,) diff --git a/migrations/env.py b/migrations/env.py index 23663ff..68feded 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,8 +1,11 @@ from __future__ import with_statement -from alembic import context -from sqlalchemy import engine_from_config, pool -from logging.config import fileConfig + 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. @@ -17,9 +20,10 @@ logger = logging.getLogger('alembic.env') # 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')) +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, @@ -41,7 +45,9 @@ def run_migrations_offline(): """ url = config.get_main_option("sqlalchemy.url") - context.configure(url=url) + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) with context.begin_transaction(): context.run_migrations() @@ -65,21 +71,19 @@ def run_migrations_online(): directives[:] = [] logger.info('No changes in schema detected.') - engine = engine_from_config(config.get_section(config.config_ini_section), - prefix='sqlalchemy.', - poolclass=pool.NullPool) + connectable = current_app.extensions['migrate'].db.get_engine() - connection = engine.connect() - context.configure(connection=connection, - target_metadata=target_metadata, - process_revision_directives=process_revision_directives, - **current_app.extensions['migrate'].configure_args) + 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 + ) - try: with context.begin_transaction(): context.run_migrations() - finally: - connection.close() + if context.is_offline_mode(): run_migrations_offline() diff --git a/migrations/versions/d3c0f90744f6_.py b/migrations/versions/d3c0f90744f6_.py deleted file mode 100644 index 08a4944..0000000 --- a/migrations/versions/d3c0f90744f6_.py +++ /dev/null @@ -1,240 +0,0 @@ -"""empty message - -Revision ID: d3c0f90744f6 -Revises: -Create Date: 2020-05-26 15:21:36.410197 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'd3c0f90744f6' -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('organizations', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('confirmed', sa.Boolean(), nullable=True), - sa.Column('name', sa.String(length=64), nullable=True), - sa.Column('teacher', sa.String(length=128), nullable=True), - sa.Column('leader_student', sa.String(length=128), nullable=True), - sa.Column('phone', sa.String(length=128), nullable=True), - sa.Column('college', sa.String(length=256), nullable=True), - sa.Column('email', sa.String(length=64), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - op.create_table('roles', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(length=64), nullable=True), - sa.Column('default', sa.Boolean(), nullable=True), - sa.Column('permissions', sa.Integer(), nullable=True), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('name') - ) - with op.batch_alter_table('roles', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_roles_default'), ['default'], unique=False) - - op.create_table('students', - sa.Column('student_id', sa.Integer(), nullable=False), - sa.Column('id_number', sa.String(length=18), nullable=True), - sa.Column('confirmed', sa.Boolean(), nullable=True), - sa.Column('role_id', sa.Integer(), nullable=True), - sa.PrimaryKeyConstraint('student_id') - ) - with op.batch_alter_table('students', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_students_role_id'), ['role_id'], unique=False) - - op.create_table('users', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('ID_number', sa.Integer(), nullable=True), - sa.Column('student_id', sa.Integer(), nullable=True), - sa.Column('confirmed', sa.Boolean(), nullable=True), - sa.Column('email', sa.String(length=64), nullable=True), - sa.Column('username', sa.String(length=64), nullable=True), - sa.Column('password_hash', sa.String(length=128), nullable=True), - sa.Column('role_id', sa.Integer(), nullable=True), - sa.Column('grade', sa.String(length=4), nullable=True), - sa.Column('college', sa.String(length=64), nullable=True), - sa.Column('about_me', sa.Text(), nullable=True), - sa.Column('member_since', sa.DateTime(), nullable=True), - sa.Column('last_seen', sa.DateTime(), nullable=True), - sa.Column('avatar_hash', sa.String(length=32), nullable=True), - sa.Column('avatar_img', sa.String(length=120), nullable=True), - sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('users', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_users_ID_number'), ['ID_number'], unique=True) - batch_op.create_index(batch_op.f('ix_users_email'), ['email'], unique=True) - batch_op.create_index(batch_op.f('ix_users_student_id'), ['student_id'], unique=True) - batch_op.create_index(batch_op.f('ix_users_username'), ['username'], unique=True) - - op.create_table('Activity', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.Column('activity_name', sa.String(length=256), nullable=False), - sa.Column('activity_time', sa.DateTime(), nullable=False), - sa.Column('activity_place', sa.String(length=256), nullable=False), - sa.Column('activity_describe', sa.String(length=256), nullable=False), - sa.Column('Organizer', sa.String(length=256), nullable=False), - sa.Column('is_schoolAgree', sa.Boolean(), nullable=False), - sa.Column('is_invalid', sa.Boolean(), nullable=True), - sa.Column('important', sa.INTEGER(), nullable=True), - sa.Column('announcer_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['announcer_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('Activity', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_Activity_timestamp'), ['timestamp'], unique=False) - - op.create_table('follow', - sa.Column('follower_id', sa.Integer(), nullable=False), - sa.Column('followed_id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['followed_id'], ['users.id'], ), - sa.ForeignKeyConstraint(['follower_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('follower_id', 'followed_id') - ) - op.create_table('notification', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('username', sa.String(length=64), nullable=False), - sa.Column('action', sa.Text(), nullable=False), - sa.Column('object', sa.String(length=64), nullable=False), - sa.Column('object_id', sa.Integer(), nullable=True), - sa.Column('is_read', sa.Boolean(), nullable=True), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.Column('receiver_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['receiver_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('notification', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_notification_timestamp'), ['timestamp'], unique=False) - - op.create_table('posts', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('title', sa.Text(), nullable=True), - sa.Column('body', sa.Text(), nullable=True), - sa.Column('body_html', sa.Text(), nullable=True), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.Column('author_id', sa.Integer(), nullable=True), - sa.Column('important', sa.INTEGER(), nullable=True), - sa.Column('recent_activity', sa.DateTime(), nullable=True), - sa.Column('is_anonymous', sa.Boolean(), nullable=True), - sa.ForeignKeyConstraint(['author_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('posts', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_posts_recent_activity'), ['recent_activity'], unique=False) - batch_op.create_index(batch_op.f('ix_posts_timestamp'), ['timestamp'], unique=False) - - op.create_table('transaction', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.Column('item_name', sa.String(length=64), nullable=False), - sa.Column('item_describe', sa.Text(), nullable=False), - sa.Column('link', sa.Text(), nullable=False), - sa.Column('transaction_mode', sa.String(length=64), nullable=False), - sa.Column('is_sold', sa.Boolean(), nullable=True), - sa.Column('seller_WeChat', sa.Text(), nullable=False), - sa.Column('seller_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['seller_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('transaction', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_transaction_timestamp'), ['timestamp'], unique=False) - - op.create_table('collect', - sa.Column('collecter_id', sa.Integer(), nullable=False), - sa.Column('collected_transaction_id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['collected_transaction_id'], ['transaction.id'], ), - sa.ForeignKeyConstraint(['collecter_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('collecter_id', 'collected_transaction_id') - ) - op.create_table('comments', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('body', sa.Text(), nullable=True), - sa.Column('body_html', sa.Text(), nullable=True), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.Column('disabled', sa.Boolean(), nullable=True), - sa.Column('author_id', sa.Integer(), nullable=True), - sa.Column('post_id', sa.Integer(), nullable=True), - sa.Column('is_anonymous', sa.Boolean(), nullable=True), - sa.Column('replied_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['author_id'], ['users.id'], ), - sa.ForeignKeyConstraint(['post_id'], ['posts.id'], ), - sa.ForeignKeyConstraint(['replied_id'], ['comments.id'], ), - sa.PrimaryKeyConstraint('id') - ) - with op.batch_alter_table('comments', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_comments_timestamp'), ['timestamp'], unique=False) - - op.create_table('likes', - sa.Column('liker_id', sa.Integer(), nullable=False), - sa.Column('liked_post_id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['liked_post_id'], ['posts.id'], ), - sa.ForeignKeyConstraint(['liker_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('liker_id', 'liked_post_id') - ) - op.create_table('want', - sa.Column('wanter_id', sa.Integer(), nullable=False), - sa.Column('wanted_Activity_id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['wanted_Activity_id'], ['Activity.id'], ), - sa.ForeignKeyConstraint(['wanter_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('wanter_id', 'wanted_Activity_id') - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('want') - op.drop_table('likes') - with op.batch_alter_table('comments', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_comments_timestamp')) - - op.drop_table('comments') - op.drop_table('collect') - with op.batch_alter_table('transaction', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_transaction_timestamp')) - - op.drop_table('transaction') - with op.batch_alter_table('posts', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_posts_timestamp')) - batch_op.drop_index(batch_op.f('ix_posts_recent_activity')) - - op.drop_table('posts') - with op.batch_alter_table('notification', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_notification_timestamp')) - - op.drop_table('notification') - op.drop_table('follow') - with op.batch_alter_table('Activity', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_Activity_timestamp')) - - op.drop_table('Activity') - with op.batch_alter_table('users', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_users_username')) - batch_op.drop_index(batch_op.f('ix_users_student_id')) - batch_op.drop_index(batch_op.f('ix_users_email')) - batch_op.drop_index(batch_op.f('ix_users_ID_number')) - - op.drop_table('users') - with op.batch_alter_table('students', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_students_role_id')) - - op.drop_table('students') - with op.batch_alter_table('roles', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_roles_default')) - - op.drop_table('roles') - op.drop_table('organizations') - # ### end Alembic commands ###