From e7848e39c656b130039cc5c62933ea9601a041bc Mon Sep 17 00:00:00 2001 From: Andrew Dickinson Date: Mon, 13 Apr 2020 19:32:54 -0400 Subject: [PATCH] Configure SQLite to disable ID reuse --- .../cb038f79982e_sqlite_autoincrement.py | 50 +++++++++++++++++++ ihatemoney/models.py | 5 ++ 2 files changed, 55 insertions(+) create mode 100644 ihatemoney/migrations/versions/cb038f79982e_sqlite_autoincrement.py diff --git a/ihatemoney/migrations/versions/cb038f79982e_sqlite_autoincrement.py b/ihatemoney/migrations/versions/cb038f79982e_sqlite_autoincrement.py new file mode 100644 index 00000000..ae5ab326 --- /dev/null +++ b/ihatemoney/migrations/versions/cb038f79982e_sqlite_autoincrement.py @@ -0,0 +1,50 @@ +"""sqlite_autoincrement + +Revision ID: cb038f79982e +Revises: 2dcb0c0048dc +Create Date: 2020-04-13 17:40:02.426957 + +""" + +# revision identifiers, used by Alembic. +revision = "cb038f79982e" +down_revision = "2dcb0c0048dc" + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + alter_table_batches = [ + op.batch_alter_table( + "person", recreate="always", table_kwargs={"sqlite_autoincrement": True} + ), + op.batch_alter_table( + "bill", recreate="always", table_kwargs={"sqlite_autoincrement": True} + ), + op.batch_alter_table( + "billowers", recreate="always", table_kwargs={"sqlite_autoincrement": True} + ), + ] + + for batch_op in alter_table_batches: + with batch_op: + pass + + +def downgrade(): + alter_table_batches = [ + op.batch_alter_table( + "person", recreate="always", table_kwargs={"sqlite_autoincrement": False} + ), + op.batch_alter_table( + "bill", recreate="always", table_kwargs={"sqlite_autoincrement": False} + ), + op.batch_alter_table( + "billowers", recreate="always", table_kwargs={"sqlite_autoincrement": False} + ), + ] + + for batch_op in alter_table_batches: + with batch_op: + pass diff --git a/ihatemoney/models.py b/ihatemoney/models.py index a9e28e14..a9eb1197 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -345,6 +345,8 @@ class Person(db.Model): # Direct SQLAlchemy-Continuum to track changes to this model __versioned__ = {} + __table_args__ = {"sqlite_autoincrement": True} + id = db.Column(db.Integer, primary_key=True) project_id = db.Column(db.String(64), db.ForeignKey("project.id")) bills = db.relationship("Bill", backref="payer") @@ -383,6 +385,7 @@ billowers = db.Table( "billowers", db.Column("bill_id", db.Integer, db.ForeignKey("bill.id"), primary_key=True), db.Column("person_id", db.Integer, db.ForeignKey("person.id"), primary_key=True), + sqlite_autoincrement=True, ) @@ -412,6 +415,8 @@ class Bill(db.Model): # Direct SQLAlchemy-Continuum to track changes to this model __versioned__ = {} + __table_args__ = {"sqlite_autoincrement": True} + id = db.Column(db.Integer, primary_key=True) payer_id = db.Column(db.Integer, db.ForeignKey("person.id"))