Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix postgresql Migration bug #1046

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,7 @@ doesTableExist getter (DBName name) = do
stmt <- getter sql
with (stmtQuery stmt vals) (\src -> runConduit $ src .| start)
where
sql = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog'"
<> " AND schemaname != 'information_schema' AND tablename=?"
sql = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE schemaname == current_schema() AND tablename=?"
vals = [PersistText name]

start = await >>= maybe (error "No results when checking doesTableExist") start'
Expand Down Expand Up @@ -684,10 +683,9 @@ getColumns getter def cols = do
,"c.column_name "
,"FROM information_schema.key_column_usage c, "
,"information_schema.table_constraints k "
,"WHERE c.table_catalog=current_database() "
,"AND c.table_catalog=k.table_catalog "
,"AND c.table_schema=current_schema() "
,"AND c.table_schema=k.table_schema "
,"WHERE "
, currentSchemaAndCatalog "c"
, currentSchemaAndCatalog "k"
,"AND c.table_name=? "
,"AND c.table_name=k.table_name "
,"AND c.column_name <> ? "
Expand Down Expand Up @@ -727,6 +725,13 @@ getColumns getter def cols = do
cols <- helper
return $ col' : cols

currentSchemaAndCatalog :: Text -> Text
currentSchemaAndCatalog table = "(" <> T.intercalate " AND " conds <> ")"
where conds = (table <>) <$> [".table_catalog = current_database()"
,".constraint_catalog = current_database()"
,".table_schema = current_schema()"
,".constraint_schema = current_schema()"]

-- | Check if a column name is listed as the "safe to remove" in the entity
-- list.
safeToRemove :: EntityDef -> DBName -> Bool
Expand Down Expand Up @@ -812,6 +817,9 @@ getColumn getter tableName' [PersistText columnName, PersistText isNullable, Per
,"information_schema.key_column_usage kcu, "
,"information_schema.table_constraints tc "
,"WHERE tc.constraint_type='FOREIGN KEY' "
, currentSchemaAndCatalog "ccu"
, currentSchemaAndCatalog "kcu"
, currentSchemaAndCatalog "tc"
,"AND kcu.constraint_name=tc.constraint_name "
,"AND ccu.constraint_name=kcu.constraint_name "
,"AND kcu.ordinal_position=1 "
Expand Down