def setup(): db().begin() db().execute('CREATE TABLE IF NOT EXISTS db_version (version INTEGER)') db().execute('CREATE TABLE IF NOT EXISTS version (version TEXT)') sql = create_table_def('card', card.card_properties()) db().execute(sql) sql = create_table_def('face', card.face_properties()) db().execute(sql) sql = create_table_def('set', card.set_properties()) db().execute(sql) sql = create_table_def('color', card.color_properties()) db().execute(sql) sql = create_table_def('card_color', card.card_color_properties()) db().execute(sql) sql = create_table_def('card_color_identity', card.card_color_properties()) db().execute(sql) sql = create_table_def('card_supertype', card.card_type_properties('supertype')) db().execute(sql) sql = create_table_def('card_type', card.card_type_properties('type')) db().execute(sql) sql = create_table_def('card_subtype', card.card_type_properties('subtype')) db().execute(sql) sql = create_table_def('format', card.format_properties()) db().execute(sql) sql = create_table_def('card_legality', card.card_legality_properties()) db().execute(sql) sql = create_table_def('card_alias', card.card_alias_properties()) db().execute(sql) sql = create_table_def('card_bug', card.card_bug_properties()) db().execute(sql) sql = create_table_def('rarity', card.format_properties( )) # This has the same profile as `format` (`id`, `name`) db().execute(sql) db().execute("""INSERT INTO color (name, symbol) VALUES ('White', 'W'), ('Blue', 'U'), ('Black', 'B'), ('Red', 'R'), ('Green', 'G') """) db().execute("""INSERT INTO rarity (name) VALUES ('Basic Land'), ('Common'), ('Uncommon'), ('Rare'), ('Mythic Rare') """) sql = create_table_def('printing', card.printing_properties()) db().execute(sql) # Speed up innermost subselect in base_query. db().execute( 'CREATE INDEX idx_card_id_format_id ON card_legality (card_id, format_id, legality)' ) db().execute( 'INSERT INTO db_version (version) VALUES ({0})'.format(SCHEMA_VERSION)) db().commit()
async def insert_cards_async(printings: List[CardDescription]) -> List[int]: next_card_id = (db().value('SELECT MAX(id) FROM card') or 0) + 1 values = await determine_values_async(printings, next_card_id) insert_many('card', card.card_properties(), values['card'], ['id']) if values[ 'card_color']: # We should not issue this query if we are only inserting colorless cards as they don't have an entry in this table. insert_many('card_color', card.card_color_properties(), values['card_color']) insert_many('card_color_identity', card.card_color_properties(), values['card_color_identity']) insert_many('printing', card.printing_properties(), values['printing']) insert_many('face', card.face_properties(), values['face'], ['position']) if values['card_legality']: insert_many('card_legality', card.card_legality_properties(), values['card_legality'], ['legality']) # Create the current Penny Dreadful format if necessary. get_format_id('Penny Dreadful', True) await update_bugged_cards_async() return [c['id'] for c in values['card']]