def read_ddl(args):
    """
    Reads database DDL and returns a database model.
    :param args: The command line arguments.
    :returns: The database read from the DDL.
    :rtype: Database
    """
    parser = DDLParser(args.database, args.schema)
    return parser.parse_ddl(args.from_ddl)
def main():
    """Main function for the script."""
    args = parse_args()

    if valid_args(args):
        print(args)

        ddl_parser = DDLParser(database_name=args.database,
                               schema_name=args.schema)
        db_1 = ddl_parser.parse_ddl(args.ddl1)
        ddl_parser = DDLParser(database_name=args.database,
                               schema_name=args.schema)
        db_2 = ddl_parser.parse_ddl(args.ddl2)

        # Returns differences for each database as a tuple.
        database_differences = DDLCompare.compare_databases(db_1, db_2)

        if args.alter1:
            logging.debug(
                "generate alters for first schema to match the second")
            print("-- changes needed for first schema to match the second")
            TQLAlterWriter().write_alters(database_differences[0])

        if args.alter2:
            logging.debug(
                "generate alters for second schema to match the first")
            print("-- changes needed for second schema to match the first")
            TQLAlterWriter().write_alters(database_differences[1])

        if not args.alter1 and not args.alter2:
            print("Database differences for DB 1:")
            for db_diff in database_differences[0]:
                print("\t%s" % db_diff)

            print("Database differences for DB 2:")
            for db_diff in database_differences[1]:
                print("\t%s" % db_diff)
Example #3
0
 def test_create_ddlparser_with_all_params(self):
     """Test creating a parser with all values provided."""
     dp = DDLParser("testdb", "testschema")
     self.assertEquals(dp.database.database_name, "testdb")
     self.assertEquals(dp.schema_name, "testschema")
Example #4
0
 def test_column_types(self):
     """Tests converting various types of columns."""
     self.assertEqual("BIGINT", DDLParser.convert_type("integer"))
     self.assertEqual("INT", DDLParser.convert_type("rowversion"))
     self.assertEqual("VARCHAR(0)",
                      DDLParser.convert_type("uniqueidentifier"))
     self.assertEqual("INT", DDLParser.convert_type("serial"))
     self.assertEqual("BOOL", DDLParser.convert_type("bit"))
     self.assertEqual("UNKNOWN", DDLParser.convert_type("blob"))
     self.assertEqual("UNKNOWN", DDLParser.convert_type("binary"))
     self.assertEqual("BIGINT", DDLParser.convert_type("number"))
     self.assertEqual("INT", DDLParser.convert_type("number(1)"))
     self.assertEqual("INT", DDLParser.convert_type("NUMBER(1)"))
     self.assertEqual("INT", DDLParser.convert_type("NUMBER(3,0)"))
     self.assertEqual("BIGINT", DDLParser.convert_type("NUMBER(10,0)"))
     self.assertEqual("BIGINT", DDLParser.convert_type("NUMBER(*,0)"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("NUMBER(4,2)"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("decimal"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("numeric"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("float"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("double"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("money"))
     self.assertEqual("DOUBLE", DDLParser.convert_type("real"))
     self.assertEqual("DATETIME", DDLParser.convert_type("datetime"))
     self.assertEqual("TIME", DDLParser.convert_type("time"))
     self.assertEqual("DATE", DDLParser.convert_type("date"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("text"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("varchar"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("char"))
     self.assertEqual("VARCHAR(88)", DDLParser.convert_type("varchar(88)"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("long"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("enum"))
     self.assertEqual("VARCHAR(0)", DDLParser.convert_type("xml"))
     self.assertEqual("UNKNOWN", DDLParser.convert_type("something_new"))
Example #5
0
 def test_create_ddlparser_with_defaults(self):
     """Test creating a parser with defaults."""
     dp = DDLParser("testdb")
     self.assertEquals(dp.database.database_name, "testdb")
     self.assertEquals(dp.schema_name, DatamodelConstants.DEFAULT_SCHEMA)