def database_migrate(db, old_ver): # Update database schema version Versions.update(val=db_schema_version).where(Versions.key == "schema_version").execute() log.info("Detected database version %i, updating to %i", old_ver, db_schema_version) # Perform migrations here migrator = None if args.db_type == "mysql": migrator = MySQLMigrator(db) else: migrator = SqliteMigrator(db) # No longer necessary, we're doing this at schema 4 as well # if old_ver < 1: # db.drop_tables([ScannedLocation]) if old_ver < 2: migrate(migrator.add_column("pokestop", "encounter_id", CharField(max_length=50, null=True))) if old_ver < 3: migrate( migrator.add_column("pokestop", "active_fort_modifier", CharField(max_length=50, null=True)), migrator.drop_column("pokestop", "encounter_id"), migrator.drop_column("pokestop", "active_pokemon_id"), ) if old_ver < 4: db.drop_tables([ScannedLocation]) if old_ver < 5: # Some pokemon were added before the 595 bug was "fixed" # Clean those up for a better UX query = Pokemon.delete().where(Pokemon.disappear_time > (datetime.now() - timedelta(hours=24))) query.execute()
def database_migrate(db, old_ver): # Update database schema version Versions.update(val=db_schema_version).where( Versions.key == 'schema_version').execute() log.info("Detected database version %i, updating to %i", old_ver, db_schema_version) # Perform migrations here migrator = None if args.db_type == 'mysql': migrator = MySQLMigrator(db) else: migrator = SqliteMigrator(db) # No longer necessary, we're doing this at schema 4 as well # if old_ver < 1: # db.drop_tables([ScannedLocation]) if old_ver < 2: migrate( migrator.add_column('pokestop', 'encounter_id', CharField(max_length=50, null=True))) if old_ver < 3: migrate( migrator.add_column('pokestop', 'active_fort_modifier', CharField(max_length=50, null=True)), migrator.drop_column('pokestop', 'encounter_id'), migrator.drop_column('pokestop', 'active_pokemon_id')) if old_ver < 4: db.drop_tables([ScannedLocation]) if old_ver < 5: # Some pokemon were added before the 595 bug was "fixed" # Clean those up for a better UX query = (Pokemon.delete().where( Pokemon.disappear_time > (datetime.utcnow() - timedelta(hours=24)))) query.execute() if old_ver < 6: migrate( migrator.add_column('gym', 'last_scanned', DateTimeField(null=True)), ) if old_ver < 7: migrate( migrator.drop_column('gymdetails', 'description'), migrator.add_column('gymdetails', 'description', TextField(null=True, default=""))) if old_ver < 8: db.create_tables([PokemonIVs], safe=True)
def database_migrate(db, old_ver): # Update database schema version. Versions.update(val=db_schema_version).where( Versions.key == 'schema_version').execute() log.info('Detected database version %i, updating to %i...', old_ver, db_schema_version) # Perform migrations here. migrator = MySQLMigrator(db) if old_ver < 17: migrate( migrator.add_column('pokemon', 'form', SmallIntegerField(null=True))) if old_ver < 18: migrate( migrator.add_column('pokemon', 'cp', SmallIntegerField(null=True))) if old_ver < 19: migrate( migrator.add_column('pokemon', 'cp_multiplier', FloatField(null=True))) if old_ver < 20: migrate( migrator.drop_column('gym', 'gym_points'), migrator.add_column('gym', 'slots_available', SmallIntegerField(null=False, default=0)), migrator.add_column('gymmember', 'cp_decayed', SmallIntegerField(null=False, default=0)), migrator.add_column( 'gymmember', 'deployment_time', DateTimeField(null=False, default=datetime.utcnow())), migrator.add_column('gym', 'total_cp', SmallIntegerField(null=False, default=0)))
def migrate(): database.create_tables([Version], safe=True) try: v = Version.select().get() except Version.DoesNotExist: database.create_tables([User, Project, Feature, Task]) v = Version(version=LAST_VERSION) v.save() if v.version >= LAST_VERSION: return if 'mysql' in config.DATABASE_URI: migrator = MySQLMigrator(database) elif 'sqlite' in config.DATABASE_URI: migrator = SqliteMigrator(database) else: migrator = PostgresqlMigrator(database) if v.version == 0: # Making a copy of Project.owner field, because it's not nullable # and we need to migrate a default value. admin = User.select( User.uid).where(User.uid == list(config.ADMINS)[0]).get() owner = ForeignKeyField(User, related_name='projects', to_field=User.uid, default=admin) peewee_migrate( migrator.add_column(User._meta.db_table, User.admin.db_column, User.admin), migrator.add_column(Project._meta.db_table, Project.owner.db_column, owner), migrator.add_column(Project._meta.db_table, Project.hidden.db_column, Project.hidden), migrator.add_column(Project._meta.db_table, Project.overlays.db_column, Project.overlays), migrator.add_column(Task._meta.db_table, Task.skipped.db_column, Task.skipped), migrator.drop_column(Project._meta.db_table, 'validated_count'), ) v.version = 1 v.save() if v.version == 1: peewee_migrate( migrator.add_column(Project._meta.db_table, Project.validate_modified.db_column, Project.validate_modified), migrator.add_column(Project._meta.db_table, Project.audit.db_column, Project.audit), ) v.version = 2 v.save() if v.version != LAST_VERSION: raise ValueError('LAST_VERSION in db.py should be {}'.format( v.version))
def database_migrate(db, old_ver): # Update database schema version. if db_schema_version > old_ver: Versions.update(val=db_schema_version).where( Versions.key == 'schema_version').execute() log.info('Detected database version %i, updating to %i...', old_ver, db_schema_version) # Perform migrations here. migrator = MySQLMigrator(db) if old_ver < 24: migrate( migrator.drop_index('pokemon', 'pokemon_disappear_time'), migrator.add_index('pokemon', ('disappear_time', 'pokemon_id'), False)) table_updates = [ # Old ver 17 ('add_column', 'pokemon', 'form', SmallIntegerField(null=True)), # Old ver 18 ('add_column', 'pokemon', 'cp', SmallIntegerField(null=True)), # old ver 19 ('add_column', 'pokemon', 'cp_multiplier', FloatField(null=True)), # old ver 20 ('drop_column', 'gym', 'gym_points', None), ('add_column', 'gym', 'slots_available', SmallIntegerField(null=False, default=0)), ('add_column', 'gymmember', 'cp_decayed', SmallIntegerField(null=False, default=0)), ('add_column', 'gymmember', 'deployment_time', DateTimeField(null=False, default=datetime.utcnow())), ('add_column', 'gym', 'total_cp', SmallIntegerField(null=False, default=0)), # old version 24 ('drop_index', 'pokemon', 'disappear_time', None), ('add_index', 'pokemon', ('disappear_time', 'pokemon_id'), False), # newer stuff ('add_column', 'pokemon', 'costume', SmallIntegerField(null=True)), ('add_column', 'gympokemon', 'form', SmallIntegerField(null=True)), ('add_column', 'gympokemon', 'costume', SmallIntegerField(null=True)), ('add_column', 'gympokemon', 'shiny', SmallIntegerField(null=True)), ('add_column', 'pokemon', 'weather_boosted_condition', SmallIntegerField(null=True)), ('add_column', 'gym', 'park', BooleanField(default=False)), ('add_column', 'gym', 'sponsor', SmallIntegerField(null=True)), ] for change in table_updates: (action, table, data, ctype) = change if action == 'add_column': if not column_exists(db, table, data): log.info("Adding '%s' column to '%s'.", data, table) migrate(migrator.add_column(table, data, ctype)) if action == 'drop_column': if column_exists(db, table, data): log.info("Dropping '%s' column from '%s'.", data, table) migrate(migrator.drop_column(table, data)) if action == 'add_index': index = index_exists(db, table, data) if not index: log.info("Adding '%s' index to '%s'.", data, table) migrate(migrator.add_index(table, data, ctype)) if action == 'drop_index': if index_name_exists(db, table, data): log.info("Dropping '%s' index from '%s'.", data, table) migrate(migrator.drop_index(table, data))
) migrate( migrator.drop_foreign_key_constraint('discord_starboard_message_starrers', 'message_id'), migrator.drop_index('discord_starboard_messages', 'PRIMARY'), migrator.add_column('discord_starboard_messages', 'id', AutoField(primary_key=True, null=True)), migrator.add_column('discord_starboard_message_starrers', 'starred_message_id', ForeignKeyField(StarredMessageModel, null=True, field=StarredMessageModel.id)), ) database.execute_sql( "UPDATE discord_starboard_message_starrers starrers " "JOIN discord_starboard_messages messages ON messages.`message_id` = starrers.`message_id` " "SET starrers.`starred_message_id` = messages.`id`" ) migrate( migrator.drop_index('discord_starboard_message_starrers', 'messagestarrermodel_user_id_message_id'), migrator.drop_index('discord_starboard_message_starrers', 'messagestarrermodel_message_id'), migrator.drop_column('discord_starboard_message_starrers', 'message_id'), ) database.execute_sql( "UPDATE discord_starboard " "JOIN discord_guild_configs config ON config.`guild_id` = discord_starboard.`guild_id` " "SET discord_starboard.`emoji` = config.`starboard_emoji_id`" ) migrate( migrator.drop_column('discord_guild_configs', 'starboard_emoji_id'), migrator.add_column('discord_guild_configs', 'config_data', JSONField(null=True)), )