11// Refuses fork changes to the database schema: T3 has no down-migrations, so a fork migration
2- // strands installs that return to upstream. Fork state goes in existing JSON fields instead.
3- // Usage: node .github/scripts/fork-schema-check.cjs <base> <head> (checks <base>...<head>).
2+ // strands installs that return to upstream. Usage: fork-schema-check.cjs <base> <head>.
3+ // Refused: migrations, schema setup and what they import; shipped files holding a schema statement
4+ // (matched across lines); this check itself and .gitattributes. Files are read as git blobs.
45
56const { execFileSync } = require ( "node:child_process" ) ;
7+ const path = require ( "node:path" ) ;
68
7- // Files that define or apply the schema. Any change to them is refused.
8- const SCHEMA_PATHS = [
9+ // Files that define or apply the schema. Any change to them, or to what they import, is refused.
10+ const SCHEMA_ROOTS = [
911 / ^ a p p s \/ s e r v e r \/ s r c \/ p e r s i s t e n c e \/ M i g r a t i o n s ( \/ | \. t s $ ) / ,
1012 / ^ a p p s \/ s e r v e r \/ s r c \/ p e r s i s t e n c e \/ d a t a b a s e M i g r a t i o n H e a d \. t s $ / ,
1113 / ^ a p p s \/ s e r v e r \/ s r c \/ p e r s i s t e n c e \/ i n i t i a l i z e V 2 D a t a b a s e \. t s $ / ,
1214 / ^ a p p s \/ s e r v e r \/ s r c \/ p e r s i s t e n c e \/ r e c o n c i l e V 2 P r e v i e w M i g r a t i o n \. t s $ / ,
1315] ;
1416
15- // Schema statements added anywhere else in shipped source (tests may build their own tables).
17+ // The check's own files. Changing them would let a PR judge itself, so it is never allowed.
18+ const CHECK_SCRIPT = ".github/scripts/fork-schema-check.cjs" ;
19+ const CHECK_FILES = [
20+ CHECK_SCRIPT ,
21+ ".github/scripts/fork-schema-check.test.cjs" ,
22+ ".github/workflows/fork-checks.yml" ,
23+ ] ;
24+ const GITATTRIBUTES = / ( ^ | \/ ) \. g i t a t t r i b u t e s $ / ;
25+
26+ // Schema statements in shipped source (tests may build their own tables). \s spans line breaks.
1627const SCHEMA_STATEMENT =
1728 / \b ( C R E A T E \s + ( U N I Q U E \s + ) ? ( T A B L E | I N D E X | V I E W | T R I G G E R | V I R T U A L \s + T A B L E ) | A L T E R \s + T A B L E | D R O P \s + ( T A B L E | I N D E X | V I E W | T R I G G E R ) ) \b / i;
18- const SHIPPED_SOURCE = / ^ ( a p p s | p a c k a g e s ) \/ .+ \. ( t s | t s x | j s | m j s | c j s | s q l ) $ / ;
19- const TEST_SOURCE = / ( \. t e s t \. | \. s p e c \. | \/ t e s t k i t \/ | \/ _ _ t e s t s _ _ \/ | \/ t e s t \/ ) / ;
29+ const SHIPPED_SOURCE = / ^ ( a p p s | p a c k a g e s ) \/ .+ \. ( t s | t s x | m t s | c t s | j s | m j s | c j s | s q l ) $ / ;
30+ const TEST_SOURCE = / ( \. t e s t \. | \. s p e c \. | \. t e s t k i t \. | \/ t e s t k i t \/ | \/ _ _ t e s t s _ _ \/ | \/ t e s t \/ ) / ;
31+ const SOURCE_EXTENSIONS = [ "" , ".ts" , ".tsx" , ".mts" , ".js" , "/index.ts" , "/index.tsx" ] ;
32+ const PACKAGE_MANIFEST = / ^ ( a p p s | p a c k a g e s ) \/ [ ^ / ] + \/ p a c k a g e \. j s o n $ / ;
33+ const IMPORT_SPECIFIER = / \b (?: f r o m | i m p o r t | r e q u i r e ) \s * \( ? \s * [ " ' ] ( [ ^ " ' \n ] + ) [ " ' ] / g;
34+
35+ const isTest = ( file ) => TEST_SOURCE . test ( file ) ;
36+ const isShippedSource = ( file ) => SHIPPED_SOURCE . test ( file ) && ! isTest ( file ) ;
37+
38+ /**
39+ * Files the schema roots import, transitively: relative and package subpath imports. Package
40+ * roots (`@t3tools/contracts`) re-export the whole package, so they are not followed.
41+ * @param {{ files: string[]; has(file: string): boolean; read(file: string): string } } tree
42+ * @returns {Set<string> }
43+ */
44+ function schemaClosure ( tree ) {
45+ const packages = packageDirectories ( tree ) ;
46+ const queue = tree . files . filter (
47+ ( file ) => SCHEMA_ROOTS . some ( ( pattern ) => pattern . test ( file ) ) && ! isTest ( file ) ,
48+ ) ;
49+ const closure = new Set ( queue ) ;
50+ while ( queue . length > 0 ) {
51+ const file = queue . pop ( ) ;
52+ const text = tree . read ( file ) ;
53+ for ( const match of text . matchAll ( IMPORT_SPECIFIER ) ) {
54+ const target = resolveImport ( tree , packages , file , match [ 1 ] ) ;
55+ if ( target && ! isTest ( target ) && ! closure . has ( target ) ) {
56+ closure . add ( target ) ;
57+ queue . push ( target ) ;
58+ }
59+ }
60+ }
61+ return closure ;
62+ }
63+
64+ function packageDirectories ( tree ) {
65+ const packages = new Map ( ) ;
66+ for ( const file of tree . files ) {
67+ if ( ! PACKAGE_MANIFEST . test ( file ) ) continue ;
68+ try {
69+ const manifest = JSON . parse ( tree . read ( file ) ) ;
70+ if ( typeof manifest . name === "string" ) {
71+ packages . set ( manifest . name , { dir : path . posix . dirname ( file ) , exports : manifest . exports } ) ;
72+ }
73+ } catch {
74+ // A broken package.json fails the build elsewhere; it defines no schema imports here.
75+ }
76+ }
77+ return packages ;
78+ }
79+
80+ function resolveImport ( tree , packages , from , specifier ) {
81+ if ( specifier . startsWith ( "." ) ) {
82+ return resolveFile ( tree , path . posix . join ( path . posix . dirname ( from ) , specifier ) ) ;
83+ }
84+ const scoped = specifier . match ( / ^ ( @ [ ^ / ] + \/ [ ^ / ] + ) \/ ( .+ ) $ / ) ;
85+ const pkg = scoped && packages . get ( scoped [ 1 ] ) ;
86+ if ( ! pkg || ! pkg . exports || typeof pkg . exports !== "object" ) return undefined ;
87+ const entry = pkg . exports [ `./${ scoped [ 2 ] } ` ] ;
88+ const target =
89+ typeof entry === "string" ? entry : entry && ( entry . import ?? entry . default ?? entry . types ) ;
90+ return typeof target === "string"
91+ ? resolveFile ( tree , path . posix . join ( pkg . dir , target ) )
92+ : undefined ;
93+ }
94+
95+ function resolveFile ( tree , file ) {
96+ const candidates = SOURCE_EXTENSIONS . map ( ( extension ) => file + extension ) ;
97+ if ( file . endsWith ( ".js" ) ) candidates . unshift ( file . replace ( / \. j s $ / , ".ts" ) ) ;
98+ return candidates . find ( ( candidate ) => tree . has ( candidate ) ) ;
99+ }
20100
21101/**
22- * @param {ReadonlyArray<{ path: string; addedLines: ReadonlyArray<string> }> } files
102+ * @param {ReadonlyArray<{ path: string; before?: string; after?: string }> } changes
103+ * one entry per changed path; `before`/`after` are the file texts (undefined when absent)
104+ * @param {{ closure: ReadonlySet<string>; baseHasCheck: boolean } } context
23105 * @returns {string[] } one reason per refused file
24106 */
25- function findSchemaChanges ( files ) {
107+ function findSchemaChanges ( changes , { closure , baseHasCheck } ) {
26108 const reasons = [ ] ;
27- for ( const file of files ) {
28- if ( SCHEMA_PATHS . some ( ( pattern ) => pattern . test ( file . path ) ) ) {
29- reasons . push ( `${ file . path } : changes the database schema or its migrations` ) ;
109+ for ( const change of changes ) {
110+ const file = change . path ;
111+ if ( CHECK_FILES . includes ( file ) ) {
112+ if ( baseHasCheck ) {
113+ reasons . push (
114+ `${ file } : changes the schema check itself. Only the owner may do that: the check stays ` +
115+ "red, and the owner merges past it." ,
116+ ) ;
117+ }
30118 continue ;
31119 }
32- if ( ! SHIPPED_SOURCE . test ( file . path ) || TEST_SOURCE . test ( file . path ) ) continue ;
33- const statement = file . addedLines . find ( ( line ) => SCHEMA_STATEMENT . test ( line ) ) ;
120+ if ( GITATTRIBUTES . test ( file ) ) {
121+ reasons . push (
122+ `${ file } : changes how git treats files, which can hide changes from review. Only the ` +
123+ "owner may do that." ,
124+ ) ;
125+ continue ;
126+ }
127+ if ( SCHEMA_ROOTS . some ( ( pattern ) => pattern . test ( file ) ) || closure . has ( file ) ) {
128+ reasons . push ( `${ file } : changes the database schema, its migrations or code they import` ) ;
129+ continue ;
130+ }
131+ if ( ! isShippedSource ( file ) ) continue ;
132+ const statement = [ change . after , change . before ]
133+ . map ( ( text ) => text ?. match ( SCHEMA_STATEMENT ) ?. [ 0 ] )
134+ . find ( Boolean ) ;
34135 if ( statement ) {
35- reasons . push ( `${ file . path } : adds a schema statement: ${ statement . trim ( ) . slice ( 0 , 120 ) } ` ) ;
136+ reasons . push (
137+ `${ file } : holds a schema statement (${ statement . replace ( / \s + / g, " " ) } ), so any change ` +
138+ "to it may change the schema" ,
139+ ) ;
36140 }
37141 }
38142 return reasons ;
39143}
40144
41- /** Parses `git diff --unified=0` output into changed files and their added lines. */
42- function parseDiff ( diff ) {
43- const files = [ ] ;
44- let oldPath = "" ;
45- let current ;
46- for ( const line of diff . split ( "\n" ) ) {
47- if ( line . startsWith ( "diff --git " ) ) {
48- current = undefined ;
49- oldPath = "" ;
50- } else if ( current === undefined && line . startsWith ( "--- " ) ) {
51- oldPath = line . slice ( 4 ) . replace ( / ^ a \/ / , "" ) ;
52- } else if ( current === undefined && line . startsWith ( "+++ " ) ) {
53- // A deleted file's new side is /dev/null; it keeps its old path, so deleting is refused too.
54- const target = line . slice ( 4 ) ;
55- current = {
56- path : target === "/dev/null" ? oldPath : target . replace ( / ^ b \/ / , "" ) ,
57- addedLines : [ ] ,
58- } ;
59- files . push ( current ) ;
60- } else if ( current && line . startsWith ( "+" ) ) {
61- current . addedLines . push ( line . slice ( 1 ) ) ;
145+ /**
146+ * Parses `git diff --raw -z --no-abbrev` output into { path, oldBlob, newBlob } entries. A side
147+ * that is absent or a submodule (no blob) is undefined; the path is still listed.
148+ */
149+ function parseRawDiff ( raw ) {
150+ const fields = raw . split ( "\0" ) ;
151+ const entries = [ ] ;
152+ for ( let index = 0 ; index + 1 < fields . length ; index += 2 ) {
153+ const header = fields [ index ] ;
154+ if ( ! header . startsWith ( ":" ) ) break ;
155+ const [ oldMode , newMode , oldSha , newSha , status ] = header . slice ( 1 ) . split ( " " ) ;
156+ if ( / ^ [ R C ] / . test ( status ) ) throw new Error ( `unexpected rename in diff: ${ header } ` ) ;
157+ const blob = ( mode , sha ) => ( / ^ 1 0 | ^ 1 2 / . test ( mode ) && ! / ^ 0 + $ / . test ( sha ) ? sha : undefined ) ;
158+ entries . push ( {
159+ path : fields [ index + 1 ] ,
160+ oldBlob : blob ( oldMode , oldSha ) ,
161+ newBlob : blob ( newMode , newSha ) ,
162+ } ) ;
163+ }
164+ return entries ;
165+ }
166+
167+ function git ( args ) {
168+ return execFileSync ( "git" , [ "-c" , "core.quotePath=false" , ...args ] , {
169+ encoding : "utf8" ,
170+ maxBuffer : 512 * 1024 * 1024 ,
171+ } ) ;
172+ }
173+
174+ /** Source files and package manifests of a commit; texts are read only when the walk asks. */
175+ function readTree ( rev ) {
176+ const blobs = new Map ( ) ;
177+ for ( const record of git ( [ "ls-tree" , "-r" , "-z" , "--full-tree" , rev ] ) . split ( "\0" ) ) {
178+ const match = record . match ( / ^ \d + b l o b ( [ 0 - 9 a - f ] + ) \t ( .+ ) $ / s) ;
179+ if ( match && ( SHIPPED_SOURCE . test ( match [ 2 ] ) || PACKAGE_MANIFEST . test ( match [ 2 ] ) ) ) {
180+ blobs . set ( match [ 2 ] , match [ 1 ] ) ;
62181 }
63182 }
64- return files ;
183+ return {
184+ files : [ ...blobs . keys ( ) ] ,
185+ has : ( file ) => blobs . has ( file ) ,
186+ read : ( file ) => ( blobs . has ( file ) ? readBlob ( blobs . get ( file ) ) : "" ) ,
187+ } ;
188+ }
189+
190+ const blobCache = new Map ( ) ;
191+ function readBlob ( sha ) {
192+ if ( ! blobCache . has ( sha ) ) blobCache . set ( sha , git ( [ "cat-file" , "blob" , sha ] ) ) ;
193+ return blobCache . get ( sha ) ;
194+ }
195+
196+ function treeHas ( rev , file ) {
197+ return git ( [ "ls-tree" , "--full-tree" , "--name-only" , rev , "--" , file ] ) . trim ( ) === file ;
65198}
66199
67200function main ( argv ) {
@@ -70,22 +203,35 @@ function main(argv) {
70203 console . error ( "Usage: fork-schema-check.cjs <base> <head>" ) ;
71204 return 2 ;
72205 }
73- const diff = execFileSync (
74- "git" ,
75- [ "diff" , "--unified=0" , "--no-renames" , "--no-color" , `${ base } ...${ head } ` ] ,
76- { encoding : "utf8" , maxBuffer : 256 * 1024 * 1024 } ,
206+ const mergeBase = git ( [ "merge-base" , base , head ] ) . trim ( ) ;
207+ const entries = parseRawDiff (
208+ git ( [ "diff" , "--raw" , "-z" , "--no-abbrev" , "--no-renames" , "--no-ext-diff" , mergeBase , head ] ) ,
77209 ) ;
78- const reasons = findSchemaChanges ( parseDiff ( diff ) ) ;
210+ // Only shipped source is searched for statements, so only its texts are read.
211+ const text = ( entry , sha ) => ( sha && isShippedSource ( entry . path ) ? readBlob ( sha ) : undefined ) ;
212+ const changes = entries . map ( ( entry ) => ( {
213+ path : entry . path ,
214+ before : text ( entry , entry . oldBlob ) ,
215+ after : text ( entry , entry . newBlob ) ,
216+ } ) ) ;
217+ const closure = new Set ( [
218+ ...schemaClosure ( readTree ( mergeBase ) ) ,
219+ ...schemaClosure ( readTree ( head ) ) ,
220+ ] ) ;
221+ const reasons = findSchemaChanges ( changes , {
222+ closure,
223+ baseHasCheck : treeHas ( base , CHECK_SCRIPT ) ,
224+ } ) ;
79225 if ( reasons . length === 0 ) {
80- console . log ( " No database schema changes." ) ;
226+ console . log ( ` No database schema changes ( ${ changes . length } changed files checked).` ) ;
81227 return 0 ;
82228 }
83229 console . error ( "Fork changes must not change the database schema (no down-migrations exist):" ) ;
84230 for ( const reason of reasons ) console . error ( `- ${ reason } ` ) ;
85231 return 1 ;
86232}
87233
88- module . exports = { findSchemaChanges, parseDiff } ;
234+ module . exports = { findSchemaChanges, parseRawDiff , schemaClosure , CHECK_FILES } ;
89235
90236if ( require . main === module ) {
91237 process . exitCode = main ( process . argv . slice ( 2 ) ) ;
0 commit comments