Skip to content

Instantly share code, notes, and snippets.

@poteznyKrolik
Created March 31, 2021 20:57
Show Gist options
  • Select an option

  • Save poteznyKrolik/afb2f2e93adf44f731cd6762bf48a08e to your computer and use it in GitHub Desktop.

Select an option

Save poteznyKrolik/afb2f2e93adf44f731cd6762bf48a08e to your computer and use it in GitHub Desktop.
open raw cursor for baseline
"""BASELINE
Revision ID: 98d0325960d8
Revises:
Create Date: 2021-03-18 16:56:53.584644
"""
import sqlalchemy as sa
from sqlalchemy.exc import SQLAlchemyError
from alembic import op
import pathlib
import subprocess
import psycopg2
from urllib.parse import urlparse
import logging
import sys
# revision identifiers, used by Alembic.
revision = '98d0325960d8'
down_revision = None
branch_labels = None
depends_on = None
path = pathlib.Path(__file__).parent.parent.parent.absolute()
baseline_path = f"{path}/baseline/affiliate_pg_baseline.dump.sql"
def upgrade():
# Open the .sql file
sql_file = open(baseline_path, 'r')
conn = op.get_bind()
# sys.stdout.write("ok")
# raw_cursor = conn.engine.raw_connection().cursor()
print(conn.engine.url)
result = urlparse(str(conn.engine.url))
# also in python 3+ use: urlparse("YourUrl") not urlparse.urlparse("YourUrl")
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
port = result.port
conn = psycopg2.connect(
database = database,
user = username,
password = password,
host = hostname,
port = 5432
)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cur:
cur.execute(open(baseline_path).read())
def downgrade():
op.get_bind().execute('''
Select 1;
''')
if __name__ == "__main__":
upgrade()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment