Skip to content

Commit 2a0dc9d

Browse files
committed
feat(migration): nullable key column in repo
1 parent a0e5316 commit 2a0dc9d

4 files changed

Lines changed: 50 additions & 1 deletion

File tree

db/Migration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func GetMigrations(dialect string) []Migration {
126126
{Version: "2.18.2"},
127127
{Version: "2.18.4"},
128128
{Version: "2.18.5"},
129+
{Version: "2.18.7"},
129130
}
130131

131132
return append(initScripts, commonScripts...)

db/sql/migration.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
190190
}
191191
}
192192

193+
if d.GetDialect() == util.DbDriverSQLite {
194+
_, err = d.Sql().Exec("PRAGMA foreign_keys = OFF")
195+
if err != nil {
196+
panic(err)
197+
}
198+
}
199+
193200
tx, err := d.Sql().Begin()
194201
if err != nil {
195202
return err
@@ -249,7 +256,16 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
249256

250257
fmt.Println()
251258

252-
return tx.Commit()
259+
res := tx.Commit()
260+
261+
if d.GetDialect() == util.DbDriverSQLite {
262+
_, err = d.Sql().Exec("PRAGMA foreign_keys = ON")
263+
if err != nil {
264+
panic(err)
265+
}
266+
}
267+
268+
return res
253269
}
254270

255271
// TryRollbackMigration attempts to rollback the database to an earlier version if a rollback exists

db/sql/migrations/v.2.18.7.err.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- do nothing

db/sql/migrations/v2.18.7.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{if .Sqlite}}
2+
create table project__repository_dg_tmp
3+
(
4+
id INTEGER
5+
primary key autoincrement,
6+
project_id INTEGER not null
7+
references project
8+
on delete cascade,
9+
git_url TEXT not null,
10+
ssh_key_id INTEGER
11+
references access_key,
12+
name VARCHAR(255),
13+
git_branch VARCHAR(255) default '' not null
14+
);
15+
16+
insert into project__repository_dg_tmp(id, project_id, git_url, ssh_key_id, name, git_branch)
17+
select id, project_id, git_url, ssh_key_id, name, git_branch
18+
from project__repository;
19+
20+
drop table project__repository;
21+
22+
alter table project__repository_dg_tmp rename to project__repository;
23+
24+
create index project__repository__project_id on project__repository (project_id);
25+
26+
create index project__repository__ssh_key_id on project__repository (ssh_key_id);
27+
{{else if .Mysql}}
28+
alter table project__repository modify ssh_key_id int null;
29+
{{else if .Postgresql}}
30+
alter table public.project__repository alter column ssh_key_id drop not null;
31+
{{end}}

0 commit comments

Comments
 (0)