def __init__(self): for attr in self.REQUIRED_ATTRS: if getattr(self, attr) is None: raise MissingTableAttribute("Missing required Table attribute: {0}".format(attr)) if self.hash_key not in self.schema.dynamorm_fields(): raise InvalidSchemaField("The hash key '{0}' does not exist in the schema".format(self.hash_key)) if self.range_key and self.range_key not in self.schema.dynamorm_fields(): raise InvalidSchemaField("The range key '{0}' does not exist in the schema".format(self.range_key))
def create(self, wait=True): """Create a new table based on our attributes :param bool wait: If set to True, the default, this call will block until the table is created """ if not self.read or not self.write: raise MissingTableAttribute("The read/write attributes are required to create a table") table = self.resource.create_table( TableName=self.name, KeySchema=self.key_schema, AttributeDefinitions=self.attribute_definitions, ProvisionedThroughput=self.provisioned_throughput ) if wait: table.meta.client.get_waiter('table_exists').wait(TableName=self.name) return table
def __init__(self, schema): self.schema = schema required_attrs = ('name', 'hash_key') optional_attrs = ('range_key', 'read', 'write') for attr in required_attrs: if not hasattr(self, attr): raise MissingTableAttribute("Missing required Table attribute: {0}".format(attr)) for attr in optional_attrs: if not hasattr(self, attr): setattr(self, attr, None) if self.hash_key not in self.schema.dynamorm_fields(): raise InvalidSchemaField("The hash key '{0}' does not exist in the schema".format(self.hash_key)) if self.range_key and self.range_key not in self.schema.dynamorm_fields(): raise InvalidSchemaField("The range key '{0}' does not exist in the schema".format(self.range_key))
def create_table(self, wait=True): """Create a new table based on our attributes :param bool wait: If set to True, the default, this call will block until the table is created """ if not self.read or not self.write: raise MissingTableAttribute("The read/write attributes are required to create a table") index_args = collections.defaultdict(list) for index in six.itervalues(self.indexes): index_args[index.ARG_KEY].append(index.index_args) log.info("Creating table %s", self.name) table = self.resource.create_table( TableName=self.name, KeySchema=self.key_schema, AttributeDefinitions=self.attribute_definitions, ProvisionedThroughput=self.provisioned_throughput, **index_args ) if wait: log.info("Waiting for table creation...") table.meta.client.get_waiter('table_exists').wait(TableName=self.name) return table