@@ -38,24 +38,32 @@ import Data.Aeson qualified as Aeson
3838sqliteExtern :: (MonadIO m ) => FilePath -> Module -> Docs. Module -> ExternsFile -> m ()
3939sqliteExtern outputDir m docs extern = liftIO $ do
4040 conn <- SQLite. open db
41+
42+ withRetry $ SQLite. execute_ conn " pragma foreign_keys = ON;"
43+
44+ withRetry $ SQLite. executeNamed conn
45+ " delete from modules where module_name = :module_name"
46+ [ " :module_name" := runModuleName ( efModuleName extern )
47+ ]
48+
4149 withRetry $ SQLite. executeNamed conn
42- " INSERT INTO modules (module_name, comment, extern, dec) VALUES (:module_name, :docs, :extern, :dec)"
50+ " insert into modules (module_name, comment, extern, dec) values (:module_name, :docs, :extern, :dec)"
4351 [ " :module_name" := runModuleName ( efModuleName extern )
4452 , " :docs" := Docs. modComments docs
4553 , " :extern" := Serialise. serialise extern
4654 , " :dec" := show ( efExports extern )
4755 ]
4856
4957 for_ (efImports extern) (\ i -> do
50- withRetry $ SQLite. executeNamed conn " INSERT INTO dependencies (module_name, dependency) VALUES (:module_name, :dependency)"
58+ withRetry $ SQLite. executeNamed conn " insert into dependencies (module_name, dependency) values (:module_name, :dependency)"
5159 [ " :module_name" := runModuleName (efModuleName extern )
5260 , " :dependency" := runModuleName (eiModule i)
5361 ])
5462
5563 for_ (toIdeDeclarationAnn m extern) (\ ideDeclaration -> do
5664 withRetry $ SQLite. executeNamed conn
57- (" INSERT INTO ide_declarations (module_name, name, namespace, declaration_type, span, declaration) " <>
58- " VALUES (:module_name, :name, :namespace, :declaration_type, :span, :declaration)"
65+ (" insert into ide_declarations (module_name, name, namespace, declaration_type, span, declaration) " <>
66+ " values (:module_name, :name, :namespace, :declaration_type, :span, :declaration)"
5967 )
6068 [ " :module_name" := runModuleName (efModuleName extern )
6169 , " :name" := identifierFromIdeDeclaration (discardAnn ideDeclaration)
@@ -67,8 +75,8 @@ sqliteExtern outputDir m docs extern = liftIO $ do
6775
6876 for_ (Docs. modDeclarations docs) (\ d -> do
6977 withRetry $ SQLite. executeNamed conn
70- (" INSERT INTO declarations (module_name, name, namespace, declaration_type, span, type, docs, declaration) " <>
71- " VALUES (:module_name, :name, :namespace, :declaration_type, :span, :type, :docs, :declaration)"
78+ (" insert into declarations (module_name, name, namespace, declaration_type, span, type, docs, declaration) " <>
79+ " values (:module_name, :name, :namespace, :declaration_type, :span, :type, :docs, :declaration)"
7280 )
7381 [ " :module_name" := runModuleName (efModuleName extern)
7482 , " :name" := Docs. declTitle d
@@ -83,8 +91,8 @@ sqliteExtern outputDir m docs extern = liftIO $ do
8391
8492 for_ (declChildren d) $ \ ch -> do
8593 withRetry $ SQLite. executeNamed conn
86- (" INSERT INTO declarations (module_name, name, span, docs, declaration) " <>
87- " VALUES (:module_name, :name, :span, :docs, :declaration)" )
94+ (" insert into declarations (module_name, name, span, docs, declaration) " <>
95+ " values (:module_name, :name, :span, :docs, :declaration)" )
8896 [ " :module_name" := runModuleName (efModuleName extern)
8997 , " :name" := Docs. cdeclTitle ch
9098 , " :span" := Aeson. encode (Docs. declSourceSpan d)
@@ -97,8 +105,8 @@ sqliteExtern outputDir m docs extern = liftIO $ do
97105 for_ (Docs. modReExports docs) $ \ rexport -> do
98106 for_ (snd rexport) $ \ d -> do
99107 withRetry $ SQLite. executeNamed conn
100- (" INSERT INTO declarations (module_name, name, rexported_from, declaration_type, span, type, docs, declaration)" <>
101- " VALUES (:module_name, :name, :rexported_from, :declaration_type, :span, :type, :docs, :declaration)"
108+ (" insert into declarations (module_name, name, rexported_from, declaration_type, span, type, docs, declaration)" <>
109+ " values (:module_name, :name, :rexported_from, :declaration_type, :span, :type, :docs, :declaration)"
102110 )
103111 [ " :module_name" := runModuleName (efModuleName extern)
104112 , " :name" := Docs. declTitle d
@@ -148,7 +156,8 @@ sqliteInit :: (MonadIO m) => FilePath -> m ()
148156sqliteInit outputDir = liftIO $ do
149157 createParentDirectory db
150158 conn <- SQLite. open db
151- withRetry $ SQLite. execute_ conn " pragma journal_mode=wal"
159+ withRetry $ SQLite. execute_ conn " pragma journal_mode=wal;"
160+ withRetry $ SQLite. execute_ conn " pragma foreign_keys = ON;"
152161 withRetry $ SQLite. execute_ conn $ SQLite. Query $ Text. pack $ unlines
153162 [ " create table if not exists modules ("
154163 , " module_name text primary key,"
@@ -158,11 +167,18 @@ sqliteInit outputDir = liftIO $ do
158167 , " unique (module_name) on conflict replace"
159168 , " )"
160169 ]
161- withRetry $ SQLite. execute_ conn " create table if not exists dependencies (id integer primary key, module_name text not null, dependency text not null, unique (module_name, dependency) on conflict ignore)"
170+
171+ withRetry $ SQLite. execute_ conn $ SQLite. Query $ Text. pack $ unlines
172+ [ " create table if not exists dependencies ("
173+ , " module_name text not null references modules(module_name) on delete cascade,"
174+ , " dependency text not null,"
175+ , " unique (module_name, dependency) on conflict ignore"
176+ , " )"
177+ ]
162178
163179 withRetry $ SQLite. execute_ conn $ SQLite. Query $ Text. pack $ unlines
164180 [ " create table if not exists declarations ("
165- , " module_name text,"
181+ , " module_name text references modules(module_name) on delete cascade ,"
166182 , " name text not null,"
167183 , " namespace text,"
168184 , " rexported_from text,"
@@ -174,7 +190,8 @@ sqliteInit outputDir = liftIO $ do
174190 , " )"
175191 ]
176192
177- withRetry $ SQLite. execute_ conn " create index if not exists dm on declarations(module_name); create index dn on declarations(name);"
193+ withRetry $ SQLite. execute_ conn " create index if not exists dm on declarations(module_name)"
194+ withRetry $ SQLite. execute_ conn " create index if not exists dn on declarations(name);"
178195
179196 withRetry $ SQLite. execute_ conn " create table if not exists ide_declarations (module_name text, name text, namespace text, declaration_type text, span blob, declaration blob)"
180197 SQLite. close conn
0 commit comments