Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GraceDB Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael William Coughlin
GraceDB Server
Commits
9e69a146
Commit
9e69a146
authored
6 years ago
by
Tanner Prestegard
Committed by
GraceDB
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
adding custom auto-increment UPDATE method to AutoIncrementModel
parent
b4e766bd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gracedb/core/models.py
+52
-0
52 additions, 0 deletions
gracedb/core/models.py
with
52 additions
and
0 deletions
gracedb/core/models.py
+
52
−
0
View file @
9e69a146
...
...
@@ -143,6 +143,58 @@ class AutoIncrementModel(models.Model):
self
.
_state
.
adding
=
False
def
auto_increment_update
(
self
,
update_field_name
,
constraints
=
[],
allow_update_to_nonnull
=
False
):
"""
UPDATE superevents_superevent SET gw_date_number = (SELECT N FROM (SELECT IFNULL(MAX(gw_date_number),0)+1 as N FROM superevents_superevent WHERE t_0_date=
'
1980-01-06
'
AND is_gw=1) AS y) WHERE id=41;
"""
if
not
allow_update_to_nonnull
and
getattr
(
self
,
update_field_name
)
is
not
None
:
logger
.
warning
(
'
Attempt to auto increment a non-null field for
'
'
object {0}. Not allowed.
'
.
format
(
self
.
__str__
))
return
# Setup for generating base SQL query for doing an update
meta
=
self
.
_meta
field
=
meta
.
get_field
(
update_field_name
)
values
=
[(
field
,
None
,
field
.
pre_save
(
self
,
False
))]
query
=
models
.
sql
.
UpdateQuery
(
self
.
__class__
)
query
.
add_update_fields
(
values
)
compiler
=
query
.
get_compiler
(
using
=
self
.
__class__
.
_base_manager
.
db
)
# Useful function
qn
=
compiler
.
quote_name_unless_alias
# SQL for doing autoincrement
custom_sql
=
(
"
(SELECT N FROM (SELECT IFNULL(MAX({field}),0)+1 AS N
"
"
FROM {tbl_name}
"
).
format
(
tbl_name
=
qn
(
meta
.
db_table
),
field
=
update_field_name
)
# Convert list of field names to be used as constraints into database
# column names and their values (retrieved from the instance itself)
constraint_fields
=
[
meta
.
get_field
(
f
)
for
f
in
constraints
]
constraint_list
=
[
"
{0}=%s
"
.
format
(
qn
(
f
.
attname
))
for
f
in
constraint_fields
]
values
=
[
f
.
get_db_prep_value
(
getattr
(
self
,
f
.
attname
),
compiler
.
connection
)
for
f
in
constraint_fields
]
# Add constraints to custom SQL (if they are provided)
if
constraint_list
:
custom_sql
+=
(
"
WHERE
"
+
"
AND
"
.
join
(
constraint_list
))
# Add end
custom_sql
+=
(
"
) AS temp) WHERE id={pk};
"
.
format
(
pk
=
self
.
pk
))
# Replace NULL in base sql update query
base_sql
=
compiler
.
as_sql
()[
0
]
sql
=
base_sql
.
replace
(
'
NULL
'
,
custom_sql
)
# Execute sql
compiler
.
connection
.
cursor
().
execute
(
sql
,
values
)
# Refresh from database
self
.
refresh_from_db
(
fields
=
[
update_field_name
])
class
LogBase
(
models
.
Model
):
"""
Abstract base class for log message-type objects. Concrete derived
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment