@@ -4,12 +4,14 @@ import (
44 "context"
55 "database/sql"
66 "fmt"
7- mssql "github.com/denisenkom/go-mssqldb"
8- "github.com/pkg/errors"
9- "github.com/vippsas/sqlcode/sqlparser"
107 "io/fs"
118 "strconv"
129 "strings"
10+ "time"
11+
12+ mssql "github.com/denisenkom/go-mssqldb"
13+ "github.com/pkg/errors"
14+ "github.com/vippsas/sqlcode/sqlparser"
1315)
1416
1517type Deployable struct {
@@ -137,6 +139,8 @@ func (d *Deployable) EnsureUploaded(ctx context.Context, dbc DB) error {
137139
138140 lockResourceName := "sqlcode.EnsureUploaded/" + d .SchemaSuffix
139141
142+ // When a lock is opened with the Transaction lock owner,
143+ // that lock is released when the transaction is committed or rolled back.
140144 var lockRetCode int
141145 err := dbc .QueryRowContext (ctx , `
142146declare @retcode int;
@@ -261,3 +265,51 @@ func MustInclude(opts Options, fsys ...fs.FS) Deployable {
261265 }
262266 return result
263267}
268+
269+ type SchemaObject struct {
270+ Name string
271+ SchemaId int
272+ Objects int
273+ CreateDate time.Time
274+ ModifyDate time.Time
275+ }
276+
277+ func (s * SchemaObject ) Suffix () string {
278+ return strings .Split (s .Name , "@" )[1 ]
279+ }
280+
281+ // Return a list of sqlcode schemas have been uploaded to the database.
282+ // This includes all current and unused schemas.
283+ func (d * Deployable ) ListUploaded (ctx context.Context , dbc DB ) []* SchemaObject {
284+ objects := []* SchemaObject {}
285+ impersonate (ctx , dbc , "sqlcode-deploy-sandbox-user" , func (conn * sql.Conn ) error {
286+ rows , err := conn .QueryContext (ctx , `
287+ select
288+ s.name
289+ , s.schema_id
290+ , o.objects
291+ , o.create_date
292+ , o.modify_date
293+ from sys.schemas s
294+ outer apply (
295+ select count(o.object_id) as objects
296+ , min(o.create_date) as create_date
297+ , max(o.modify_date) as modify_date
298+ from sys.objects o
299+ where o.schema_id = s.schema_id
300+ ) as o
301+ where s.name like 'code@%'` )
302+ if err != nil {
303+ return err
304+ }
305+
306+ for rows .Next () {
307+ zero := & SchemaObject {}
308+ rows .Scan (& zero .Name , & zero .Objects , & zero .SchemaId , & zero .CreateDate , & zero .ModifyDate )
309+ objects = append (objects , zero )
310+ }
311+
312+ return nil
313+ })
314+ return objects
315+ }
0 commit comments