Skip to content

Commit

Permalink
refactor(db): get rid of sqlite and its migration complexities
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Moreno authored and photonbit committed Oct 18, 2021
1 parent b8791f8 commit 213394d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 252 deletions.
9 changes: 4 additions & 5 deletions giges/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from pathlib import Path
from typing import Dict


Expand Down Expand Up @@ -39,17 +38,17 @@ class DevelopmentSettings(BaseSettings):
ENVIRONMENT = "development"
DEBUG = True
SERVER_BASE_URI = "http://localhost:8080"
SQLALCHEMY_DATABASE_URI = (
f"sqlite:///{Path(__file__).parents[1]}/development.db"
SQLALCHEMY_DATABASE_URI = os.getenv("GIGES_DATABASE_URI") or (
"postgresql://postgres@localhost:5432/giges_dev"
)


class TestingSettings(BaseSettings):
ENVIRONMENT = "testing"
SERVER_BASE_URI = "http://localhost:8080"
ASANA_TOKEN = "FAKETOKEN"
SQLALCHEMY_DATABASE_URI = (
f"sqlite:///{Path(__file__).parents[1]}/testing.db"
SQLALCHEMY_DATABASE_URI = os.getenv("GIGES_DATABASE_URI") or (
"postgresql://postgres@localhost:5432/giges_test"
)


Expand Down
3 changes: 0 additions & 3 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from flask import current_app
from sqlalchemy import engine_from_config, pool

from migrations.utils import is_sqlite

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
Expand Down Expand Up @@ -88,7 +86,6 @@ def process_revision_directives(
process_revision_directives=process_revision_directives,
include_schemas=True,
compare_type=True,
render_as_batch=is_sqlite(),
**current_app.extensions["migrate"].configure_args,
)

Expand Down
7 changes: 0 additions & 7 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Create Date: ${create_date}
from alembic import op
import sqlalchemy as sa

from migrations.utils import is_sqlite
${imports if imports else ""}

# revision identifiers, used by Alembic.
Expand All @@ -19,14 +18,8 @@ depends_on = ${repr(depends_on)}


def upgrade() -> None:
# if is_sqlite():
${upgrades if upgrades else "pass"}
# else:
# pass


def downgrade() -> None:
# if is_sqlite():
${downgrades if downgrades else "pass"}
# else:
# pass
24 changes: 0 additions & 24 deletions migrations/utils.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import sqlalchemy as sa
from alembic import op

from migrations.utils import is_sqlite

# revision identifiers, used by Alembic.
revision = "2bb66a995457"
down_revision = "a6a81fe8dbf0"
Expand All @@ -18,91 +16,45 @@


def upgrade() -> None:
if is_sqlite():
with op.batch_alter_table("asana_event", schema=None) as batch_op:
batch_op.drop_constraint(
"asana_event_webhook_fk", type_="foreignkey"
)
batch_op.create_foreign_key(
"asana_event_webhook_fk",
"asana_webhook",
["webhook_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)

with op.batch_alter_table("asana_webhook", schema=None) as batch_op:
batch_op.add_column(
sa.Column("project_id", sa.CHAR(length=36), nullable=True)
)
batch_op.create_foreign_key(
"asana_webhook_project_fk",
"asana_project",
["project_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)
else:
op.drop_constraint(
"asana_event_webhook_fk", "asana_event", type_="foreignkey"
)
op.create_foreign_key(
"asana_event_webhook_fk",
"asana_event",
"asana_webhook",
["webhook_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)
op.add_column(
"asana_webhook",
sa.Column("project_id", sa.CHAR(length=36), nullable=True),
)
op.create_foreign_key(
"asana_webhook_project_fk",
"asana_webhook",
"asana_project",
["project_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)
op.drop_constraint(
"asana_event_webhook_fk", "asana_event", type_="foreignkey"
)
op.create_foreign_key(
"asana_event_webhook_fk",
"asana_event",
"asana_webhook",
["webhook_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)
op.add_column(
"asana_webhook",
sa.Column("project_id", sa.CHAR(length=36), nullable=True),
)
op.create_foreign_key(
"asana_webhook_project_fk",
"asana_webhook",
"asana_project",
["project_id"],
["id"],
initially="DEFERRED",
deferrable=True,
)


def downgrade() -> None:
if is_sqlite():
with op.batch_alter_table("asana_webhook", schema=None) as batch_op:
batch_op.drop_constraint(
"asana_webhook_project_fk", type_="foreignkey"
)
batch_op.drop_column("project_id")

with op.batch_alter_table("asana_event", schema=None) as batch_op:
batch_op.drop_constraint(
"asana_event_webhook_fk", type_="foreignkey"
)
batch_op.create_foreign_key(
"asana_event_webhook_fk",
"asana_webhook",
["webhook_id"],
["id"],
)

else:
op.drop_constraint(
"asana_webhook_project_fk", "asana_webhook", type_="foreignkey"
)
op.drop_column("asana_webhook", "project_id")
op.drop_constraint(
"asana_event_webhook_fk", "asana_event", type_="foreignkey"
)
op.create_foreign_key(
"asana_event_webhook_fk",
"asana_event",
"asana_webhook",
["webhook_id"],
["id"],
)
op.drop_constraint(
"asana_webhook_project_fk", "asana_webhook", type_="foreignkey"
)
op.drop_column("asana_webhook", "project_id")
op.drop_constraint(
"asana_event_webhook_fk", "asana_event", type_="foreignkey"
)
op.create_foreign_key(
"asana_event_webhook_fk",
"asana_event",
"asana_webhook",
["webhook_id"],
["id"],
)
Loading

0 comments on commit 213394d

Please sign in to comment.