Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
aa.gusti
/
opensipkd-base
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f409c726
authored
Jun 26, 2026
by
aa.gustiana@gmail.com
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add error handling for table creation in Alembic migrations
1 parent
884d29c3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
111 additions
and
72 deletions
opensipkd/base/alembic/versions/021a8a342d5b_init_db.py
opensipkd/base/alembic/versions/10a68b1510a6_.py
opensipkd/base/alembic/versions/663de8bbd563_text_printer_init.py
opensipkd/base/models/__init__.py
opensipkd/base/models/departmen.py
opensipkd/base/models/users.py
opensipkd/base/views/departemen.py
opensipkd/base/alembic/versions/021a8a342d5b_init_db.py
View file @
f409c72
This diff is collapsed.
Click to expand it.
opensipkd/base/alembic/versions/10a68b1510a6_.py
View file @
f409c72
...
...
@@ -18,7 +18,8 @@ depends_on = None
def
upgrade
():
# ### commands auto generated by Alembic - please adjust! ###
op
.
create_table
(
'eselon'
,
try
:
op
.
create_table
(
'eselon'
,
sa
.
Column
(
'pangkat'
,
sa
.
String
(
length
=
32
),
nullable
=
True
),
sa
.
Column
(
'ruang'
,
sa
.
String
(
length
=
1
),
nullable
=
True
),
sa
.
Column
(
'tunjangan'
,
sa
.
BigInteger
(),
nullable
=
True
),
...
...
@@ -29,11 +30,15 @@ def upgrade():
sa
.
Column
(
'updated'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'create_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'update_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
nullable
=
False
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
sa
.
Identity
(
always
=
True
),
primary_key
=
True
,
nullable
=
False
),
sa
.
PrimaryKeyConstraint
(
'id'
,
name
=
op
.
f
(
'pk_eselon'
)),
# schema='public'
)
op
.
create_table
(
'pangkat'
,
except
Exception
as
e
:
print
(
str
(
e
))
try
:
op
.
create_table
(
'pangkat'
,
sa
.
Column
(
'pangkat'
,
sa
.
String
(
length
=
32
),
nullable
=
True
),
sa
.
Column
(
'ruang'
,
sa
.
String
(
length
=
1
),
nullable
=
True
),
sa
.
Column
(
'nama'
,
sa
.
String
(
length
=
128
),
nullable
=
False
),
...
...
@@ -43,11 +48,15 @@ def upgrade():
sa
.
Column
(
'updated'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'create_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'update_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
nullable
=
False
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
sa
.
Identity
(
always
=
True
),
primary_key
=
True
,
nullable
=
False
),
sa
.
PrimaryKeyConstraint
(
'id'
,
name
=
op
.
f
(
'pk_pangkat'
)),
# schema='public'
)
op
.
create_table
(
'jabatan'
,
except
Exception
as
e
:
print
(
str
(
e
))
try
:
op
.
create_table
(
'jabatan'
,
sa
.
Column
(
'jenis'
,
sa
.
SmallInteger
(),
nullable
=
True
),
sa
.
Column
(
'nama_lain'
,
sa
.
String
(
length
=
128
),
nullable
=
True
),
...
...
@@ -61,19 +70,24 @@ def upgrade():
sa
.
Column
(
'updated'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'create_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'update_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
nullable
=
False
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
sa
.
Identity
(
always
=
True
),
primary_key
=
True
,
nullable
=
False
),
sa
.
ForeignKeyConstraint
([
'eselon_id'
],
[
'eselon.id'
],
name
=
op
.
f
(
'fk_jabatan_eselon_id_eselon'
)),
sa
.
PrimaryKeyConstraint
(
'id'
,
name
=
op
.
f
(
'pk_jabatan'
)),
# schema='public'
)
op
.
create_table
(
'partner_departemen'
,
except
Exception
as
e
:
print
(
str
(
e
))
try
:
op
.
create_table
(
'partner_departemen'
,
sa
.
Column
(
'partner_id'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'departemen_id'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'jabatan_id'
,
sa
.
SmallInteger
(),
nullable
=
True
),
sa
.
Column
(
'mulai'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'selesai'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
nullable
=
False
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
sa
.
Identity
(
always
=
True
),
primary_key
=
True
,
nullable
=
False
),
sa
.
ForeignKeyConstraint
([
'departemen_id'
],
[
'departemen.id'
],
name
=
op
.
f
(
'fk_partner_departemen_departemen_id_departemen'
)),
sa
.
ForeignKeyConstraint
([
'jabatan_id'
],
[
'jabatan.id'
],
name
=
op
.
f
(
...
...
@@ -86,6 +100,9 @@ def upgrade():
'jabatan_id'
,
'mulai'
,
name
=
'partner_dept_uq'
),
# schema='public'
)
except
Exception
as
e
:
print
(
str
(
e
))
# ### end Alembic commands ###
...
...
opensipkd/base/alembic/versions/663de8bbd563_text_printer_init.py
View file @
f409c72
...
...
@@ -30,7 +30,8 @@ def upgrade():
sa
.
Column
(
'updated'
,
sa
.
DateTime
(),
nullable
=
True
),
sa
.
Column
(
'create_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'update_uid'
,
sa
.
Integer
(),
nullable
=
True
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
nullable
=
False
),
sa
.
Column
(
'id'
,
sa
.
Integer
(),
sa
.
Identity
(
always
=
True
),
primary_key
=
True
,
nullable
=
False
),
sa
.
PrimaryKeyConstraint
(
'id'
,
name
=
op
.
f
(
'pk_text_printers'
)),
# schema='public'
)
...
...
opensipkd/base/models/__init__.py
View file @
f409c72
from
datetime
import
datetime
,
timezone
import
sqlalchemy
as
sa
class
ForceUTCDatetime
(
sa
.
TypeDecorator
):
"""Ensures datetimes are timezone-aware UTC objects across both DBs."""
impl
=
sa
.
TIMESTAMP
(
timezone
=
True
)
cache_ok
=
True
def
process_bind_param
(
self
,
value
,
dialect
):
if
value
is
not
None
:
if
value
.
tzinfo
is
None
:
# Interpret naive datetime as UTC or handle according to business logic
value
=
value
.
replace
(
tzinfo
=
timezone
.
utc
)
else
:
# Convert any other timezone directly to UTC
value
=
value
.
astimezone
(
timezone
.
utc
)
return
value
def
process_result_value
(
self
,
value
,
dialect
):
if
value
is
not
None
and
value
.
tzinfo
is
None
:
# Force the Python object to be aware if Oracle stripped the offset
return
value
.
replace
(
tzinfo
=
timezone
.
utc
)
return
value
from
.meta
import
*
from
.common
import
*
from
.base
import
*
...
...
opensipkd/base/models/departmen.py
View file @
f409c72
...
...
@@ -59,72 +59,66 @@ class _Departemen(NamaModel):
eng
=
cls
.
db_session
.
get_bind
()
if
eng
.
dialect
.
name
.
lower
()
==
"oracle"
:
sql
=
"""
SELECT
--LPAD(' ', (LEVEL - 1) * 2) || nama AS hierarchy,
SYS_CONNECT_BY_PATH(nama, '/') AS hierarchy,
id,
kode,
nama,
parent_id,
status,
(LEVEL - 1) AS lvl,
SYS_CONNECT_BY_PATH(id, ',') AS path
FROM
departemen
START WITH
parent_id IS NULL
CONNECT BY
PRIOR id = parent_id
ORDER SIBLINGS BY
id
"""
SELECT
--LPAD(' ', (LEVEL - 1) * 2) || nama AS hierarchy,
SYS_CONNECT_BY_PATH(nama, '/') AS hierarchy,
id,
kode,
nama,
parent_id,
status,
(LEVEL - 1) AS lvl,
SYS_CONNECT_BY_PATH(id, ',') AS path
FROM
departemen
START WITH
parent_id IS NULL
CONNECT BY
PRIOR id = parent_id
ORDER SIBLINGS BY
id
"""
else
:
sql
=
"""
WITH RECURSIVE dep_tree AS (
SELECT
id,
kode,
nama,
parent_id,
status,
0 AS lvl,
ARRAY[id] AS path
FROM
public.departemen
WHERE
{str_where}
UNION ALL
SELECT
c.id,
c.kode,
c.nama,
c.parent_id,
c.status,
ct.level + 1,
ct.path || c.id
FROM
public.departemen c
JOIN
dep_tree ct ON c.parent_id = ct.id
)
SELECT
REPEAT(' ', lvl) || nama AS hierarchy, -- Indent names based on level
WITH RECURSIVE dep_tree AS (
SELECT
id,
kode,
nama,
nama
::text AS nama
,
parent_id,
status,
level,
path
FROM
dep_tree
"""
.
format
(
str_where
=
str_where
)
0 AS lvl,
ARRAY[id] AS path
FROM departemen
UNION ALL
SELECT
c.id,
c.kode,
ct.nama || '/' || c.nama::text AS nama,
c.parent_id,
c.status,
ct.lvl + 1,
ct.path || c.id
FROM departemen c
JOIN dep_tree ct ON c.parent_id = ct.id
)
SELECT DISTINCT ON (kode)
id,
kode,
nama as hierarchy,
parent_id,
status,
lvl,
path
FROM dep_tree
"""
#.format(str_where=str_where)
if
search
:
sql
=
f
"{sql} WHERE nama ILIKE '
%
{search}
%
' "
sql
=
sql
+
"""ORDER BY
path;
"""
sql
=
sql
+
"""ORDER BY
kode, path
"""
return
cls
.
db_session
.
execute
(
text
(
sql
))
.
fetchall
()
...
...
opensipkd/base/models/users.py
View file @
f409c72
from
datetime
import
datetime
import
logging
from
math
import
log
from
requests
import
session
import
pytz
import
sqlalchemy
as
sa
from
pyramid.authorization
import
(
Allow
,
Authenticated
,
ALL_PERMISSIONS
)
# from sqlalchemy import TIMESTAMP
from
sqlalchemy
import
(
Column
,
Integer
,
DateTime
,
SmallInteger
,
String
)
from
sqlalchemy.orm
import
(
relationship
,
backref
)
...
...
@@ -28,7 +26,7 @@ from opensipkd.tools import as_timezone
from
.base
import
CommonModel
,
DBSession
,
DefaultModel
from
.meta
import
Base
from
.base
import
TABLE_ARGS
from
.
import
ForceUTCDatetime
log
=
logging
.
getLogger
(
__name__
)
class
_GroupPermission
(
GroupPermissionMixin
):
pass
...
...
@@ -93,7 +91,7 @@ class _UserResourcePermission(UserResourcePermissionMixin):
class
UserResourcePermission
(
_UserResourcePermission
,
Base
):
pass
from
sqlalchemy
import
TIMESTAMP
class
_User
(
UserMixin
,
BaseModel
):
db_session
=
DBSession
last_login_date
=
Column
(
DateTime
(
timezone
=
True
),
nullable
=
True
)
...
...
@@ -106,7 +104,8 @@ class _User(UserMixin, BaseModel):
# 'Asia/Jakarta')),
# server_default="2000-01-01 01:01+7",
# )
security_code_date
=
Column
(
TIMESTAMP
(
timezone
=
True
))
# security_code_date = Column(TIMESTAMP(timezone=True))
security_code_date
=
Column
(
ForceUTCDatetime
)
api_key
=
Column
(
String
(
256
))
partner_id
=
Column
(
Integer
)
# , ForeignKey(Partner.id))
company_id
=
Column
(
Integer
)
# , ForeignKey(Partner.id))
...
...
opensipkd/base/views/departemen.py
View file @
f409c72
...
...
@@ -179,7 +179,10 @@ class Views(BaseView):
url_dict
=
request
.
matchdict
if
url_dict
[
'act'
]
==
'grid'
:
query
=
Departemen
.
cte_get
()
data
=
[{
"id"
:
d
.
id
,
"kode"
:
d
.
kode
,
"nama"
:
d
.
hierarchy
[
1
:],
"status"
:
d
.
status
,
data
=
[{
"id"
:
d
.
id
,
"kode"
:
d
.
kode
,
"nama"
:
d
.
hierarchy
.
startswith
(
'/'
)
and
d
.
hierarchy
[
1
:]
or
d
.
hierarchy
,
"status"
:
d
.
status
,
"level_id"
:
d
.
lvl
,
"parent_id"
:
d
.
parent_id
}
for
d
in
query
]
return
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment