@Test public void testAddSourcePrefixCreateTable() throws Exception { String ddl = "create table `gibraltar_production`.`_instrument_details_paypal_new` (\n" + " `instrument_token` varbinary(255) NOT NULL,\n" + " `version` int(11) NOT NULL,\n" + " `paypal_email_encrypted` varbinary(255) NOT NULL,\n" + " `created_at` datetime NOT NULL,\n" + " PRIMARY KEY (`instrument_token`,`version`),\n" + " KEY `index_instrument_details_paypal_paypal_email` (`paypal_email_encrypted`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"; String expectedDDL = "create table `source/gibraltar_production`.`_instrument_details_paypal_new` (\n" + " `instrument_token` varbinary(255) NOT NULL,\n" + " `version` int(11) NOT NULL,\n" + " `paypal_email_encrypted` varbinary(255) NOT NULL,\n" + " `created_at` datetime NOT NULL,\n" + " PRIMARY KEY (`instrument_token`,`version`),\n" + " KEY `index_instrument_details_paypal_paypal_email` (`paypal_email_encrypted`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"; assertEquals(expectedDDL, schemaDatabase.addSourcePrefix(ddl)); ddl = "CREATE TABLE table123 (\n" + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + "`name` varchar(255) NOT NULL\n" + ") ENGINE=InnoDB AUTO_INCREMENT=2145755390 DEFAULT CHARSET=latin1"; assertEquals(ddl, schemaDatabase.addSourcePrefix(ddl)); ddl = "CREATE TABLE `table1234` (\n" + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + "`name` varchar(255) NOT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=latin1"; assertEquals(ddl, schemaDatabase.addSourcePrefix(ddl)); ddl = "CREATE TABLE IF NOT EXISTS my_database.`my_table` (\n" + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + "`name` varchar(255) NOT NULL\n" + ") ENGINE=InnoDB AUTO_INCREMENT=2145755390 DEFAULT CHARSET=latin1"; expectedDDL = "CREATE TABLE IF NOT EXISTS `source/my_database`.`my_table` (\n" + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + "`name` varchar(255) NOT NULL\n" + ") ENGINE=InnoDB AUTO_INCREMENT=2145755390 DEFAULT CHARSET=latin1"; assertEquals(expectedDDL, schemaDatabase.addSourcePrefix(ddl)); ddl = "create table `test3`.`test_table` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(45) DEFAULT NULL,\n" + " `balance` varchar(45) DEFAULT NULL, \n" + " `timestamp` timestamp NULL DEFAULT NULL,\n" + " `float_col` float DEFAULT NULL,\n" + " `created_at` datetime DEFAULT NULL,\n" + " `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE NOW(),\n" + " PRIMARY KEY (`id`) ) ENGINE=InnoDB"; expectedDDL = "create table `source/test3`.`test_table` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(45) DEFAULT NULL,\n" + " `balance` varchar(45) DEFAULT NULL, \n" + " `timestamp` timestamp NULL DEFAULT NULL,\n" + " `float_col` float DEFAULT NULL,\n" + " `created_at` datetime DEFAULT NULL,\n" + " `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE NOW(),\n" + " PRIMARY KEY (`id`) ) ENGINE=InnoDB"; assertEquals(expectedDDL, schemaDatabase.addSourcePrefix(ddl)); ddl = "create table `airbed3_production`.`_users_ghc` ( \t\t\t" + "id bigint auto_increment, \t\t\t" + "last_update timestamp not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, \t\t\t" + "hint varchar(64) charset ascii not null, \t\t\t" + "value varchar(4096) charset ascii not null, \t\t\t" + "primary key(id), \t\t\t" + "unique key hint_uidx(hint) \t\t\t" + ") auto_increment=256"; expectedDDL = "create table `source/airbed3_production`.`_users_ghc` ( \t\t\t" + "id bigint auto_increment, \t\t\t" + "last_update timestamp not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, \t\t\t" + "hint varchar(64) charset ascii not null, \t\t\t" + "value varchar(4096) charset ascii not null, \t\t\t" + "primary key(id), \t\t\t" + "unique key hint_uidx(hint) \t\t\t" + ") auto_increment=256"; String d = schemaDatabase.addSourcePrefix(ddl); assertEquals(expectedDDL, d); }
@VisibleForTesting String addSourcePrefix(@NotNull final String sql) { CharStream charStream = CharStreams.fromString(sql); MySQLLexer lexer = new MySQLLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); ParseTree tree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); MySQLDBNamePrefixAdder prefixAdder = new com.airbnb.spinaltap.mysql.schema.MysqlSchemaDatabase.MySQLDBNamePrefixAdder(tokens); walker.walk(prefixAdder, tree); return prefixAdder.rewriter.getText(); }
MysqlSchemaDatabase { @VisibleForTesting String addSourcePrefix(@NotNull final String sql) { CharStream charStream = CharStreams.fromString(sql); MySQLLexer lexer = new MySQLLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); ParseTree tree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); MySQLDBNamePrefixAdder prefixAdder = new com.airbnb.spinaltap.mysql.schema.MysqlSchemaDatabase.MySQLDBNamePrefixAdder(tokens); walker.walk(prefixAdder, tree); return prefixAdder.rewriter.getText(); } }
MysqlSchemaDatabase { @VisibleForTesting String addSourcePrefix(@NotNull final String sql) { CharStream charStream = CharStreams.fromString(sql); MySQLLexer lexer = new MySQLLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); ParseTree tree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); MySQLDBNamePrefixAdder prefixAdder = new com.airbnb.spinaltap.mysql.schema.MysqlSchemaDatabase.MySQLDBNamePrefixAdder(tokens); walker.walk(prefixAdder, tree); return prefixAdder.rewriter.getText(); }  }
MysqlSchemaDatabase { @VisibleForTesting String addSourcePrefix(@NotNull final String sql) { CharStream charStream = CharStreams.fromString(sql); MySQLLexer lexer = new MySQLLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); ParseTree tree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); MySQLDBNamePrefixAdder prefixAdder = new com.airbnb.spinaltap.mysql.schema.MysqlSchemaDatabase.MySQLDBNamePrefixAdder(tokens); walker.walk(prefixAdder, tree); return prefixAdder.rewriter.getText(); }   }
MysqlSchemaDatabase { @VisibleForTesting String addSourcePrefix(@NotNull final String sql) { CharStream charStream = CharStreams.fromString(sql); MySQLLexer lexer = new MySQLLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lexer); MySQLParser parser = new MySQLParser(tokens); ParseTree tree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); MySQLDBNamePrefixAdder prefixAdder = new com.airbnb.spinaltap.mysql.schema.MysqlSchemaDatabase.MySQLDBNamePrefixAdder(tokens); walker.walk(prefixAdder, tree); return prefixAdder.rewriter.getText(); }    }