def test__filter_attributes(self): column_line = ColumnLine() column_line.attributes = [] column_line.filter_attributes("A", "B") self.assertEqual(column_line.attributes, []) column_line.attributes = ["A", "B"] column_line.filter_attributes("A", "B") self.assertEqual(column_line.attributes, ["A"]) return
def main(): """ Main routine. """ usage = '%prog [options] schema_file' + '\nVersion: %s' % VERSION parser = OptionParser(usage=usage) parser.add_option('-c', '--config-file', dest='config_file', help='Name of configuration file.') parser.add_option('-f', '--full-help', action='store_true', dest='full_help', help=' '.join(['Print full help and exit.', 'Full help includes examples and notes.']), ) parser.add_option('-i', '--include-headers', action='store_true', dest='headers', help='Print table documentation headers.') parser.add_option('-s', '--ignore-signature', action='store_true', dest='ignore_signature', help='Do not print signature fields.') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='Print messages to stdout', ) (options, args) = parser.parse_args() if options.verbose: # Add a stream handler to print messages to stderr. ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s - %(message)s') ch.setFormatter(formatter) LOG.addHandler(ch) if options.full_help: parser.print_help() print print usage_full() return if len(args) == 0: parser.print_help() exit(1) ignore_columns = [] if options.ignore_signature: APP_ENV = env('shared', import_models=True) for field in ModelDb(APP_ENV).auth.signature.fields: if not field == 'id': ignore_columns.append(field) f = None if args[0] == '-': f = sys.stdin else: f = open(args[0], 'r') schema_file = SchemaFile(file_object=f) schema_file.parse() tables = [] for line in schema_file.filter(['create', 'create_close', 'column']): LOG.debug('line: %s' % line.text) if line.type == 'create': create_line = CreateLine(text=line.text) create_line.parse() LOG.debug('create_line.table_name: %s' % create_line.table_name) if create_line.table_name: table = MySQLTable(name=create_line.table_name) tables.append(table) if line.type == 'create_close': pass if line.type == 'column': if not len(tables) > 0: raise ValueError('Column without table: {text}'.format( text=line.text)) column_line = ColumnLine(text=line.text) column_line.parse() column = MySQLColumn( name=column_line.name, data_type=column_line.type, length=column_line.length, decimals=column_line.decimals, table=tables[-1], attributes=column_line.attributes, ) if column.name not in ignore_columns: tables[-1].columns.append(column) f.close() # Set tables references # This step must be done after all MySQLTable objects are defined and # added to tables list since so the tables are available for columns that # reference it. for t in tables: for c in t.columns: c.set_referenced_table(tables=tables) if c.referenced_table: c.set_referenced_column() c.set_descriptor_column() defaults_set = None if options.config_file: defaults_set = FieldPropertyDefaultsSet() defaults_set.load(options.config_file) for t in tables: print '' if options.headers: print '"""' print t.documentation() print '"""' print define_table_code(t, defaults_set=defaults_set) first = True for t in sorted(tables, cmp=lambda x, y: cmp(x.name, y.name)): for c in sorted(t.columns, cmp=lambda x, y: cmp(x.name, y.name)): if c.referenced_table: if first: print '' first = False print c.requires_statement()
def test__parse(self): tests = [ {"label": "empty string", "text": "", "name": "", "type": "", "attributes": []}, { "label": "basic typical", "text": "`my_column` text,", "name": "my_column", "type": "text", "attributes": [], }, { "label": "basic without ticks", "text": "my_column text,", "name": "my_column", "type": "text", "attributes": [], }, { "label": "basic without comma", "text": "`my_column` text", "name": "my_column", "type": "text", "attributes": [], }, { "label": "basic indented", "text": " `my_column` text,", "name": "my_column", "type": "text", "attributes": [], }, { "label": "length", "text": "`my_column` varchar(255),", "name": "my_column", "type": "varchar", "length": 255, "attributes": [], }, { "label": "decimals", "text": "`my_decimals` decimal(18,2),", "name": "my_decimals", "type": "decimal", "length": 18, "decimals": 2, "attributes": [], }, { "label": "AUTO_INCREMENT", "text": "`my_not_null` integer AUTO_INCREMENT,", "name": "my_not_null", "type": "integer", "attributes": ["AUTO_INCREMENT"], }, { "label": "COMMENT", "text": """`my_not_null` integer COMMENT 'this is my comment',""", "name": "my_not_null", "type": "integer", "attributes": ["COMMENT"], "comment": "this is my comment", }, { "label": "DEFAULT", "text": """`my_not_null` integer DEFAULT '0',""", "name": "my_not_null", "type": "integer", "attributes": ["DEFAULT"], "default": "0", }, { "label": "NOT NULL", "text": "`my_not_null` integer NOT NULL,", "name": "my_not_null", "type": "integer", "attributes": ["NOT NULL"], }, { "label": "NULL", "text": "`my_not_null` integer NULL,", "name": "my_not_null", "type": "integer", "attributes": ["NULL"], }, { "label": "PRIMARY KEY", "text": "`my_not_null` integer PRIMARY KEY,", "name": "my_not_null", "type": "integer", "attributes": ["PRIMARY KEY"], }, { "label": "PRIMARY", "text": "`my_not_null` integer PRIMARY,", "name": "my_not_null", "type": "integer", "attributes": ["PRIMARY"], }, { "label": "UNIQUE KEY", "text": "`my_not_null` integer UNIQUE KEY,", "name": "my_not_null", "type": "integer", "attributes": ["UNIQUE KEY"], }, { "label": "UNIQUE", "text": "`my_not_null` integer UNIQUE,", "name": "my_not_null", "type": "integer", "attributes": ["UNIQUE"], }, { "label": "typical id", "text": "`id` int(11) NOT NULL AUTO_INCREMENT,", "name": "id", "type": "int", "length": 11, "attributes": ["AUTO_INCREMENT", "NOT NULL"], }, { "label": "unsigned and zerofill", "text": "`my_unsigned` integer UNSIGNED ZEROFILL,", "name": "my_unsigned", "type": "integer", "attributes": [], }, ] for test in tests: cl = ColumnLine(text=test["text"]) try: cl.parse() except ValueError, e: print "%s: %s" % (test["label"], e) continue self.assertEqual(cl.name, test["name"]) self.assertEqual(cl.type, test["type"]) if "length" in test: self.assertEqual(cl.length, test["length"]) else: self.assertEqual(cl.length, None) if "decimals" in test: self.assertEqual(cl.decimals, test["decimals"]) else: self.assertEqual(cl.decimals, None) self.assertEqual(cl.attributes, test["attributes"]) if "comment" in test: self.assertEqual(cl.comment, test["comment"]) else: self.assertEqual(cl.comment, None) if "default" in test: self.assertEqual(cl.default, test["default"]) else: self.assertEqual(cl.default, None)