-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] feat(metrics): PoC of running metrics indexer migrations against spanner #37532
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
_db_table_to_db = { | ||
"sentry_stringindexer": "metrics-spanner", # will replace ^ table above | ||
"sentry_perfstringindexer": "metrics-spanner", # will replace ^ table above | ||
} | ||
|
||
|
||
def db_for_model(model): | ||
return db_for_table(model._meta.db_table) | ||
|
||
|
||
def db_for_table(table): | ||
return _db_table_to_db.get(table, "default") | ||
|
||
|
||
class MultiDatabaseRouter: | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._setup_replicas() | ||
|
||
def _setup_replicas(self): | ||
from django.conf import settings | ||
|
||
self._replicas = {} | ||
for db, db_conf in settings.DATABASES.items(): | ||
primary = db_conf.get("REPLICA_OF") | ||
if primary: | ||
self._replicas[primary] = db | ||
|
||
def db_for_read(self, model, **hints): | ||
db = db_for_model(model) | ||
if hints.get("replica", False): | ||
db = self._replicas.get(db, db) | ||
return db | ||
|
||
def db_for_write(self, model, **hints): | ||
return db_for_model(model) | ||
|
||
def allow_relation(self, obj1, obj2, **hints): | ||
return db_for_model(obj1) == db_for_model(obj2) | ||
|
||
def allow_syncdb(self, db, model): | ||
if db_for_model(model) == db: | ||
return True | ||
return False | ||
|
||
def allow_migrate(self, db, app_label, model=None, **hints): | ||
if model: | ||
return db_for_model(model) == db | ||
|
||
# We use this hint in our RunSql/RunPython migrations to help resolve databases. | ||
if "tables" in hints: | ||
dbs = {db_for_table(table) for table in hints["tables"]} | ||
if len(dbs) > 1: | ||
raise RuntimeError( | ||
"Migration tables resolve to multiple databases. " | ||
f"Got {dbs} when only one database should be used." | ||
) | ||
return dbs.pop() == db | ||
# Assume migrations with no model routing or hints need to run on | ||
# the default database. | ||
return db == "default" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,16 +27,4 @@ class Migration(CheckedMigration): | |
("sentry", "0283_extend_externalissue_key"), | ||
] | ||
|
||
operations = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. raw sql statements mentioning sequence don't work. We cannot run this migration as-is, so I think we would need to edit it to skip doing that for spanner, or create a new table name etc for spanner where this migration doesn't run. |
||
migrations.RunSQL( | ||
""" | ||
ALTER SEQUENCE sentry_stringindexer_id_seq START WITH 65536; | ||
ALTER SEQUENCE sentry_stringindexer_id_seq RESTART; | ||
""", | ||
hints={"tables": ["sentry_stringindexer"]}, | ||
reverse_sql=""" | ||
ALTER SEQUENCE sentry_stringindexer_id_seq START WITH 1; | ||
ALTER SEQUENCE sentry_stringindexer_id_seq RESTART; | ||
""", | ||
) | ||
] | ||
operations = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have to import this as well ?
https://github.com/googleapis/python-spanner-django
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but i couldn't figure out how to add a git dependency in our requirements. We need a fix from master to get anywhere at all