@@ -1658,18 +1658,18 @@ func TestAddColumnDefaultTransformation(t *testing.T) {
1658
1658
}, roll .WithSQLTransformer (sqlTransformer ))
1659
1659
}
1660
1660
1661
- func TestAddColumnToATableCreatedInTheSameMigration (t * testing.T ) {
1661
+ func TestAddColumnInMultiOperationMigrations (t * testing.T ) {
1662
1662
t .Parallel ()
1663
1663
1664
1664
ExecuteTests (t , TestCases {
1665
1665
{
1666
- name : "add column to newly created table " ,
1666
+ name : "create table, add column " ,
1667
1667
migrations : []migrations.Migration {
1668
1668
{
1669
- Name : "01_add_table " ,
1669
+ Name : "01_multi_operation " ,
1670
1670
Operations : migrations.Operations {
1671
1671
& migrations.OpCreateTable {
1672
- Name : "users " ,
1672
+ Name : "items " ,
1673
1673
Columns : []migrations.Column {
1674
1674
{
1675
1675
Name : "id" ,
@@ -1683,46 +1683,193 @@ func TestAddColumnToATableCreatedInTheSameMigration(t *testing.T) {
1683
1683
},
1684
1684
},
1685
1685
& migrations.OpAddColumn {
1686
- Table : "users " ,
1686
+ Table : "items " ,
1687
1687
Column : migrations.Column {
1688
- Name : "age" ,
1689
- Type : "integer" ,
1690
- Nullable : false ,
1691
- Default : ptr ("18" ),
1692
- Check : & migrations.CheckConstraint {
1693
- Name : "age_check" ,
1694
- Constraint : "age >= 18" ,
1688
+ Name : "description" ,
1689
+ Type : "text" ,
1690
+ },
1691
+ Up : "UPPER(name)" ,
1692
+ },
1693
+ },
1694
+ },
1695
+ },
1696
+ afterStart : func (t * testing.T , db * sql.DB , schema string ) {
1697
+ // Can insert into the view in the new schema (the only version schema)
1698
+ MustInsert (t , db , schema , "01_multi_operation" , "items" , map [string ]string {
1699
+ "name" : "apples" ,
1700
+ "description" : "green" ,
1701
+ })
1702
+
1703
+ // The table has the expected rows
1704
+ rows := MustSelect (t , db , schema , "01_multi_operation" , "items" )
1705
+ assert .Equal (t , []map [string ]any {
1706
+ {"id" : 1 , "name" : "apples" , "description" : "green" },
1707
+ }, rows )
1708
+ },
1709
+ afterRollback : func (t * testing.T , db * sql.DB , schema string ) {
1710
+ // The table no longer exists
1711
+ TableMustNotExist (t , db , schema , "items" )
1712
+ },
1713
+ afterComplete : func (t * testing.T , db * sql.DB , schema string ) {
1714
+ // Can insert into the view in the new schema (the only version schema)
1715
+ MustInsert (t , db , schema , "01_multi_operation" , "items" , map [string ]string {
1716
+ "name" : "bananas" ,
1717
+ "description" : "yellow" ,
1718
+ })
1719
+
1720
+ // The table has the expected rows
1721
+ rows := MustSelect (t , db , schema , "01_multi_operation" , "items" )
1722
+ assert .Equal (t , []map [string ]any {
1723
+ {"id" : 1 , "name" : "bananas" , "description" : "yellow" },
1724
+ }, rows )
1725
+ },
1726
+ },
1727
+ {
1728
+ name : "rename table, add column" ,
1729
+ migrations : []migrations.Migration {
1730
+ {
1731
+ Name : "01_create_table" ,
1732
+ Operations : migrations.Operations {
1733
+ & migrations.OpCreateTable {
1734
+ Name : "items" ,
1735
+ Columns : []migrations.Column {
1736
+ {
1737
+ Name : "id" ,
1738
+ Type : "serial" ,
1739
+ Pk : true ,
1740
+ },
1741
+ {
1742
+ Name : "name" ,
1743
+ Type : "varchar(255)" ,
1695
1744
},
1696
- Comment : ptr ("the age of the user" ),
1697
1745
},
1698
1746
},
1699
1747
},
1700
1748
},
1749
+ {
1750
+ Name : "02_multi_operation" ,
1751
+ Operations : migrations.Operations {
1752
+ & migrations.OpRenameTable {
1753
+ From : "items" ,
1754
+ To : "products" ,
1755
+ },
1756
+ & migrations.OpAddColumn {
1757
+ Table : "products" ,
1758
+ Column : migrations.Column {
1759
+ Name : "description" ,
1760
+ Type : "text" ,
1761
+ },
1762
+ Up : "UPPER(name)" ,
1763
+ },
1764
+ },
1765
+ },
1701
1766
},
1702
1767
afterStart : func (t * testing.T , db * sql.DB , schema string ) {
1703
- // Inserting into the new column on the new table works.
1704
- MustInsert (t , db , schema , "01_add_table" , "users" , map [string ]string {
1705
- "name" : "Alice" , "age" : "30" ,
1768
+ // Can insert into the new table in the new schema using its new name
1769
+ MustInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1770
+ "name" : "apples" ,
1771
+ "description" : "green" ,
1706
1772
})
1707
1773
1708
- // Inserting a value that doesn't meet the check constraint fails.
1709
- MustNotInsert (t , db , schema , "01_add_table" , "users" , map [string ]string {
1710
- "name" : "Bob" , "age" : "8" ,
1711
- }, testutils .CheckViolationErrorCode )
1774
+ // Can't insert into the new table in the new schema using its old name
1775
+ MustNotInsert (t , db , schema , "02_multi_operation" , "items" , map [string ]string {
1776
+ "name" : "bananas" ,
1777
+ "description" : "yellow" ,
1778
+ }, testutils .UndefinedTableErrorCode )
1779
+
1780
+ // The table has the expected rows in the old schema
1781
+ rows := MustSelect (t , db , schema , "01_create_table" , "items" )
1782
+ assert .Equal (t , []map [string ]any {
1783
+ {"id" : 1 , "name" : "apples" },
1784
+ }, rows )
1785
+
1786
+ // The table has the expected rows in the new schema
1787
+ rows = MustSelect (t , db , schema , "02_multi_operation" , "products" )
1788
+ assert .Equal (t , []map [string ]any {
1789
+ {"id" : 1 , "name" : "apples" , "description" : "green" },
1790
+ }, rows )
1712
1791
},
1713
1792
afterRollback : func (t * testing.T , db * sql.DB , schema string ) {
1793
+ // Can insert into the old table in the old schema using its old name
1794
+ MustInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1795
+ "name" : "bananas" ,
1796
+ })
1797
+
1798
+ // The temporary column, functions and triggers have been cleaned up
1799
+ TableMustBeCleanedUp (t , db , schema , "items" , "description" )
1714
1800
},
1715
1801
afterComplete : func (t * testing.T , db * sql.DB , schema string ) {
1716
- // Inserting into the new column on the new table works.
1717
- MustInsert (t , db , schema , "01_add_table" , "users" , map [string ]string {
1718
- "name" : "Bob" , "age" : "31" ,
1802
+ // Can insert into the new table in the new schema using its new name
1803
+ MustInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1804
+ "name" : "carrots" ,
1805
+ "description" : "crunchy" ,
1719
1806
})
1720
1807
1721
- // Inserting a value that doesn't meet the check constraint fails.
1722
- MustNotInsert (t , db , schema , "01_add_table" , "users" , map [string ]string {
1723
- "name" : "Carl" , "age" : "8" ,
1724
- }, testutils .CheckViolationErrorCode )
1808
+ // The table has the new name in the new schema and has the expected
1809
+ // rows
1810
+ rows := MustSelect (t , db , schema , "02_multi_operation" , "products" )
1811
+ assert .Equal (t , []map [string ]any {
1812
+ {"id" : 1 , "name" : "apples" , "description" : "APPLES" },
1813
+ {"id" : 2 , "name" : "bananas" , "description" : "BANANAS" },
1814
+ {"id" : 3 , "name" : "carrots" , "description" : "crunchy" },
1815
+ }, rows )
1816
+
1817
+ // The temporary column, functions and triggers have been cleaned up
1818
+ TableMustBeCleanedUp (t , db , schema , "products" , "description" )
1819
+ },
1820
+ },
1821
+ })
1822
+ }
1823
+
1824
+ func TestAddColumnValidationInMultiOperationMigrations (t * testing.T ) {
1825
+ t .Parallel ()
1826
+
1827
+ ExecuteTests (t , TestCases {
1828
+ {
1829
+ name : "adding a column with the same name twice fails to validate" ,
1830
+ migrations : []migrations.Migration {
1831
+ {
1832
+ Name : "01_create_table" ,
1833
+ Operations : migrations.Operations {
1834
+ & migrations.OpCreateTable {
1835
+ Name : "items" ,
1836
+ Columns : []migrations.Column {
1837
+ {
1838
+ Name : "id" ,
1839
+ Type : "serial" ,
1840
+ Pk : true ,
1841
+ },
1842
+ {
1843
+ Name : "name" ,
1844
+ Type : "varchar(255)" ,
1845
+ },
1846
+ },
1847
+ },
1848
+ },
1849
+ },
1850
+ {
1851
+ Name : "02_multi_operation" ,
1852
+ Operations : migrations.Operations {
1853
+ & migrations.OpAddColumn {
1854
+ Table : "items" ,
1855
+ Column : migrations.Column {
1856
+ Name : "description" ,
1857
+ Type : "text" ,
1858
+ },
1859
+ Up : "UPPER(name)" ,
1860
+ },
1861
+ & migrations.OpAddColumn {
1862
+ Table : "items" ,
1863
+ Column : migrations.Column {
1864
+ Name : "description" ,
1865
+ Type : "varchar(255)" ,
1866
+ },
1867
+ Up : "UPPER(name)" ,
1868
+ },
1869
+ },
1870
+ },
1725
1871
},
1872
+ wantStartErr : migrations.ColumnAlreadyExistsError {Table : "items" , Name : "description" },
1726
1873
},
1727
1874
})
1728
1875
}
0 commit comments