Oracle Dialect Init

1 parent 557aaf37
......@@ -27,7 +27,7 @@ from .models.handlers import LogDBSession
from .models.meta import Base
from .models.users import init_model
from .models import Route
# from .models import TABLE_ARGS
# from deform import ZPTRendererFactory, Form
# from deform.widget import default_resource_registry
......@@ -302,10 +302,24 @@ def get_config(settings):
return config
# def get_schema_for_dialect(engine_dialect_name: str) -> str | None:
# """Returns 'public' for PostgreSQL or None for Oracle to match native defaults."""
# if "postgresql" in engine_dialect_name.lower():
# return "public"
# return None # Triggers default user-schema fallback on Oracle
def init_db(settings):
engine = engine_from_config(
settings, 'sqlalchemy.', client_encoding='utf8',
max_identifier_length=30) # , convert_unicode=True
# global TABLE_ARGS
# # Resolve the target schema namespace dynamically based on active engine
# TABLE_ARGS = dict(extend_existing=True,
# schema=get_schema_for_dialect(engine.dialect.name))
DBSession.configure(bind=engine)
LogDBSession.configure(bind=engine)
Base.metadata.bind = engine
......
import logging
from logging.config import fileConfig
from sqlalchemy import engine_from_config
......@@ -11,6 +12,19 @@ from opensipkd.models import Base
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
log = logging.getLogger(__name__)
url = config.get_main_option("sqlalchemy.url")
if url.find("oracledb") > -1:
log.error("OracleDB used: %s", url)
try:
import oracledb
lib_dir = config.get_main_option("lib_dir")
if lib_dir:
oracledb.init_oracle_client(lib_dir=lib_dir)
except Exception as e:
log.error(f"An error occurred: {str(e)}")
log.error("Oracle not initialize")
if config.config_file_name is not None:
fileConfig(config.config_file_name)
......
......@@ -31,7 +31,7 @@ def upgrade():
sa.Column('update_uid', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_eselon')),
schema='public'
# schema='public'
)
op.create_table('pangkat',
sa.Column('pangkat', sa.String(length=32), nullable=True),
......@@ -45,7 +45,7 @@ def upgrade():
sa.Column('update_uid', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_pangkat')),
schema='public'
# schema='public'
)
op.create_table('jabatan',
sa.Column('jenis', sa.SmallInteger(), nullable=True),
......@@ -62,10 +62,10 @@ def upgrade():
sa.Column('create_uid', sa.Integer(), nullable=True),
sa.Column('update_uid', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['eselon_id'], ['public.eselon.id'], name=op.f(
sa.ForeignKeyConstraint(['eselon_id'], ['eselon.id'], name=op.f(
'fk_jabatan_eselon_id_eselon')),
sa.PrimaryKeyConstraint('id', name=op.f('pk_jabatan')),
schema='public'
# schema='public'
)
op.create_table('partner_departemen',
sa.Column('partner_id', sa.Integer(), nullable=True),
......@@ -74,9 +74,9 @@ def upgrade():
sa.Column('mulai', sa.DateTime(), nullable=True),
sa.Column('selesai', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['departemen_id'], ['public.departemen.id'], name=op.f(
sa.ForeignKeyConstraint(['departemen_id'], ['departemen.id'], name=op.f(
'fk_partner_departemen_departemen_id_departemen')),
sa.ForeignKeyConstraint(['jabatan_id'], ['public.jabatan.id'], name=op.f(
sa.ForeignKeyConstraint(['jabatan_id'], ['jabatan.id'], name=op.f(
'fk_partner_departemen_jabatan_id_jabatan')),
sa.ForeignKeyConstraint(['partner_id'], ['partner.id'], name=op.f(
'fk_partner_departemen_partner_id_partner')),
......@@ -84,15 +84,15 @@ def upgrade():
'id', name=op.f('pk_partner_departemen')),
sa.UniqueConstraint('partner_id', 'departemen_id',
'jabatan_id', 'mulai', name='partner_dept_uq'),
schema='public'
# schema='public'
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('partner_departemen', schema='public')
op.drop_table('jabatan', schema='public')
op.drop_table('pangkat', schema='public')
op.drop_table('eselon', schema='public')
op.drop_table('partner_departemen',)# schema='public')
op.drop_table('jabatan', )#schema='public')
op.drop_table('pangkat', ) # schema='public')
op.drop_table('eselon', ) # schema='public')
# ### end Alembic commands ###
......@@ -32,12 +32,12 @@ def upgrade():
sa.Column('update_uid', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_text_printers')),
schema='public'
# schema='public'
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('text_printers', schema='public')
op.drop_table('text_printers', ) #schema='public')
# ### end Alembic commands ###
......@@ -23,7 +23,8 @@ DBSession = scoped_session(session_factory)
register(DBSession)
ziggurat_foundations.models.DBSession = DBSession
TABLE_ARGS = dict(extend_existing=True, schema="public")
TABLE_ARGS = dict(extend_existing=True,)
#schema="public")
def flush(row, db_session=DBSession):
......
......@@ -5,9 +5,10 @@ from ..models import (NamaModel, TABLE_ARGS)
class _Departemen(NamaModel):
__table_args__ = (TABLE_ARGS,)
# __table_args__ = (TABLE_ARGS,)
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('public.departemen.id'))
# parent_id = Column(Integer, ForeignKey('public.departemen.id'))
parent_id = Column(Integer, ForeignKey('departemen.id'))
kategori = Column(String(32))
alamat = Column(String(255))
singkat = Column(String(32))
......
from sqlalchemy import (Column, Integer, String, DateTime, func, )
from sqlalchemy import (Column, Integer, String, DateTime, func, Text)
from sqlalchemy.orm import (scoped_session, sessionmaker, )
from ..models.base import CommonModel
from ..models.meta import Base
......@@ -11,9 +11,9 @@ class Log(Base, CommonModel):
__tablename__ = 'logs'
id = Column(Integer, primary_key=True) # auto incrementing
line_id = Column(String(32), nullable=False, unique=True)
logger = Column(String) # the name of the logger. (e.g. myapp.views)
level = Column(String) # info, debug, or error?
trace = Column(String) # the full traceback printout
logger = Column(Text) # the name of the logger. (e.g. myapp.views)
level = Column(Text) # info, debug, or error?
trace = Column(Text) # the full traceback printout
msg = Column(String, nullable=False)
created_at = Column(
DateTime(timezone=True),
......
user_id/users.user_name,group_id/groups.group_name
admin,Superuser
\ No newline at end of file
......@@ -115,6 +115,7 @@ def restore_csv(table, filename, get_file_func=get_file, db_session=DBSession):
eng = db_session.get_bind()
q = db_session.query(table)
if q.first():
log.error("Restore discarded")
return
with get_file_func(filename) as f:
reader = csv.DictReader(f)
......@@ -135,7 +136,7 @@ def restore_csv(table, filename, get_file_func=get_file, db_session=DBSession):
raise e
fname_orig = t[0]
schema = "public"
schema = None # "public"
if t[1:]:
t_array = t[1].split('.')
if len(t_array) == 2:
......@@ -218,7 +219,7 @@ def append_csv(table, filename, keys, get_file_func=get_file,
# print(dir(table.__table__))
# print("____")
schema = hasattr(
table.__table__, "schema") and table.__table__.schema or "public"
table.__table__, "schema") and table.__table__.schema or None # "public"
columns_table = insp.get_columns(table.__tablename__, schema)
fields = {}
for c in columns_table:
......@@ -245,7 +246,7 @@ def append_csv(table, filename, keys, get_file_func=get_file,
raise e
fname_orig = t[0]
schema = "public"
schema = None # "public"
if t[1:]:
t_array = t[1].split('.')
if len(t_array) == 2:
......@@ -255,7 +256,7 @@ def append_csv(table, filename, keys, get_file_func=get_file,
schema = t_array[0]
foreign_table = t_array[1]
foreign_field = t_array[2]
log.debug("%s.%s", schema, foreign_table)
foreign_table = Table(foreign_table, base.metadata,
# autoload=True, # merubah v1.4 ke v.2
autoload_with=eng,
......@@ -286,8 +287,8 @@ def append_csv(table, filename, keys, get_file_func=get_file,
with eng.connect() as conn:
q = conn.execute(sql)
row = q.fetchone()
if not row:
raise Exception(f"Foreign key value '{value}' not found in table '{foreign_table.name}' for field '{fname}'")
# if not row:
# raise Exception(f"Foreign key value '{value}' not found in table '{foreign_table.name}' for field '{fname}'")
value = row and row.id or None
q.close()
# connection.close()
......@@ -377,8 +378,9 @@ def reset_sequence_(cls, seq):
def reset_sequences():
reset_sequence_(User, 'users_id_seq')
reset_sequence_(Group, 'groups_id_seq')
pass
# reset_sequence_(User, 'users_id_seq')
# reset_sequence_(Group, 'groups_id_seq')
def alembic_run(ini_file, name=None):
......@@ -430,8 +432,9 @@ def main(argv=sys.argv):
q = DBSession.query(User).filter_by(id=1)
user = q.first()
init_model()
password = ask_password(user.user_name)
UserService.set_password(user, password)
if user:
password = ask_password(user.user_name)
UserService.set_password(user, password)
append_csv(Group, 'groups.csv', ['group_name'])
restore_csv(UserGroup, 'users_groups.csv')
append_csv(Permission, 'permissions.csv', ['perm_name'])
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!