WHEN David Dewhurst entered the race to be Texas’s next senator, his victory seemed assured. Republicans have controlled Texas since the mid-1990s and until recently, without much internal discord. Mr Dewhurst has been the lieutenant-governor since 2003 and his views have been solidly in line with the state’s Republican majority. He was endorsed by most of the state’s top Republican officials, poured a small fortune into his campaign and won the primary, in May.

He did not, however, win sufficiently well. The nomination went to a run-off between Mr Dewhurst and Ted Cruz, a favourite of the “tea-party” movement and formerly a state solicitor-general. In Mr Cruz’s view, the lieutenant-governor was not conservative enough. The evidence was flimsy; one of the charges was that Mr Dewhurst had publicly mentioned the possibility of a state-level income tax. But many Republicans agreed. On July 31st Mr Cruz thumped Mr Dewhurst in the run-off, by a 14-point margin.

It was a huge upset, and one that points to a looming divide among Texas Republicans. The party is thoroughly dominant in the state; Democrats have not won any statewide election since 1994. But in the past few years the state has been on a rightward swing. In 2009 Republicans controlled the state’s House of Representatives, but only by the slimmest possible majority, 76-74. At the start of the session moderate Republicans, joined by Democrats, installed Joe Straus, a relatively moderate Republican from San Antonio, as Speaker.

That was the year the tea party emerged on the national scene. By 2010 it was apparent that Texas Republicans were animated too, even if they had less to be angry about. In that year’s elections, they picked up a whopping 32 seats in the legislature. A number of incumbent Republicans had to deal with primary challenges from the right—including Rick Perry, the governor, who is generally considered a conservative. Dan Patrick, a senator from Houston, announced that he was starting a Tea Party Caucus. When the legislature reconvened, in 2011, the Republicans had a supermajority in the House and retained their 19-12 lead in the Senate.

Mr Cruz entered the Senate race with hardly any money or support. He was probably better known outside the state than inside it, having been tipped as a rising star by national conservative groups. But as outsiders poured money into his campaign, and Mr Cruz barnstormed around the state, Republican voters took notice and approved.

The strength of the right was evident at this year’s state Republican convention, held in June in Fort Worth. “What’s happened is those liberals are infiltrating the Republican Party,” said a delegate from the tiny town of Edith. Delegates were critical of Mr Straus, Mr Dewhurst, and a number of other legislators also deemed moderate.

And Mr Dewhurst is not the only high-profile Texas Republican to face a revolt from his own party lately. Mr Straus handily won his own primary challenge, but the tea-party want him binned as Speaker when the legislature reconvenes next year. Mr Perry is not routinely booed by the conservative right, but he has disappointed on some of their priorities, immigration being the most notable. Moderates have started to push back; business interests, for example, are sounding alarms about conservative willingness to cut the schools budget. The tea-partiers are unlikely to back down, however, after such a big win.

Mr Cruz’s upset may also be a blow to national Democrats. For several years, pointing to the state’s surging Hispanic population, they have proclaimed that Texas is bound to become a blue state, or at least a purple one, in time. “You’re not considered one of the battleground states, although that’s going to be changing soon,” said Barack Obama last month, fund-raising in San Antonio. On July 31st the Democrats announced that Julián Castro, the mayor of that city, will give the keynote speech at this year’s nominating convention—the same spot that launched Mr Obama to national attention in 2004. If Mr Cruz goes on to win at the general election in November, and that seems like a safe bet, it will dampen Democratic spirits, which are, in Texas, quite damp as it is.

There might, however, be a silver lining for Democrats in the longer term. If the Republicans keep fighting among themselves, it could create an opportunity for Democrats to make a pitch for moderates. Mr Obama was right, then, to say that Texas is becoming a battleground. In the short term, however, the battle will be within the Republican Party.