Skip to content

Conversation

@guineawheek
Copy link
Collaborator

@guineawheek guineawheek commented Jan 7, 2023

Overview

ORM tables are a new variant on DatabaseTables:

  • they are defined from class attributes
  • initial_create, initial_migrate, get_by, delete, and update_or_add (upsert) are handled for you
  • ability to instantiate objects directly from the results of SQL queries using from_record
  • inferred versioning from column parameters (mostly adds columns)

Differences from DatabaseTable:

  • __uniques__ MUST be a tuple of primary key column names! Do not set it to a string! Runtime will check for this and yell at you!
  • By default, __version__ is None. This means that versions/migrations will be computed from arguments given to Column (see next section)
  • initial_create/initial_migrate will create the latest version of the table, rather than version 0.

Versioning:

  • In general, columns should be defined using the latest version of the schema.
  • If your table previously had an id field that's now channel_id, name the thing
    channel_id: int = db.Column("bigint NOT NULL")
  • The default versioning scheme looks at db.Column.version to see when the first version a column is introduced.
    It will then either just add the column using ALTER TABLE ADD COLUMN or run a custom script via whatever script is in Column.alter_tbl.
  • You can set version to a List for the default functionality of calling migration functions in order.
    You can also override cls.initial_create and cls.initial_migrate to have tables created at version 0 and upgraded all the way up,
    like DatabaseTable.

Worked example:

    class StarboardConfig(db.ORMTable):
        __tablename__ = 'starboard_settings'
        __uniques__ = ('guild_id',) 
        
        # the column definitions
        guild_id: int     = db.Column("bigint NOT NULL")
        channel_id: int   = db.Column("bigint NOT NULL")
        star_emoji: str   = db.Column("varchar NOT NULL")
        cancel_emoji: str = db.Column("varchar")
        threshold: int    = db.Column("bigint NOT NULL")
        # this parameter could be added later down the line, and it will be added using ALTER TABLE.
        some_new_col: int = db.Column("bigint NOT NULL DEFAULT 10", version=1)

TODO

  • add and test a sample class for test instrumentation
  • feedback from other dozer maintainers
  • adapt agreed-upon classes to paradigm

@tweirtx tweirtx marked this pull request as draft January 10, 2023 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant