scenic: three ways to change a view, and only one keeps dependencies
Versioned database views for Rails
At a glance
- What is it?
- Scenic brings SQL views into Rails without changing your schema format, and its whole design is the versioning convention: the SQL lives in a numbered file, the migration only names a version. The readme is unusually concrete about what that means at the difficult moments, including the fact that a materialized view cannot be replaced in place at all.
- Who is it for?
- Scenic fits a Rails team whose schema is expressed as Ruby but whose reporting logic wants real SQL, and the convention it provides is the reason to choose it over hand-written migrations: the SQL lives in a file you can highlight and run, and the migration that references it stays reversible. Two things to weigh before you commit.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 139 days ago.
- What is it written in?
- Mainly Ruby, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- scenic-views/scenic3,628 ★ · +2
| Repository | 2026-09-24 | 2026-11-15 |
|---|---|---|
| scenic-views/scenic | 3.6k | 3.6k |
2 Stars in the last 52 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The SQL lives in a numbered file and the migration only names a version
The whole design fits in one sentence: the view structure is defined in a SQL file, and the migration refers to it by number.
That split is what makes Scenic different from writing SQL inside a migration. Two things follow from it. The SQL gets full syntax highlighting in whatever editor you use, because it is a real SQL file rather than a string. And you can paste it into a database console during development and see whether it works before you migrate anything.
The other thing the convention buys is that a view, like a table, has a migration history. Scenic provides a versioning scheme for views that keeps the history consistent and reversible, and it means you are not duplicating the same SQL string across several migrations when you change something. Instead there is one file per version.
The generated artefacts make the shape concrete. A view generator creates a version file under the views directory and a timestamped migration; running the migration leaves the schema dumped into the schema file, and the migration is reversible.
So the answer to the question this project exists to answer is: you can have SQL views in a Rails app without switching the schema format away from Ruby.
Three strategies exist, and the default one loses dependencies
Changing a view is where this design has to make decisions, and the readme is explicit that the default is the wrong choice for some schemas.
The default update statement drops your view and then creates the new version of it. The readme says plainly that this may not be desirable when you have complicated hierarchies of dependent views, and that is the whole problem: anything built on top of the view is dropped along with it.
So there is a second strategy. A replace statement issues the equivalent of a create-or-replace query, which updates the view in place and keeps its dependencies. You get it by passing an option to the generator, and the migration it writes is short: it names the view, the version it is moving to, and the version to revert to.
A third strategy is mentioned by name, side by side, and is said to yield results similar to replacement for materialized views. The section that would explain it is cut off in the copy of the document available here, so the details of how it works are not readable.
Two of the three are therefore documented and one is named only. For an ordinary view you have drop-and-create or replace-in-place, and the readme's own framing tells you which to pick: replace-in-place when other views depend on this one.
The generator bumps the version and copies the old definition for you
The workflow for a second version is the part of the readme that is most reassuring about the convention.
You run the same generator command again with the same name:
$ rails generate scenic:view search_resultsScenic detects that a view of that name already exists at version 1, creates a copy of the current definition as version 2, and creates a migration named for the update to version 2. All you have to do afterwards is edit the new definition and run the migration.
That is a small automation with two real consequences. You never hand-write the version number, so the file naming and the migration cannot drift apart. And you never lose the previous definition, because it is copied rather than overwritten, which means the revert target in the migration points at something that still exists.
The generated file names also encode the version, with a two-digit suffix and an underscore, and the migration filename carries the timestamp and the target version. So a view's history can be read from a directory listing, which matters when you are looking at a migration six months later and want to know which file to open.
It also means the same generator works for a table-like object backed by a view, since there is a second generator for models that is described as a superset of the view one.
A view-backed model is read-only, and its primary key cannot be inferred
Backing a model with a view is supported, and the readme gives you the two adjustments ActiveRecord needs before it stops treating the class like a normal table.
The first is the primary key. The sample model carries it as a commented-out line, with the note that if you want to be able to call the finder method you must declare the primary key yourself, because it cannot be inferred from the column information. That is not a Scenic limitation so much as a consequence of what a view is: the column list looks like a table's, so ActiveRecord infers what it can and gets it wrong.
The second is read-only. The sample defines the predicate that makes Rails refuse to save, with a comment explaining that it is not strictly necessary but prevents Rails from attempting a save that would fail anyway. On a view, a write has nowhere to go, so the failure is guaranteed and the declaration just moves it earlier.
The stated benefit is worth repeating, because it is the argument for doing this rather than writing a query: concepts that are hidden in relational data can be promoted to first-class domain objects, and complex query or query-builder code can be cleaned up. As far as ActiveRecord is concerned, the readme says, a view is no different than a table.
There is also no special base class or mixin to inherit from, and the readme notes that anything the model generator adds can be deleted without consequence.
A concurrent materialized refresh needs a unique index that covers every row
Materialized views are supported, and the refresh semantics are specified tightly enough to be useful.
Both generators accept an option for materialized views. With the model generator, you also get a class method that calls the database's refresh for the materialized view, with two options: whether to refresh concurrently, and whether to cascade. The generated call passes neither, so it performs a non-concurrent refresh, and the readme is explicit that this locks the view for selects until the refresh finishes.
To avoid the lock you pass the concurrent option, and that has a precondition which is the part worth knowing. Concurrent refresh requires both a minimum PostgreSQL version and that your view have at least one unique index covering all rows. It is a database constraint on your view definition, not a performance hint.
The second half of the answer is clever. You add and update indexes for a materialized view using the ordinary table migration methods, and those index changes are automatically re-applied when the view is updated. So a materialized view gets the indexes it needs for a concurrent refresh without the versioned view mechanism having to model indexes itself.
That is the kind of detail that decides whether the feature is usable: a view can be recreated from its SQL file, and the indexes that make the fast refresh possible come back with it.
The cascade option exists because refresh order is a real constraint
The second refresh option gets a worked example, which is unusually generous documentation.
Cascade is for when one materialized view selects from another. The example is a view that selects data from another materialized view, and the stated problem is ordering: to get the most current information in the outer view, you have to refresh the inner one first and then immediately refresh the outer one. Setting the cascade option on the inner view's refresh method is what arranges that.
So the option is not about error handling or about nested queries in one statement. It is about a sequence of refreshes across a dependency chain, which is the part of materialized views that no amount of clever SQL removes.
The readme is equally clear about the limit on the other side. A materialized view cannot be updated in place with the replace strategy used for ordinary views, and the question of whether you can change its definition without dropping it is answered with a no, followed by a sentence that cuts off mid-word in the copy available here. The side by side strategy is the fallback the document points at.
For anyone planning a materialized view hierarchy, that sentence is the one to go and read in the real documentation.
One database is shipped, and the extension point for others is small
Scenic ships with PostgreSQL support, and everything else is somebody else's problem, in a way that is at least explicit.
The installation instructions say it plainly. If you are using Postgres, add the gem to the Gemfile and run bundle install. If you are using something else, go and look at the third-party adapters, and the link points at an in-document question about when MySQL, SQLite and other databases will be supported.
The reason that question has an answer is that the adapter surface is deliberately minimal. It is configurable through a configuration object, and the adapter itself implements a minimal interface, documented by naming its Postgres implementation. Other gems can provide that interface.
That is a good design for a project that does not want to maintain a driver per database, and it has a cost: your choice of database is mediated by a community package, and the version of that package has to move when Scenic does. The in-document question is worth reading for that reason, because it is where the project states its position on support.
Everything else in the readme, from the generators to the materialized view options, assumes you have a view definition in a file and a migration that names its version. The database is the only part that leaves the project.
The newest tag is from 2017 and its title carries a contributor handle
The release history explains more about this project than the readme does, and it is worth reading slowly.
The three most recent releases are 1.2.0, then 1.3.0 a few months later, then 1.4.0 in May 2017. Between 1.3.0 and 1.4.0 there is almost exactly a year; between 1.2.0 and 1.3.0 there are four months. The last recorded push to the branch is dated 2026-06-29.
So the newest tag is roughly nine years older than the last commit. That can mean several things, and the repository does not say which: a maintained project that no longer cuts releases, a project whose releases moved to another mechanism, or a project that is effectively complete and receives occasional fixes. What is certain is that if you install by version, you are installing code from 2017, and if you build from the branch you are building nine years of unreleased changes.
Then the release titles. 1.4.0 is titled with a contributor's handle and an issue number, and 1.3.0 is titled with a contributor's name and nothing else. Releases named after the person who made them and the issue they closed are a project convention, not a version scheme, and it is the kind of thing that tells you the maintainer reviews pull requests individually.
For anyone pinning a version, that is the practical warning: pin deliberately, and expect the branch to be considerably further along than the tag.
Editorial conclusion
Scenic fits a Rails team whose schema is expressed as Ruby but whose reporting logic wants real SQL, and the convention it provides is the reason to choose it over hand-written migrations: the SQL lives in a file you can highlight and run, and the migration that references it stays reversible. Two things to weigh before you commit. Changing a view is the interesting operation, and the default strategy drops the view before recreating it, which means anything built on top of it goes with it unless you remember to ask for the in-place replacement. And the shipped adapter covers one database, so anything else depends on a third-party gem implementing a small interface. For views that need to refresh concurrently, the requirement is stricter than it looks: a unique index covering every row is a precondition, not an optimisation.
Frequently asked questions
how to install scenic
For PostgreSQL, add the gem to your Gemfile and run bundle install. For any other database the project points you to third-party adapters, because the shipped adapter covers Postgres and other gems implement a small adapter interface instead.
What does Scenic do for Rails?
It adds methods to the migration class so you can create and manage SQL views while keeping your schema format as Ruby. Each view gets a versioned SQL file and a reversible migration that names the version, so the same SQL is not duplicated across migrations.
How do I change a Scenic view after creating it?
Run the view generator again with the same name. Scenic detects the existing version, copies the current definition as the next version, and writes an update migration naming the version to move to and the version to revert to. You edit the new SQL file and run the migration.
Can Scenic update a view without dropping it?
Yes for ordinary views. The default update statement drops the view and recreates it, which the readme flags as a problem when views depend on each other. A replace statement issues a create-or-replace query that keeps dependencies, and you get it by passing the replace option to the generator. Materialized views cannot be replaced this way at all.
How do materialized views work in Scenic?
Both generators accept an option for materialized views, and the model generator adds a class method that refreshes the view non-concurrently, locking it for selects until it finishes. Passing the concurrent option avoids the lock but needs a minimum PostgreSQL version and at least one unique index covering all rows, and indexes added through ordinary table migration methods are re-applied when the view is updated.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/scenic-views-scenic)Star history chart for your README
[](https://hysenlabs.com/projects/scenic-views-scenic)Was this useful?