Exemplo n.º 1
0
    def test__filter(self):
        schema_file = SchemaFile()
        self.assertEqual(schema_file.lines, [])  # Control - no lines
        # filter returns expected
        self.assertEqual(schema_file.filter(["a", "c"]), [])

        schema_line1 = SchemaLine()
        schema_line2 = SchemaLine()
        schema_line3 = SchemaLine()
        schema_line1.type = "a"
        schema_line2.type = "b"
        schema_line3.type = "c"
        schema_file.lines.append(schema_line1)
        schema_file.lines.append(schema_line2)
        schema_file.lines.append(schema_line3)
        self.assertEqual(schema_file.filter(["a", "c"]), [schema_line1, schema_line3])  # filter returns expected
        return
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
    def test__parse(self):
        tests = [
            {"label": "empty file", "expect": 0, "text": ""},
            {
                "label": "basic file",
                "expect": 3,
                "text": """CREATE TABLE my_table (
    id integer,
)
""",
            },
            {
                "label": "typical file",
                "expect": 55,
                "text": """
-- MySQL dump 10.13  Distrib 5.1.47, for pc-linux-gnu (i686)
--
-- Host: localhost    Database: accounting
-- ------------------------------------------------------
-- Server version    5.1.47-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `company`
--

DROP TABLE IF EXISTS `company`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text COLLATE latin1_general_ci,
  `bank_account_id` int(11) DEFAULT '0',
  `hst_account_id` int(11) DEFAULT '0',
  `pst_account_id` int(11) DEFAULT '0',
  `transfer_account_id` int(11) DEFAULT '0',
  `unsorted_account_id` int(11) DEFAULT '0',
  `earnings_account_id` int(11) DEFAULT '0',
  `opening_account_id` int(11) DEFAULT '0',
  `dfa_account_id` int(11) DEFAULT '0',
  `colour` text COLLATE latin1_general_ci,
  `fiscal_month` int(11) DEFAULT '0',
  `freeze_date` date DEFAULT NULL,
  `creation_date` datetime DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=305 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2010-07-01 11:40:10
""",
            },
        ]

        f_text = "/tmp/test_mysql_schema.txt"
        for t in tests:
            self._config_file_from_text(f_text, t["text"])
            f = open(f_text)
            schema_file = SchemaFile(file_object=f)
            schema_file.parse()
            f.close()
            self.assertEqual(len(schema_file.lines), t["expect"])
        os.unlink(f_text)
        return