@@ -639,17 +639,11 @@ public function testInformationSchemaTablesAutoIncrement(): void {
639639 $ result = $ this ->assertQuery ( "SELECT * FROM information_schema.tables WHERE table_name = 't' " );
640640 $ this ->assertSame ( '1 ' , $ result [0 ]->AUTO_INCREMENT );
641641
642- // After inserts, the stored value advances, and a backtick-quoted
643- // projection returns the same result as SELECT *.
642+ // After inserts, the sequence advances.
644643 $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a'), ('b'), ('c') " );
645644 $ result = $ this ->assertQuery ( "SELECT * FROM information_schema.tables WHERE table_name = 't' " );
646645 $ this ->assertSame ( '4 ' , $ result [0 ]->AUTO_INCREMENT );
647646
648- $ result = $ this ->assertQuery (
649- "SELECT table_name, `auto_increment` FROM information_schema.tables WHERE table_name = 't' "
650- );
651- $ this ->assertSame ( '4 ' , $ result [0 ]->AUTO_INCREMENT );
652-
653647 // DELETE preserves the counter.
654648 $ this ->assertQuery ( 'DELETE FROM t ' );
655649 $ result = $ this ->assertQuery ( "SELECT * FROM information_schema.tables WHERE table_name = 't' " );
@@ -661,123 +655,198 @@ public function testInformationSchemaTablesAutoIncrement(): void {
661655 $ this ->assertSame ( '1 ' , $ result [0 ]->AUTO_INCREMENT );
662656 }
663657
664- public function testShowTableStatusAutoIncrement () {
658+ public function testInformationSchemaTablesFilterByAutoIncrement (): void {
659+ $ this ->assertQuery ( 'CREATE TABLE low (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
660+ $ this ->assertQuery ( 'CREATE TABLE high (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
661+ $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) ' );
662+ $ this ->assertQuery ( "INSERT INTO low (name) VALUES ('a') " );
663+ $ this ->assertQuery ( "INSERT INTO high (name) VALUES ('a'), ('b'), ('c'), ('d'), ('e') " );
664+
665+ // > 3
666+ $ result = $ this ->assertQuery (
667+ 'SELECT TABLE_NAME FROM information_schema.tables WHERE `AUTO_INCREMENT` > 3 '
668+ );
669+ $ this ->assertCount ( 1 , $ result );
670+ $ this ->assertSame ( 'high ' , $ result [0 ]->TABLE_NAME );
671+
672+ // IS NULL
673+ $ result = $ this ->assertQuery (
674+ 'SELECT TABLE_NAME FROM information_schema.tables WHERE `AUTO_INCREMENT` IS NULL '
675+ );
676+ $ this ->assertCount ( 1 , $ result );
677+ $ this ->assertSame ( 'plain ' , $ result [0 ]->TABLE_NAME );
678+ }
679+
680+ public function testShowTableStatusAutoIncrement (): void {
665681 // A non-AUTO_INCREMENT table reports NULL.
666682 $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) ' );
667- $ this ->assertSame ( null , $ this ->getAutoIncrement ( 'plain ' ) );
683+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 'plain' " );
684+ $ this ->assertSame ( null , $ result [0 ]->Auto_increment );
668685
669686 // A fresh AUTO_INCREMENT table reports 1.
670687 $ this ->assertQuery ( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
671- $ this ->assertSame ( '1 ' , $ this ->getAutoIncrement ( 't ' ) );
688+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
689+ $ this ->assertSame ( '1 ' , $ result [0 ]->Auto_increment );
672690
673- // After inserts, Auto_increment is the next value .
691+ // After inserts, the sequence advances .
674692 $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a'), ('b'), ('c') " );
675- $ this ->assertSame ( '4 ' , $ this ->getAutoIncrement ( 't ' ) );
676-
677- // DELETE must preserve the counter, matching InnoDB behavior.
678- $ this ->assertQuery ( 'DELETE FROM t WHERE id = 3 ' );
679- $ this ->assertSame ( '4 ' , $ this ->getAutoIncrement ( 't ' ) );
693+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
694+ $ this ->assertSame ( '4 ' , $ result [0 ]->Auto_increment );
680695
681- // DELETE of all rows still preserves the counter.
696+ // DELETE preserves the counter.
682697 $ this ->assertQuery ( 'DELETE FROM t ' );
683- $ this ->assertSame ( '4 ' , $ this ->getAutoIncrement ( 't ' ) );
698+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
699+ $ this ->assertSame ( '4 ' , $ result [0 ]->Auto_increment );
684700
685701 // TRUNCATE resets the counter.
686702 $ this ->assertQuery ( 'TRUNCATE TABLE t ' );
687- $ this ->assertSame ( '1 ' , $ this ->getAutoIncrement ( 't ' ) );
703+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
704+ $ this ->assertSame ( '1 ' , $ result [0 ]->Auto_increment );
688705 }
689706
690- public function testShowTableStatusFilterByAutoIncrement () {
707+ public function testShowTableStatusFilterByAutoIncrement (): void {
691708 $ this ->assertQuery ( 'CREATE TABLE low (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
692709 $ this ->assertQuery ( 'CREATE TABLE high (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
693710 $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) ' );
694711 $ this ->assertQuery ( "INSERT INTO low (name) VALUES ('a') " );
695712 $ this ->assertQuery ( "INSERT INTO high (name) VALUES ('a'), ('b'), ('c'), ('d'), ('e') " );
696713
697- // WHERE must filter on the computed Auto_increment alias, not on the
698- // always-NULL underlying info_schema column.
714+ // > 3
699715 $ result = $ this ->assertQuery ( 'SHOW TABLE STATUS WHERE `Auto_increment` > 3 ' );
700716 $ this ->assertCount ( 1 , $ result );
701717 $ this ->assertSame ( 'high ' , $ result [0 ]->Name );
702718
719+ // IS NULL
703720 $ result = $ this ->assertQuery ( 'SHOW TABLE STATUS WHERE `Auto_increment` IS NULL ' );
704721 $ this ->assertCount ( 1 , $ result );
705722 $ this ->assertSame ( 'plain ' , $ result [0 ]->Name );
706723 }
707724
708725 public function testShowCreateTableAutoIncrement (): void {
709- // No AUTO_INCREMENT=N on a non-AI table.
726+ // No AUTO_INCREMENT=N on a non-AUTO_INCREMENT table.
710727 $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) ' );
711- $ this ->assertStringNotContainsString (
712- 'AUTO_INCREMENT= ' ,
713- $ this ->getCreateTable ( 'plain ' )
714- );
728+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE plain ' );
729+ $ this ->assertStringNotContainsString ( 'AUTO_INCREMENT= ' , $ result [0 ]->{'Create Table ' } );
715730
716- // No AUTO_INCREMENT=N on a fresh AI table (counter has not advanced).
731+ // No AUTO_INCREMENT=N on a fresh AUTO_INCREMENT table (sequence not advanced).
717732 $ this ->assertQuery ( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
718- $ this ->assertStringNotContainsString (
719- 'AUTO_INCREMENT= ' ,
720- $ this ->getCreateTable ( 't ' )
721- );
733+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
734+ $ this ->assertStringNotContainsString ( 'AUTO_INCREMENT= ' , $ result [0 ]->{'Create Table ' } );
722735
723- // After inserts, AUTO_INCREMENT=<next value> is emitted .
736+ // After inserts, the sequence advances .
724737 $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a'), ('b'), ('c') " );
725- $ this ->assertStringContainsString (
726- 'AUTO_INCREMENT=4 ' ,
727- $ this ->getCreateTable ( 't ' )
728- );
738+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
739+ $ this ->assertStringContainsString ( 'AUTO_INCREMENT=4 ' , $ result [0 ]->{'Create Table ' } );
729740
730741 // DELETE preserves the counter.
731742 $ this ->assertQuery ( 'DELETE FROM t ' );
732- $ this ->assertStringContainsString (
733- 'AUTO_INCREMENT=4 ' ,
734- $ this ->getCreateTable ( 't ' )
735- );
743+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
744+ $ this ->assertStringContainsString ( 'AUTO_INCREMENT=4 ' , $ result [0 ]->{'Create Table ' } );
736745
737- // TRUNCATE resets the counter; AUTO_INCREMENT=N is omitted again .
746+ // TRUNCATE resets the counter.
738747 $ this ->assertQuery ( 'TRUNCATE TABLE t ' );
739- $ this ->assertStringNotContainsString (
740- 'AUTO_INCREMENT= ' ,
741- $ this ->getCreateTable ( 't ' )
742- );
748+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
749+ $ this ->assertStringNotContainsString ( 'AUTO_INCREMENT= ' , $ result [0 ]->{'Create Table ' } );
743750 }
744751
745- public function testShowCreateTableAutoIncrementOnTemporaryTable (): void {
746- // Temporary tables live in SQLite's `temp` schema with their own
747- // `sqlite_sequence`. Make sure the counter is read from the right one
748- // even when a persistent AI table in `main` might shadow it.
749- $ this ->assertQuery ( 'CREATE TABLE main_tbl (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
750- $ this ->assertQuery ( "INSERT INTO main_tbl (name) VALUES ('m') " );
751752
752- $ this ->assertQuery ( 'CREATE TEMPORARY TABLE tmp (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
753- $ this ->assertStringNotContainsString (
754- 'AUTO_INCREMENT= ' ,
755- $ this ->getCreateTable ( 'tmp ' )
753+ public function testCreateTableSetAutoIncrement (): void {
754+ $ this ->assertQuery (
755+ 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) AUTO_INCREMENT=100 '
756756 );
757+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
758+ $ this ->assertSame ( '100 ' , $ result [0 ]->Auto_increment );
757759
758- $ this ->assertQuery ( "INSERT INTO tmp (name) VALUES ('a'), ('b') " );
759- $ this ->assertStringContainsString (
760- 'AUTO_INCREMENT=3 ' ,
761- $ this ->getCreateTable ( 'tmp ' )
762- );
760+ // INSERT advances the sequence.
761+ $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a') " );
762+ $ this ->assertSame ( '100 ' , $ this ->assertQuery ( 'SELECT id FROM t ' )[0 ]->id );
763763
764- // The persistent table's counter must remain independent.
765- $ this ->assertStringContainsString (
766- 'AUTO_INCREMENT=2 ' ,
767- $ this ->getCreateTable ( 'main_tbl ' )
768- );
764+ // SHOW TABLE STATUS
765+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
766+ $ this ->assertSame ( '101 ' , $ result [0 ]->Auto_increment );
767+
768+ // SHOW CREATE TABLE
769+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
770+ $ this ->assertStringContainsString ( 'AUTO_INCREMENT=101 ' , $ result [0 ]->{'Create Table ' } );
771+
772+ // Without an AUTO_INCREMENT column, the option is ignored.
773+ $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) AUTO_INCREMENT=500 ' );
774+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 'plain' " );
775+ $ this ->assertSame ( null , $ result [0 ]->Auto_increment );
769776 }
770777
771- private function getAutoIncrement ( string $ table_name ): ?string {
778+ public function testAlterTableSetAutoIncrement (): void {
779+ $ this ->assertQuery ( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
780+
781+ // Set the sequence value on an empty table.
782+ $ this ->assertQuery ( 'ALTER TABLE t AUTO_INCREMENT = 50 ' );
783+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
784+ $ this ->assertSame ( '50 ' , $ result [0 ]->Auto_increment );
785+
786+ // INSERT advances the sequence.
787+ $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a') " );
788+ $ this ->assertSame ( '50 ' , $ this ->assertQuery ( 'SELECT id FROM t ' )[0 ]->id );
789+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
790+ $ this ->assertSame ( '51 ' , $ result [0 ]->Auto_increment );
791+
792+ // Update the sequence value.
793+ $ this ->assertQuery ( 'ALTER TABLE t AUTO_INCREMENT = 200 ' );
794+
795+ // SHOW TABLE STATUS
796+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
797+ $ this ->assertSame ( '200 ' , $ result [0 ]->Auto_increment );
798+
799+ // SHOW CREATE TABLE
800+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
801+ $ this ->assertStringContainsString ( 'AUTO_INCREMENT=200 ' , $ result [0 ]->{'Create Table ' } );
802+
803+ // Lowering the counter at or below MAX(id) clamps to MAX(id) + 1.
804+ $ this ->assertQuery ( 'ALTER TABLE t AUTO_INCREMENT = 1 ' );
805+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
806+ $ this ->assertSame ( '51 ' , $ result [0 ]->Auto_increment );
807+
808+ // Without an AUTO_INCREMENT column, the option is ignored.
809+ $ this ->assertQuery ( 'CREATE TABLE plain (id INT, name TEXT) AUTO_INCREMENT=500 ' );
810+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 'plain' " );
811+ $ this ->assertSame ( null , $ result [0 ]->Auto_increment );
812+ }
813+
814+ public function testTemporaryTableAutoIncrement (): void {
815+ // Persistent and temporary tables sharing a name must keep independent sequences.
816+
817+ // Persistent t: next id = 3.
818+ $ this ->assertQuery ( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) ' );
819+ $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('a'), ('b') " );
820+
821+ // Shadowing temporary t seeded to start at 500.
822+ $ this ->assertQuery (
823+ 'CREATE TEMPORARY TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) AUTO_INCREMENT=500 '
824+ );
825+
826+ // SHOW TABLE STATUS and information_schema.tables list only the
827+ // persistent table; the temporary one is not visible here.
828+ $ result = $ this ->assertQuery ( "SHOW TABLE STATUS LIKE 't' " );
829+ $ this ->assertCount ( 1 , $ result );
830+ $ this ->assertSame ( '3 ' , $ result [0 ]->Auto_increment );
831+
772832 $ result = $ this ->assertQuery (
773- sprintf ( " SHOW TABLE STATUS LIKE '%s' " , $ table_name )
833+ " SELECT `AUTO_INCREMENT` FROM information_schema.tables WHERE table_name = 't' "
774834 );
775- return $ result [0 ]->Auto_increment ;
776- }
835+ $ this ->assertCount ( 1 , $ result );
836+ $ this ->assertSame ( '3 ' , $ result [0 ]->AUTO_INCREMENT );
837+
838+ // Unqualified statements target the shadowing temporary table:
839+ // INSERT picks up the seeded value, ALTER updates the temp sequence.
840+ $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('x') " );
841+ $ this ->assertSame ( '500 ' , $ this ->assertQuery ( "SELECT id FROM t WHERE name = 'x' " )[0 ]->id );
842+
843+ $ this ->assertQuery ( 'ALTER TABLE t AUTO_INCREMENT = 1000 ' );
844+ $ this ->assertQuery ( "INSERT INTO t (name) VALUES ('y') " );
845+ $ this ->assertSame ( '1000 ' , $ this ->assertQuery ( "SELECT id FROM t WHERE name = 'y' " )[0 ]->id );
777846
778- private function getCreateTable ( string $ table_name ): string {
779- $ result = $ this ->assertQuery ( sprintf ( ' SHOW CREATE TABLE %s ' , $ table_name ) );
780- return $ result [0 ]->{ ' Create Table ' } ;
847+ // The persistent table's sequence must remain unaffected throughout.
848+ $ result = $ this ->assertQuery ( " SHOW TABLE STATUS LIKE 't' " );
849+ $ this -> assertSame ( ' 3 ' , $ result [0 ]->Auto_increment ) ;
781850 }
782851
783852 public function testShowFullColumns (): void {
0 commit comments