scenic-views/scenic · error · RuntimeError
Define view query in #{path} before migrating.
Error message
Define view query in #{path} before migrating. What it means
Scenic resolves each view's SQL from db/views/<name>_vNN.sql (version zero-padded to two digits) under Rails.root. Definition#to_sql reads that file and raises RuntimeError ('Define view query in <path> before migrating.') when the content is empty. The scenic:view generator deliberately scaffolds an empty stub for you to fill in, and migrating before writing the SELECT into it triggers this. A missing file raises Errno::ENOENT instead - this message means the file exists but has zero bytes.
Source
Thrown at lib/scenic/definition.rb:12
module Scenic
# @api private
class Definition
def initialize(name, version)
@name = name.to_s
@version = version.to_i
end
def to_sql
File.read(full_path).tap do |content|
if content.empty?
raise "Define view query in #{path} before migrating."
end
end
end
def full_path
Rails.root.join(path)
end
def path
File.join("db", "views", filename)
end
def version
@version.to_s.rjust(2, "0")
end
private
View on GitHub (pinned to f2162dbddb)
Solutions
- Open the exact path named in the message and write the view query (for example: SELECT id, email FROM users WHERE active;)
- If the version file was never created, run rails g scenic:view <name> (add --materialized for matviews), fill the stub, then migrate
- If content was lost in a merge or sync, recover it from git history: git log --oneline -- db/views/searches_v01.sql and restore the last non-empty revision
Example fix
# before: db/views/searches_v01.sql exists but is empty # migration => Define view query in db/views/searches_v01.sql before migrating. # after: db/views/searches_v01.sql SELECT id, email FROM users WHERE active;
Defensive patterns
Strategy: validation
Validate before calling
Dir[Rails.root.join('db/views/*.sql')].each do |f|
abort f + ' is empty; fill in the view query before migrating' if File.read(f).strip.empty?
end Prevention
- Enhance db:migrate with a preflight task that aborts on any empty db/views/*.sql
- Never commit a generated view stub without its query; review generator output before pushing
- When CI hits this error, check git status for zero-byte definition files caused by merges or sync tools
When it happens
Trigger: Running rails g scenic:view searches followed by db:migrate without editing db/views/searches_v01.sql; bumping to create_view :searches, version: 2 while db/views/searches_v02.sql is empty; a definition file truncated to zero bytes by a bad merge, editor crash, or file-sync tool.
Common situations: Committing the generated stub so CI's migration job hits it; a merge conflict resolved by keeping an empty side; definition files emptied by sync or filter processes (LFS, Docker copy, formatting hooks).
Related errors
- #{method} is reversible only if given a revert_to_version
- a transaction is required to perform a side-by-side update
- version is required
- Materialized views require Postgres 9.3 or newer
- Cannot replace materialized views
AI-assisted analysis of scenic-views/scenic@f2162dbddb (2026-08-23).
Data as JSON: /api/errors/79ca1a13277dfa4c.
Report an issue: GitHub.