def handle(self, *args, **options): if not options['database'] or options['database'] == 'all': databases = self.get_all_but_replica_dbs() elif options['database'] not in self.get_all_but_replica_dbs(): raise CommandError('You must migrate an existing primary DB.') else: databases = [options['database']] has_errors = False for database in databases: sequence_name = options["sequence_name"] try: shard_id = settings.DATABASES[database].get('SHARD_ID', None) if shard_id is None: continue if not options["dry_run"]: create_postgres_global_sequence(sequence_name=sequence_name, db_alias=database, reset_sequence=options["reset_sequence"]) create_postgres_shard_id_function(sequence_name=sequence_name, db_alias=database, shard_id=shard_id) if not verify_postres_id_field_setup_correctly(sequence_name=sequence_name, db_alias=database, function_name="next_sharded_id"): raise Exception("The sequence could not be found.") self.stdout.write(getattr(self.style, "SUCCESS", lambda a: a)("\nDatabase {} with shard id {}:sequence {} is present").format(database, shard_id, sequence_name)) except Exception as e: self.stdout.write(getattr(self.style, "ERROR", lambda a: a)("\nDatabase {}: Error occured: {}").format(database, str(e))) has_errors = True if has_errors: raise CommandError("Some databases do not have the sequence on them.")
def migration_receiver(*args, **kwargs): sequence_name = "global_id_sequence" db_alias = kwargs.get('using') if not db_alias: raise EnvironmentError("A pre-migration receiver did not receive a database alias. " "Perhaps your app is not registered correctly?") if settings.DATABASES[db_alias]['ENGINE'] in Backends.POSTGRES: shard_id = settings.DATABASES[db_alias].get('SHARD_ID', 0) create_postgres_global_sequence(sequence_name, db_alias, True) create_postgres_shard_id_function(sequence_name, db_alias, shard_id)
def handle(self, *args, **options): if not options['database'] or options['database'] == 'all': databases = self.get_all_but_replica_dbs() elif options['database'] not in self.get_all_but_replica_dbs(): raise CommandError('You must migrate an existing primary DB.') else: databases = [options['database']] has_errors = False for database in databases: sequence_name = options["sequence_name"] try: shard_id = settings.DATABASES[database].get('SHARD_ID', None) if shard_id is None: continue if not options["dry_run"]: create_postgres_global_sequence( sequence_name=sequence_name, db_alias=database, reset_sequence=options["reset_sequence"]) create_postgres_shard_id_function( sequence_name=sequence_name, db_alias=database, shard_id=shard_id) if not verify_postres_id_field_setup_correctly( sequence_name=sequence_name, db_alias=database, function_name="next_sharded_id"): raise Exception("The sequence could not be found.") self.stdout.write( getattr(self.style, "SUCCESS", lambda a: a) ("\nDatabase {} with shard id {}:sequence {} is present" ).format(database, shard_id, sequence_name)) except Exception as e: self.stdout.write( getattr(self.style, "ERROR", lambda a: a)( "\nDatabase {}: Error occured: {}").format( database, str(e))) has_errors = True if has_errors: raise CommandError( "Some databases do not have the sequence on them.")