summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-02-24 02:57:29 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-02-24 02:57:29 +0200
commit3ce694bbebb9c24606e83f8923ca8055b7c9c63d (patch)
tree79bdc75a28eadabb18fb65978f0dec914eea546d
parentf1ab105a7620160c16d7c91bdf3ca1cff6deef7c (diff)
Added Alembic stuff
-rw-r--r--alembic.ini102
-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
10 files changed, 390 insertions, 0 deletions
diff --git a/alembic.ini b/alembic.ini
new file mode 100644
index 0000000..299d321
--- /dev/null
+++ b/alembic.ini
@@ -0,0 +1,102 @@
+# A generic, single database configuration.
+
+[alembic]
+# path to migration scripts
+script_location = alembic
+
+# template used to generate migration files
+# file_template = %%(rev)s_%%(slug)s
+
+# sys.path path, will be prepended to sys.path if present.
+# defaults to the current working directory.
+prepend_sys_path = .
+
+# timezone to use when rendering the date within the migration file
+# as well as the filename.
+# If specified, requires the python-dateutil library that can be
+# installed by adding `alembic[tz]` to the pip requirements
+# 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 alembic/versions. When using multiple version
+# directories, initial revisions must be specified with --version-path.
+# The path separator used here should be the separator specified by "version_path_separator" below.
+# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
+
+# version path separator; As mentioned above, this is the character used to split
+# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
+# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
+# Valid values for version_path_separator are:
+#
+# version_path_separator = :
+# version_path_separator = ;
+# version_path_separator = space
+version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
+
+# the output encoding used when revision files
+# are written from script.py.mako
+# output_encoding = utf-8
+
+sqlalchemy.url =
+
+
+[post_write_hooks]
+# post_write_hooks defines scripts or Python functions that are run
+# on newly generated revision scripts. See the documentation for further
+# detail and examples
+
+# format using "black" - use the console_scripts runner, against the "black" entrypoint
+# hooks = black
+# black.type = console_scripts
+# black.entrypoint = black
+# black.options = -l 79 REVISION_SCRIPT_FILENAME
+
+# 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
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