Skip to content

Commit

Permalink
Create the column nullable
Browse files Browse the repository at this point in the history
There's no real point in ever making the column non-nullable, and doing so
breaks the sytests.
  • Loading branch information
richvdh committed Jul 26, 2018
1 parent 5c1d301 commit 51d7df1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
15 changes: 7 additions & 8 deletions synapse/storage/schema/delta/50/make_event_content_nullable.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,33 @@


def run_create(cur, database_engine, *args, **kwargs):
pass


def run_upgrade(cur, database_engine, *args, **kwargs):
if isinstance(database_engine, PostgresEngine):
cur.execute("""
ALTER TABLE events ALTER COLUMN content DROP NOT NULL;
""")
return

# sqlite is an arse about this. ref: https://www.sqlite.org/lang_altertable.html
cur.execute("PRAGMA schema_version")
(oldver,) = cur.fetchone()

cur.execute("SELECT sql FROM sqlite_master WHERE tbl_name='events' AND type='table'")
(oldsql,) = cur.fetchone()

sql = oldsql.replace("content TEXT NOT NULL", "content TEXT")
if sql == oldsql:
raise Exception("Couldn't find null constraint to drop in %s" % oldsql)

logger.info("Replacing definition of 'events' with: %s", sql)

cur.execute("PRAGMA schema_version")
(oldver,) = cur.fetchone()
cur.execute("PRAGMA writable_schema=ON")

cur.execute(
"UPDATE sqlite_master SET sql=? WHERE tbl_name='events' AND type='table'",
(sql, ),
)

cur.execute("PRAGMA schema_version=%i" % (oldver+1,))
cur.execute("PRAGMA writable_schema=OFF")


def run_upgrade(*args, **kwargs):
pass
7 changes: 6 additions & 1 deletion synapse/storage/schema/full_schemas/16/im.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ CREATE TABLE IF NOT EXISTS events(
event_id TEXT NOT NULL,
type TEXT NOT NULL,
room_id TEXT NOT NULL,
content TEXT NOT NULL,

-- 'content' used to be created NULLable, but as of delta 50 we drop that constraint.
-- the hack we use to drop the constraint doesn't work for an in-memory sqlite
-- database, which breaks the sytests. Hence, we no longer make it nullable.
content TEXT,

unrecognized_keys TEXT,
processed BOOL NOT NULL,
outlier BOOL NOT NULL,
Expand Down

0 comments on commit 51d7df1

Please sign in to comment.