def hydrate(self, bundle): if not bundle.data.get('id'): bundle.obj.creator = self.resolve_user(bundle.request.GET) # Update the key if this is a new instance but the key already is in use while DbEntity.objects.filter(key=bundle.data['key']).count() > 0: bundle.data['key'] = increment_key(bundle.data['key']) # Set this field to 0 so we can track post save progress and know when # the DbEntity is completely ready bundle.obj.setup_percent_complete = 0 bundle.obj.key = bundle.data['key'] bundle.obj.updater = self.resolve_user(bundle.request.GET) return bundle
def hydrate(self, bundle): if not bundle.data.get('id'): bundle.obj.creator = self.resolve_user(bundle.request.GET) # Update the key if this is a new instance but the key already is in use while DbEntity.objects.filter(key=bundle.data['key']).count() > 0: bundle.data['key'] = increment_key(bundle.data['key']) # Set this field to 0 so we can track post save progress and know when # the DbEntity is completely ready bundle.obj.setup_percent_complete = 0 bundle.obj.key = bundle.data['key'] bundle.obj.updater = self.resolve_user(bundle.request.GET) return bundle
def get_db_entity_params(layer_name): """ For DbEntity creation we need a key and name. The key is a 'slugified' version of the name, and name is extracted from the layer. """ db_entity_name = titleize(layer_name) # ensure we have unique names if the layer has been uploaded before while DbEntity.objects.filter(name=db_entity_name).count() > 0: db_entity_name = increment_key(db_entity_name) db_entity_key = slugify(db_entity_name).replace('-', '_') return db_entity_key, db_entity_name
def get_db_entity_params(layer_name): """ For DbEntity creation we need a key and name. The key is a 'slugified' version of the name, and name is extracted from the layer. """ db_entity_name = titleize(layer_name) # ensure we have unique names if the layer has been uploaded before while DbEntity.objects.filter(name=db_entity_name).count() > 0: db_entity_name = increment_key(db_entity_name) db_entity_key = slugify(db_entity_name).replace('-', '_') return db_entity_key, db_entity_name
def create_primary_key_column_from_another_column(cls, schema, table, primary_key_column, from_column=None): """ Adds the column of the given type to the given table if absent :param schema: The database schema name :param table: The table name :param primary_key_column: Name of primary key column to create. If a primary key already exists it will be renamed from this, unless from_column is specified, in which case the existing primary_key will lose its constraint """ full_tablename = '"{schema}"."{table}"'.format(schema=schema, table=table) conn = psycopg2.connect( **pg_connection_parameters(settings.DATABASES['default'])) conn.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) cursor = conn.cursor() existing_primary_key = InformationSchema.get_primary_key_name( schema, table) if existing_primary_key: logger.info('Found existing primary key %s' % existing_primary_key) if not InformationSchema.objects.has_column(schema, table, primary_key_column): # If not create a primary key or alter the existing one's name # Copy values from the from_column to the new primary_key_column if existing_primary_key and not from_column: # Rename the primary key to primary_key_column and end alter_source_id_sql = 'alter table {full_tablename} rename column {existing_primary_key} to {primary_key_column}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key, primary_key_column=primary_key_column) logger.info( 'Existing primary key exists and no from column is specified. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) return if from_column: # Create a new primary key column without values create_column_sql = 'alter table {full_tablename} add column {primary_key_column} integer'.format( full_tablename=full_tablename, primary_key_column=primary_key_column) logger.info( 'From column is specified to be primary key source. Executing: %s' % create_column_sql) cursor.execute(create_column_sql) # Copy values from the from_column, always casting to integer update_sql = 'update {full_tablename} set {primary_key_column} = cast({from_column} AS integer)'.format( full_tablename=full_tablename, primary_key_column=primary_key_column, from_column=from_column) logger.info( 'Copying values from column to primary key. Executing: %s' % update_sql) cursor.execute(update_sql) else: # Populate with a serial primary key alter_source_id_sql = 'alter table {full_tablename} add column {primary_key_column} serial primary key'.format( full_tablename=full_tablename, primary_key_column=primary_key_column) logger.info( 'Copying values from column to primary key. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) # Drop the original_primary_key column if it exists if existing_primary_key: alter_source_id_sql = 'alter table {full_tablename} drop column {existing_primary_key}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key) logger.info( 'Existing primary key being removed. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) if from_column: # Create the primary key constraint if we haven't yet alter_source_id_sql = 'alter table {full_tablename} add constraint {table}_{schema}_{primary_key_column}_pk primary key ({primary_key_column})'.format( full_tablename=full_tablename, table=table, schema=schema, primary_key_column=primary_key_column) logger.info('Adding constraint to primary key. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) elif existing_primary_key != primary_key_column: # If there a column matching primary_key_column that isn't the primary key, we need to rename # the primary_key_column to something unique and then rename existing_primary_key to primary_key_column # Find a unique column to rename primary_key_column (e.g. renamed id to id_1, or id_2, etc) unique_column = increment_key(primary_key_column) while InformationSchema.objects.has_column(schema, table, unique_column): unique_column = increment_key(unique_column) # Rename the primary_key_column rename_primary_key_column_name_sql = 'alter table {full_tablename} rename column {primary_key_column} to {unique_column}'.format( full_tablename=full_tablename, primary_key_column=primary_key_column, unique_column=unique_column) logger.info( 'Existing column with primary key name exists that needs to be renamed: %s' % rename_primary_key_column_name_sql) cursor.execute(rename_primary_key_column_name_sql) # Rename the existing_primary_key to primary_key_column (e.g. rename to ogc_fid to id for uploaded tables) rename_existing_primary_key_sql = 'alter table {full_tablename} rename column {existing_primary_key} to {primary_key_column}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key, primary_key_column=primary_key_column) logger.info( 'Existing primary key exists that needs to be renamed to desired primary key column name. Executing: %s' % rename_existing_primary_key_sql) cursor.execute(rename_existing_primary_key_sql)
def create_primary_key_column_from_another_column(cls, schema, table, primary_key_column, from_column=None): """ Adds the column of the given type to the given table if absent :param schema: The database schema name :param table: The table name :param primary_key_column: Name of primary key column to create. If a primary key already exists it will be renamed from this, unless from_column is specified, in which case the existing primary_key will lose its constraint """ full_tablename = '"{schema}"."{table}"'.format(schema=schema, table=table) conn = psycopg2.connect(**pg_connection_parameters(settings.DATABASES['default'])) conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) cursor = conn.cursor() existing_primary_key = InformationSchema.get_primary_key_name(schema, table) if existing_primary_key: logger.info('Found existing primary key %s' % existing_primary_key) if not InformationSchema.objects.has_column(schema, table, primary_key_column): # If not create a primary key or alter the existing one's name # Copy values from the from_column to the new primary_key_column if existing_primary_key and not from_column: # Rename the primary key to primary_key_column and end alter_source_id_sql = 'alter table {full_tablename} rename column {existing_primary_key} to {primary_key_column}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key, primary_key_column=primary_key_column) logger.info('Existing primary key exists and no from column is specified. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) return if from_column: # Create a new primary key column without values create_column_sql = 'alter table {full_tablename} add column {primary_key_column} integer'.format( full_tablename=full_tablename, primary_key_column=primary_key_column) logger.info('From column is specified to be primary key source. Executing: %s' % create_column_sql) cursor.execute(create_column_sql) # Copy values from the from_column, always casting to integer update_sql = 'update {full_tablename} set {primary_key_column} = cast({from_column} AS integer)'.format( full_tablename=full_tablename, primary_key_column=primary_key_column, from_column=from_column) logger.info('Copying values from column to primary key. Executing: %s' % update_sql) cursor.execute(update_sql) else: # Populate with a serial primary key alter_source_id_sql = 'alter table {full_tablename} add column {primary_key_column} serial primary key'.format( full_tablename=full_tablename, primary_key_column=primary_key_column) logger.info('Copying values from column to primary key. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) # Drop the original_primary_key column if it exists if existing_primary_key: alter_source_id_sql = 'alter table {full_tablename} drop column {existing_primary_key}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key) logger.info('Existing primary key being removed. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) if from_column: # Create the primary key constraint if we haven't yet alter_source_id_sql = 'alter table {full_tablename} add constraint {table}_{schema}_{primary_key_column}_pk primary key ({primary_key_column})'.format( full_tablename=full_tablename, table=table, schema=schema, primary_key_column=primary_key_column) logger.info('Adding constraint to primary key. Executing: %s' % alter_source_id_sql) cursor.execute(alter_source_id_sql) elif existing_primary_key != primary_key_column: # If there a column matching primary_key_column that isn't the primary key, we need to rename # the primary_key_column to something unique and then rename existing_primary_key to primary_key_column # Find a unique column to rename primary_key_column (e.g. renamed id to id_1, or id_2, etc) unique_column = increment_key(primary_key_column) while InformationSchema.objects.has_column(schema, table, unique_column): unique_column = increment_key(unique_column) # Rename the primary_key_column rename_primary_key_column_name_sql = 'alter table {full_tablename} rename column {primary_key_column} to {unique_column}'.format( full_tablename=full_tablename, primary_key_column=primary_key_column, unique_column=unique_column) logger.info('Existing column with primary key name exists that needs to be renamed: %s' % rename_primary_key_column_name_sql) cursor.execute(rename_primary_key_column_name_sql) # Rename the existing_primary_key to primary_key_column (e.g. rename to ogc_fid to id for uploaded tables) rename_existing_primary_key_sql = 'alter table {full_tablename} rename column {existing_primary_key} to {primary_key_column}'.format( full_tablename=full_tablename, existing_primary_key=existing_primary_key, primary_key_column=primary_key_column) logger.info('Existing primary key exists that needs to be renamed to desired primary key column name. Executing: %s' % rename_existing_primary_key_sql) cursor.execute(rename_existing_primary_key_sql)