summaryrefslogtreecommitdiff
path: root/alembic
diff options
context:
space:
mode:
Diffstat (limited to 'alembic')
-rw-r--r--alembic/README1
-rw-r--r--alembic/env.py84
-rw-r--r--alembic/script.py.mako24
-rw-r--r--alembic/versions/19d57d527c14_create_posts_table.py27
-rw-r--r--alembic/versions/4fa455e8771b_auto_add_votes_table.py34
-rw-r--r--alembic/versions/7fb31859972f_add_revision_table.py33
-rw-r--r--alembic/versions/e68b0c5f7380_add_foreign_key_to_posts_table.py29
-rw-r--r--alembic/versions/f18f4db81d80_add_content_column_to_posts_table.py26
-rw-r--r--alembic/versions/f60bfe68b20f_add_other_posts_columns.py30
9 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..acb3a7a
--- /dev/null
+++ b/alembic/env.py
@@ -0,0 +1,84 @@
+from logging.config import fileConfig
+
+from sqlalchemy import engine_from_config
+from sqlalchemy import pool
+
+from alembic import context
+
+from app.models import Base
+from app.config import settings
+
+
+# this is the Alembic Config object, which provides
+# access to the values within the .ini file in use.
+config = context.config
+config.set_main_option("sqlalchemy.url", f'postgresql+psycopg2://{settings.database_username}:\
+ {settings.database_password}@{settings.database_hostname}:{settings.database_port}\
+ /{settings.database_name}')
+
+# 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 = Base.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,
+ 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/19d57d527c14_create_posts_table.py b/alembic/versions/19d57d527c14_create_posts_table.py
new file mode 100644
index 0000000..8550646
--- /dev/null
+++ b/alembic/versions/19d57d527c14_create_posts_table.py
@@ -0,0 +1,27 @@
+"""create posts table
+
+Revision ID: 19d57d527c14
+Revises:
+Create Date: 2022-02-24 01:18:04.684095
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '19d57d527c14'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.create_table("posts", sa.Column('id', sa.Integer(), nullable=False, primary_key=True ),
+ sa.Column("title", sa.String(), nullable=False) )
+ pass
+
+
+def downgrade():
+ op.drop_table('posts')
+ pass
diff --git a/alembic/versions/4fa455e8771b_auto_add_votes_table.py b/alembic/versions/4fa455e8771b_auto_add_votes_table.py
new file mode 100644
index 0000000..6c60f85
--- /dev/null
+++ b/alembic/versions/4fa455e8771b_auto_add_votes_table.py
@@ -0,0 +1,34 @@
+"""auto-add Votes table
+
+Revision ID: 4fa455e8771b
+Revises: f60bfe68b20f
+Create Date: 2022-02-24 02:53:06.350609
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '4fa455e8771b'
+down_revision = 'f60bfe68b20f'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_table('votes',
+ sa.Column('user_id', sa.Integer(), nullable=False),
+ sa.Column('post_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['post_id'], ['posts.id'], ondelete='CASCADE'),
+ sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
+ sa.PrimaryKeyConstraint('user_id', 'post_id')
+ )
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_table('votes')
+ # ### end Alembic commands ###
diff --git a/alembic/versions/7fb31859972f_add_revision_table.py b/alembic/versions/7fb31859972f_add_revision_table.py
new file mode 100644
index 0000000..440e695
--- /dev/null
+++ b/alembic/versions/7fb31859972f_add_revision_table.py
@@ -0,0 +1,33 @@
+"""add revision table
+
+Revision ID: 7fb31859972f
+Revises: f18f4db81d80
+Create Date: 2022-02-24 02:14:33.592965
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '7fb31859972f'
+down_revision = 'f18f4db81d80'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.create_table("users",
+ sa.Column("id", sa.Integer(), nullable=False),
+ sa.Column("email", sa.String(), nullable=False),
+ sa.Column("password", sa.String(), nullable=False),
+ sa.Column("created_at", sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
+ sa.PrimaryKeyConstraint("id"),
+ sa.UniqueConstraint("email")
+ )
+ pass
+
+
+def downgrade():
+ op.drop_table("users")
+ pass
diff --git a/alembic/versions/e68b0c5f7380_add_foreign_key_to_posts_table.py b/alembic/versions/e68b0c5f7380_add_foreign_key_to_posts_table.py
new file mode 100644
index 0000000..4ef704a
--- /dev/null
+++ b/alembic/versions/e68b0c5f7380_add_foreign_key_to_posts_table.py
@@ -0,0 +1,29 @@
+"""Add foreign-key to posts table
+
+Revision ID: e68b0c5f7380
+Revises: 7fb31859972f
+Create Date: 2022-02-24 02:21:51.623836
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'e68b0c5f7380'
+down_revision = '7fb31859972f'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.add_column("posts", sa.Column("owner_id", sa.Integer(), nullable=False) )
+ op.create_foreign_key("post_users_fk", source_table="posts", referent_table="users",
+ local_cols=["owner_id"], remote_cols=["id"], ondelete="CASCADE")
+ pass
+
+
+def downgrade():
+ op.drop_constraint("post_users_fk", table_name="posts")
+ op.drop_column("posts", "owner_id")
+ pass
diff --git a/alembic/versions/f18f4db81d80_add_content_column_to_posts_table.py b/alembic/versions/f18f4db81d80_add_content_column_to_posts_table.py
new file mode 100644
index 0000000..a783bf5
--- /dev/null
+++ b/alembic/versions/f18f4db81d80_add_content_column_to_posts_table.py
@@ -0,0 +1,26 @@
+"""add content column to posts table
+
+Revision ID: f18f4db81d80
+Revises: 19d57d527c14
+Create Date: 2022-02-24 02:03:54.409749
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'f18f4db81d80'
+down_revision = '19d57d527c14'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.add_column("posts", sa.Column("content", sa.String(), nullable=False) )
+ pass
+
+
+def downgrade():
+ op.drop_column("posts", "content")
+ pass
diff --git a/alembic/versions/f60bfe68b20f_add_other_posts_columns.py b/alembic/versions/f60bfe68b20f_add_other_posts_columns.py
new file mode 100644
index 0000000..0f2c369
--- /dev/null
+++ b/alembic/versions/f60bfe68b20f_add_other_posts_columns.py
@@ -0,0 +1,30 @@
+"""add other posts columns
+
+Revision ID: f60bfe68b20f
+Revises: e68b0c5f7380
+Create Date: 2022-02-24 02:26:37.946997
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'f60bfe68b20f'
+down_revision = 'e68b0c5f7380'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.add_column("posts", sa.Column("published", sa.Boolean(), nullable=False,
+ server_default="TRUE"),)
+ op.add_column("posts", sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False,
+ server_default=sa.text("NOW()")),)
+ pass
+
+
+def downgrade():
+ op.drop_column("posts", "published")
+ op.drop_column("posts", "created_at")
+ pass