Skip to content

Commit c061a54

Browse files
committed
Revert "Allow using nonreserved keywords in table indices. (#136)"
This reverts commit efa5c3f.
1 parent 3188039 commit c061a54

4 files changed

Lines changed: 7 additions & 104 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Adds a `Function` datatype and a `defaultTriggerFunction` backwards-compatible
55
trigger function helper.
66
* Remove `Read` instance of `IndexMethod`.
7-
* Allow using nonreserved keywords, e.g. `timestamp`, in table indices.
87
* Add support for the `NUMERIC` column type.
98
* Fix getting the row estimate for `ModifyColumnMigration` if a table with the
109
same name is present in multiple schemas.

src/Database/PostgreSQL/PQTypes/Checks.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,8 @@ fetchTableIndex
15391539
-> (TableIndex, RawSQL ())
15401540
fetchTableIndex (name, Array1 keyColumns, Array1 includeColumns, method, unique, nullsNotDistinct, mconstraint) =
15411541
( TableIndex
1542-
{ idxColumns = map (indexColumn . (`rawSQL` ()) . stripIdentifierQuotes) keyColumns
1543-
, idxInclude = map ((`rawSQL` ()) . stripIdentifierQuotes) includeColumns
1542+
{ idxColumns = map (indexColumn . (`rawSQL` ())) keyColumns
1543+
, idxInclude = map (`rawSQL` ()) includeColumns
15441544
, idxMethod = case method of
15451545
"gin" -> GIN
15461546
"btree" -> BTree
@@ -1551,10 +1551,6 @@ fetchTableIndex (name, Array1 keyColumns, Array1 includeColumns, method, unique,
15511551
}
15521552
, rawSQL name ()
15531553
)
1554-
where
1555-
stripIdentifierQuotes str = case fmap T.unsnoc <$> T.uncons str of
1556-
Just ('"', Just (identifier, '"')) -> identifier
1557-
_ -> str
15581554

15591555
-- *** FOREIGN KEYS ***
15601556

src/Database/PostgreSQL/PQTypes/Model/Index.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ instance Ord IndexColumn where
6565
instance IsString IndexColumn where
6666
fromString s = IndexColumn (fromString s) Nothing
6767

68+
-- | Build an 'IndexColumn' from a column name. If the column name is a
69+
-- non-reserved keyword in PostgreSQL (for example @timestamp@), it must be
70+
-- given double-quoted (@\"\\\"timestamp\\\"\"@). PostgreSQL stores the index
71+
-- definition with the keyword quoted, so the column name passed here has to
72+
-- match that exactly or the database consistency check will report a mismatch.
6873
indexColumn :: RawSQL () -> IndexColumn
6974
indexColumn col = IndexColumn col Nothing
7075

test/Main.hs

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,102 +2325,6 @@ nullsNotDistinctTests connSource = do
23252325
]
23262326
}
23272327

2328-
reservedWordColumnTests :: ConnectionSourceM (LogT IO) -> TestTree
2329-
reservedWordColumnTests connSource =
2330-
testCaseSteps' "Reserved word column tests" connSource $ \step -> do
2331-
freshTestDB step
2332-
2333-
step "Create a table with a plain index on a 'timestamp' column"
2334-
assertNoException "Table with index on 'timestamp' column should be created successfully" $ do
2335-
let definitions = tableDefsWithPgCrypto [tableWithTimestampIndex]
2336-
migrateDatabase
2337-
defaultExtrasOptions
2338-
definitions
2339-
[createTableMigration tableWithTimestampIndex]
2340-
checkDatabase defaultExtrasOptions definitions
2341-
2342-
freshTestDB step
2343-
2344-
step "Create a table with a unique index on a 'timestamp' column"
2345-
assertNoException "Table with unique index on 'timestamp' column should be created successfully" $ do
2346-
let definitions = tableDefsWithPgCrypto [tableWithUniqueTimestampIndex]
2347-
migrateDatabase
2348-
defaultExtrasOptions
2349-
definitions
2350-
[createTableMigration tableWithUniqueTimestampIndex]
2351-
checkDatabase defaultExtrasOptions definitions
2352-
2353-
freshTestDB step
2354-
2355-
step "Create a table with a composite index including a 'timestamp' column"
2356-
assertNoException "Table with composite index on 'timestamp' column should be created successfully" $ do
2357-
let definitions = tableDefsWithPgCrypto [tableWithCompositeTimestampIndex]
2358-
migrateDatabase
2359-
defaultExtrasOptions
2360-
definitions
2361-
[createTableMigration tableWithCompositeTimestampIndex]
2362-
checkDatabase defaultExtrasOptions definitions
2363-
2364-
freshTestDB step
2365-
2366-
step "Attempt to create a table with an index on a 'select' column"
2367-
assertException "Table with index on 'select' column can't be created" $ do
2368-
let definitions = tableDefsWithPgCrypto [tableWithSelectIndex]
2369-
migrateDatabase
2370-
defaultExtrasOptions
2371-
definitions
2372-
[createTableMigration tableWithSelectIndex]
2373-
checkDatabase defaultExtrasOptions definitions
2374-
where
2375-
tableWithTimestampIndex =
2376-
tblTable
2377-
{ tblName = "reserved_word_test1"
2378-
, tblVersion = 1
2379-
, tblColumns =
2380-
[ tblColumn {colName = "id", colType = UuidT, colNullable = False, colDefault = Just "gen_random_uuid()"}
2381-
, tblColumn {colName = "timestamp", colType = TimestampWithZoneT, colNullable = False}
2382-
]
2383-
, tblPrimaryKey = pkOnColumn "id"
2384-
, tblIndexes = [indexOnColumn "timestamp"]
2385-
}
2386-
2387-
tableWithUniqueTimestampIndex =
2388-
tblTable
2389-
{ tblName = "reserved_word_test2"
2390-
, tblVersion = 1
2391-
, tblColumns =
2392-
[ tblColumn {colName = "id", colType = UuidT, colNullable = False, colDefault = Just "gen_random_uuid()"}
2393-
, tblColumn {colName = "timestamp", colType = TimestampWithZoneT, colNullable = False}
2394-
]
2395-
, tblPrimaryKey = pkOnColumn "id"
2396-
, tblIndexes = [uniqueIndexOnColumn "timestamp"]
2397-
}
2398-
2399-
tableWithCompositeTimestampIndex =
2400-
tblTable
2401-
{ tblName = "reserved_word_test3"
2402-
, tblVersion = 1
2403-
, tblColumns =
2404-
[ tblColumn {colName = "id", colType = UuidT, colNullable = False, colDefault = Just "gen_random_uuid()"}
2405-
, tblColumn {colName = "timestamp", colType = TimestampWithZoneT, colNullable = False}
2406-
, tblColumn {colName = "name", colType = TextT, colNullable = True}
2407-
]
2408-
, tblPrimaryKey = pkOnColumn "id"
2409-
, tblIndexes = [indexOnColumns [indexColumn "timestamp", indexColumn "name"]]
2410-
}
2411-
2412-
tableWithSelectIndex =
2413-
tblTable
2414-
{ tblName = "reserved_word_test4"
2415-
, tblVersion = 1
2416-
, tblColumns =
2417-
[ tblColumn {colName = "id", colType = UuidT, colNullable = False, colDefault = Just "gen_random_uuid()"}
2418-
, tblColumn {colName = "select", colType = TextT, colNullable = True}
2419-
]
2420-
, tblPrimaryKey = pkOnColumn "id"
2421-
, tblIndexes = [indexOnColumn "select"]
2422-
}
2423-
24242328
sqlAnyAllTests :: TestTree
24252329
sqlAnyAllTests =
24262330
testGroup
@@ -2720,7 +2624,6 @@ main = do
27202624
, foreignKeyIndexesTests connSource
27212625
, overlapingIndexesTests connSource
27222626
, nullsNotDistinctTests connSource
2723-
, reservedWordColumnTests connSource
27242627
, sqlAnyAllTests
27252628
, enumTest connSource
27262629
, numericColumnTypeTest connSource

0 commit comments

Comments
 (0)